@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,234 +1,212 @@
1
- import * as assert from 'assert';
2
- import * as base58 from 'bs58';
3
- import { describe, it } from 'mocha';
4
- import * as bitcoin from '..';
5
- import * as base58EncodeDecode from './fixtures/core/base58_encode_decode.json';
6
- import * as base58KeysInvalid from './fixtures/core/base58_keys_invalid.json';
7
- import * as base58KeysValid from './fixtures/core/base58_keys_valid.json';
8
- import * as blocksValid from './fixtures/core/blocks.json';
9
- import * as sigCanonical from './fixtures/core/sig_canonical.json';
10
- import * as sigNoncanonical from './fixtures/core/sig_noncanonical.json';
11
- import * as sigHash from './fixtures/core/sighash.json';
12
- import * as txValid from './fixtures/core/tx_valid.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.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
- const version = network[typeMap[params.addrType]];
55
-
56
- it('can export ' + expected, () => {
57
- assert.strictEqual(
58
- bitcoin.address.toBase58Check(hash, version),
59
- expected,
60
- );
61
- });
62
- });
63
- });
64
-
65
- // base58KeysInvalid
66
- describe('address.fromBase58Check', () => {
67
- const allowedNetworks = [
68
- bitcoin.networks.bitcoin.pubKeyHash,
69
- bitcoin.networks.bitcoin.scriptHash,
70
- bitcoin.networks.testnet.pubKeyHash,
71
- bitcoin.networks.testnet.scriptHash,
72
- ];
73
-
74
- base58KeysInvalid.forEach(f => {
75
- const strng = f[0];
76
-
77
- it('throws on ' + strng, () => {
78
- assert.throws(() => {
79
- const address = bitcoin.address.fromBase58Check(strng);
80
-
81
- assert.notStrictEqual(
82
- allowedNetworks.indexOf(address.version),
83
- -1,
84
- 'Invalid network',
85
- );
86
- }, /(Invalid (checksum|network))|(too (short|long))/);
87
- });
88
- });
89
- });
90
-
91
- describe('Block.fromHex', () => {
92
- blocksValid.forEach(f => {
93
- it('can parse ' + f.id, () => {
94
- const block = bitcoin.Block.fromHex(f.hex);
95
-
96
- assert.strictEqual(block.getId(), f.id);
97
- assert.strictEqual(block.transactions!.length, f.transactions);
98
- });
99
- });
100
- });
101
-
102
- // txValid
103
- describe('Transaction.fromHex', () => {
104
- txValid.forEach(f => {
105
- // Objects that are only a single string are ignored
106
- if (f.length === 1) return;
107
-
108
- const inputs = f[0];
109
- const fhex = f[1];
110
- // const verifyFlags = f[2] // TODO: do we need to test this?
111
-
112
- it('can decode ' + fhex, () => {
113
- const transaction = bitcoin.Transaction.fromHex(fhex as string);
114
-
115
- transaction.ins.forEach((txIn, i) => {
116
- const input = inputs[i];
117
-
118
- // reverse because test data is reversed
119
- const prevOutHash = Buffer.from(
120
- input[0] as string,
121
- 'hex',
122
- ).reverse();
123
- const prevOutIndex = input[1];
124
-
125
- assert.deepStrictEqual(txIn.hash, prevOutHash);
126
-
127
- // we read UInt32, not Int32
128
- assert.strictEqual(txIn.index & 0xffffffff, prevOutIndex);
129
- });
130
- });
131
- });
132
- });
133
-
134
- // sighash
135
- describe('Transaction', () => {
136
- sigHash.forEach(f => {
137
- // Objects that are only a single string are ignored
138
- if (f.length === 1) return;
139
-
140
- const txHex = f[0] as string;
141
- const scriptHex = f[1] as string;
142
- const inIndex = f[2] as number;
143
- const hashType = f[3] as number;
144
- const expectedHash = f[4];
145
-
146
- const hashTypes = [];
147
- if ((hashType & 0x1f) === bitcoin.Transaction.SIGHASH_NONE)
148
- hashTypes.push('SIGHASH_NONE');
149
- else if ((hashType & 0x1f) === bitcoin.Transaction.SIGHASH_SINGLE)
150
- hashTypes.push('SIGHASH_SINGLE');
151
- else hashTypes.push('SIGHASH_ALL');
152
- if (hashType & bitcoin.Transaction.SIGHASH_ANYONECANPAY)
153
- hashTypes.push('SIGHASH_ANYONECANPAY');
154
-
155
- const hashTypeName = hashTypes.join(' | ');
156
-
157
- it(
158
- 'should hash ' +
159
- txHex.slice(0, 40) +
160
- '... (' +
161
- hashTypeName +
162
- ')',
163
- () => {
164
- const transaction = bitcoin.Transaction.fromHex(txHex);
165
- assert.strictEqual(transaction.toHex(), txHex);
166
-
167
- const script = Buffer.from(scriptHex, 'hex');
168
- const scriptChunks = bitcoin.script.decompile(script);
169
- assert.strictEqual(
170
- bitcoin.script.compile(scriptChunks!).toString('hex'),
171
- scriptHex,
172
- );
173
-
174
- const hash = transaction.hashForSignature(
175
- inIndex,
176
- script,
177
- hashType,
178
- );
179
-
180
- // reverse because test data is reversed
181
- assert.strictEqual(
182
- (hash.reverse() as Buffer).toString('hex'),
183
- expectedHash,
184
- );
185
-
186
- assert.doesNotThrow(() =>
187
- transaction.hashForWitnessV0(
188
- inIndex,
189
- script,
190
- 0,
191
- // convert to UInt32
192
- hashType < 0 ? 0x100000000 + hashType : hashType,
193
- ),
194
- );
195
- },
196
- );
197
- });
198
- });
199
-
200
- describe('script.signature.decode', () => {
201
- sigCanonical.forEach(hex => {
202
- const buffer = Buffer.from(hex, 'hex');
203
-
204
- it('can parse ' + hex, () => {
205
- const parsed = bitcoin.script.signature.decode(buffer);
206
- const actual = bitcoin.script.signature.encode(
207
- parsed.signature,
208
- parsed.hashType,
209
- );
210
-
211
- assert.strictEqual(actual.toString('hex'), hex);
212
- });
213
- });
214
-
215
- sigNoncanonical.forEach((hex, i) => {
216
- if (i === 0) return;
217
- if (i % 2 !== 0) return;
218
-
219
- const description = sigNoncanonical[i - 1].slice(0, -1);
220
- const buffer = Buffer.from(hex, 'hex');
221
-
222
- it('throws on ' + description, () => {
223
- const reg = new RegExp(
224
- 'Expected DER (integer|sequence)|(R|S) value (excessively ' +
225
- 'padded|is negative)|(R|S|DER sequence) length is (zero|too ' +
226
- 'short|too long|invalid)|Invalid hashType',
227
- );
228
- assert.throws(() => {
229
- bitcoin.script.signature.decode(buffer);
230
- }, reg);
231
- });
232
- });
233
- });
234
- });
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
+ });