From 5bc414c45a5442fba82b150adaad96a1f44e05c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20=C5=A0ev=C4=8D=C3=ADk?= Date: Sun, 5 Sep 2021 02:38:47 +0200 Subject: [PATCH] Derive postmix change decoy addresses more simply --- lib/bitcoin/hd-accounts-helper.js | 46 ++++++---------------- lib/bitcoin/parallel-address-derivation.js | 12 +++++- 2 files changed, 21 insertions(+), 37 deletions(-) diff --git a/lib/bitcoin/hd-accounts-helper.js b/lib/bitcoin/hd-accounts-helper.js index c61b06a..dbfa6ce 100644 --- a/lib/bitcoin/hd-accounts-helper.js +++ b/lib/bitcoin/hd-accounts-helper.js @@ -299,7 +299,7 @@ class HDAccountsHelper { * @param {bitcoin.bip32.BIP32Interface} chainNode - Parent bip32 used for derivation * @param {number} index - index to be derived * @param {number} type - type of derivation - * @returns {Promise} returns an object {address: '...', chain: , index: , publicKey: , address: string } + * @returns {Promise} returns an object {address: '...', chain: , index: , address: string } */ async deriveAddress(chain, chainNode, index, type) { // Derive M/chain/index @@ -308,7 +308,6 @@ class HDAccountsHelper { const addr = { chain: chain, index: index, - publicKey: indexNode.publicKey } switch (type) { @@ -363,17 +362,20 @@ class HDAccountsHelper { ) { // Few addresses to be derived or external derivation deactivated // Let's do it here - const addresses = await Promise.all(indices.map(index => { + const promises = indices.map(index => { return this.deriveAddress(chain, chainNode, index, info.type) - })) + }) // Generate additional change address types for postmix account if (this.isPostmixAcct(node) && chain === 1) { - const additionalPostmixAddresses = this._generateAdditionalChangeAddresses(addresses) - - addresses.push(...additionalPostmixAddresses) + indices.forEach((index) => { + promises.push(this.deriveAddress(chain, chainNode, index, this.BIP44)) + promises.push(this.deriveAddress(chain, chainNode, index, this.BIP49)) + }) } + const addresses = await Promise.all(promises) + return addresses; } else { @@ -385,19 +387,13 @@ class HDAccountsHelper { xpub: this.xlatXPUB(xpub), chain: chain, indices: indices, - type: info.type + type: info.type, + isPostmixChange: this.isPostmixAcct(node) && chain === 1 } const msg = await this.derivationPool.exec('deriveAddresses', [data]) if (msg.status === 'ok') { - // Generate additional change address types for postmix account - if (this.isPostmixAcct(node) && chain === 1) { - const additionalPostmixAddresses = this._generateAdditionalChangeAddresses(msg.addresses) - - msg.addresses.push(...additionalPostmixAddresses) - } - resolve(msg.addresses) } else { Logger.error(null, 'HdAccountsHelper : A problem was met during parallel addresses derivation') @@ -416,26 +412,6 @@ class HDAccountsHelper { } } - /** - * @description Derive additional change addresses (P2PKH & P2SH) for postmix account - * @param addresses {{ chain: number, index: number, publicKey: Buffer, address: string }[]} - list of derived addresses - * @returns {{ chain: number, index: number, publicKey: Buffer, address: string }[]} - array of additional address types - * @private - */ - _generateAdditionalChangeAddresses(addresses) { - const additionalPostmixAddresses = addresses.map((address) => { - const newP2PKHAddress = {...address} - const newP2SHAddress = {...address} - - newP2PKHAddress.address = addrHelper.p2pkhAddress(address.publicKey) - newP2SHAddress.address = addrHelper.p2wpkhP2shAddress(address.publicKey) - - return [newP2PKHAddress, newP2SHAddress]; - }) - - return additionalPostmixAddresses.flat() - } - /** * @description Detect postmix account * @param {[bitcoin.bip32.BIP32Interface, bitcoin.bip32.BIP32Interface, bitcoin.bip32.BIP32Interface]} node - array of BIP32 node interfaces diff --git a/lib/bitcoin/parallel-address-derivation.js b/lib/bitcoin/parallel-address-derivation.js index 9c2bc08..4033406 100644 --- a/lib/bitcoin/parallel-address-derivation.js +++ b/lib/bitcoin/parallel-address-derivation.js @@ -25,7 +25,7 @@ const BIP84 = 2 * @param {bip32} chainNode - Parent bip32 used for derivation * @param {number} index - index to be derived * @param {number} type - type of derivation - * @returns {Promise} returns an object {address: '...', chain: , index: , publicKey: } + * @returns {Promise} returns an object {address: '...', chain: , index: } */ async function deriveAddress(chain, chainNode, index, type) { // Derive M/chain/index @@ -34,7 +34,6 @@ async function deriveAddress(chain, chainNode, index, type) { const addr = { chain: chain, index: index, - publicKey: indexNode.publicKey } switch (type) { @@ -63,6 +62,7 @@ async function deriveAddresses(msg) { const chain = msg.chain const indices = msg.indices const type = msg.type + const isPostmixChange = msg.isPostmixChange // Parse input as an HD Node. Throws if invalid const node = bitcoin.bip32.fromBase58(xpub, activeNet) @@ -77,6 +77,14 @@ async function deriveAddresses(msg) { return deriveAddress(chain, chainNode, index, type) }) + // Generate additional change address types for postmix account + if (isPostmixChange) { + indices.forEach((index) => { + promises.push(deriveAddress(chain, chainNode, index, BIP44)) + promises.push(deriveAddress(chain, chainNode, index, BIP49)) + }) + } + const addresses = await Promise.all(promises) // Send response to parent process