@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.
- package/.mocharc.json +13 -0
- package/browser/address.d.ts +1 -1
- package/browser/index.js +1 -1
- package/browser/index.js.LICENSE.txt +3 -3
- package/browser/networks.d.ts +1 -0
- package/browser/psbt/psbtutils.d.ts +1 -1
- package/build/address.d.ts +1 -1
- package/build/address.js +12 -5
- package/build/block.js +2 -2
- package/build/bufferutils.js +5 -5
- package/build/networks.d.ts +1 -0
- package/build/networks.js +11 -0
- package/build/psbt/psbtutils.js +2 -2
- package/build/psbt.js +3 -7
- package/package.json +26 -26
- package/src/address.ts +20 -6
- package/src/block.ts +233 -233
- package/src/bufferutils.ts +188 -180
- package/src/index.ts +86 -86
- package/src/networks.ts +12 -0
- package/src/psbt/bip371.ts +441 -441
- package/src/psbt/psbtutils.ts +4 -3
- package/src/psbt.ts +2187 -2187
- package/test/address.spec.ts +155 -177
- package/test/bitcoin.core.spec.ts +212 -234
- package/test/block.spec.ts +171 -194
- package/test/bufferutils.spec.ts +450 -513
- package/test/crypto.spec.ts +49 -55
- package/test/fixtures/address.json +3 -3
- package/test/integration/addresses.spec.ts +142 -154
- package/test/integration/bip32.spec.ts +130 -151
- package/test/integration/blocks.spec.ts +28 -28
- package/test/integration/cltv.spec.ts +241 -283
- package/test/integration/csv.spec.ts +452 -527
- package/test/integration/payments.spec.ts +110 -135
- package/test/integration/taproot.spec.ts +663 -707
- package/test/integration/transactions.spec.ts +668 -769
- package/test/payments.spec.ts +114 -125
- package/test/payments.utils.ts +165 -208
- package/test/psbt.spec.ts +1285 -1414
- package/test/script.spec.ts +186 -210
- package/test/script_number.spec.ts +26 -29
- package/test/script_signature.spec.ts +66 -66
- package/test/transaction.spec.ts +337 -387
- package/test/ts-node-register.js +7 -5
- package/test/tsconfig.json +4 -1
- package/test/types.spec.ts +53 -58
- package/.nyc_output/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +0 -1
- package/.nyc_output/processinfo/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +0 -1
- package/.nyc_output/processinfo/index.json +0 -1
- package/test/address.spec.js +0 -124
- package/test/bitcoin.core.spec.js +0 -170
- package/test/block.spec.js +0 -141
- package/test/bufferutils.spec.js +0 -427
- package/test/crypto.spec.js +0 -41
- package/test/integration/_regtest.js +0 -7
- package/test/integration/addresses.spec.js +0 -116
- package/test/integration/bip32.spec.js +0 -85
- package/test/integration/blocks.spec.js +0 -26
- package/test/integration/cltv.spec.js +0 -199
- package/test/integration/csv.spec.js +0 -362
- package/test/integration/payments.spec.js +0 -98
- package/test/integration/taproot.spec.js +0 -532
- package/test/integration/transactions.spec.js +0 -561
- package/test/payments.spec.js +0 -97
- package/test/payments.utils.js +0 -190
- package/test/psbt.spec.js +0 -1044
- package/test/script.spec.js +0 -151
- package/test/script_number.spec.js +0 -24
- package/test/script_signature.spec.js +0 -52
- package/test/transaction.spec.js +0 -269
- package/test/types.spec.js +0 -46
package/test/script.spec.ts
CHANGED
|
@@ -1,210 +1,186 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { describe, it } from 'mocha';
|
|
3
|
-
import * as bscript from '../src/script';
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
assert.strictEqual(
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
assert.strictEqual(
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
bscript.
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
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
|
|
2
|
-
import { describe, it } from 'mocha';
|
|
3
|
-
import * as scriptNumber from '../src/script_number';
|
|
4
|
-
import
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
|
2
|
-
import { describe, it } from 'mocha';
|
|
3
|
-
import { signature as bscriptSig } from '../src/script';
|
|
4
|
-
import
|
|
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
|
+
});
|