Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to use latest bitcoinjs #71

Merged
merged 2 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions lib/bitcoin/addresses-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const bitcoin = require('bitcoinjs-lib')
const btcMessage = require('bitcoinjs-message')
const activeNet = require('./network').network

const { p2pkh, p2sh, p2wpkh } = bitcoin.payments

/**
* A singleton providing Addresses helper functions
Expand All @@ -20,8 +20,10 @@ class AddressesHelper {
* @returns {string} return the derived address
*/
p2pkhAddress(pubKeyBuffer) {
const pubKeyHash = bitcoin.crypto.hash160(pubKeyBuffer)
return bitcoin.address.toBase58Check(pubKeyHash, activeNet.pubKeyHash)
return p2pkh({
pubkey: pubKeyBuffer,
network: activeNet,
}).address
}

/**
Expand All @@ -30,11 +32,13 @@ class AddressesHelper {
* @returns {string} return the derived address
*/
p2wpkhP2shAddress(pubKeyBuffer) {
const pubKeyHash = bitcoin.crypto.hash160(pubKeyBuffer)
const witnessProgram = bitcoin.script.witnessPubKeyHash.output.encode(pubKeyHash)
const scriptPubKey = bitcoin.crypto.hash160(witnessProgram)
const outputScript = bitcoin.script.scriptHash.output.encode(scriptPubKey)
return bitcoin.address.fromOutputScript(outputScript, activeNet)
return p2sh({
redeem: p2wpkh({
pubkey: pubKeyBuffer,
network: activeNet,
}),
network: activeNet,
}).address
}

/**
Expand All @@ -43,9 +47,10 @@ class AddressesHelper {
* @returns {string} return the derived address
*/
p2wpkhAddress(pubKeyBuffer) {
const pubKeyHash = bitcoin.crypto.hash160(pubKeyBuffer)
const outputScript = bitcoin.script.witnessPubKeyHash.output.encode(pubKeyHash)
return bitcoin.address.fromOutputScript(outputScript, activeNet).toLowerCase()
return p2wpkh({
pubkey: pubKeyBuffer,
network: activeNet,
}).address.toLowerCase()
}

/**
Expand Down
34 changes: 17 additions & 17 deletions lib/bitcoin/hd-accounts-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class HDAccountsHelper {
}

/**
* Translates
* Translates
* - a xpub/ypub/zpub into a xpub
* - a tpub/upub/vpub into a tpub
* @param {string} xpub - extended public key to be translated
Expand All @@ -120,7 +120,7 @@ class HDAccountsHelper {
&& ver != this.MAGIC_TPUB
&& ver != this.MAGIC_YPUB
&& ver != this.MAGIC_UPUB
&& ver != this.MAGIC_ZPUB
&& ver != this.MAGIC_ZPUB
&& ver != this.MAGIC_VPUB
) {
//Logger.error(null, 'HdAccountsHelper.xlatXPUB() : Incorrect format')
Expand Down Expand Up @@ -177,20 +177,20 @@ class HDAccountsHelper {
}

let p = v

if (p >= this.LOCKED) {
ret.locked = true
p -= this.LOCKED
}

switch (p) {
case this.BIP44:
case this.BIP49:
case this.BIP84:
ret.type = p
break
}

return ret
}

Expand All @@ -201,16 +201,16 @@ class HDAccountsHelper {
* @returns {integer}
*/
makeType(type, locked) {
let p =
let p =
(type >= this.LOCKED)
? type - this.LOCKED
: type

locked = !!locked

if (locked)
p += this.LOCKED

return p
}

Expand Down Expand Up @@ -245,7 +245,7 @@ class HDAccountsHelper {
}

/**
* Checks if a hd account is a valid hdnode
* Checks if a hd account is a valid bip32
* @param {string} xpub - hd account
* @returns {boolean} returns true if hd account is valid, false otherwise
*/
Expand All @@ -258,7 +258,7 @@ class HDAccountsHelper {
const xlatedXpub = this.xlatXPUB(xpub)

// Parse input as an HD Node. Throws if invalid
const node = bitcoin.HDNode.fromBase58(xlatedXpub, activeNet)
const node = bitcoin.bip32.fromBase58(xlatedXpub, activeNet)

// Check and see if this is a private key
if (!node.isNeutered())
Expand All @@ -278,7 +278,7 @@ class HDAccountsHelper {
/**
* Get the hd node associated to an hd account
* @param {string} xpub - hd account
* @returns {HDNode}
* @returns {bip32}
*/
getNode(xpub) {
if (this.isValid(xpub))
Expand All @@ -291,7 +291,7 @@ class HDAccountsHelper {
* Derives an address for an hd account
* @param {int} chain - chain to be derived
* must have a value on [0,1] for BIP44/BIP49/BIP84 derivation
* @param {HDNode} chainNode - Parent HDNode used for derivation
* @param {bip32} chainNode - Parent bip32 used for derivation
* @param {int} index - index to be derived
* @param {int} type - type of derivation
* @returns {Promise - object} returns an object {address: '...', chain: <int>, index: <int>}
Expand All @@ -307,13 +307,13 @@ class HDAccountsHelper {

switch (type) {
case this.BIP44:
addr.address = indexNode.getAddress()
addr.address = addrHelper.p2pkhAddress(indexNode.publicKey)
break
case this.BIP49:
addr.address = addrHelper.p2wpkhP2shAddress(indexNode.getPublicKeyBuffer())
addr.address = addrHelper.p2wpkhP2shAddress(indexNode.publicKey)
break
case this.BIP84:
addr.address = addrHelper.p2wpkhAddress(indexNode.getPublicKeyBuffer())
addr.address = addrHelper.p2wpkhAddress(indexNode.publicKey)
break
}

Expand Down Expand Up @@ -382,7 +382,7 @@ class HDAccountsHelper {
Logger.error(null, 'A problem was met during parallel addresses derivation')
reject()
}

} catch(e) {
Logger.error(e, 'A problem was met during parallel addresses derivation')
reject(e)
Expand Down
12 changes: 6 additions & 6 deletions lib/bitcoin/parallel-address-derivation.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const BIP84 = 2
* Derives an address for an hd account
* @param {int} chain - chain to be derived
* must have a value on [0,1] for BIP44/BIP49/BIP84 derivation
* @param {HDNode} chainNode - Parent HDNode used for derivation
* @param {bip32} chainNode - Parent bip32 used for derivation
* @param {int} index - index to be derived
* @param {int} type - type of derivation
* @returns {Promise - object} returns an object {address: '...', chain: <int>, index: <int>}
Expand All @@ -37,13 +37,13 @@ const deriveAddress = async function(chain, chainNode, index, type) {

switch (type) {
case BIP44:
addr.address = indexNode.getAddress()
addr.address = addrHelper.p2pkhAddress(indexNode.publicKey)
break
case BIP49:
addr.address = addrHelper.p2wpkhP2shAddress(indexNode.getPublicKeyBuffer())
addr.address = addrHelper.p2wpkhP2shAddress(indexNode.publicKey)
break
case BIP84:
addr.address = addrHelper.p2wpkhAddress(indexNode.getPublicKeyBuffer())
addr.address = addrHelper.p2wpkhAddress(indexNode.publicKey)
break
}

Expand All @@ -61,7 +61,7 @@ process.on('message', async (msg) => {
const type = msg.type

// Parse input as an HD Node. Throws if invalid
const node = bitcoin.HDNode.fromBase58(xpub, activeNet)
const node = bitcoin.bip32.fromBase58(xpub, activeNet)

// Check and see if this is a private key
if (!node.isNeutered())
Expand All @@ -88,5 +88,5 @@ process.on('message', async (msg) => {
error: e
})
}

})
Loading