@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,210 +1,186 @@
1
- import * as assert from 'assert';
2
- import { describe, it } from 'mocha';
3
- import * as bscript from '../src/script';
4
- import * as fixtures from './fixtures/script.json';
5
-
6
- const minimalData = require('minimaldata');
7
-
8
- describe('script', () => {
9
- // TODO
10
- describe('isCanonicalPubKey', () => {
11
- it('rejects if not provided a Buffer', () => {
12
- assert.strictEqual(false, bscript.isCanonicalPubKey(0 as any));
13
- });
14
- it('rejects smaller than 33', () => {
15
- for (let i = 0; i < 33; i++) {
16
- assert.strictEqual(
17
- false,
18
- bscript.isCanonicalPubKey(Buffer.allocUnsafe(i)),
19
- );
20
- }
21
- });
22
- });
23
- describe.skip('isCanonicalScriptSignature', () => {
24
- assert.ok(true);
25
- });
26
-
27
- describe('fromASM/toASM', () => {
28
- fixtures.valid.forEach(f => {
29
- it('encodes/decodes ' + f.asm, () => {
30
- const script = bscript.fromASM(f.asm);
31
- assert.strictEqual(bscript.toASM(script), f.asm);
32
- });
33
- });
34
-
35
- fixtures.invalid.fromASM.forEach(f => {
36
- it('throws ' + f.description, () => {
37
- assert.throws(() => {
38
- bscript.fromASM(f.script);
39
- }, new RegExp(f.description));
40
- });
41
- });
42
- });
43
-
44
- describe('toASM', () => {
45
- const OP_RETURN = bscript.OPS.OP_RETURN;
46
- it('encodes empty buffer as OP_0', () => {
47
- const chunks = [OP_RETURN, Buffer.from([])];
48
- assert.strictEqual(bscript.toASM(chunks), 'OP_RETURN OP_0');
49
- });
50
-
51
- for (let i = 1; i <= 16; i++) {
52
- it(`encodes one byte buffer [${i}] as OP_${i}`, () => {
53
- const chunks = [OP_RETURN, Buffer.from([i])];
54
- assert.strictEqual(bscript.toASM(chunks), 'OP_RETURN OP_' + i);
55
- });
56
- }
57
- });
58
-
59
- describe('fromASM/toASM (templates)', () => {
60
- fixtures.valid2.forEach(f => {
61
- if (f.inputHex) {
62
- const ih = bscript.toASM(Buffer.from(f.inputHex, 'hex'));
63
-
64
- it('encodes/decodes ' + ih, () => {
65
- const script = bscript.fromASM(f.input);
66
- assert.strictEqual(script.toString('hex'), f.inputHex);
67
- assert.strictEqual(bscript.toASM(script), f.input);
68
- });
69
- }
70
-
71
- if (f.outputHex) {
72
- it('encodes/decodes ' + f.output, () => {
73
- const script = bscript.fromASM(f.output);
74
- assert.strictEqual(script.toString('hex'), f.outputHex);
75
- assert.strictEqual(bscript.toASM(script), f.output);
76
- });
77
- }
78
- });
79
- });
80
-
81
- describe('isPushOnly', () => {
82
- fixtures.valid.forEach(f => {
83
- it('returns ' + !!f.stack + ' for ' + f.asm, () => {
84
- const script = bscript.fromASM(f.asm);
85
- const chunks = bscript.decompile(script);
86
-
87
- assert.strictEqual(bscript.isPushOnly(chunks!), !!f.stack);
88
- });
89
- });
90
- });
91
-
92
- describe('toStack', () => {
93
- fixtures.valid.forEach(f => {
94
- it('returns ' + !!f.stack + ' for ' + f.asm, () => {
95
- if (!f.stack || !f.asm) return;
96
-
97
- const script = bscript.fromASM(f.asm);
98
-
99
- const stack = bscript.toStack(script);
100
- assert.deepStrictEqual(
101
- stack.map(x => {
102
- return x.toString('hex');
103
- }),
104
- f.stack,
105
- );
106
-
107
- assert.strictEqual(
108
- bscript.toASM(bscript.compile(stack)),
109
- f.asm,
110
- 'should rebuild same script from stack',
111
- );
112
- });
113
- });
114
- });
115
-
116
- describe('compile (via fromASM)', () => {
117
- fixtures.valid.forEach(f => {
118
- it('compiles ' + f.asm, () => {
119
- const scriptSig = bscript.fromASM(f.asm);
120
-
121
- assert.strictEqual(scriptSig.toString('hex'), f.script);
122
-
123
- if (f.nonstandard) {
124
- const scriptSigNS = bscript.fromASM(
125
- f.nonstandard.scriptSig,
126
- );
127
-
128
- assert.strictEqual(scriptSigNS.toString('hex'), f.script);
129
- }
130
- });
131
- });
132
- });
133
-
134
- describe('decompile', () => {
135
- fixtures.valid.forEach(f => {
136
- it('decompiles ' + f.asm, () => {
137
- const chunks = bscript.decompile(Buffer.from(f.script, 'hex'));
138
-
139
- assert.strictEqual(
140
- bscript.compile(chunks!).toString('hex'),
141
- f.script,
142
- );
143
- assert.strictEqual(bscript.toASM(chunks!), f.asm);
144
-
145
- if (f.nonstandard) {
146
- const chunksNS = bscript.decompile(
147
- Buffer.from(f.nonstandard.scriptSigHex, 'hex'),
148
- );
149
-
150
- assert.strictEqual(
151
- bscript.compile(chunksNS!).toString('hex'),
152
- f.script,
153
- );
154
-
155
- // toASM converts verbatim, only `compile` transforms the script to a minimalpush compliant script
156
- assert.strictEqual(
157
- bscript.toASM(chunksNS!),
158
- f.nonstandard.scriptSig,
159
- );
160
- }
161
- });
162
- });
163
-
164
- fixtures.invalid.decompile.forEach(f => {
165
- it(
166
- 'fails to decompile ' +
167
- f.script +
168
- ', because "' +
169
- f.description +
170
- '"',
171
- () => {
172
- const chunks = bscript.decompile(
173
- Buffer.from(f.script, 'hex'),
174
- );
175
-
176
- assert.strictEqual(chunks, null);
177
- },
178
- );
179
- });
180
- });
181
-
182
- describe('SCRIPT_VERIFY_MINIMALDATA policy', () => {
183
- fixtures.valid.forEach(f => {
184
- it('compliant for scriptSig ' + f.asm, () => {
185
- const script = Buffer.from(f.script, 'hex');
186
-
187
- assert(minimalData(script));
188
- });
189
- });
190
-
191
- function testEncodingForSize(num: number): void {
192
- it('compliant for data PUSH of length ' + num, () => {
193
- const buffer = Buffer.alloc(num);
194
- const script = bscript.compile([buffer]);
195
-
196
- assert(
197
- minimalData(script),
198
- 'Failed for ' +
199
- num +
200
- ' length script: ' +
201
- script.toString('hex'),
202
- );
203
- });
204
- }
205
-
206
- for (let i = 0; i < 520; ++i) {
207
- testEncodingForSize(i);
208
- }
209
- });
210
- });
1
+ import assert from 'assert';
2
+ import { describe, it } from 'mocha';
3
+ import * as bscript from '../src/script.js';
4
+ import fixtures from './fixtures/script.json' with { type: 'json' };
5
+
6
+ // @ts-ignore
7
+ import minimalData from 'minimaldata';
8
+
9
+ describe('script', () => {
10
+ // TODO
11
+ describe('isCanonicalPubKey', () => {
12
+ it('rejects if not provided a Buffer', () => {
13
+ assert.strictEqual(false, bscript.isCanonicalPubKey(0 as any));
14
+ });
15
+ it('rejects smaller than 33', () => {
16
+ for (let i = 0; i < 33; i++) {
17
+ assert.strictEqual(false, bscript.isCanonicalPubKey(Buffer.allocUnsafe(i)));
18
+ }
19
+ });
20
+ });
21
+
22
+ describe.skip('isCanonicalScriptSignature', () => {
23
+ assert.ok(true);
24
+ });
25
+
26
+ describe('fromASM/toASM', () => {
27
+ fixtures.valid.forEach((f) => {
28
+ it('encodes/decodes ' + f.asm, () => {
29
+ const script = bscript.fromASM(f.asm);
30
+ assert.strictEqual(bscript.toASM(script), f.asm);
31
+ });
32
+ });
33
+
34
+ fixtures.invalid.fromASM.forEach((f) => {
35
+ it('throws ' + f.description, () => {
36
+ assert.throws(() => {
37
+ bscript.fromASM(f.script);
38
+ }, new RegExp(f.description));
39
+ });
40
+ });
41
+ });
42
+
43
+ describe('toASM', () => {
44
+ const OP_RETURN = bscript.OPS.OP_RETURN;
45
+ it('encodes empty buffer as OP_0', () => {
46
+ const chunks = [OP_RETURN, Buffer.from([])];
47
+ assert.strictEqual(bscript.toASM(chunks), 'OP_RETURN OP_0');
48
+ });
49
+
50
+ for (let i = 1; i <= 16; i++) {
51
+ it(`encodes one byte buffer [${i}] as OP_${i}`, () => {
52
+ const chunks = [OP_RETURN, Buffer.from([i])];
53
+ assert.strictEqual(bscript.toASM(chunks), 'OP_RETURN OP_' + i);
54
+ });
55
+ }
56
+ });
57
+
58
+ describe('fromASM/toASM (templates)', () => {
59
+ fixtures.valid2.forEach((f) => {
60
+ if (f.inputHex) {
61
+ const ih = bscript.toASM(Buffer.from(f.inputHex, 'hex'));
62
+
63
+ it('encodes/decodes ' + ih, () => {
64
+ const script = bscript.fromASM(f.input);
65
+ assert.strictEqual(script.toString('hex'), f.inputHex);
66
+ assert.strictEqual(bscript.toASM(script), f.input);
67
+ });
68
+ }
69
+
70
+ if (f.outputHex) {
71
+ it('encodes/decodes ' + f.output, () => {
72
+ const script = bscript.fromASM(f.output);
73
+ assert.strictEqual(script.toString('hex'), f.outputHex);
74
+ assert.strictEqual(bscript.toASM(script), f.output);
75
+ });
76
+ }
77
+ });
78
+ });
79
+
80
+ describe('isPushOnly', () => {
81
+ fixtures.valid.forEach((f) => {
82
+ it('returns ' + !!f.stack + ' for ' + f.asm, () => {
83
+ const script = bscript.fromASM(f.asm);
84
+ const chunks = bscript.decompile(script);
85
+
86
+ assert.strictEqual(bscript.isPushOnly(chunks!), !!f.stack);
87
+ });
88
+ });
89
+ });
90
+
91
+ describe('toStack', () => {
92
+ fixtures.valid.forEach((f) => {
93
+ it('returns ' + !!f.stack + ' for ' + f.asm, () => {
94
+ if (!f.stack || !f.asm) return;
95
+
96
+ const script = bscript.fromASM(f.asm);
97
+
98
+ const stack = bscript.toStack(script);
99
+ assert.deepStrictEqual(
100
+ stack.map((x) => {
101
+ return x.toString('hex');
102
+ }),
103
+ f.stack,
104
+ );
105
+
106
+ assert.strictEqual(
107
+ bscript.toASM(bscript.compile(stack)),
108
+ f.asm,
109
+ 'should rebuild same script from stack',
110
+ );
111
+ });
112
+ });
113
+ });
114
+
115
+ describe('compile (via fromASM)', () => {
116
+ fixtures.valid.forEach((f) => {
117
+ it('compiles ' + f.asm, () => {
118
+ const scriptSig = bscript.fromASM(f.asm);
119
+
120
+ assert.strictEqual(scriptSig.toString('hex'), f.script);
121
+
122
+ if (f.nonstandard) {
123
+ const scriptSigNS = bscript.fromASM(f.nonstandard.scriptSig);
124
+
125
+ assert.strictEqual(scriptSigNS.toString('hex'), f.script);
126
+ }
127
+ });
128
+ });
129
+ });
130
+
131
+ describe('decompile', () => {
132
+ fixtures.valid.forEach((f) => {
133
+ it('decompiles ' + f.asm, () => {
134
+ const chunks = bscript.decompile(Buffer.from(f.script, 'hex'));
135
+
136
+ assert.strictEqual(bscript.compile(chunks!).toString('hex'), f.script);
137
+ assert.strictEqual(bscript.toASM(chunks!), f.asm);
138
+
139
+ if (f.nonstandard) {
140
+ const chunksNS = bscript.decompile(
141
+ Buffer.from(f.nonstandard.scriptSigHex, 'hex'),
142
+ );
143
+
144
+ assert.strictEqual(bscript.compile(chunksNS!).toString('hex'), f.script);
145
+
146
+ // toASM converts verbatim, only `compile` transforms the script to a minimalpush compliant script
147
+ assert.strictEqual(bscript.toASM(chunksNS!), f.nonstandard.scriptSig);
148
+ }
149
+ });
150
+ });
151
+
152
+ fixtures.invalid.decompile.forEach((f) => {
153
+ it('fails to decompile ' + f.script + ', because "' + f.description + '"', () => {
154
+ const chunks = bscript.decompile(Buffer.from(f.script, 'hex'));
155
+
156
+ assert.strictEqual(chunks, null);
157
+ });
158
+ });
159
+ });
160
+
161
+ describe('SCRIPT_VERIFY_MINIMALDATA policy', () => {
162
+ fixtures.valid.forEach((f) => {
163
+ it('compliant for scriptSig ' + f.asm, () => {
164
+ const script = Buffer.from(f.script, 'hex');
165
+
166
+ assert(minimalData(script));
167
+ });
168
+ });
169
+
170
+ function testEncodingForSize(num: number): void {
171
+ it('compliant for data PUSH of length ' + num, () => {
172
+ const buffer = Buffer.alloc(num);
173
+ const script = bscript.compile([buffer]);
174
+
175
+ assert(
176
+ minimalData(script),
177
+ 'Failed for ' + num + ' length script: ' + script.toString('hex'),
178
+ );
179
+ });
180
+ }
181
+
182
+ for (let i = 0; i < 520; ++i) {
183
+ testEncodingForSize(i);
184
+ }
185
+ });
186
+ });
@@ -1,29 +1,26 @@
1
- import * as assert from 'assert';
2
- import { describe, it } from 'mocha';
3
- import * as scriptNumber from '../src/script_number';
4
- import * as fixtures from './fixtures/script_number.json';
5
-
6
- describe('script-number', () => {
7
- describe('decode', () => {
8
- fixtures.forEach(f => {
9
- it(f.hex + ' returns ' + f.number, () => {
10
- const actual = scriptNumber.decode(
11
- Buffer.from(f.hex, 'hex'),
12
- f.bytes,
13
- );
14
-
15
- assert.strictEqual(actual, f.number);
16
- });
17
- });
18
- });
19
-
20
- describe('encode', () => {
21
- fixtures.forEach(f => {
22
- it(f.number + ' returns ' + f.hex, () => {
23
- const actual = scriptNumber.encode(f.number);
24
-
25
- assert.strictEqual(actual.toString('hex'), f.hex);
26
- });
27
- });
28
- });
29
- });
1
+ import assert from 'assert';
2
+ import { describe, it } from 'mocha';
3
+ import * as scriptNumber from '../src/script_number.js';
4
+ import fixtures from './fixtures/script_number.json' with { type: 'json' };
5
+
6
+ describe('script-number', () => {
7
+ describe('decode', () => {
8
+ fixtures.forEach((f) => {
9
+ it(f.hex + ' returns ' + f.number, () => {
10
+ const actual = scriptNumber.decode(Buffer.from(f.hex, 'hex'), f.bytes);
11
+
12
+ assert.strictEqual(actual, f.number);
13
+ });
14
+ });
15
+ });
16
+
17
+ describe('encode', () => {
18
+ fixtures.forEach((f) => {
19
+ it(f.number + ' returns ' + f.hex, () => {
20
+ const actual = scriptNumber.encode(f.number);
21
+
22
+ assert.strictEqual(actual.toString('hex'), f.hex);
23
+ });
24
+ });
25
+ });
26
+ });
@@ -1,66 +1,66 @@
1
- import * as assert from 'assert';
2
- import { describe, it } from 'mocha';
3
- import { signature as bscriptSig } from '../src/script';
4
- import * as fixtures from './fixtures/signature.json';
5
-
6
- describe('Script Signatures', () => {
7
- function fromRaw(signature: { r: string; s: string }): Buffer {
8
- return Buffer.concat(
9
- [Buffer.from(signature.r, 'hex'), Buffer.from(signature.s, 'hex')],
10
- 64,
11
- );
12
- }
13
-
14
- function toRaw(signature: Buffer): {
15
- r: string;
16
- s: string;
17
- } {
18
- return {
19
- r: signature.slice(0, 32).toString('hex'),
20
- s: signature.slice(32, 64).toString('hex'),
21
- };
22
- }
23
-
24
- describe('encode', () => {
25
- fixtures.valid.forEach(f => {
26
- it('encodes ' + f.hex, () => {
27
- const buffer = bscriptSig.encode(fromRaw(f.raw), f.hashType);
28
-
29
- assert.strictEqual(buffer.toString('hex'), f.hex);
30
- });
31
- });
32
-
33
- fixtures.invalid.forEach(f => {
34
- if (!f.raw) return;
35
-
36
- it('throws ' + f.exception, () => {
37
- const signature = fromRaw(f.raw);
38
-
39
- assert.throws(() => {
40
- bscriptSig.encode(signature, f.hashType);
41
- }, new RegExp(f.exception));
42
- });
43
- });
44
- });
45
-
46
- describe('decode', () => {
47
- fixtures.valid.forEach(f => {
48
- it('decodes ' + f.hex, () => {
49
- const decode = bscriptSig.decode(Buffer.from(f.hex, 'hex'));
50
-
51
- assert.deepStrictEqual(toRaw(decode.signature), f.raw);
52
- assert.strictEqual(decode.hashType, f.hashType);
53
- });
54
- });
55
-
56
- fixtures.invalid.forEach(f => {
57
- it('throws on ' + f.hex, () => {
58
- const buffer = Buffer.from(f.hex, 'hex');
59
-
60
- assert.throws(() => {
61
- bscriptSig.decode(buffer);
62
- }, new RegExp(f.exception));
63
- });
64
- });
65
- });
66
- });
1
+ import assert from 'assert';
2
+ import { describe, it } from 'mocha';
3
+ import { signature as bscriptSig } from '../src/script.js';
4
+ import fixtures from './fixtures/signature.json' with { type: 'json' };
5
+
6
+ describe('Script Signatures', () => {
7
+ function fromRaw(signature: { r: string; s: string }): Buffer {
8
+ return Buffer.concat(
9
+ [Buffer.from(signature.r, 'hex'), Buffer.from(signature.s, 'hex')],
10
+ 64,
11
+ );
12
+ }
13
+
14
+ function toRaw(signature: Buffer): {
15
+ r: string;
16
+ s: string;
17
+ } {
18
+ return {
19
+ r: signature.slice(0, 32).toString('hex'),
20
+ s: signature.slice(32, 64).toString('hex'),
21
+ };
22
+ }
23
+
24
+ describe('encode', () => {
25
+ fixtures.valid.forEach((f) => {
26
+ it('encodes ' + f.hex, () => {
27
+ const buffer = bscriptSig.encode(fromRaw(f.raw), f.hashType);
28
+
29
+ assert.strictEqual(buffer.toString('hex'), f.hex);
30
+ });
31
+ });
32
+
33
+ fixtures.invalid.forEach((f) => {
34
+ if (!f.raw) return;
35
+
36
+ it('throws ' + f.exception, () => {
37
+ const signature = fromRaw(f.raw);
38
+
39
+ assert.throws(() => {
40
+ bscriptSig.encode(signature, f.hashType);
41
+ }, new RegExp(f.exception));
42
+ });
43
+ });
44
+ });
45
+
46
+ describe('decode', () => {
47
+ fixtures.valid.forEach((f) => {
48
+ it('decodes ' + f.hex, () => {
49
+ const decode = bscriptSig.decode(Buffer.from(f.hex, 'hex'));
50
+
51
+ assert.deepStrictEqual(toRaw(decode.signature), f.raw);
52
+ assert.strictEqual(decode.hashType, f.hashType);
53
+ });
54
+ });
55
+
56
+ fixtures.invalid.forEach((f) => {
57
+ it('throws on ' + f.hex, () => {
58
+ const buffer = Buffer.from(f.hex, 'hex');
59
+
60
+ assert.throws(() => {
61
+ bscriptSig.decode(buffer);
62
+ }, new RegExp(f.exception));
63
+ });
64
+ });
65
+ });
66
+ });