-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
68 lines (58 loc) · 1.9 KB
/
index.js
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
const bip39 = require('bip39');
const test = require('tape');
const preFixtures = require('./fixtures');
const { SLIP77Factory } = require('../src/');
const ecc = require('tiny-secp256k1');
const initBuffers = object =>
JSON.parse(JSON.stringify(object), (_, value) => {
const regex = new RegExp(/^Buffer.from\(['"](.*)['"], ['"](.*)['"]\)$/);
const result = regex.exec(value);
if (!result) return value;
const data = result[1];
const encoding = result[2];
return Buffer.from(data, encoding);
});
const fixtures = initBuffers(preFixtures);
const slip77 = SLIP77Factory(ecc)
fixtures.valid.fromSeed.forEach(f => {
test('fromSeed', t => {
const seed = bip39.mnemonicToSeedSync(f.mnemonic);
const node1 = slip77.fromSeed(seed);
const node2 = slip77.fromSeed(seed.toString('hex'));
t.same(node1.masterKey.toString('hex'), f.expected);
t.same(node2.masterKey.toString('hex'), f.expected);
t.end();
});
});
fixtures.valid.fromMasterBlindingKey.forEach(f => {
test('fromMasterBlindingKey', t => {
const slip77Node = slip77.fromMasterBlindingKey(f.masterKey);
t.same(slip77Node.masterKey.toString('hex'), f.masterKey);
t.end();
});
});
fixtures.valid.derive.forEach(f => {
test('derive', t => {
const master = slip77.fromMasterBlindingKey(f.masterKey);
const derived = master.derive(f.script);
t.same(derived.privateKey.toString('hex'), f.expectedPrivKey);
t.same(derived.publicKey.toString('hex'), f.expectedPubKey);
t.end();
});
});
fixtures.invalid.fromSeed.forEach(f => {
test('fromSeed throws', t => {
t.throws(() => {
slip77.fromSeed(f.seed);
}, new RegExp(f.exception));
t.end();
});
});
fixtures.invalid.fromMasterBlindingKey.forEach(f => {
test('fromMasterBlindingKey throws', t => {
t.throws(() => {
slip77.fromMasterBlindingKey(f.masterKey);
}, new RegExp(f.exception));
t.end();
});
});