2019-09-07 06:42:03 +02:00
|
|
|
import * as assert from 'assert';
|
|
|
|
import { describe, it } from 'mocha';
|
|
|
|
import * as bitcoin from '../..';
|
|
|
|
import { regtestUtils } from './_regtest';
|
|
|
|
const dhttp = regtestUtils.dhttp;
|
|
|
|
const TESTNET = bitcoin.networks.testnet;
|
2018-06-05 09:21:43 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('bitcoinjs-lib (addresses)', () => {
|
2019-09-12 10:35:08 +02:00
|
|
|
it(
|
|
|
|
'can generate a random address [and support the retrieval of ' +
|
|
|
|
'transactions for that address (via 3PBP)]',
|
|
|
|
async () => {
|
|
|
|
const keyPair = bitcoin.ECPair.makeRandom();
|
|
|
|
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey });
|
|
|
|
|
|
|
|
// bitcoin P2PKH addresses start with a '1'
|
|
|
|
assert.strictEqual(address!.startsWith('1'), true);
|
|
|
|
|
|
|
|
const result = await dhttp({
|
|
|
|
method: 'GET',
|
|
|
|
url: 'https://blockchain.info/rawaddr/' + address,
|
|
|
|
});
|
|
|
|
|
|
|
|
// random private keys [probably!] have no transactions
|
|
|
|
assert.strictEqual((result as any).n_tx, 0);
|
|
|
|
assert.strictEqual((result as any).total_received, 0);
|
|
|
|
assert.strictEqual((result as any).total_sent, 0);
|
|
|
|
},
|
|
|
|
);
|
2017-08-09 08:03:12 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('can import an address via WIF', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const keyPair = bitcoin.ECPair.fromWIF(
|
|
|
|
'KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn',
|
|
|
|
);
|
|
|
|
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey });
|
2017-08-09 08:03:12 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(address, '1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH');
|
|
|
|
});
|
2017-08-09 08:03:12 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('can generate a P2SH, pay-to-multisig (2-of-3) address', () => {
|
2018-06-27 05:54:47 +02:00
|
|
|
const pubkeys = [
|
2017-08-09 08:03:12 +02:00
|
|
|
'026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01',
|
|
|
|
'02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9',
|
2019-09-07 06:42:03 +02:00
|
|
|
'03c6103b3b83e4a24a0e33a4df246ef11772f9992663db0c35759a5e2ebf68d8e9',
|
|
|
|
].map(hex => Buffer.from(hex, 'hex'));
|
2018-06-27 05:54:47 +02:00
|
|
|
const { address } = bitcoin.payments.p2sh({
|
2019-09-07 06:42:03 +02:00
|
|
|
redeem: bitcoin.payments.p2ms({ m: 2, pubkeys }),
|
|
|
|
});
|
2017-08-09 08:03:12 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(address, '36NUkt6FWUi3LAWBqWRdDmdTWbt91Yvfu7');
|
|
|
|
});
|
2017-08-09 08:03:12 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('can generate a SegWit address', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const keyPair = bitcoin.ECPair.fromWIF(
|
|
|
|
'KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn',
|
|
|
|
);
|
|
|
|
const { address } = bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey });
|
2017-08-21 03:34:57 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(address, 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4');
|
|
|
|
});
|
2017-08-21 03:34:57 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('can generate a SegWit address (via P2SH)', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const keyPair = bitcoin.ECPair.fromWIF(
|
|
|
|
'KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn',
|
|
|
|
);
|
2018-06-27 05:54:47 +02:00
|
|
|
const { address } = bitcoin.payments.p2sh({
|
2019-09-07 06:42:03 +02:00
|
|
|
redeem: bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey }),
|
|
|
|
});
|
2017-08-21 03:34:57 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(address, '3JvL6Ymt8MVWiCNHC7oWU6nLeHNJKLZGLN');
|
|
|
|
});
|
2017-08-21 03:34:57 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('can generate a P2WSH (SegWit), pay-to-multisig (3-of-4) address', () => {
|
2018-06-27 05:54:47 +02:00
|
|
|
const pubkeys = [
|
2017-08-21 03:34:57 +02:00
|
|
|
'026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01',
|
|
|
|
'02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9',
|
|
|
|
'023e4740d0ba639e28963f3476157b7cf2fb7c6fdf4254f97099cf8670b505ea59',
|
2019-09-07 06:42:03 +02:00
|
|
|
'03c6103b3b83e4a24a0e33a4df246ef11772f9992663db0c35759a5e2ebf68d8e9',
|
|
|
|
].map(hex => Buffer.from(hex, 'hex'));
|
2018-06-27 05:54:47 +02:00
|
|
|
const { address } = bitcoin.payments.p2wsh({
|
2019-09-07 06:42:03 +02:00
|
|
|
redeem: bitcoin.payments.p2ms({ m: 3, pubkeys }),
|
|
|
|
});
|
2017-08-21 03:34:57 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(
|
|
|
|
address,
|
|
|
|
'bc1q75f6dv4q8ug7zhujrsp5t0hzf33lllnr3fe7e2pra3v24mzl8rrqtp3qul',
|
|
|
|
);
|
|
|
|
});
|
2017-08-21 03:34:57 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('can generate a P2SH(P2WSH(...)), pay-to-multisig (2-of-2) address', () => {
|
2018-06-27 05:54:47 +02:00
|
|
|
const pubkeys = [
|
2017-08-21 03:34:57 +02:00
|
|
|
'026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01',
|
2019-09-07 06:42:03 +02:00
|
|
|
'02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9',
|
|
|
|
].map(hex => Buffer.from(hex, 'hex'));
|
2018-06-27 05:54:47 +02:00
|
|
|
const { address } = bitcoin.payments.p2sh({
|
|
|
|
redeem: bitcoin.payments.p2wsh({
|
2019-09-07 06:42:03 +02:00
|
|
|
redeem: bitcoin.payments.p2ms({ m: 2, pubkeys }),
|
|
|
|
}),
|
|
|
|
});
|
2017-08-21 03:34:57 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(address, '3P4mrxQfmExfhxqjLnR2Ah4WES5EB1KBrN');
|
|
|
|
});
|
2017-08-21 03:34:57 +02:00
|
|
|
|
2019-01-01 13:24:26 +01:00
|
|
|
// examples using other network information
|
2019-04-09 08:09:50 +02:00
|
|
|
it('can generate a Testnet address', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const keyPair = bitcoin.ECPair.makeRandom({ network: TESTNET });
|
|
|
|
const { address } = bitcoin.payments.p2pkh({
|
|
|
|
pubkey: keyPair.publicKey,
|
|
|
|
network: TESTNET,
|
|
|
|
});
|
2019-01-01 01:41:00 +01:00
|
|
|
|
2019-01-07 08:33:57 +01:00
|
|
|
// bitcoin testnet P2PKH addresses start with a 'm' or 'n'
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(
|
|
|
|
address!.startsWith('m') || address!.startsWith('n'),
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
});
|
2017-08-09 08:03:12 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('can generate a Litecoin address', () => {
|
2019-01-01 13:24:26 +01:00
|
|
|
// WARNING: although possible, bitcoinjs is NOT necessarily compatible with Litecoin
|
|
|
|
const LITECOIN = {
|
|
|
|
messagePrefix: '\x19Litecoin Signed Message:\n',
|
2019-09-07 06:42:03 +02:00
|
|
|
bech32: 'ltc',
|
2019-01-01 13:24:26 +01:00
|
|
|
bip32: {
|
|
|
|
public: 0x019da462,
|
2019-09-07 06:42:03 +02:00
|
|
|
private: 0x019d9cfe,
|
2019-01-01 13:24:26 +01:00
|
|
|
},
|
|
|
|
pubKeyHash: 0x30,
|
|
|
|
scriptHash: 0x32,
|
2019-09-07 06:42:03 +02:00
|
|
|
wif: 0xb0,
|
|
|
|
};
|
|
|
|
|
|
|
|
const keyPair = bitcoin.ECPair.makeRandom({ network: LITECOIN });
|
|
|
|
const { address } = bitcoin.payments.p2pkh({
|
|
|
|
pubkey: keyPair.publicKey,
|
|
|
|
network: LITECOIN,
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.strictEqual(address!.startsWith('L'), true);
|
|
|
|
});
|
|
|
|
});
|