From d677c83795deee7143825a83c6c5dbc27c3d9ef4 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Fri, 29 Mar 2024 11:36:35 -0700 Subject: [PATCH 1/4] Removing wallet.json file --- wallet.json | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 wallet.json diff --git a/wallet.json b/wallet.json deleted file mode 100644 index 3c81c77..0000000 --- a/wallet.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "mnemonic": "course abstract aerobic deer try switch turtle diet fence affair butter top", - "privateKey": "L5D2UAam8tvo3uii5kpgaGyjvVMimdrXu8nWGQSQjuuAix6ji1YQ", - "publicKey": "0379433ffc401483ade310469953c1cba77c71af904f07c15bde330d7198b4d6dc", - "cashAddress": "bitcoincash:qzl0d3gcqeypv4cy7gh8rgdszxa9vvm2acv7fqtd00", - "address": "bitcoincash:qzl0d3gcqeypv4cy7gh8rgdszxa9vvm2acv7fqtd00", - "slpAddress": "simpleledger:qzl0d3gcqeypv4cy7gh8rgdszxa9vvm2acq9zm7d33", - "legacyAddress": "1JQj1KcQL7GPKzc1D2PvdUSgw3MbDtrHzi", - "hdPath": "m/44'/245'/0'/0/0", - "nextAddress": 1 -} \ No newline at end of file From 2a522775ebb49466344a0579f6ae89164f9c3059 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Fri, 29 Mar 2024 11:36:58 -0700 Subject: [PATCH 2/4] Adding wallet.json to gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 2c1bf6c..0b3c6c1 100644 --- a/.gitignore +++ b/.gitignore @@ -69,3 +69,5 @@ data/ run-dev.sh !README.md + +wallet.json From 5a6e3247e8ca76cc06bc4832ea1ccd7ecea6ba1f Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Fri, 29 Mar 2024 12:43:02 -0700 Subject: [PATCH 3/4] fix(incrementNextAddress): fixing bug --- src/adapters/wallet.adapter.js | 20 +++++++++++++++----- test/unit/adapters/wallet.adapter.unit.js | 14 +++++++++----- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/adapters/wallet.adapter.js b/src/adapters/wallet.adapter.js index 9dd2052..1870a33 100644 --- a/src/adapters/wallet.adapter.js +++ b/src/adapters/wallet.adapter.js @@ -49,7 +49,7 @@ class Wallet { advancedConfig.restURL = this.config.apiServer } - console.log('advancedConfig setting when creating wallet: ', advancedConfig) + // console.log('advancedConfig setting when creating wallet: ', advancedConfig) // Instantiate minimal-slp-wallet. if (walletData.mnemonic) { @@ -96,8 +96,8 @@ class Wallet { // Try to open the wallet.json file. try { - // console.log('this.config.walletFile: ', this.config.walletFile) walletData = await this.jsonFiles.readJSON(this.config.walletFile) + walletData = walletData.wallet } catch (err) { // Create a new wallet file if one does not already exist. console.log('Wallet file not found. Creating new wallet.json file.') @@ -112,7 +112,7 @@ class Wallet { walletData.nextAddress = 1 // Write the wallet data to the JSON file. - await this.jsonFiles.writeJSON(walletData, this.config.walletFile) + await this.jsonFiles.writeJSON({ wallet: walletData }, this.config.walletFile) } // console.log('walletData: ', walletData) @@ -149,13 +149,18 @@ class Wallet { async incrementNextAddress () { try { const walletData = await this.openWallet() - // console.log('original walletdata: ', walletData) + + await this.instanceWalletWithoutInitialization(walletData) + walletData.nextAddress++ // console.log('walletData finish: ', walletData) - await this.jsonFiles.writeJSON(walletData, this.WALLET_FILE) + + await this.jsonFiles.writeJSON({ wallet: walletData }, this.config.walletFile) + // Update the working instance of the wallet. this.bchWallet.walletInfo.nextAddress++ // console.log('this.bchWallet.walletInfo: ', this.bchWallet.walletInfo) + return walletData.nextAddress } catch (err) { console.error('Error in incrementNextAddress()') @@ -173,16 +178,21 @@ class Wallet { // Increment the HD index and generate a new key pair. hdIndex = await this.incrementNextAddress() } + const mnemonic = this.bchWallet.walletInfo.mnemonic + // root seed buffer const rootSeed = await this.bchWallet.bchjs.Mnemonic.toSeed(mnemonic) const masterHDNode = this.bchWallet.bchjs.HDNode.fromSeed(rootSeed) + // HDNode of BIP44 account // const account = this.bchWallet.bchjs.HDNode.derivePath(masterHDNode, "m/44'/245'/0'") const childNode = masterHDNode.derivePath(`m/44'/245'/0'/0/${hdIndex}`) const cashAddress = this.bchWallet.bchjs.HDNode.toCashAddress(childNode) console.log('Generating a new key pair for cashAddress: ', cashAddress) + const wif = this.bchWallet.bchjs.HDNode.toWIF(childNode) + const outObj = { cashAddress, wif, diff --git a/test/unit/adapters/wallet.adapter.unit.js b/test/unit/adapters/wallet.adapter.unit.js index 7ac98ac..1d3658e 100644 --- a/test/unit/adapters/wallet.adapter.unit.js +++ b/test/unit/adapters/wallet.adapter.unit.js @@ -18,7 +18,7 @@ import * as url from 'url' const __dirname = url.fileURLToPath(new URL('.', import.meta.url)) // Global constants -const testWalletFile = `${__dirname.toString()}/test-wallet.json` +const testWalletFile = `${__dirname.toString()}test-wallet.json` describe('#wallet', () => { let uut @@ -59,7 +59,7 @@ describe('#wallet', () => { // Mock dependencies uut.BchWallet = MockBchWallet // Ensure we open the test file, not the production wallet file. - uut.walletFile = testWalletFile + uut.config.walletFile = testWalletFile const result = await uut.openWallet() // console.log('result: ', result) assert.property(result, 'mnemonic') @@ -75,7 +75,7 @@ describe('#wallet', () => { it('should open existing wallet file', async () => { // This test case uses the file created in the previous test case. // Ensure we open the test file, not the production wallet file. - uut.walletFile = testWalletFile + uut.config.walletFile = testWalletFile const result = await uut.openWallet() // console.log('result: ', result) assert.property(result, 'mnemonic') @@ -274,11 +274,15 @@ describe('#wallet', () => { describe('#getKeyPair', () => { it('should return an object with a key pair', async () => { // Ensure we open the test file, not the production wallet file. - uut.WALLET_FILE = testWalletFile + // uut.WALLET_FILE = testWalletFile + uut.config.walletFile = testWalletFile + // mock instance of minimal-slp-wallet - uut.bchWallet = new MockBchWallet() + // uut.bchWallet = new MockBchWallet() + const result = await uut.getKeyPair() // console.log('result: ', result) + assert.property(result, 'cashAddress') assert.property(result, 'wif') assert.property(result, 'hdIndex') From 55d8d131cf32b81b699a4030e27c1a06694a83c5 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Fri, 29 Mar 2024 12:43:15 -0700 Subject: [PATCH 4/4] Removing commented code --- test/unit/adapters/wallet.adapter.unit.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/unit/adapters/wallet.adapter.unit.js b/test/unit/adapters/wallet.adapter.unit.js index 1d3658e..29dc833 100644 --- a/test/unit/adapters/wallet.adapter.unit.js +++ b/test/unit/adapters/wallet.adapter.unit.js @@ -277,9 +277,6 @@ describe('#wallet', () => { // uut.WALLET_FILE = testWalletFile uut.config.walletFile = testWalletFile - // mock instance of minimal-slp-wallet - // uut.bchWallet = new MockBchWallet() - const result = await uut.getKeyPair() // console.log('result: ', result)