@btc-vision/bitcoin 6.4.0 → 6.4.2

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 (45) hide show
  1. package/browser/address.d.ts +1 -0
  2. package/browser/index.js +1 -1
  3. package/browser/payments/index.d.ts +4 -0
  4. package/browser/payments/p2op.d.ts +2 -0
  5. package/browser/psbt/psbtutils.d.ts +1 -0
  6. package/build/address.d.ts +1 -0
  7. package/build/address.js +56 -8
  8. package/build/networks.js +6 -6
  9. package/build/payments/index.d.ts +4 -0
  10. package/build/payments/index.js +1 -0
  11. package/build/payments/p2op.d.ts +2 -0
  12. package/build/payments/p2op.js +108 -0
  13. package/build/psbt/psbtutils.d.ts +1 -0
  14. package/build/psbt/psbtutils.js +2 -0
  15. package/package.json +1 -1
  16. package/src/address.ts +68 -9
  17. package/src/block.ts +233 -233
  18. package/src/bufferutils.ts +188 -188
  19. package/src/networks.ts +6 -6
  20. package/src/payments/index.ts +68 -62
  21. package/src/payments/p2op.ts +136 -0
  22. package/src/psbt/psbtutils.ts +2 -0
  23. package/src/psbt.ts +2187 -2187
  24. package/test/address.spec.ts +155 -155
  25. package/test/bitcoin.core.spec.ts +212 -212
  26. package/test/block.spec.ts +171 -171
  27. package/test/bufferutils.spec.ts +450 -450
  28. package/test/crypto.spec.ts +49 -49
  29. package/test/integration/addresses.spec.ts +142 -142
  30. package/test/integration/bip32.spec.ts +130 -130
  31. package/test/integration/blocks.spec.ts +28 -28
  32. package/test/integration/cltv.spec.ts +241 -241
  33. package/test/integration/csv.spec.ts +452 -452
  34. package/test/integration/payments.spec.ts +110 -110
  35. package/test/integration/taproot.spec.ts +663 -663
  36. package/test/integration/transactions.spec.ts +668 -668
  37. package/test/payments.spec.ts +114 -114
  38. package/test/payments.utils.ts +165 -165
  39. package/test/psbt.spec.ts +1285 -1285
  40. package/test/script.spec.ts +186 -186
  41. package/test/script_number.spec.ts +26 -26
  42. package/test/script_signature.spec.ts +66 -66
  43. package/test/transaction.spec.ts +337 -337
  44. package/test/ts-node-register.js +7 -7
  45. package/test/types.spec.ts +53 -53
@@ -1,212 +1,212 @@
1
- import assert from 'assert';
2
- import base58 from 'bs58';
3
- import { describe, it } from 'mocha';
4
- import * as bitcoin from '../src/index.js';
5
- import base58EncodeDecode from './fixtures/core/base58_encode_decode.json' with { type: 'json' };
6
- import base58KeysInvalid from './fixtures/core/base58_keys_invalid.json' with { type: 'json' };
7
- import base58KeysValid from './fixtures/core/base58_keys_valid.json' with { type: 'json' };
8
- import blocksValid from './fixtures/core/blocks.json' with { type: 'json' };
9
- import sigCanonical from './fixtures/core/sig_canonical.json' with { type: 'json' };
10
- import sigNoncanonical from './fixtures/core/sig_noncanonical.json' with { type: 'json' };
11
- import sigHash from './fixtures/core/sighash.json' with { type: 'json' };
12
- import txValid from './fixtures/core/tx_valid.json' with { type: 'json' };
13
-
14
- describe('Bitcoin-core', () => {
15
- // base58EncodeDecode
16
- describe('base58', () => {
17
- base58EncodeDecode.forEach((f) => {
18
- const fhex = f[0];
19
- const fb58 = f[1];
20
-
21
- it('can decode ' + fb58, () => {
22
- const buffer = base58.decode(fb58);
23
- const actual = Buffer.from(buffer).toString('hex');
24
-
25
- assert.strictEqual(actual, fhex);
26
- });
27
-
28
- it('can encode ' + fhex, () => {
29
- const buffer = Buffer.from(fhex, 'hex');
30
- const actual = base58.encode(buffer);
31
-
32
- assert.strictEqual(actual, fb58);
33
- });
34
- });
35
- });
36
-
37
- // base58KeysValid
38
- describe('address.toBase58Check', () => {
39
- const typeMap: any = {
40
- pubkey: 'pubKeyHash',
41
- script: 'scriptHash',
42
- };
43
-
44
- base58KeysValid.forEach((f) => {
45
- const expected = f[0];
46
- const hash = Buffer.from(f[1] as any, 'hex');
47
- const params = f[2] as any;
48
-
49
- if (params.isPrivkey) return;
50
-
51
- const network: any = params.isTestnet
52
- ? bitcoin.networks.testnet
53
- : bitcoin.networks.bitcoin;
54
-
55
- const version = network[typeMap[params.addrType]];
56
-
57
- it(`can export ${expected as string}`, () => {
58
- assert.strictEqual(bitcoin.address.toBase58Check(hash, version), expected);
59
- });
60
- });
61
- });
62
-
63
- // base58KeysInvalid
64
- describe('address.fromBase58Check', () => {
65
- const allowedNetworks = [
66
- bitcoin.networks.bitcoin.pubKeyHash,
67
- bitcoin.networks.bitcoin.scriptHash,
68
- bitcoin.networks.testnet.pubKeyHash,
69
- bitcoin.networks.testnet.scriptHash,
70
- ];
71
-
72
- base58KeysInvalid.forEach((f) => {
73
- const strng = f[0];
74
-
75
- it('throws on ' + strng, () => {
76
- assert.throws(() => {
77
- const address = bitcoin.address.fromBase58Check(strng);
78
-
79
- assert.notStrictEqual(
80
- allowedNetworks.indexOf(address.version),
81
- -1,
82
- 'Invalid network',
83
- );
84
- }, /(Invalid (checksum|network))|(too (short|long))/);
85
- });
86
- });
87
- });
88
-
89
- describe('Block.fromHex', () => {
90
- blocksValid.forEach((f) => {
91
- it('can parse ' + f.id, () => {
92
- const block = bitcoin.Block.fromHex(f.hex);
93
-
94
- assert.strictEqual(block.getId(), f.id);
95
- assert.strictEqual(block.transactions!.length, f.transactions);
96
- });
97
- });
98
- });
99
-
100
- // txValid
101
- describe('Transaction.fromHex', () => {
102
- txValid.forEach((f) => {
103
- // Objects that are only a single string are ignored
104
- if (f.length === 1) return;
105
-
106
- const inputs = f[0];
107
- const fhex = f[1];
108
- // const verifyFlags = f[2] // TODO: do we need to test this?
109
-
110
- it('can decode ' + fhex, () => {
111
- const transaction = bitcoin.Transaction.fromHex(fhex as string);
112
-
113
- transaction.ins.forEach((txIn, i) => {
114
- const input = inputs[i];
115
-
116
- // reverse because test data is reversed
117
- const prevOutHash = Buffer.from(input[0] as string, 'hex').reverse();
118
- const prevOutIndex = input[1];
119
-
120
- assert.deepStrictEqual(txIn.hash, prevOutHash);
121
-
122
- // we read UInt32, not Int32
123
- assert.strictEqual(txIn.index & 0xffffffff, prevOutIndex);
124
- });
125
- });
126
- });
127
- });
128
-
129
- // sighash
130
- describe('Transaction', () => {
131
- sigHash.forEach((f) => {
132
- // Objects that are only a single string are ignored
133
- if (f.length === 1) return;
134
-
135
- const txHex = f[0] as string;
136
- const scriptHex = f[1] as string;
137
- const inIndex = f[2] as number;
138
- const hashType = f[3] as number;
139
- const expectedHash = f[4];
140
-
141
- const hashTypes = [];
142
- if ((hashType & 0x1f) === bitcoin.Transaction.SIGHASH_NONE)
143
- hashTypes.push('SIGHASH_NONE');
144
- else if ((hashType & 0x1f) === bitcoin.Transaction.SIGHASH_SINGLE)
145
- hashTypes.push('SIGHASH_SINGLE');
146
- else hashTypes.push('SIGHASH_ALL');
147
- if (hashType & bitcoin.Transaction.SIGHASH_ANYONECANPAY)
148
- hashTypes.push('SIGHASH_ANYONECANPAY');
149
-
150
- const hashTypeName = hashTypes.join(' | ');
151
-
152
- it('should hash ' + txHex.slice(0, 40) + '... (' + hashTypeName + ')', () => {
153
- const transaction = bitcoin.Transaction.fromHex(txHex);
154
- assert.strictEqual(transaction.toHex(), txHex);
155
-
156
- const script = Buffer.from(scriptHex, 'hex');
157
- const scriptChunks = bitcoin.script.decompile(script);
158
- assert.strictEqual(
159
- bitcoin.script.compile(scriptChunks!).toString('hex'),
160
- scriptHex,
161
- );
162
-
163
- const hash = transaction.hashForSignature(inIndex, script, hashType);
164
-
165
- // reverse because test data is reversed
166
- assert.strictEqual((hash.reverse() as Buffer).toString('hex'), expectedHash);
167
-
168
- assert.doesNotThrow(() =>
169
- transaction.hashForWitnessV0(
170
- inIndex,
171
- script,
172
- 0,
173
- // convert to UInt32
174
- hashType < 0 ? 0x100000000 + hashType : hashType,
175
- ),
176
- );
177
- });
178
- });
179
- });
180
-
181
- describe('script.signature.decode', () => {
182
- sigCanonical.forEach((hex) => {
183
- const buffer = Buffer.from(hex, 'hex');
184
-
185
- it('can parse ' + hex, () => {
186
- const parsed = bitcoin.script.signature.decode(buffer);
187
- const actual = bitcoin.script.signature.encode(parsed.signature, parsed.hashType);
188
-
189
- assert.strictEqual(actual.toString('hex'), hex);
190
- });
191
- });
192
-
193
- sigNoncanonical.forEach((hex, i) => {
194
- if (i === 0) return;
195
- if (i % 2 !== 0) return;
196
-
197
- const description = sigNoncanonical[i - 1].slice(0, -1);
198
- const buffer = Buffer.from(hex, 'hex');
199
-
200
- it('throws on ' + description, () => {
201
- const reg = new RegExp(
202
- 'Expected DER (integer|sequence)|(R|S) value (excessively ' +
203
- 'padded|is negative)|(R|S|DER sequence) length is (zero|too ' +
204
- 'short|too long|invalid)|Invalid hashType',
205
- );
206
- assert.throws(() => {
207
- bitcoin.script.signature.decode(buffer);
208
- }, reg);
209
- });
210
- });
211
- });
212
- });
1
+ import assert from 'assert';
2
+ import base58 from 'bs58';
3
+ import { describe, it } from 'mocha';
4
+ import * as bitcoin from '../src/index.js';
5
+ import base58EncodeDecode from './fixtures/core/base58_encode_decode.json' with { type: 'json' };
6
+ import base58KeysInvalid from './fixtures/core/base58_keys_invalid.json' with { type: 'json' };
7
+ import base58KeysValid from './fixtures/core/base58_keys_valid.json' with { type: 'json' };
8
+ import blocksValid from './fixtures/core/blocks.json' with { type: 'json' };
9
+ import sigCanonical from './fixtures/core/sig_canonical.json' with { type: 'json' };
10
+ import sigNoncanonical from './fixtures/core/sig_noncanonical.json' with { type: 'json' };
11
+ import sigHash from './fixtures/core/sighash.json' with { type: 'json' };
12
+ import txValid from './fixtures/core/tx_valid.json' with { type: 'json' };
13
+
14
+ describe('Bitcoin-core', () => {
15
+ // base58EncodeDecode
16
+ describe('base58', () => {
17
+ base58EncodeDecode.forEach((f) => {
18
+ const fhex = f[0];
19
+ const fb58 = f[1];
20
+
21
+ it('can decode ' + fb58, () => {
22
+ const buffer = base58.decode(fb58);
23
+ const actual = Buffer.from(buffer).toString('hex');
24
+
25
+ assert.strictEqual(actual, fhex);
26
+ });
27
+
28
+ it('can encode ' + fhex, () => {
29
+ const buffer = Buffer.from(fhex, 'hex');
30
+ const actual = base58.encode(buffer);
31
+
32
+ assert.strictEqual(actual, fb58);
33
+ });
34
+ });
35
+ });
36
+
37
+ // base58KeysValid
38
+ describe('address.toBase58Check', () => {
39
+ const typeMap: any = {
40
+ pubkey: 'pubKeyHash',
41
+ script: 'scriptHash',
42
+ };
43
+
44
+ base58KeysValid.forEach((f) => {
45
+ const expected = f[0];
46
+ const hash = Buffer.from(f[1] as any, 'hex');
47
+ const params = f[2] as any;
48
+
49
+ if (params.isPrivkey) return;
50
+
51
+ const network: any = params.isTestnet
52
+ ? bitcoin.networks.testnet
53
+ : bitcoin.networks.bitcoin;
54
+
55
+ const version = network[typeMap[params.addrType]];
56
+
57
+ it(`can export ${expected as string}`, () => {
58
+ assert.strictEqual(bitcoin.address.toBase58Check(hash, version), expected);
59
+ });
60
+ });
61
+ });
62
+
63
+ // base58KeysInvalid
64
+ describe('address.fromBase58Check', () => {
65
+ const allowedNetworks = [
66
+ bitcoin.networks.bitcoin.pubKeyHash,
67
+ bitcoin.networks.bitcoin.scriptHash,
68
+ bitcoin.networks.testnet.pubKeyHash,
69
+ bitcoin.networks.testnet.scriptHash,
70
+ ];
71
+
72
+ base58KeysInvalid.forEach((f) => {
73
+ const strng = f[0];
74
+
75
+ it('throws on ' + strng, () => {
76
+ assert.throws(() => {
77
+ const address = bitcoin.address.fromBase58Check(strng);
78
+
79
+ assert.notStrictEqual(
80
+ allowedNetworks.indexOf(address.version),
81
+ -1,
82
+ 'Invalid network',
83
+ );
84
+ }, /(Invalid (checksum|network))|(too (short|long))/);
85
+ });
86
+ });
87
+ });
88
+
89
+ describe('Block.fromHex', () => {
90
+ blocksValid.forEach((f) => {
91
+ it('can parse ' + f.id, () => {
92
+ const block = bitcoin.Block.fromHex(f.hex);
93
+
94
+ assert.strictEqual(block.getId(), f.id);
95
+ assert.strictEqual(block.transactions!.length, f.transactions);
96
+ });
97
+ });
98
+ });
99
+
100
+ // txValid
101
+ describe('Transaction.fromHex', () => {
102
+ txValid.forEach((f) => {
103
+ // Objects that are only a single string are ignored
104
+ if (f.length === 1) return;
105
+
106
+ const inputs = f[0];
107
+ const fhex = f[1];
108
+ // const verifyFlags = f[2] // TODO: do we need to test this?
109
+
110
+ it('can decode ' + fhex, () => {
111
+ const transaction = bitcoin.Transaction.fromHex(fhex as string);
112
+
113
+ transaction.ins.forEach((txIn, i) => {
114
+ const input = inputs[i];
115
+
116
+ // reverse because test data is reversed
117
+ const prevOutHash = Buffer.from(input[0] as string, 'hex').reverse();
118
+ const prevOutIndex = input[1];
119
+
120
+ assert.deepStrictEqual(txIn.hash, prevOutHash);
121
+
122
+ // we read UInt32, not Int32
123
+ assert.strictEqual(txIn.index & 0xffffffff, prevOutIndex);
124
+ });
125
+ });
126
+ });
127
+ });
128
+
129
+ // sighash
130
+ describe('Transaction', () => {
131
+ sigHash.forEach((f) => {
132
+ // Objects that are only a single string are ignored
133
+ if (f.length === 1) return;
134
+
135
+ const txHex = f[0] as string;
136
+ const scriptHex = f[1] as string;
137
+ const inIndex = f[2] as number;
138
+ const hashType = f[3] as number;
139
+ const expectedHash = f[4];
140
+
141
+ const hashTypes = [];
142
+ if ((hashType & 0x1f) === bitcoin.Transaction.SIGHASH_NONE)
143
+ hashTypes.push('SIGHASH_NONE');
144
+ else if ((hashType & 0x1f) === bitcoin.Transaction.SIGHASH_SINGLE)
145
+ hashTypes.push('SIGHASH_SINGLE');
146
+ else hashTypes.push('SIGHASH_ALL');
147
+ if (hashType & bitcoin.Transaction.SIGHASH_ANYONECANPAY)
148
+ hashTypes.push('SIGHASH_ANYONECANPAY');
149
+
150
+ const hashTypeName = hashTypes.join(' | ');
151
+
152
+ it('should hash ' + txHex.slice(0, 40) + '... (' + hashTypeName + ')', () => {
153
+ const transaction = bitcoin.Transaction.fromHex(txHex);
154
+ assert.strictEqual(transaction.toHex(), txHex);
155
+
156
+ const script = Buffer.from(scriptHex, 'hex');
157
+ const scriptChunks = bitcoin.script.decompile(script);
158
+ assert.strictEqual(
159
+ bitcoin.script.compile(scriptChunks!).toString('hex'),
160
+ scriptHex,
161
+ );
162
+
163
+ const hash = transaction.hashForSignature(inIndex, script, hashType);
164
+
165
+ // reverse because test data is reversed
166
+ assert.strictEqual((hash.reverse() as Buffer).toString('hex'), expectedHash);
167
+
168
+ assert.doesNotThrow(() =>
169
+ transaction.hashForWitnessV0(
170
+ inIndex,
171
+ script,
172
+ 0,
173
+ // convert to UInt32
174
+ hashType < 0 ? 0x100000000 + hashType : hashType,
175
+ ),
176
+ );
177
+ });
178
+ });
179
+ });
180
+
181
+ describe('script.signature.decode', () => {
182
+ sigCanonical.forEach((hex) => {
183
+ const buffer = Buffer.from(hex, 'hex');
184
+
185
+ it('can parse ' + hex, () => {
186
+ const parsed = bitcoin.script.signature.decode(buffer);
187
+ const actual = bitcoin.script.signature.encode(parsed.signature, parsed.hashType);
188
+
189
+ assert.strictEqual(actual.toString('hex'), hex);
190
+ });
191
+ });
192
+
193
+ sigNoncanonical.forEach((hex, i) => {
194
+ if (i === 0) return;
195
+ if (i % 2 !== 0) return;
196
+
197
+ const description = sigNoncanonical[i - 1].slice(0, -1);
198
+ const buffer = Buffer.from(hex, 'hex');
199
+
200
+ it('throws on ' + description, () => {
201
+ const reg = new RegExp(
202
+ 'Expected DER (integer|sequence)|(R|S) value (excessively ' +
203
+ 'padded|is negative)|(R|S|DER sequence) length is (zero|too ' +
204
+ 'short|too long|invalid)|Invalid hashType',
205
+ );
206
+ assert.throws(() => {
207
+ bitcoin.script.signature.decode(buffer);
208
+ }, reg);
209
+ });
210
+ });
211
+ });
212
+ });