-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathscript_signature.spec.ts
66 lines (55 loc) · 1.68 KB
/
script_signature.spec.ts
1
2
3
4
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
import * as assert from 'assert';
import { describe, it } from 'mocha';
import { signature as bscriptSig } from '../src/script';
import * as fixtures from './fixtures/signature.json';
describe('Script Signatures', () => {
function fromRaw(signature: { r: string; s: string }): Buffer {
return Buffer.concat(
[Buffer.from(signature.r, 'hex'), Buffer.from(signature.s, 'hex')],
64,
);
}
function toRaw(signature: Buffer): {
r: string;
s: string;
} {
return {
r: signature.slice(0, 32).toString('hex'),
s: signature.slice(32, 64).toString('hex'),
};
}
describe('encode', () => {
fixtures.valid.forEach(f => {
it('encodes ' + f.hex, () => {
const buffer = bscriptSig.encode(fromRaw(f.raw), f.hashType);
assert.strictEqual(buffer.toString('hex'), f.hex);
});
});
fixtures.invalid.forEach(f => {
if (!f.raw) return;
it('throws ' + f.exception, () => {
const signature = fromRaw(f.raw);
assert.throws(() => {
bscriptSig.encode(signature, f.hashType);
}, new RegExp(f.exception));
});
});
});
describe('decode', () => {
fixtures.valid.forEach(f => {
it('decodes ' + f.hex, () => {
const decode = bscriptSig.decode(Buffer.from(f.hex, 'hex'));
assert.deepStrictEqual(toRaw(decode.signature), f.raw);
assert.strictEqual(decode.hashType, f.hashType);
});
});
fixtures.invalid.forEach(f => {
it('throws on ' + f.hex, () => {
const buffer = Buffer.from(f.hex, 'hex');
assert.throws(() => {
bscriptSig.decode(buffer);
}, new RegExp(f.exception));
});
});
});
});