|
@ -18,18 +18,10 @@ const LITECOIN = { |
|
|
// deterministic RNG for testing only
|
|
|
// deterministic RNG for testing only
|
|
|
function rng () { return Buffer.from('zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz') } |
|
|
function rng () { return Buffer.from('zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz') } |
|
|
|
|
|
|
|
|
// TODO: remove
|
|
|
|
|
|
const baddress = bitcoin.address |
|
|
|
|
|
const bcrypto = bitcoin.crypto |
|
|
|
|
|
function getAddress (node, network) { |
|
|
|
|
|
network = network || bitcoin.networks.bitcoin |
|
|
|
|
|
return baddress.toBase58Check(bcrypto.hash160(node.publicKey), network.pubKeyHash) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
describe('bitcoinjs-lib (addresses)', function () { |
|
|
describe('bitcoinjs-lib (addresses)', function () { |
|
|
it('can generate a random address', function () { |
|
|
it('can generate a random address', function () { |
|
|
const keyPair = bitcoin.ECPair.makeRandom({ rng: rng }) |
|
|
const keyPair = bitcoin.ECPair.makeRandom({ rng: rng }) |
|
|
const address = getAddress(keyPair) |
|
|
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey }) |
|
|
|
|
|
|
|
|
assert.strictEqual(address, '1F5VhMHukdnUES9kfXqzPzMeF1GPHKiF64') |
|
|
assert.strictEqual(address, '1F5VhMHukdnUES9kfXqzPzMeF1GPHKiF64') |
|
|
}) |
|
|
}) |
|
@ -38,7 +30,7 @@ describe('bitcoinjs-lib (addresses)', function () { |
|
|
const hash = bitcoin.crypto.sha256(Buffer.from('correct horse battery staple')) |
|
|
const hash = bitcoin.crypto.sha256(Buffer.from('correct horse battery staple')) |
|
|
|
|
|
|
|
|
const keyPair = bitcoin.ECPair.fromPrivateKey(hash) |
|
|
const keyPair = bitcoin.ECPair.fromPrivateKey(hash) |
|
|
const address = getAddress(keyPair) |
|
|
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey }) |
|
|
|
|
|
|
|
|
// Generating addresses from SHA256 hashes is not secure if the input to the hash function is predictable
|
|
|
// Generating addresses from SHA256 hashes is not secure if the input to the hash function is predictable
|
|
|
// Do not use with predictable inputs
|
|
|
// Do not use with predictable inputs
|
|
@ -47,76 +39,71 @@ describe('bitcoinjs-lib (addresses)', function () { |
|
|
|
|
|
|
|
|
it('can import an address via WIF', function () { |
|
|
it('can import an address via WIF', function () { |
|
|
const keyPair = bitcoin.ECPair.fromWIF('Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct') |
|
|
const keyPair = bitcoin.ECPair.fromWIF('Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct') |
|
|
const address = getAddress(keyPair) |
|
|
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey }) |
|
|
|
|
|
|
|
|
assert.strictEqual(address, '19AAjaTUbRjQCMuVczepkoPswiZRhjtg31') |
|
|
assert.strictEqual(address, '19AAjaTUbRjQCMuVczepkoPswiZRhjtg31') |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
it('can generate a 2-of-3 multisig P2SH address', function () { |
|
|
it('can generate a P2SH, pay-to-multisig (2-of-3) address', function () { |
|
|
const pubKeys = [ |
|
|
const pubkeys = [ |
|
|
'026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01', |
|
|
'026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01', |
|
|
'02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9', |
|
|
'02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9', |
|
|
'03c6103b3b83e4a24a0e33a4df246ef11772f9992663db0c35759a5e2ebf68d8e9' |
|
|
'03c6103b3b83e4a24a0e33a4df246ef11772f9992663db0c35759a5e2ebf68d8e9' |
|
|
].map(function (hex) { return Buffer.from(hex, 'hex') }) |
|
|
].map((hex) => Buffer.from(hex, 'hex')) |
|
|
|
|
|
const { address } = bitcoin.payments.p2sh({ |
|
|
const redeemScript = bitcoin.script.multisig.output.encode(2, pubKeys) // 2 of 3
|
|
|
redeem: bitcoin.payments.p2ms({ m: 2, pubkeys }) |
|
|
const scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript)) |
|
|
}) |
|
|
const address = bitcoin.address.fromOutputScript(scriptPubKey) |
|
|
|
|
|
|
|
|
|
|
|
assert.strictEqual(address, '36NUkt6FWUi3LAWBqWRdDmdTWbt91Yvfu7') |
|
|
assert.strictEqual(address, '36NUkt6FWUi3LAWBqWRdDmdTWbt91Yvfu7') |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
it('can generate a SegWit address', function () { |
|
|
it('can generate a SegWit address', function () { |
|
|
const keyPair = bitcoin.ECPair.fromWIF('Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct') |
|
|
const keyPair = bitcoin.ECPair.fromWIF('Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct') |
|
|
|
|
|
const { address } = bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey }) |
|
|
const scriptPubKey = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(keyPair.publicKey)) |
|
|
|
|
|
const address = bitcoin.address.fromOutputScript(scriptPubKey) |
|
|
|
|
|
|
|
|
|
|
|
assert.strictEqual(address, 'bc1qt97wqg464zrhnx23upykca5annqvwkwujjglky') |
|
|
assert.strictEqual(address, 'bc1qt97wqg464zrhnx23upykca5annqvwkwujjglky') |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
it('can generate a SegWit address (via P2SH)', function () { |
|
|
it('can generate a SegWit address (via P2SH)', function () { |
|
|
const keyPair = bitcoin.ECPair.fromWIF('Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct') |
|
|
const keyPair = bitcoin.ECPair.fromWIF('Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct') |
|
|
|
|
|
const { address } = bitcoin.payments.p2sh({ |
|
|
const redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(keyPair.publicKey)) |
|
|
redeem: bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey }) |
|
|
const scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript)) |
|
|
}) |
|
|
const address = bitcoin.address.fromOutputScript(scriptPubKey) |
|
|
|
|
|
|
|
|
|
|
|
assert.strictEqual(address, '34AgLJhwXrvmkZS1o5TrcdeevMt22Nar53') |
|
|
assert.strictEqual(address, '34AgLJhwXrvmkZS1o5TrcdeevMt22Nar53') |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
it('can generate a SegWit 3-of-4 multisig address', function () { |
|
|
it('can generate a P2WSH (SegWit), pay-to-multisig (3-of-4) address', function () { |
|
|
const pubKeys = [ |
|
|
const pubkeys = [ |
|
|
'026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01', |
|
|
'026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01', |
|
|
'02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9', |
|
|
'02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9', |
|
|
'023e4740d0ba639e28963f3476157b7cf2fb7c6fdf4254f97099cf8670b505ea59', |
|
|
'023e4740d0ba639e28963f3476157b7cf2fb7c6fdf4254f97099cf8670b505ea59', |
|
|
'03c6103b3b83e4a24a0e33a4df246ef11772f9992663db0c35759a5e2ebf68d8e9' |
|
|
'03c6103b3b83e4a24a0e33a4df246ef11772f9992663db0c35759a5e2ebf68d8e9' |
|
|
].map(function (hex) { return Buffer.from(hex, 'hex') }) |
|
|
].map((hex) => Buffer.from(hex, 'hex')) |
|
|
|
|
|
const { address } = bitcoin.payments.p2wsh({ |
|
|
const witnessScript = bitcoin.script.multisig.output.encode(3, pubKeys) // 3 of 4
|
|
|
redeem: bitcoin.payments.p2ms({ m: 3, pubkeys }) |
|
|
const scriptPubKey = bitcoin.script.witnessScriptHash.output.encode(bitcoin.crypto.sha256(witnessScript)) |
|
|
}) |
|
|
const address = bitcoin.address.fromOutputScript(scriptPubKey) |
|
|
|
|
|
|
|
|
|
|
|
assert.strictEqual(address, 'bc1q75f6dv4q8ug7zhujrsp5t0hzf33lllnr3fe7e2pra3v24mzl8rrqtp3qul') |
|
|
assert.strictEqual(address, 'bc1q75f6dv4q8ug7zhujrsp5t0hzf33lllnr3fe7e2pra3v24mzl8rrqtp3qul') |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
it('can generate a SegWit 2-of-2 multisig address (via P2SH)', function () { |
|
|
it('can generate a P2SH(P2WSH(...)), pay-to-multisig (2-of-2) address', function () { |
|
|
const pubKeys = [ |
|
|
const pubkeys = [ |
|
|
'026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01', |
|
|
'026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01', |
|
|
'02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9' |
|
|
'02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9' |
|
|
].map(function (hex) { return Buffer.from(hex, 'hex') }) |
|
|
].map((hex) => Buffer.from(hex, 'hex')) |
|
|
|
|
|
const { address } = bitcoin.payments.p2sh({ |
|
|
const witnessScript = bitcoin.script.multisig.output.encode(2, pubKeys) // 2 of 2
|
|
|
redeem: bitcoin.payments.p2wsh({ |
|
|
const redeemScript = bitcoin.script.witnessScriptHash.output.encode(bitcoin.crypto.sha256(witnessScript)) |
|
|
redeem: bitcoin.payments.p2ms({ m: 2, pubkeys }) |
|
|
const scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript)) |
|
|
}) |
|
|
const address = bitcoin.address.fromOutputScript(scriptPubKey) |
|
|
}) |
|
|
|
|
|
|
|
|
assert.strictEqual(address, '3P4mrxQfmExfhxqjLnR2Ah4WES5EB1KBrN') |
|
|
assert.strictEqual(address, '3P4mrxQfmExfhxqjLnR2Ah4WES5EB1KBrN') |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
it('can support the retrieval of transactions for an address (via 3PBP)', function (done) { |
|
|
it('can support the retrieval of transactions for an address (via 3PBP)', function (done) { |
|
|
const keyPair = bitcoin.ECPair.makeRandom() |
|
|
const keyPair = bitcoin.ECPair.makeRandom() |
|
|
const address = getAddress(keyPair) |
|
|
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey }) |
|
|
|
|
|
|
|
|
dhttp({ |
|
|
dhttp({ |
|
|
method: 'GET', |
|
|
method: 'GET', |
|
@ -137,7 +124,7 @@ describe('bitcoinjs-lib (addresses)', function () { |
|
|
const testnet = bitcoin.networks.testnet |
|
|
const testnet = bitcoin.networks.testnet |
|
|
const keyPair = bitcoin.ECPair.makeRandom({ network: testnet, rng: rng }) |
|
|
const keyPair = bitcoin.ECPair.makeRandom({ network: testnet, rng: rng }) |
|
|
const wif = keyPair.toWIF() |
|
|
const wif = keyPair.toWIF() |
|
|
const address = getAddress(keyPair, testnet) |
|
|
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey, network: testnet }) |
|
|
|
|
|
|
|
|
assert.strictEqual(address, 'mubSzQNtZfDj1YdNP6pNDuZy6zs6GDn61L') |
|
|
assert.strictEqual(address, 'mubSzQNtZfDj1YdNP6pNDuZy6zs6GDn61L') |
|
|
assert.strictEqual(wif, 'cRgnQe9MUu1JznntrLaoQpB476M8PURvXVQB5R2eqms5tXnzNsrr') |
|
|
assert.strictEqual(wif, 'cRgnQe9MUu1JznntrLaoQpB476M8PURvXVQB5R2eqms5tXnzNsrr') |
|
@ -146,7 +133,7 @@ describe('bitcoinjs-lib (addresses)', function () { |
|
|
it('can generate a Litecoin address', function () { |
|
|
it('can generate a Litecoin address', function () { |
|
|
const keyPair = bitcoin.ECPair.makeRandom({ network: LITECOIN, rng: rng }) |
|
|
const keyPair = bitcoin.ECPair.makeRandom({ network: LITECOIN, rng: rng }) |
|
|
const wif = keyPair.toWIF() |
|
|
const wif = keyPair.toWIF() |
|
|
const address = getAddress(keyPair, LITECOIN) |
|
|
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey, network: LITECOIN }) |
|
|
|
|
|
|
|
|
assert.strictEqual(address, 'LZJSxZbjqJ2XVEquqfqHg1RQTDdfST5PTn') |
|
|
assert.strictEqual(address, 'LZJSxZbjqJ2XVEquqfqHg1RQTDdfST5PTn') |
|
|
assert.strictEqual(wif, 'T7A4PUSgTDHecBxW1ZiYFrDNRih2o7M8Gf9xpoCgudPF9gDiNvuS') |
|
|
assert.strictEqual(wif, 'T7A4PUSgTDHecBxW1ZiYFrDNRih2o7M8Gf9xpoCgudPF9gDiNvuS') |
|
|