forked from bcoin-org/bcoin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mnemonic-test.js
67 lines (54 loc) · 1.97 KB
/
mnemonic-test.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
/* eslint-env mocha */
/* eslint prefer-arrow-callback: "off" */
'use strict';
const assert = require('bsert');
const Mnemonic = require('../lib/hd/mnemonic');
const HDPrivateKey = require('../lib/hd/private');
const tests = {
english: require('./data/mnemonic-english.json'),
japanese: require('./data/mnemonic-japanese.json')
};
describe('Mnemonic', function() {
for (const language of Object.keys(tests)) {
const test = tests[language];
let i = 0;
for (const data of test) {
const entropy = Buffer.from(data[0], 'hex');
const phrase = data[1];
const passphrase = data[2];
const seed = Buffer.from(data[3], 'hex');
const xpriv = data[4];
it(`should create a ${language} mnemonic from entropy (${i})`, () => {
const mnemonic = new Mnemonic({
language,
entropy
});
assert.strictEqual(mnemonic.getPhrase(), phrase);
assert.bufferEqual(mnemonic.getEntropy(), entropy);
assert.bufferEqual(mnemonic.toSeed(passphrase), seed);
const key = HDPrivateKey.fromMnemonic(mnemonic, passphrase);
assert.strictEqual(key.toBase58('main'), xpriv);
});
it(`should create a ${language} mnemonic from phrase (${i})`, () => {
const mnemonic = new Mnemonic({
language,
phrase
});
assert.strictEqual(mnemonic.getPhrase(), phrase);
assert.bufferEqual(mnemonic.getEntropy(), entropy);
assert.bufferEqual(mnemonic.toSeed(passphrase), seed);
const key = HDPrivateKey.fromMnemonic(mnemonic, passphrase);
assert.strictEqual(key.toBase58('main'), xpriv);
});
i += 1;
}
}
it('should verify phrase', () => {
const m1 = new Mnemonic();
const m2 = Mnemonic.fromPhrase(m1.getPhrase());
assert.bufferEqual(m2.getEntropy(), m1.getEntropy());
assert.strictEqual(m2.bits, m1.bits);
assert.strictEqual(m2.language, m1.language);
assert.bufferEqual(m2.toSeed(), m1.toSeed());
});
});