|
|
@ -7,7 +7,9 @@ |
|
|
|
const bitcoin = require('bitcoinjs-lib') |
|
|
|
const btcMessage = require('bitcoinjs-message') |
|
|
|
const activeNet = require('./network').network |
|
|
|
const { p2pkh, p2sh, p2wpkh } = bitcoin.payments |
|
|
|
const { p2pkh, p2sh, p2wpkh, p2wsh } = bitcoin.payments |
|
|
|
const { OPS } = bitcoin.script |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* A singleton providing Addresses helper functions |
|
|
@ -106,6 +108,86 @@ class AddressesHelper { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Check if an output script is a P2PKH script |
|
|
|
* @param {Buffer} scriptpubkey - scriptpubkey |
|
|
|
* @returns {boolean} return true if output is a P2PKH script, otherwise return false |
|
|
|
*/ |
|
|
|
isP2pkhScript(scriptpubkey) { |
|
|
|
return scriptpubkey.length == 25 |
|
|
|
&& scriptpubkey[0] == OPS.OP_DUP |
|
|
|
&& scriptpubkey[1] == OPS.OP_HASH160 |
|
|
|
&& scriptpubkey[2] == 0x14 |
|
|
|
&& scriptpubkey[23] == OPS.OP_EQUALVERIFY |
|
|
|
&& scriptpubkey[24] == OPS.OP_CHECKSIG |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Check if an output script is a P2SH script |
|
|
|
* @param {Buffer} scriptpubkey - scriptpubkey |
|
|
|
* @returns {boolean} return true if output is a P2SH script, otherwise return false |
|
|
|
*/ |
|
|
|
isP2shScript(scriptpubkey) { |
|
|
|
return scriptpubkey.length == 23 |
|
|
|
&& scriptpubkey[0] == OPS.OP_HASH160 |
|
|
|
&& scriptpubkey[1] == 0x14 |
|
|
|
&& scriptpubkey[22] == OPS.OP_EQUAL |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Check if an output script is a P2WPKH script |
|
|
|
* @param {Buffer} scriptpubkey - scriptpubkey |
|
|
|
* @returns {boolean} return true if output is a P2WPKH script, otherwise return false |
|
|
|
*/ |
|
|
|
isP2wpkhScript(scriptpubkey) { |
|
|
|
return scriptpubkey.length == 22 |
|
|
|
&& scriptpubkey[0] == OPS.OP_0 |
|
|
|
&& scriptpubkey[1] == 0x14 |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Check if an output script is a P2WSH script |
|
|
|
* @param {Buffer} scriptpubkey - scriptpubkey |
|
|
|
* @returns {boolean} return true if output is a P2WSH script, otherwise return false |
|
|
|
*/ |
|
|
|
isP2wshScript(scriptpubkey) { |
|
|
|
return scriptpubkey.length == 34 |
|
|
|
&& scriptpubkey[0] == OPS.OP_0 |
|
|
|
&& scriptpubkey[1] == 0x20 |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Return the bitcoin address corresponding to an output script |
|
|
|
* @param {Buffer} scriptpubkey - scriptpubkey |
|
|
|
* @returns {string} bitcoin address |
|
|
|
*/ |
|
|
|
outputScript2Address(scriptpubkey) { |
|
|
|
if (this.isP2pkhScript(scriptpubkey)) |
|
|
|
return p2pkh({ |
|
|
|
output: scriptpubkey, |
|
|
|
network: activeNet, |
|
|
|
}).address |
|
|
|
|
|
|
|
if (this.isP2shScript(scriptpubkey)) |
|
|
|
return p2sh({ |
|
|
|
output: scriptpubkey, |
|
|
|
network: activeNet, |
|
|
|
}).address |
|
|
|
|
|
|
|
if (this.isP2wpkhScript(scriptpubkey)) |
|
|
|
return p2wpkh({ |
|
|
|
output: scriptpubkey, |
|
|
|
network: activeNet, |
|
|
|
}).address |
|
|
|
|
|
|
|
if (this.isP2wshScript(scriptpubkey)) |
|
|
|
return p2wsh({ |
|
|
|
output: scriptpubkey, |
|
|
|
network: activeNet, |
|
|
|
}).address |
|
|
|
|
|
|
|
throw 'unknown address format' |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
module.exports = new AddressesHelper() |
|
|
|