ethers.js/tests/run-hdnode.js

46 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-02-24 22:44:19 +03:00
'use strict';
var hdnode = require('../hdnode/index.js');
function testHDNode(test) {
var Wallet = require('../wallet/index.js');
var c = 0;
var testcases = require('./tests/hdnode.json');
testcases.forEach(function(testcase) {
//if (c++ > 10) { return; }
var rootNode = new hdnode.HDNode.fromSeed(testcase.seed);
testcase.hdnodes.forEach(function(hdTestcase) {
var node = rootNode.derivePath(hdTestcase.path);
test.equal(node.privateKey, hdTestcase.privateKey,
'Failed to generate privateKey - ' + testcase.name);
var wallet = new Wallet(node.privateKey);
test.equal(wallet.address.toLowerCase(), hdTestcase.address,
'Failed to generate address - ' + testcase.name);
});
});
test.done();
}
function testMnemonic(test) {
var c = 0;
var testcases = require('./tests/hdnode.json');
testcases.forEach(function(testcase) {
//if (c++ > 10) { return; }
test.equal(hdnode.entropyToMnemonic(testcase.entropy), testcase.mnemonic,
'Failed to convert mnemonic - ' + testcase.name);
test.equal(hdnode.mnemonicToEntropy(testcase.mnemonic), testcase.entropy,
'Failed to convert entropy - ' + testcase.name);
test.equal(hdnode.mnemonicToSeed(testcase.mnemonic, testcase.password), testcase.seed,
'Failed to convert seed - ' + testcase.name);
});
test.done();
}
module.exports = {
"hdnode": testHDNode,
"mnemonic": testMnemonic,
}