Browse Source

Update to use latest bitcoinjs

feat-mydojo_upgrade_explorer
junderw 5 years ago
parent
commit
73ba366d50
No known key found for this signature in database GPG Key ID: B256185D3A971908
  1. 21
      lib/bitcoin/addresses-helper.js
  2. 34
      lib/bitcoin/hd-accounts-helper.js
  3. 12
      lib/bitcoin/parallel-address-derivation.js
  4. 1607
      package-lock.json
  5. 4
      package.json

21
lib/bitcoin/addresses-helper.js

@ -7,7 +7,7 @@
const bitcoin = require('bitcoinjs-lib')
const btcMessage = require('bitcoinjs-message')
const activeNet = require('./network').network
const { p2sh, p2wpkh } = bitcoin.payments
/**
* A singleton providing Addresses helper functions
@ -30,11 +30,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
}
/**
@ -43,9 +45,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()
}
/**

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

@ -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
@ -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')
@ -177,12 +177,12 @@ class HDAccountsHelper {
}
let p = v
if (p >= this.LOCKED) {
ret.locked = true
p -= this.LOCKED
}
switch (p) {
case this.BIP44:
case this.BIP49:
@ -190,7 +190,7 @@ class HDAccountsHelper {
ret.type = p
break
}
return ret
}
@ -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
}
@ -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
*/
@ -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())
@ -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))
@ -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>}
@ -307,13 +307,13 @@ class HDAccountsHelper {
switch (type) {
case this.BIP44:
addr.address = indexNode.getAddress()
addr.address = bitcoin.payments.p2pkh({ pubkey: indexNode.publicKey, network: activeNet }).address
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
}
@ -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)

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

@ -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>}
@ -37,13 +37,13 @@ const deriveAddress = async function(chain, chainNode, index, type) {
switch (type) {
case BIP44:
addr.address = indexNode.getAddress()
addr.address = bitcoin.payments.p2pkh({ pubkey: indexNode.publicKey, network: activeNet }).address
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
}
@ -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())
@ -88,5 +88,5 @@ process.on('message', async (msg) => {
error: e
})
}
})

1607
package-lock.json

File diff suppressed because it is too large

4
package.json

@ -17,7 +17,7 @@
"async-sema": "2.1.2",
"bip39": "2.4.0",
"bitcoind-rpc-client": "0.3.1",
"bitcoinjs-lib": "3.2.0",
"bitcoinjs-lib": "5.1.4",
"bitcoinjs-message": "1.0.1",
"body-parser": "1.18.3",
"express": "4.16.3",
@ -37,6 +37,6 @@
"zeromq": "4.2.0"
},
"devDependencies": {
"mocha": "^3.5.0"
"mocha": "^6.2.0"
}
}

Loading…
Cancel
Save