From 5a6e3247e8ca76cc06bc4832ea1ccd7ecea6ba1f Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Fri, 29 Mar 2024 12:43:02 -0700 Subject: [PATCH] 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')