@btc-vision/bitcoin 6.3.6 → 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 (71) 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/build/address.d.ts +1 -1
  7. package/build/address.js +12 -5
  8. package/build/block.js +2 -2
  9. package/build/bufferutils.js +5 -5
  10. package/build/networks.d.ts +1 -0
  11. package/build/networks.js +11 -0
  12. package/build/psbt/psbtutils.js +2 -2
  13. package/build/psbt.js +3 -7
  14. package/package.json +26 -26
  15. package/src/address.ts +20 -6
  16. package/src/block.ts +233 -233
  17. package/src/bufferutils.ts +188 -180
  18. package/src/index.ts +86 -86
  19. package/src/networks.ts +12 -0
  20. package/src/psbt/bip371.ts +441 -441
  21. package/src/psbt/psbtutils.ts +4 -3
  22. package/src/psbt.ts +2187 -2187
  23. package/test/address.spec.ts +155 -177
  24. package/test/bitcoin.core.spec.ts +212 -234
  25. package/test/block.spec.ts +171 -194
  26. package/test/bufferutils.spec.ts +450 -513
  27. package/test/crypto.spec.ts +49 -55
  28. package/test/fixtures/address.json +3 -3
  29. package/test/integration/addresses.spec.ts +142 -154
  30. package/test/integration/bip32.spec.ts +130 -151
  31. package/test/integration/blocks.spec.ts +28 -28
  32. package/test/integration/cltv.spec.ts +241 -283
  33. package/test/integration/csv.spec.ts +452 -527
  34. package/test/integration/payments.spec.ts +110 -135
  35. package/test/integration/taproot.spec.ts +663 -707
  36. package/test/integration/transactions.spec.ts +668 -769
  37. package/test/payments.spec.ts +114 -125
  38. package/test/payments.utils.ts +165 -208
  39. package/test/psbt.spec.ts +1285 -1414
  40. package/test/script.spec.ts +186 -210
  41. package/test/script_number.spec.ts +26 -29
  42. package/test/script_signature.spec.ts +66 -66
  43. package/test/transaction.spec.ts +337 -387
  44. package/test/ts-node-register.js +7 -5
  45. package/test/tsconfig.json +4 -1
  46. package/test/types.spec.ts +53 -58
  47. package/.nyc_output/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +0 -1
  48. package/.nyc_output/processinfo/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +0 -1
  49. package/.nyc_output/processinfo/index.json +0 -1
  50. package/test/address.spec.js +0 -124
  51. package/test/bitcoin.core.spec.js +0 -170
  52. package/test/block.spec.js +0 -141
  53. package/test/bufferutils.spec.js +0 -427
  54. package/test/crypto.spec.js +0 -41
  55. package/test/integration/_regtest.js +0 -7
  56. package/test/integration/addresses.spec.js +0 -116
  57. package/test/integration/bip32.spec.js +0 -85
  58. package/test/integration/blocks.spec.js +0 -26
  59. package/test/integration/cltv.spec.js +0 -199
  60. package/test/integration/csv.spec.js +0 -362
  61. package/test/integration/payments.spec.js +0 -98
  62. package/test/integration/taproot.spec.js +0 -532
  63. package/test/integration/transactions.spec.js +0 -561
  64. package/test/payments.spec.js +0 -97
  65. package/test/payments.utils.js +0 -190
  66. package/test/psbt.spec.js +0 -1044
  67. package/test/script.spec.js +0 -151
  68. package/test/script_number.spec.js +0 -24
  69. package/test/script_signature.spec.js +0 -52
  70. package/test/transaction.spec.js +0 -269
  71. package/test/types.spec.js +0 -46
@@ -1,151 +1,130 @@
1
- import * as assert from 'assert';
2
- import BIP32Factory from 'bip32';
3
- import * as ecc from 'tiny-secp256k1';
4
- import * as bip39 from 'bip39';
5
- import { describe, it } from 'mocha';
6
- import * as bitcoin from '../..';
7
-
8
- const bip32 = BIP32Factory(ecc);
9
-
10
- function getAddress(node: any, network?: any): string {
11
- return bitcoin.payments.p2pkh({ pubkey: node.publicKey, network }).address!;
12
- }
13
-
14
- describe('bitcoinjs-lib (BIP32)', () => {
15
- it('can import a BIP32 testnet xpriv and export to WIF', () => {
16
- const xpriv =
17
- 'tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK';
18
- const node = bip32.fromBase58(xpriv, bitcoin.networks.testnet);
19
-
20
- assert.strictEqual(
21
- node.toWIF(),
22
- 'cQfoY67cetFNunmBUX5wJiw3VNoYx3gG9U9CAofKE6BfiV1fSRw7',
23
- );
24
- });
25
-
26
- it('can export a BIP32 xpriv, then import it', () => {
27
- const mnemonic =
28
- 'praise you muffin lion enable neck grocery crumble super myself license ghost';
29
- const seed = bip39.mnemonicToSeedSync(mnemonic);
30
- const node = bip32.fromSeed(seed);
31
- const strng = node.toBase58();
32
- const restored = bip32.fromBase58(strng);
33
-
34
- assert.strictEqual(getAddress(node), getAddress(restored)); // same public key
35
- assert.strictEqual(node.toWIF(), restored.toWIF()); // same private key
36
- });
37
-
38
- it('can export a BIP32 xpub', () => {
39
- const mnemonic =
40
- 'praise you muffin lion enable neck grocery crumble super myself license ghost';
41
- const seed = bip39.mnemonicToSeedSync(mnemonic);
42
- const node = bip32.fromSeed(seed);
43
- const strng = node.neutered().toBase58();
44
-
45
- assert.strictEqual(
46
- strng,
47
- 'xpub661MyMwAqRbcGhVeaVfEBA25e3cP9DsJQZoE8iep5fZSxy3TnPBNBgWnMZx56oreNc48ZoTkQfatNJ9VWnQ7ZcLZcVStpaXLTeG8bGrzX3n',
48
- );
49
- });
50
-
51
- it('can create a BIP32, bitcoin, account 0, external address', () => {
52
- const path = "m/0'/0/0";
53
- const root = bip32.fromSeed(
54
- Buffer.from(
55
- 'dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd',
56
- 'hex',
57
- ),
58
- );
59
-
60
- const child1 = root.derivePath(path);
61
-
62
- // option 2, manually
63
- const child1b = root.deriveHardened(0).derive(0).derive(0);
64
-
65
- assert.strictEqual(
66
- getAddress(child1),
67
- '1JHyB1oPXufr4FXkfitsjgNB5yRY9jAaa7',
68
- );
69
- assert.strictEqual(
70
- getAddress(child1b),
71
- '1JHyB1oPXufr4FXkfitsjgNB5yRY9jAaa7',
72
- );
73
- });
74
-
75
- it('can create a BIP44, bitcoin, account 0, external address', () => {
76
- const root = bip32.fromSeed(
77
- Buffer.from(
78
- 'dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd',
79
- 'hex',
80
- ),
81
- );
82
-
83
- const child1 = root.derivePath("m/44'/0'/0'/0/0");
84
-
85
- // option 2, manually
86
- const child1b = root
87
- .deriveHardened(44)
88
- .deriveHardened(0)
89
- .deriveHardened(0)
90
- .derive(0)
91
- .derive(0);
92
-
93
- assert.strictEqual(
94
- getAddress(child1),
95
- '12Tyvr1U8A3ped6zwMEU5M8cx3G38sP5Au',
96
- );
97
- assert.strictEqual(
98
- getAddress(child1b),
99
- '12Tyvr1U8A3ped6zwMEU5M8cx3G38sP5Au',
100
- );
101
- });
102
-
103
- it('can create a BIP49, bitcoin testnet, account 0, external address', () => {
104
- const mnemonic =
105
- 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about';
106
- const seed = bip39.mnemonicToSeedSync(mnemonic);
107
- const root = bip32.fromSeed(seed);
108
-
109
- const path = "m/49'/1'/0'/0/0";
110
- const child = root.derivePath(path);
111
-
112
- const { address } = bitcoin.payments.p2sh({
113
- redeem: bitcoin.payments.p2wpkh({
114
- pubkey: child.publicKey,
115
- network: bitcoin.networks.testnet,
116
- }),
117
- network: bitcoin.networks.testnet,
118
- });
119
- assert.strictEqual(address, '2Mww8dCYPUpKHofjgcXcBCEGmniw9CoaiD2');
120
- });
121
-
122
- it('can use BIP39 to generate BIP32 addresses', () => {
123
- // var mnemonic = bip39.generateMnemonic()
124
- const mnemonic =
125
- 'praise you muffin lion enable neck grocery crumble super myself license ghost';
126
- assert(bip39.validateMnemonic(mnemonic));
127
-
128
- const seed = bip39.mnemonicToSeedSync(mnemonic);
129
- const root = bip32.fromSeed(seed);
130
-
131
- // receive addresses
132
- assert.strictEqual(
133
- getAddress(root.derivePath("m/0'/0/0")),
134
- '1AVQHbGuES57wD68AJi7Gcobc3RZrfYWTC',
135
- );
136
- assert.strictEqual(
137
- getAddress(root.derivePath("m/0'/0/1")),
138
- '1Ad6nsmqDzbQo5a822C9bkvAfrYv9mc1JL',
139
- );
140
-
141
- // change addresses
142
- assert.strictEqual(
143
- getAddress(root.derivePath("m/0'/1/0")),
144
- '1349KVc5NgedaK7DvuD4xDFxL86QN1Hvdn',
145
- );
146
- assert.strictEqual(
147
- getAddress(root.derivePath("m/0'/1/1")),
148
- '1EAvj4edpsWcSer3duybAd4KiR4bCJW5J6',
149
- );
150
- });
151
- });
1
+ import assert from 'assert';
2
+ import { BIP32Factory } from 'bip32';
3
+ import * as ecc from 'tiny-secp256k1';
4
+ import * as bip39 from 'bip39';
5
+ import { describe, it } from 'mocha';
6
+ import * as bitcoin from '../../src/index.js';
7
+
8
+ const bip32 = BIP32Factory(ecc);
9
+
10
+ function getAddress(node: any, network?: any): string {
11
+ return bitcoin.payments.p2pkh({ pubkey: node.publicKey, network }).address!;
12
+ }
13
+
14
+ describe('bitcoinjs-lib (BIP32)', () => {
15
+ it('can import a BIP32 testnet xpriv and export to WIF', () => {
16
+ const xpriv =
17
+ 'tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK';
18
+ const node = bip32.fromBase58(xpriv, bitcoin.networks.testnet);
19
+
20
+ assert.strictEqual(node.toWIF(), 'cQfoY67cetFNunmBUX5wJiw3VNoYx3gG9U9CAofKE6BfiV1fSRw7');
21
+ });
22
+
23
+ it('can export a BIP32 xpriv, then import it', () => {
24
+ const mnemonic =
25
+ 'praise you muffin lion enable neck grocery crumble super myself license ghost';
26
+ const seed = bip39.mnemonicToSeedSync(mnemonic);
27
+ const node = bip32.fromSeed(seed);
28
+ const strng = node.toBase58();
29
+ const restored = bip32.fromBase58(strng);
30
+
31
+ assert.strictEqual(getAddress(node), getAddress(restored)); // same public key
32
+ assert.strictEqual(node.toWIF(), restored.toWIF()); // same private key
33
+ });
34
+
35
+ it('can export a BIP32 xpub', () => {
36
+ const mnemonic =
37
+ 'praise you muffin lion enable neck grocery crumble super myself license ghost';
38
+ const seed = bip39.mnemonicToSeedSync(mnemonic);
39
+ const node = bip32.fromSeed(seed);
40
+ const strng = node.neutered().toBase58();
41
+
42
+ assert.strictEqual(
43
+ strng,
44
+ 'xpub661MyMwAqRbcGhVeaVfEBA25e3cP9DsJQZoE8iep5fZSxy3TnPBNBgWnMZx56oreNc48ZoTkQfatNJ9VWnQ7ZcLZcVStpaXLTeG8bGrzX3n',
45
+ );
46
+ });
47
+
48
+ it('can create a BIP32, bitcoin, account 0, external address', () => {
49
+ const path = "m/0'/0/0";
50
+ const root = bip32.fromSeed(
51
+ Buffer.from('dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd', 'hex'),
52
+ );
53
+
54
+ const child1 = root.derivePath(path);
55
+
56
+ // option 2, manually
57
+ const child1b = root.deriveHardened(0).derive(0).derive(0);
58
+
59
+ assert.strictEqual(getAddress(child1), '1JHyB1oPXufr4FXkfitsjgNB5yRY9jAaa7');
60
+ assert.strictEqual(getAddress(child1b), '1JHyB1oPXufr4FXkfitsjgNB5yRY9jAaa7');
61
+ });
62
+
63
+ it('can create a BIP44, bitcoin, account 0, external address', () => {
64
+ const root = bip32.fromSeed(
65
+ Buffer.from('dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd', 'hex'),
66
+ );
67
+
68
+ const child1 = root.derivePath("m/44'/0'/0'/0/0");
69
+
70
+ // option 2, manually
71
+ const child1b = root
72
+ .deriveHardened(44)
73
+ .deriveHardened(0)
74
+ .deriveHardened(0)
75
+ .derive(0)
76
+ .derive(0);
77
+
78
+ assert.strictEqual(getAddress(child1), '12Tyvr1U8A3ped6zwMEU5M8cx3G38sP5Au');
79
+ assert.strictEqual(getAddress(child1b), '12Tyvr1U8A3ped6zwMEU5M8cx3G38sP5Au');
80
+ });
81
+
82
+ it('can create a BIP49, bitcoin testnet, account 0, external address', () => {
83
+ const mnemonic =
84
+ 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about';
85
+ const seed = bip39.mnemonicToSeedSync(mnemonic);
86
+ const root = bip32.fromSeed(seed);
87
+
88
+ const path = "m/49'/1'/0'/0/0";
89
+ const child = root.derivePath(path);
90
+
91
+ const { address } = bitcoin.payments.p2sh({
92
+ redeem: bitcoin.payments.p2wpkh({
93
+ pubkey: child.publicKey,
94
+ network: bitcoin.networks.testnet,
95
+ }),
96
+ network: bitcoin.networks.testnet,
97
+ });
98
+ assert.strictEqual(address, '2Mww8dCYPUpKHofjgcXcBCEGmniw9CoaiD2');
99
+ });
100
+
101
+ it('can use BIP39 to generate BIP32 addresses', () => {
102
+ // var mnemonic = bip39.generateMnemonic()
103
+ const mnemonic =
104
+ 'praise you muffin lion enable neck grocery crumble super myself license ghost';
105
+ assert(bip39.validateMnemonic(mnemonic));
106
+
107
+ const seed = bip39.mnemonicToSeedSync(mnemonic);
108
+ const root = bip32.fromSeed(seed);
109
+
110
+ // receive addresses
111
+ assert.strictEqual(
112
+ getAddress(root.derivePath("m/0'/0/0")),
113
+ '1AVQHbGuES57wD68AJi7Gcobc3RZrfYWTC',
114
+ );
115
+ assert.strictEqual(
116
+ getAddress(root.derivePath("m/0'/0/1")),
117
+ '1Ad6nsmqDzbQo5a822C9bkvAfrYv9mc1JL',
118
+ );
119
+
120
+ // change addresses
121
+ assert.strictEqual(
122
+ getAddress(root.derivePath("m/0'/1/0")),
123
+ '1349KVc5NgedaK7DvuD4xDFxL86QN1Hvdn',
124
+ );
125
+ assert.strictEqual(
126
+ getAddress(root.derivePath("m/0'/1/1")),
127
+ '1EAvj4edpsWcSer3duybAd4KiR4bCJW5J6',
128
+ );
129
+ });
130
+ });
@@ -1,28 +1,28 @@
1
- import * as assert from 'assert';
2
- import { describe, it } from 'mocha';
3
- import * as bitcoin from '../..';
4
-
5
- describe('bitcoinjs-lib (blocks)', () => {
6
- it('can extract a height from a CoinBase transaction', () => {
7
- // from 00000000000000000097669cdca131f24d40c4cc7d80eaa65967a2d09acf6ce6
8
- const txHex =
9
- '010000000001010000000000000000000000000000000000000000000000000000000' +
10
- '000000000ffffffff50037f9a07174d696e656420627920416e74506f6f6c685b205a' +
11
- '2b1f7bfabe6d6d36afe1910eca9405b66f97750940a656e38e2c0312958190ff8e98f' +
12
- 'd16761d220400000000000000aa340000d49f0000ffffffff02b07fc3660000000019' +
13
- '76a9148349212dc27ce3ab4c5b29b85c4dec643d764b1788ac0000000000000000266' +
14
- 'a24aa21a9ed72d9432948505e3d3062f1307a3f027a5dea846ff85e47159680919c12' +
15
- 'bf1e40012000000000000000000000000000000000000000000000000000000000000' +
16
- '0000000000000';
17
- const tx = bitcoin.Transaction.fromHex(txHex);
18
-
19
- assert.strictEqual(tx.ins.length, 1);
20
- const script = tx.ins[0].script;
21
- // bitcoin.script.decompile(script) // returns [] :(
22
-
23
- assert.strictEqual(script[0], 0x03);
24
- const heightBuffer = script.slice(1, 4);
25
- const height = bitcoin.script.number.decode(heightBuffer);
26
- assert.strictEqual(height, 498303);
27
- });
28
- });
1
+ import assert from 'assert';
2
+ import { describe, it } from 'mocha';
3
+ import * as bitcoin from '../../src/index.js';
4
+
5
+ describe('bitcoinjs-lib (blocks)', () => {
6
+ it('can extract a height from a CoinBase transaction', () => {
7
+ // from 00000000000000000097669cdca131f24d40c4cc7d80eaa65967a2d09acf6ce6
8
+ const txHex =
9
+ '010000000001010000000000000000000000000000000000000000000000000000000' +
10
+ '000000000ffffffff50037f9a07174d696e656420627920416e74506f6f6c685b205a' +
11
+ '2b1f7bfabe6d6d36afe1910eca9405b66f97750940a656e38e2c0312958190ff8e98f' +
12
+ 'd16761d220400000000000000aa340000d49f0000ffffffff02b07fc3660000000019' +
13
+ '76a9148349212dc27ce3ab4c5b29b85c4dec643d764b1788ac0000000000000000266' +
14
+ 'a24aa21a9ed72d9432948505e3d3062f1307a3f027a5dea846ff85e47159680919c12' +
15
+ 'bf1e40012000000000000000000000000000000000000000000000000000000000000' +
16
+ '0000000000000';
17
+ const tx = bitcoin.Transaction.fromHex(txHex);
18
+
19
+ assert.strictEqual(tx.ins.length, 1);
20
+ const script = tx.ins[0].script;
21
+ // bitcoin.script.decompile(script) // returns [] :(
22
+
23
+ assert.strictEqual(script[0], 0x03);
24
+ const heightBuffer = script.slice(1, 4);
25
+ const height = bitcoin.script.number.decode(heightBuffer);
26
+ assert.strictEqual(height, 498303);
27
+ });
28
+ });