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';
2017-02-27 08:17:22 +03:00
var HDNode = require('../hdnode/index.js');
2017-02-24 22:44:19 +03:00
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; }
2017-02-27 08:17:22 +03:00
var rootNode = new HDNode.fromSeed(testcase.seed);
2017-02-24 22:44:19 +03:00
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; }
2017-02-27 08:17:22 +03:00
test.equal(HDNode.entropyToMnemonic(testcase.entropy), testcase.mnemonic,
2017-02-24 22:44:19 +03:00
'Failed to convert mnemonic - ' + testcase.name);
2017-02-27 08:17:22 +03:00
test.equal(HDNode.mnemonicToEntropy(testcase.mnemonic), testcase.entropy,
2017-02-24 22:44:19 +03:00
'Failed to convert entropy - ' + testcase.name);
2017-02-27 08:17:22 +03:00
test.equal(HDNode.mnemonicToSeed(testcase.mnemonic, testcase.password), testcase.seed,
2017-02-24 22:44:19 +03:00
'Failed to convert seed - ' + testcase.name);
});
test.done();
}
module.exports = {
"hdnode": testHDNode,
"mnemonic": testMnemonic,
}