From 1e88903169dda19f42c408f4ebebda51ae071133 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Thu, 16 Sep 2021 20:50:39 -0700 Subject: [PATCH] feat(getKeyPair): Can generate key pairs --- src/adapters/wallet.js | 40 +++++++++++++++++ test/unit/adapters/wallet.adapter.unit.js | 53 ++++++++++++++++++++++- test/unit/mocks/adapters/wallet.js | 3 +- 3 files changed, 94 insertions(+), 2 deletions(-) diff --git a/src/adapters/wallet.js b/src/adapters/wallet.js index 0e85f6a..8043ec6 100644 --- a/src/adapters/wallet.js +++ b/src/adapters/wallet.js @@ -74,12 +74,52 @@ class WalletAdapter { // console.log('walletData finish: ', walletData) await this.jsonFiles.writeJSON(walletData, this.WALLET_FILE) + + // 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()') throw err } } + // This method returns an object that contains a private key WIF, public address, + // and the index of the HD wallet that the key pair was generated from. + async getKeyPair () { + try { + const 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) + const wif = this.bchWallet.bchjs.HDNode.toWIF(childNode) + + const outObj = { + cashAddress, + wif, + hdIndex + } + + return outObj + } catch (err) { + console.error('Error in getKeyPair()') + throw err + } + } + // Create an instance of minimal-slp-wallet. Use data in the wallet.json file, // and pass the bch-js information to the minimal-slp-wallet library. async instanceWallet (walletData, bchjs) { diff --git a/test/unit/adapters/wallet.adapter.unit.js b/test/unit/adapters/wallet.adapter.unit.js index f146366..90e0b64 100644 --- a/test/unit/adapters/wallet.adapter.unit.js +++ b/test/unit/adapters/wallet.adapter.unit.js @@ -6,6 +6,7 @@ const assert = require('chai').assert const sinon = require('sinon') const fs = require('fs') +const BCHJS = require('@psf/bch-js') // Local libraries. const WalletAdapter = require('../../../src/adapters/wallet') @@ -202,7 +203,57 @@ describe('#wallet', () => { // Ensure we open the test file, not the production wallet file. uut.WALLET_FILE = testWalletFile - await uut.incrementNextAddress() + // mock instance of minimal-slp-wallet + uut.bchWallet = new MockBchWallet() + + const result = await uut.incrementNextAddress() + + assert.equal(result, 2) + }) + + it('should catch and throw an error', async () => { + try { + // Force an error + sandbox.stub(uut, 'openWallet').rejects(new Error('test error')) + + await uut.incrementNextAddress() + + assert.fail('Unexpected code path') + } catch (err) { + assert.include(err.message, 'test error') + } + }) + }) + + 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 + + // mock instance of minimal-slp-wallet + 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') + }) + + it('should catch and throw an error', async () => { + try { + // Force an error + sandbox + .stub(uut, 'incrementNextAddress') + .rejects(new Error('test error')) + + await uut.getKeyPair() + + assert.fail('Unexpected code path') + } catch (err) { + assert.include(err.message, 'test error') + } }) }) }) diff --git a/test/unit/mocks/adapters/wallet.js b/test/unit/mocks/adapters/wallet.js index f36f1b2..a6912a0 100644 --- a/test/unit/mocks/adapters/wallet.js +++ b/test/unit/mocks/adapters/wallet.js @@ -14,7 +14,8 @@ const mockWallet = { address: 'bitcoincash:qzl0d3gcqeypv4cy7gh8rgdszxa9vvm2acv7fqtd00', slpAddress: 'simpleledger:qzl0d3gcqeypv4cy7gh8rgdszxa9vvm2acq9zm7d33', legacyAddress: '1JQj1KcQL7GPKzc1D2PvdUSgw3MbDtrHzi', - hdPath: "m/44'/245'/0'/0/0" + hdPath: "m/44'/245'/0'/0/0", + nextAddress: 1 } class MockBchWallet {