Browse Source

Derive postmix change decoy addresses more simply

Pavel Ševčík 3 years ago
parent
commit
5bc414c45a
No known key found for this signature in database GPG Key ID: CFA54E4C0CD58DF0
  1. 46
      lib/bitcoin/hd-accounts-helper.js
  2. 12
      lib/bitcoin/parallel-address-derivation.js

46
lib/bitcoin/hd-accounts-helper.js

@ -299,7 +299,7 @@ class HDAccountsHelper {
* @param {bitcoin.bip32.BIP32Interface} chainNode - Parent bip32 used for derivation * @param {bitcoin.bip32.BIP32Interface} chainNode - Parent bip32 used for derivation
* @param {number} index - index to be derived * @param {number} index - index to be derived
* @param {number} type - type of derivation * @param {number} type - type of derivation
* @returns {Promise<object>} returns an object {address: '...', chain: <int>, index: <int>, publicKey: <Buffer>, address: string } * @returns {Promise<object>} returns an object {address: '...', chain: <int>, index: <int>, address: string }
*/ */
async deriveAddress(chain, chainNode, index, type) { async deriveAddress(chain, chainNode, index, type) {
// Derive M/chain/index // Derive M/chain/index
@ -308,7 +308,6 @@ class HDAccountsHelper {
const addr = { const addr = {
chain: chain, chain: chain,
index: index, index: index,
publicKey: indexNode.publicKey
} }
switch (type) { switch (type) {
@ -363,17 +362,20 @@ class HDAccountsHelper {
) { ) {
// Few addresses to be derived or external derivation deactivated // Few addresses to be derived or external derivation deactivated
// Let's do it here // 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) return this.deriveAddress(chain, chainNode, index, info.type)
})) })
// Generate additional change address types for postmix account // Generate additional change address types for postmix account
if (this.isPostmixAcct(node) && chain === 1) { if (this.isPostmixAcct(node) && chain === 1) {
const additionalPostmixAddresses = this._generateAdditionalChangeAddresses(addresses) indices.forEach((index) => {
promises.push(this.deriveAddress(chain, chainNode, index, this.BIP44))
addresses.push(...additionalPostmixAddresses) promises.push(this.deriveAddress(chain, chainNode, index, this.BIP49))
})
} }
const addresses = await Promise.all(promises)
return addresses; return addresses;
} else { } else {
@ -385,19 +387,13 @@ class HDAccountsHelper {
xpub: this.xlatXPUB(xpub), xpub: this.xlatXPUB(xpub),
chain: chain, chain: chain,
indices: indices, indices: indices,
type: info.type type: info.type,
isPostmixChange: this.isPostmixAcct(node) && chain === 1
} }
const msg = await this.derivationPool.exec('deriveAddresses', [data]) const msg = await this.derivationPool.exec('deriveAddresses', [data])
if (msg.status === 'ok') { 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) resolve(msg.addresses)
} else { } else {
Logger.error(null, 'HdAccountsHelper : A problem was met during parallel addresses derivation') 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 * @description Detect postmix account
* @param {[bitcoin.bip32.BIP32Interface, bitcoin.bip32.BIP32Interface, bitcoin.bip32.BIP32Interface]} node - array of BIP32 node interfaces * @param {[bitcoin.bip32.BIP32Interface, bitcoin.bip32.BIP32Interface, bitcoin.bip32.BIP32Interface]} node - array of BIP32 node interfaces

12
lib/bitcoin/parallel-address-derivation.js

@ -25,7 +25,7 @@ const BIP84 = 2
* @param {bip32} chainNode - Parent bip32 used for derivation * @param {bip32} chainNode - Parent bip32 used for derivation
* @param {number} index - index to be derived * @param {number} index - index to be derived
* @param {number} type - type of derivation * @param {number} type - type of derivation
* @returns {Promise<object>} returns an object {address: '...', chain: <int>, index: <int>, publicKey: <Buffer>} * @returns {Promise<object>} returns an object {address: '...', chain: <int>, index: <int>}
*/ */
async function deriveAddress(chain, chainNode, index, type) { async function deriveAddress(chain, chainNode, index, type) {
// Derive M/chain/index // Derive M/chain/index
@ -34,7 +34,6 @@ async function deriveAddress(chain, chainNode, index, type) {
const addr = { const addr = {
chain: chain, chain: chain,
index: index, index: index,
publicKey: indexNode.publicKey
} }
switch (type) { switch (type) {
@ -63,6 +62,7 @@ async function deriveAddresses(msg) {
const chain = msg.chain const chain = msg.chain
const indices = msg.indices const indices = msg.indices
const type = msg.type const type = msg.type
const isPostmixChange = msg.isPostmixChange
// Parse input as an HD Node. Throws if invalid // Parse input as an HD Node. Throws if invalid
const node = bitcoin.bip32.fromBase58(xpub, activeNet) const node = bitcoin.bip32.fromBase58(xpub, activeNet)
@ -77,6 +77,14 @@ async function deriveAddresses(msg) {
return deriveAddress(chain, chainNode, index, type) 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) const addresses = await Promise.all(promises)
// Send response to parent process // Send response to parent process

Loading…
Cancel
Save