diff --git a/.gitignore b/.gitignore index c685744..f3bb1ce 100644 --- a/.gitignore +++ b/.gitignore @@ -72,3 +72,5 @@ data/ run-dev.sh !README.md + +wallet.json diff --git a/package.json b/package.json index 620f27f..81b245e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ipfs-bch-wallet-service", - "version": "3.0.0", + "version": "3.1.0", "description": "IPFS and REST service providing blockchain access needed by a wallet. ", "main": "index.js", "type": "module", diff --git a/production/docker/docker-compose.yml b/production/docker/docker-compose.yml index 6bfc309..9722fc9 100644 --- a/production/docker/docker-compose.yml +++ b/production/docker/docker-compose.yml @@ -22,7 +22,7 @@ services: options: max-size: '10m' max-file: '10' - mem_limit: 1000mb + #mem_limit: 1500mb links: - mongo-bch-service ports: 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..29dc833 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,12 @@ 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 - // mock instance of minimal-slp-wallet - uut.bchWallet = new MockBchWallet() + // uut.WALLET_FILE = testWalletFile + uut.config.walletFile = testWalletFile + const result = await uut.getKeyPair() // console.log('result: ', result) + assert.property(result, 'cashAddress') assert.property(result, 'wif') assert.property(result, 'hdIndex') 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