@btc-vision/bitcoin 6.4.0 → 6.4.1

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.
@@ -1,186 +1,186 @@
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
+ 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,26 +1,26 @@
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
+ 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 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
- });
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
+ });