@btc-vision/bitcoin 6.3.5 → 6.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/.mocharc.json +13 -0
  2. package/browser/address.d.ts +1 -1
  3. package/browser/index.js +1 -1
  4. package/browser/index.js.LICENSE.txt +3 -3
  5. package/browser/networks.d.ts +1 -0
  6. package/browser/psbt/psbtutils.d.ts +1 -1
  7. package/build/address.d.ts +1 -1
  8. package/build/address.js +12 -5
  9. package/build/block.js +2 -2
  10. package/build/bufferutils.js +5 -5
  11. package/build/networks.d.ts +1 -0
  12. package/build/networks.js +11 -0
  13. package/build/psbt/psbtutils.js +2 -2
  14. package/build/psbt.js +3 -7
  15. package/package.json +26 -26
  16. package/src/address.ts +20 -6
  17. package/src/block.ts +233 -233
  18. package/src/bufferutils.ts +188 -180
  19. package/src/index.ts +86 -86
  20. package/src/networks.ts +12 -0
  21. package/src/psbt/bip371.ts +441 -441
  22. package/src/psbt/psbtutils.ts +4 -3
  23. package/src/psbt.ts +2187 -2187
  24. package/test/address.spec.ts +155 -177
  25. package/test/bitcoin.core.spec.ts +212 -234
  26. package/test/block.spec.ts +171 -194
  27. package/test/bufferutils.spec.ts +450 -513
  28. package/test/crypto.spec.ts +49 -55
  29. package/test/fixtures/address.json +3 -3
  30. package/test/integration/addresses.spec.ts +142 -154
  31. package/test/integration/bip32.spec.ts +130 -151
  32. package/test/integration/blocks.spec.ts +28 -28
  33. package/test/integration/cltv.spec.ts +241 -283
  34. package/test/integration/csv.spec.ts +452 -527
  35. package/test/integration/payments.spec.ts +110 -135
  36. package/test/integration/taproot.spec.ts +663 -707
  37. package/test/integration/transactions.spec.ts +668 -769
  38. package/test/payments.spec.ts +114 -125
  39. package/test/payments.utils.ts +165 -208
  40. package/test/psbt.spec.ts +1285 -1414
  41. package/test/script.spec.ts +186 -210
  42. package/test/script_number.spec.ts +26 -29
  43. package/test/script_signature.spec.ts +66 -66
  44. package/test/transaction.spec.ts +337 -387
  45. package/test/ts-node-register.js +7 -5
  46. package/test/tsconfig.json +4 -1
  47. package/test/types.spec.ts +53 -58
  48. package/.nyc_output/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +0 -1
  49. package/.nyc_output/processinfo/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +0 -1
  50. package/.nyc_output/processinfo/index.json +0 -1
  51. package/test/address.spec.js +0 -124
  52. package/test/bitcoin.core.spec.js +0 -170
  53. package/test/block.spec.js +0 -141
  54. package/test/bufferutils.spec.js +0 -427
  55. package/test/crypto.spec.js +0 -41
  56. package/test/integration/_regtest.js +0 -7
  57. package/test/integration/addresses.spec.js +0 -116
  58. package/test/integration/bip32.spec.js +0 -85
  59. package/test/integration/blocks.spec.js +0 -26
  60. package/test/integration/cltv.spec.js +0 -199
  61. package/test/integration/csv.spec.js +0 -362
  62. package/test/integration/payments.spec.js +0 -98
  63. package/test/integration/taproot.spec.js +0 -532
  64. package/test/integration/transactions.spec.js +0 -561
  65. package/test/payments.spec.js +0 -97
  66. package/test/payments.utils.js +0 -190
  67. package/test/psbt.spec.js +0 -1044
  68. package/test/script.spec.js +0 -151
  69. package/test/script_number.spec.js +0 -24
  70. package/test/script_signature.spec.js +0 -52
  71. package/test/transaction.spec.js +0 -269
  72. package/test/types.spec.js +0 -46
@@ -1,55 +1,49 @@
1
- import * as assert from 'assert';
2
- import { describe, it } from 'mocha';
3
- import { crypto as bcrypto, TaggedHashPrefix } from '..';
4
- import * as fixtures from './fixtures/crypto.json';
5
- import { sha256, TAGGED_HASH_PREFIXES, TAGS } from '../src/crypto';
6
-
7
- describe('crypto', () => {
8
- ['hash160', 'hash256', 'ripemd160', 'sha1', 'sha256'].forEach(algorithm => {
9
- describe(algorithm, () => {
10
- fixtures.hashes.forEach(f => {
11
- const fn = (bcrypto as any)[algorithm];
12
- const expected = (f as any)[algorithm];
13
-
14
- it('returns ' + expected + ' for ' + f.hex, () => {
15
- const data = Buffer.from(f.hex, 'hex');
16
- const actual = fn(data).toString('hex');
17
-
18
- assert.strictEqual(actual, expected);
19
- });
20
- });
21
- });
22
- });
23
-
24
- describe('taggedHash', () => {
25
- fixtures.taggedHash.forEach(f => {
26
- const bytes = Buffer.from(f.hex, 'hex');
27
- const expected = Buffer.from(f.result, 'hex');
28
- it(`returns ${f.result} for taggedHash "${f.tag}" of ${f.hex}`, () => {
29
- const actual = bcrypto.taggedHash(
30
- f.tag as TaggedHashPrefix,
31
- bytes,
32
- );
33
- assert.strictEqual(
34
- actual.toString('hex'),
35
- expected.toString('hex'),
36
- );
37
- });
38
- });
39
- });
40
-
41
- describe('TAGGED_HASH_PREFIXES', () => {
42
- const taggedHashPrefixes = Object.fromEntries(
43
- TAGS.map((tag: TaggedHashPrefix) => {
44
- const tagHash = sha256(Buffer.from(tag));
45
- return [tag, Buffer.concat([tagHash, tagHash])];
46
- }),
47
- );
48
- it('stored the result of operation', () => {
49
- assert.strictEqual(
50
- JSON.stringify(TAGGED_HASH_PREFIXES),
51
- JSON.stringify(taggedHashPrefixes),
52
- );
53
- });
54
- });
55
- });
1
+ import assert from 'assert';
2
+ import { describe, it } from 'mocha';
3
+ import { crypto as bcrypto, TaggedHashPrefix } from '../src/index.js';
4
+ import fixtures from './fixtures/crypto.json' with { type: 'json' };
5
+ import { sha256, TAGGED_HASH_PREFIXES, TAGS } from '../src/crypto.js';
6
+
7
+ describe('crypto', () => {
8
+ ['hash160', 'hash256', 'ripemd160', 'sha1', 'sha256'].forEach((algorithm) => {
9
+ describe(algorithm, () => {
10
+ fixtures.hashes.forEach((f) => {
11
+ const fn = (bcrypto as any)[algorithm];
12
+ const expected = (f as any)[algorithm];
13
+
14
+ it('returns ' + expected + ' for ' + f.hex, () => {
15
+ const data = Buffer.from(f.hex, 'hex');
16
+ const actual = fn(data).toString('hex');
17
+
18
+ assert.strictEqual(actual, expected);
19
+ });
20
+ });
21
+ });
22
+ });
23
+
24
+ describe('taggedHash', () => {
25
+ fixtures.taggedHash.forEach((f) => {
26
+ const bytes = Buffer.from(f.hex, 'hex');
27
+ const expected = Buffer.from(f.result, 'hex');
28
+ it(`returns ${f.result} for taggedHash "${f.tag}" of ${f.hex}`, () => {
29
+ const actual = bcrypto.taggedHash(f.tag as TaggedHashPrefix, bytes);
30
+ assert.strictEqual(actual.toString('hex'), expected.toString('hex'));
31
+ });
32
+ });
33
+ });
34
+
35
+ describe('TAGGED_HASH_PREFIXES', () => {
36
+ const taggedHashPrefixes = Object.fromEntries(
37
+ TAGS.map((tag: TaggedHashPrefix) => {
38
+ const tagHash = sha256(Buffer.from(tag));
39
+ return [tag, Buffer.concat([tagHash, tagHash])];
40
+ }),
41
+ );
42
+ it('stored the result of operation', () => {
43
+ assert.strictEqual(
44
+ JSON.stringify(TAGGED_HASH_PREFIXES),
45
+ JSON.stringify(taggedHashPrefixes),
46
+ );
47
+ });
48
+ });
49
+ });
@@ -86,7 +86,7 @@
86
86
  },
87
87
  {
88
88
  "network": "bitcoin",
89
- "bech32": "BC1SW50QGDZ25J",
89
+ "bech32": "opnet1sw50qpgryz8",
90
90
  "version": 16,
91
91
  "data": "751e",
92
92
  "script": "OP_16 751e"
@@ -133,7 +133,7 @@
133
133
  "data": "751e76e8199196d454941c45d1b3a323"
134
134
  },
135
135
  {
136
- "address": "BC1SW50QGDZ25J",
136
+ "address": "bc1sw50qgdz25j",
137
137
  "version": 16,
138
138
  "prefix": "bc",
139
139
  "data": "751e"
@@ -222,7 +222,7 @@
222
222
  },
223
223
  {
224
224
  "exception": "has an invalid prefix",
225
- "address": "BC1SW50QGDZ25J",
225
+ "address": "bc1sw50qgdz25j",
226
226
  "network": {
227
227
  "bech32": "foo"
228
228
  }
@@ -1,154 +1,142 @@
1
- import * as assert from 'assert';
2
- import ECPairFactory from 'ecpair';
3
- import * as ecc from 'tiny-secp256k1';
4
- import { describe, it } from 'mocha';
5
- import * as bitcoin from '../..';
6
- import { regtestUtils } from './_regtest';
7
-
8
- const ECPair = ECPairFactory(ecc);
9
- const dhttp = regtestUtils.dhttp;
10
- const TESTNET = bitcoin.networks.testnet;
11
-
12
- describe('bitcoinjs-lib (addresses)', () => {
13
- it(
14
- 'can generate a random address [and support the retrieval of ' +
15
- 'transactions for that address (via 3PBP)]',
16
- async () => {
17
- const keyPair = ECPair.makeRandom();
18
- const { address } = bitcoin.payments.p2pkh({
19
- pubkey: keyPair.publicKey,
20
- });
21
-
22
- // bitcoin P2PKH addresses start with a '1'
23
- assert.strictEqual(address!.startsWith('1'), true);
24
-
25
- const result = await dhttp({
26
- method: 'GET',
27
- url: 'https://blockchain.info/rawaddr/' + address,
28
- });
29
-
30
- // random private keys [probably!] have no transactions
31
- assert.strictEqual((result as any).n_tx, 0);
32
- assert.strictEqual((result as any).total_received, 0);
33
- assert.strictEqual((result as any).total_sent, 0);
34
- },
35
- );
36
-
37
- it('can import an address via WIF', () => {
38
- const keyPair = ECPair.fromWIF(
39
- 'KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn',
40
- );
41
- const { address } = bitcoin.payments.p2pkh({
42
- pubkey: keyPair.publicKey,
43
- });
44
-
45
- assert.strictEqual(address, '1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH');
46
- });
47
-
48
- it('can generate a P2SH, pay-to-multisig (2-of-3) address', () => {
49
- const pubkeys = [
50
- '026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01',
51
- '02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9',
52
- '03c6103b3b83e4a24a0e33a4df246ef11772f9992663db0c35759a5e2ebf68d8e9',
53
- ].map(hex => Buffer.from(hex, 'hex'));
54
- const { address } = bitcoin.payments.p2sh({
55
- redeem: bitcoin.payments.p2ms({ m: 2, pubkeys }),
56
- });
57
-
58
- assert.strictEqual(address, '36NUkt6FWUi3LAWBqWRdDmdTWbt91Yvfu7');
59
- });
60
-
61
- it('can generate a SegWit address', () => {
62
- const keyPair = ECPair.fromWIF(
63
- 'KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn',
64
- );
65
- const { address } = bitcoin.payments.p2wpkh({
66
- pubkey: keyPair.publicKey,
67
- });
68
-
69
- assert.strictEqual(
70
- address,
71
- 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4',
72
- );
73
- });
74
-
75
- it('can generate a SegWit address (via P2SH)', () => {
76
- const keyPair = ECPair.fromWIF(
77
- 'KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn',
78
- );
79
- const { address } = bitcoin.payments.p2sh({
80
- redeem: bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey }),
81
- });
82
-
83
- assert.strictEqual(address, '3JvL6Ymt8MVWiCNHC7oWU6nLeHNJKLZGLN');
84
- });
85
-
86
- it('can generate a P2WSH (SegWit), pay-to-multisig (3-of-4) address', () => {
87
- const pubkeys = [
88
- '026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01',
89
- '02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9',
90
- '023e4740d0ba639e28963f3476157b7cf2fb7c6fdf4254f97099cf8670b505ea59',
91
- '03c6103b3b83e4a24a0e33a4df246ef11772f9992663db0c35759a5e2ebf68d8e9',
92
- ].map(hex => Buffer.from(hex, 'hex'));
93
- const { address } = bitcoin.payments.p2wsh({
94
- redeem: bitcoin.payments.p2ms({ m: 3, pubkeys }),
95
- });
96
-
97
- assert.strictEqual(
98
- address,
99
- 'bc1q75f6dv4q8ug7zhujrsp5t0hzf33lllnr3fe7e2pra3v24mzl8rrqtp3qul',
100
- );
101
- });
102
-
103
- it('can generate a P2SH(P2WSH(...)), pay-to-multisig (2-of-2) address', () => {
104
- const pubkeys = [
105
- '026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01',
106
- '02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9',
107
- ].map(hex => Buffer.from(hex, 'hex'));
108
- const { address } = bitcoin.payments.p2sh({
109
- redeem: bitcoin.payments.p2wsh({
110
- redeem: bitcoin.payments.p2ms({ m: 2, pubkeys }),
111
- }),
112
- });
113
-
114
- assert.strictEqual(address, '3P4mrxQfmExfhxqjLnR2Ah4WES5EB1KBrN');
115
- });
116
-
117
- // examples using other network information
118
- it('can generate a Testnet address', () => {
119
- const keyPair = ECPair.makeRandom({ network: TESTNET });
120
- const { address } = bitcoin.payments.p2pkh({
121
- pubkey: keyPair.publicKey,
122
- network: TESTNET,
123
- });
124
-
125
- // bitcoin testnet P2PKH addresses start with a 'm' or 'n'
126
- assert.strictEqual(
127
- address!.startsWith('m') || address!.startsWith('n'),
128
- true,
129
- );
130
- });
131
-
132
- it('can generate a Litecoin address', () => {
133
- // WARNING: although possible, bitcoinjs is NOT necessarily compatible with Litecoin
134
- const LITECOIN = {
135
- messagePrefix: '\x19Litecoin Signed Message:\n',
136
- bech32: 'ltc',
137
- bip32: {
138
- public: 0x019da462,
139
- private: 0x019d9cfe,
140
- },
141
- pubKeyHash: 0x30,
142
- scriptHash: 0x32,
143
- wif: 0xb0,
144
- };
145
-
146
- const keyPair = ECPair.makeRandom({ network: LITECOIN });
147
- const { address } = bitcoin.payments.p2pkh({
148
- pubkey: keyPair.publicKey,
149
- network: LITECOIN,
150
- });
151
-
152
- assert.strictEqual(address!.startsWith('L'), true);
153
- });
154
- });
1
+ import assert from 'assert';
2
+ import { ECPairFactory } from 'ecpair';
3
+ import * as ecc from 'tiny-secp256k1';
4
+ import { describe, it } from 'mocha';
5
+ import * as bitcoin from '../../src/index.js';
6
+ import { regtestUtils } from './_regtest.js';
7
+
8
+ const ECPair = ECPairFactory(ecc);
9
+ const dhttp = regtestUtils.dhttp;
10
+ const TESTNET = bitcoin.networks.testnet;
11
+
12
+ describe('bitcoinjs-lib (addresses)', () => {
13
+ it(
14
+ 'can generate a random address [and support the retrieval of ' +
15
+ 'transactions for that address (via 3PBP)]',
16
+ async () => {
17
+ const keyPair = ECPair.makeRandom();
18
+ const { address } = bitcoin.payments.p2pkh({
19
+ pubkey: keyPair.publicKey,
20
+ });
21
+
22
+ // bitcoin P2PKH addresses start with a '1'
23
+ assert.strictEqual(address!.startsWith('1'), true);
24
+
25
+ const result = await dhttp({
26
+ method: 'GET',
27
+ url: 'https://blockchain.info/rawaddr/' + address,
28
+ });
29
+
30
+ // random private keys [probably!] have no transactions
31
+ assert.strictEqual((result as any).n_tx, 0);
32
+ assert.strictEqual((result as any).total_received, 0);
33
+ assert.strictEqual((result as any).total_sent, 0);
34
+ },
35
+ );
36
+
37
+ it('can import an address via WIF', () => {
38
+ const keyPair = ECPair.fromWIF('KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn');
39
+ const { address } = bitcoin.payments.p2pkh({
40
+ pubkey: keyPair.publicKey,
41
+ });
42
+
43
+ assert.strictEqual(address, '1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH');
44
+ });
45
+
46
+ it('can generate a P2SH, pay-to-multisig (2-of-3) address', () => {
47
+ const pubkeys = [
48
+ '026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01',
49
+ '02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9',
50
+ '03c6103b3b83e4a24a0e33a4df246ef11772f9992663db0c35759a5e2ebf68d8e9',
51
+ ].map((hex) => Buffer.from(hex, 'hex'));
52
+ const { address } = bitcoin.payments.p2sh({
53
+ redeem: bitcoin.payments.p2ms({ m: 2, pubkeys }),
54
+ });
55
+
56
+ assert.strictEqual(address, '36NUkt6FWUi3LAWBqWRdDmdTWbt91Yvfu7');
57
+ });
58
+
59
+ it('can generate a SegWit address', () => {
60
+ const keyPair = ECPair.fromWIF('KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn');
61
+ const { address } = bitcoin.payments.p2wpkh({
62
+ pubkey: keyPair.publicKey,
63
+ });
64
+
65
+ assert.strictEqual(address, 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4');
66
+ });
67
+
68
+ it('can generate a SegWit address (via P2SH)', () => {
69
+ const keyPair = ECPair.fromWIF('KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn');
70
+ const { address } = bitcoin.payments.p2sh({
71
+ redeem: bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey }),
72
+ });
73
+
74
+ assert.strictEqual(address, '3JvL6Ymt8MVWiCNHC7oWU6nLeHNJKLZGLN');
75
+ });
76
+
77
+ it('can generate a P2WSH (SegWit), pay-to-multisig (3-of-4) address', () => {
78
+ const pubkeys = [
79
+ '026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01',
80
+ '02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9',
81
+ '023e4740d0ba639e28963f3476157b7cf2fb7c6fdf4254f97099cf8670b505ea59',
82
+ '03c6103b3b83e4a24a0e33a4df246ef11772f9992663db0c35759a5e2ebf68d8e9',
83
+ ].map((hex) => Buffer.from(hex, 'hex'));
84
+ const { address } = bitcoin.payments.p2wsh({
85
+ redeem: bitcoin.payments.p2ms({ m: 3, pubkeys }),
86
+ });
87
+
88
+ assert.strictEqual(
89
+ address,
90
+ 'bc1q75f6dv4q8ug7zhujrsp5t0hzf33lllnr3fe7e2pra3v24mzl8rrqtp3qul',
91
+ );
92
+ });
93
+
94
+ it('can generate a P2SH(P2WSH(...)), pay-to-multisig (2-of-2) address', () => {
95
+ const pubkeys = [
96
+ '026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01',
97
+ '02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9',
98
+ ].map((hex) => Buffer.from(hex, 'hex'));
99
+ const { address } = bitcoin.payments.p2sh({
100
+ redeem: bitcoin.payments.p2wsh({
101
+ redeem: bitcoin.payments.p2ms({ m: 2, pubkeys }),
102
+ }),
103
+ });
104
+
105
+ assert.strictEqual(address, '3P4mrxQfmExfhxqjLnR2Ah4WES5EB1KBrN');
106
+ });
107
+
108
+ // examples using other network information
109
+ it('can generate a Testnet address', () => {
110
+ const keyPair = ECPair.makeRandom({ network: TESTNET });
111
+ const { address } = bitcoin.payments.p2pkh({
112
+ pubkey: keyPair.publicKey,
113
+ network: TESTNET,
114
+ });
115
+
116
+ // bitcoin testnet P2PKH addresses start with a 'm' or 'n'
117
+ assert.strictEqual(address!.startsWith('m') || address!.startsWith('n'), true);
118
+ });
119
+
120
+ it('can generate a Litecoin address', () => {
121
+ // WARNING: although possible, bitcoinjs is NOT necessarily compatible with Litecoin
122
+ const LITECOIN = {
123
+ messagePrefix: '\x19Litecoin Signed Message:\n',
124
+ bech32: 'ltc',
125
+ bip32: {
126
+ public: 0x019da462,
127
+ private: 0x019d9cfe,
128
+ },
129
+ pubKeyHash: 0x30,
130
+ scriptHash: 0x32,
131
+ wif: 0xb0,
132
+ };
133
+
134
+ const keyPair = ECPair.makeRandom({ network: LITECOIN });
135
+ const { address } = bitcoin.payments.p2pkh({
136
+ pubkey: keyPair.publicKey,
137
+ network: LITECOIN,
138
+ });
139
+
140
+ assert.strictEqual(address!.startsWith('L'), true);
141
+ });
142
+ });