|
|
@ -2,34 +2,35 @@ const { describe, it } = require('mocha') |
|
|
|
const assert = require('assert') |
|
|
|
const bitcoin = require('../../') |
|
|
|
const dhttp = require('dhttp/200') |
|
|
|
|
|
|
|
const LITECOIN = { |
|
|
|
messagePrefix: '\x19Litecoin Signed Message:\n', |
|
|
|
bip32: { |
|
|
|
public: 0x019da462, |
|
|
|
private: 0x019d9cfe |
|
|
|
}, |
|
|
|
pubKeyHash: 0x30, |
|
|
|
scriptHash: 0x32, |
|
|
|
wif: 0xb0 |
|
|
|
} |
|
|
|
|
|
|
|
// deterministic RNG for testing only
|
|
|
|
function rng () { return Buffer.from('zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz') } |
|
|
|
const TESTNET = bitcoin.networks.testnet |
|
|
|
|
|
|
|
describe('bitcoinjs-lib (addresses)', function () { |
|
|
|
it('can generate a random address', function () { |
|
|
|
const keyPair = bitcoin.ECPair.makeRandom({ rng: rng }) |
|
|
|
it('can generate a random address [and support the retrieval of transactions for that address (via 3PBP)', function (done) { |
|
|
|
const keyPair = bitcoin.ECPair.makeRandom() |
|
|
|
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey }) |
|
|
|
|
|
|
|
assert.strictEqual(address, '1F5VhMHukdnUES9kfXqzPzMeF1GPHKiF64') |
|
|
|
// bitcoin P2PKH addresses start with a '1'
|
|
|
|
assert.strictEqual(address.startsWith('1'), true) |
|
|
|
|
|
|
|
dhttp({ |
|
|
|
method: 'GET', |
|
|
|
url: 'https://blockchain.info/rawaddr/' + address |
|
|
|
}, function (err, result) { |
|
|
|
if (err) return done(err) |
|
|
|
|
|
|
|
// random private keys [probably!] have no transactions
|
|
|
|
assert.strictEqual(result.n_tx, 0) |
|
|
|
assert.strictEqual(result.total_received, 0) |
|
|
|
assert.strictEqual(result.total_sent, 0) |
|
|
|
done() |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
it('can import an address via WIF', function () { |
|
|
|
const keyPair = bitcoin.ECPair.fromWIF('Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct') |
|
|
|
const keyPair = bitcoin.ECPair.fromWIF('KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn') |
|
|
|
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey }) |
|
|
|
|
|
|
|
assert.strictEqual(address, '19AAjaTUbRjQCMuVczepkoPswiZRhjtg31') |
|
|
|
assert.strictEqual(address, '1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH') |
|
|
|
}) |
|
|
|
|
|
|
|
it('can generate a P2SH, pay-to-multisig (2-of-3) address', function () { |
|
|
@ -46,19 +47,19 @@ describe('bitcoinjs-lib (addresses)', function () { |
|
|
|
}) |
|
|
|
|
|
|
|
it('can generate a SegWit address', function () { |
|
|
|
const keyPair = bitcoin.ECPair.fromWIF('Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct') |
|
|
|
const keyPair = bitcoin.ECPair.fromWIF('KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn') |
|
|
|
const { address } = bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey }) |
|
|
|
|
|
|
|
assert.strictEqual(address, 'bc1qt97wqg464zrhnx23upykca5annqvwkwujjglky') |
|
|
|
assert.strictEqual(address, 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4') |
|
|
|
}) |
|
|
|
|
|
|
|
it('can generate a SegWit address (via P2SH)', function () { |
|
|
|
const keyPair = bitcoin.ECPair.fromWIF('Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct') |
|
|
|
const keyPair = bitcoin.ECPair.fromWIF('KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn') |
|
|
|
const { address } = bitcoin.payments.p2sh({ |
|
|
|
redeem: bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey }) |
|
|
|
}) |
|
|
|
|
|
|
|
assert.strictEqual(address, '34AgLJhwXrvmkZS1o5TrcdeevMt22Nar53') |
|
|
|
assert.strictEqual(address, '3JvL6Ymt8MVWiCNHC7oWU6nLeHNJKLZGLN') |
|
|
|
}) |
|
|
|
|
|
|
|
it('can generate a P2WSH (SegWit), pay-to-multisig (3-of-4) address', function () { |
|
|
@ -89,41 +90,31 @@ describe('bitcoinjs-lib (addresses)', function () { |
|
|
|
assert.strictEqual(address, '3P4mrxQfmExfhxqjLnR2Ah4WES5EB1KBrN') |
|
|
|
}) |
|
|
|
|
|
|
|
it('can support the retrieval of transactions for an address (via 3PBP)', function (done) { |
|
|
|
const keyPair = bitcoin.ECPair.makeRandom() |
|
|
|
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey }) |
|
|
|
|
|
|
|
dhttp({ |
|
|
|
method: 'GET', |
|
|
|
url: 'https://blockchain.info/rawaddr/' + address |
|
|
|
}, function (err, result) { |
|
|
|
if (err) return done(err) |
|
|
|
|
|
|
|
// random private keys [probably!] have no transactions
|
|
|
|
assert.strictEqual(result.n_tx, 0) |
|
|
|
assert.strictEqual(result.total_received, 0) |
|
|
|
assert.strictEqual(result.total_sent, 0) |
|
|
|
done() |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
// other networks
|
|
|
|
// examples using other network information
|
|
|
|
it('can generate a Testnet address', function () { |
|
|
|
const testnet = bitcoin.networks.testnet |
|
|
|
const keyPair = bitcoin.ECPair.makeRandom({ network: testnet, rng: rng }) |
|
|
|
const wif = keyPair.toWIF() |
|
|
|
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey, network: testnet }) |
|
|
|
const keyPair = bitcoin.ECPair.makeRandom({ network: TESTNET }) |
|
|
|
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey, network: TESTNET }) |
|
|
|
|
|
|
|
assert.strictEqual(address, 'mubSzQNtZfDj1YdNP6pNDuZy6zs6GDn61L') |
|
|
|
assert.strictEqual(wif, 'cRgnQe9MUu1JznntrLaoQpB476M8PURvXVQB5R2eqms5tXnzNsrr') |
|
|
|
// bitcoin testnet P2PKH addresses start with a 'm' or 'n'
|
|
|
|
assert.strictEqual(address.startsWith('m') || address.startsWith('n'), true) |
|
|
|
}) |
|
|
|
|
|
|
|
it('can generate a Litecoin address', function () { |
|
|
|
const keyPair = bitcoin.ECPair.makeRandom({ network: LITECOIN, rng: rng }) |
|
|
|
const wif = keyPair.toWIF() |
|
|
|
// WARNING: although possible, bitcoinjs is NOT necessarily compatible with Litecoin
|
|
|
|
const LITECOIN = { |
|
|
|
messagePrefix: '\x19Litecoin Signed Message:\n', |
|
|
|
bip32: { |
|
|
|
public: 0x019da462, |
|
|
|
private: 0x019d9cfe |
|
|
|
}, |
|
|
|
pubKeyHash: 0x30, |
|
|
|
scriptHash: 0x32, |
|
|
|
wif: 0xb0 |
|
|
|
} |
|
|
|
|
|
|
|
const keyPair = bitcoin.ECPair.makeRandom({ network: LITECOIN }) |
|
|
|
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey, network: LITECOIN }) |
|
|
|
|
|
|
|
assert.strictEqual(address, 'LZJSxZbjqJ2XVEquqfqHg1RQTDdfST5PTn') |
|
|
|
assert.strictEqual(wif, 'T7A4PUSgTDHecBxW1ZiYFrDNRih2o7M8Gf9xpoCgudPF9gDiNvuS') |
|
|
|
assert.strictEqual(address.startsWith('L'), true) |
|
|
|
}) |
|
|
|
}) |
|
|
|