-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathtypes.spec.ts
123 lines (106 loc) · 3.86 KB
/
types.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
import * as assert from 'assert';
import { describe, it } from 'mocha';
import * as types from '../src/types';
const typeforce = require('typeforce');
import * as fixtures from './fixtures/types.json';
describe('types', () => {
describe('Buffer Hash160/Hash256', () => {
const buffer20byte = Buffer.alloc(20);
const buffer32byte = Buffer.alloc(32);
it('return true for valid size', () => {
assert(types.Hash160bit(buffer20byte));
assert(types.Hash256bit(buffer32byte));
});
it('return true for oneOf', () => {
assert.doesNotThrow(() => {
typeforce(
types.oneOf(types.Hash160bit, types.Hash256bit),
buffer32byte,
);
});
assert.doesNotThrow(() => {
typeforce(
types.oneOf(types.Hash256bit, types.Hash160bit),
buffer32byte,
);
});
});
it('throws for invalid size', () => {
assert.throws(() => {
types.Hash160bit(buffer32byte);
}, /Expected Buffer\(Length: 20\), got Buffer\(Length: 32\)/);
assert.throws(() => {
types.Hash256bit(buffer20byte);
}, /Expected Buffer\(Length: 32\), got Buffer\(Length: 20\)/);
});
});
describe('Satoshi', () => {
[
{ value: -1, result: false },
{ value: 0, result: true },
{ value: 1, result: true },
{ value: 20999999 * 1e8, result: true },
{ value: 21000000 * 1e8, result: true },
{ value: 21000001 * 1e8, result: false },
].forEach(f => {
it('returns ' + f.result + ' for valid for ' + f.value, () => {
assert.strictEqual(types.Satoshi(f.value), f.result);
});
});
});
describe('UInt31', () => {
const UINT31_MAX = Math.pow(2, 31) - 1;
it('return true for valid values', () => {
assert.strictEqual(types.UInt31(0), true);
assert.strictEqual(types.UInt31(1000), true);
assert.strictEqual(types.UInt31(UINT31_MAX), true);
});
it('return false for negative values', () => {
assert.strictEqual(types.UInt31(-1), false);
assert.strictEqual(types.UInt31(-UINT31_MAX), false);
});
it(`return false for value > ${UINT31_MAX}`, () => {
assert.strictEqual(types.UInt31(UINT31_MAX + 1), false);
});
});
describe('BIP32Path', () => {
it('return true for valid paths', () => {
assert.strictEqual(types.BIP32Path("m/0'/0'"), true);
assert.strictEqual(types.BIP32Path("m/0'/0"), true);
assert.strictEqual(types.BIP32Path("m/0'/1'/2'/3/4'"), true);
});
it('return false for invalid paths', () => {
assert.strictEqual(types.BIP32Path('m'), false);
assert.strictEqual(types.BIP32Path("n/0'/0'"), false);
assert.strictEqual(types.BIP32Path("m/0'/x"), false);
});
it('return "BIP32 derivation path" for JSON.strigify()', () => {
const toJsonValue = JSON.stringify(types.BIP32Path);
assert.equal(toJsonValue, '"BIP32 derivation path"');
});
});
describe('isPoint (uncompressed)', () => {
fixtures.isPoint.forEach(f => {
it(`returns ${f.expected} for isPoint(${f.hex})`, () => {
const bytes = Buffer.from(f.hex, 'hex');
assert.strictEqual(types.isPoint(bytes), f.expected);
});
});
});
describe('isPoint (compressed) + isXOnlyPoint', () => {
fixtures.isXOnlyPoint.forEach(f => {
it(`returns ${f.expected} for isPoint(02${f.hex})`, () => {
const bytes = Buffer.from(`02${f.hex}`, 'hex');
assert.strictEqual(types.isPoint(bytes), f.expected);
});
it(`returns ${f.expected} for isPoint(03${f.hex})`, () => {
const bytes = Buffer.from(`03${f.hex}`, 'hex');
assert.strictEqual(types.isPoint(bytes), f.expected);
});
it(`returns ${f.expected} for isXOnlyPoint(${f.hex})`, () => {
const bytes = Buffer.from(f.hex, 'hex');
assert.strictEqual(types.isXOnlyPoint(bytes), f.expected);
});
});
});
});