2017-10-18 21:28:45 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var assert = require('assert');
|
|
|
|
|
|
|
|
var utils = require('./utils');
|
2018-07-23 03:02:39 -04:00
|
|
|
var ethers = utils.getEthers(__filename);
|
2017-10-18 21:28:45 -04:00
|
|
|
|
|
|
|
describe('Test HD Node Derivation', function(test) {
|
|
|
|
|
|
|
|
var tests = utils.loadTests('hdnode');
|
|
|
|
tests.forEach(function(test) {
|
|
|
|
it('Derives the HD nodes - ' + test.name, function() {
|
2017-10-22 17:47:17 -04:00
|
|
|
this.timeout(10000);
|
|
|
|
|
2018-09-24 16:07:14 -04:00
|
|
|
var rootNode = new ethers.utils.HDNode.fromSeed(test.seed);
|
2017-10-18 21:28:45 -04:00
|
|
|
test.hdnodes.forEach(function(nodeTest) {
|
|
|
|
|
|
|
|
var node = rootNode.derivePath(nodeTest.path);
|
|
|
|
assert.equal(node.privateKey, nodeTest.privateKey,
|
|
|
|
'Generates privateKey - ' + nodeTest.privateKey);
|
|
|
|
|
2018-03-04 19:31:09 -05:00
|
|
|
var wallet = new ethers.Wallet(node.privateKey);
|
2017-10-18 21:28:45 -04:00
|
|
|
assert.equal(wallet.address.toLowerCase(), nodeTest.address,
|
|
|
|
'Generates address - ' + nodeTest.privateKey);
|
2018-10-07 01:09:56 -04:00
|
|
|
|
|
|
|
assert.equal(node.address, (new ethers.Wallet(node)).address, 'HDNode address matches - ' + nodeTest.privateKey);
|
2017-10-18 21:28:45 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Test HD Mnemonic Phrases', function testMnemonic() {
|
|
|
|
var tests = utils.loadTests('hdnode');
|
|
|
|
tests.forEach(function(test) {
|
|
|
|
it(('converts mnemonic phrases - ' + test.name), function() {
|
2017-10-22 17:47:17 -04:00
|
|
|
this.timeout(1000000);
|
|
|
|
|
2018-09-24 16:07:14 -04:00
|
|
|
assert.equal(ethers.utils.HDNode.entropyToMnemonic(test.entropy), test.mnemonic,
|
2017-10-18 21:28:45 -04:00
|
|
|
'Converts entropy to mnemonic ' + test.name);
|
2018-09-24 16:07:14 -04:00
|
|
|
assert.equal(ethers.utils.HDNode.mnemonicToEntropy(test.mnemonic), test.entropy,
|
2017-10-18 21:28:45 -04:00
|
|
|
'Converts mnemonic to entropy - ' + test.mnemonic);
|
2018-09-24 16:07:14 -04:00
|
|
|
assert.equal(ethers.utils.HDNode.mnemonicToSeed(test.mnemonic, test.password), test.seed,
|
2017-10-18 21:28:45 -04:00
|
|
|
'Converts mnemonic to seed - ' + test.mnemonic + ':' + test.password);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-06-20 20:29:54 -04:00
|
|
|
|
|
|
|
// See: https://github.com/nym-zone/easyseed
|
|
|
|
function testEasySeed(lang, locale) {
|
|
|
|
describe('Test easyseed BIP39 Test cases - ' + locale, function() {
|
|
|
|
var tests = utils.loadJson('easyseed-bip39/bip39_vectors.' + locale + '.json');
|
|
|
|
tests.forEach(function(test) {
|
|
|
|
it('test - ' + test.entropy, function() {
|
2018-06-22 03:36:00 -04:00
|
|
|
this.timeout(100000);
|
2018-09-24 16:07:14 -04:00
|
|
|
var seed = ethers.utils.HDNode.mnemonicToSeed(test.mnemonic, test.passphrase);
|
2018-06-20 20:29:54 -04:00
|
|
|
assert.equal(seed, '0x' + test.seed, 'seeds match');
|
|
|
|
|
2018-09-24 16:07:14 -04:00
|
|
|
var entropy = ethers.utils.HDNode.mnemonicToEntropy(test.mnemonic, lang);
|
2018-06-20 20:29:54 -04:00
|
|
|
assert.equal(entropy, '0x' + test.entropy, 'entropy match');
|
|
|
|
|
2018-09-24 16:07:14 -04:00
|
|
|
var mnemonic = ethers.utils.HDNode.entropyToMnemonic('0x' + test.entropy, lang);
|
2018-06-20 20:29:54 -04:00
|
|
|
assert.equal(mnemonic.normalize('NFKD'), test.mnemonic.normalize('NFKD'), 'mnemonic match');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-23 05:38:53 -04:00
|
|
|
testEasySeed(ethers.wordlists.en, 'en');
|
2018-10-07 01:05:49 -04:00
|
|
|
testEasySeed(ethers.wordlists.es, 'es');
|
|
|
|
testEasySeed(ethers.wordlists.fr, 'fr');
|
2018-07-23 05:38:53 -04:00
|
|
|
testEasySeed(ethers.wordlists.ja, 'ja');
|
|
|
|
testEasySeed(ethers.wordlists.zh_cn, 'zh_cn');
|
|
|
|
testEasySeed(ethers.wordlists.zh_tw, 'zh_tw');
|
|
|
|
testEasySeed(ethers.wordlists.it, 'it');
|
|
|
|
testEasySeed(ethers.wordlists.ko, 'ko');
|