2019-05-14 18:48:48 -04:00
|
|
|
'use strict';
|
|
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
var assert_1 = __importDefault(require("assert"));
|
|
|
|
var ethers_1 = require("ethers");
|
|
|
|
var testcases_1 = require("@ethersproject/testcases");
|
2019-07-09 17:30:10 -04:00
|
|
|
function randomCase(seed, text) {
|
|
|
|
return text.split("").map(function (c, index) {
|
|
|
|
if (testcases_1.randomNumber(seed + "-" + index, 0, 2)) {
|
|
|
|
return c.toUpperCase();
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}).join("");
|
|
|
|
}
|
2020-02-12 18:21:51 -05:00
|
|
|
// Too many test cases are caussing issues for the CI
|
|
|
|
// Only run random cases under random-128
|
|
|
|
function checkRandom(name) {
|
2020-06-01 05:04:12 -04:00
|
|
|
/*
|
2020-02-12 18:21:51 -05:00
|
|
|
if (name.substring(0, 7) === "random-") {
|
|
|
|
return (parseInt(name.substring(7)) <= 128);
|
|
|
|
}
|
2020-06-01 05:04:12 -04:00
|
|
|
*/
|
2020-02-12 18:21:51 -05:00
|
|
|
return true;
|
|
|
|
}
|
2019-07-09 17:30:10 -04:00
|
|
|
describe('Test HD Node Derivation is Case Agnostic', function () {
|
|
|
|
var tests = testcases_1.loadTests('hdnode');
|
|
|
|
tests.forEach(function (test) {
|
2020-02-12 18:21:51 -05:00
|
|
|
if (!checkRandom(test.name)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-07-09 17:30:10 -04:00
|
|
|
it("Normalizes case - " + test.name, function () {
|
|
|
|
this.timeout(10000);
|
|
|
|
var wordlist = (ethers_1.ethers.wordlists)[test.locale];
|
|
|
|
var rootNode = ethers_1.ethers.utils.HDNode.fromMnemonic(test.mnemonic, test.password || null, wordlist);
|
|
|
|
var altMnemonic = randomCase(test.name, test.mnemonic);
|
|
|
|
var altNode = ethers_1.ethers.utils.HDNode.fromMnemonic(altMnemonic, test.password || null, wordlist);
|
|
|
|
assert_1.default.equal(altNode.privateKey, rootNode.privateKey, altMnemonic);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-05-14 18:48:48 -04:00
|
|
|
describe('Test HD Node Derivation from Seed', function () {
|
|
|
|
var tests = testcases_1.loadTests('hdnode');
|
|
|
|
tests.forEach(function (test) {
|
2020-02-12 18:21:51 -05:00
|
|
|
if (!checkRandom(test.name)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
// If there is nothing to derive, skip this portion of the test
|
|
|
|
if (test.hdnodes.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
it('Derives the HD nodes - ' + test.name, function () {
|
|
|
|
this.timeout(10000);
|
|
|
|
var rootNode = ethers_1.ethers.utils.HDNode.fromSeed(test.seed);
|
|
|
|
test.hdnodes.forEach(function (nodeTest) {
|
|
|
|
var node = rootNode.derivePath(nodeTest.path);
|
|
|
|
assert_1.default.equal(node.privateKey, nodeTest.privateKey, 'Generates privateKey - ' + nodeTest.privateKey);
|
|
|
|
var wallet = new ethers_1.ethers.Wallet(node.privateKey);
|
|
|
|
assert_1.default.equal(wallet.address.toLowerCase(), nodeTest.address, 'Generates address - ' + nodeTest.privateKey);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('Test HD Node Derivation from Mnemonic', function () {
|
|
|
|
var tests = testcases_1.loadTests('hdnode');
|
|
|
|
tests.forEach(function (test) {
|
2020-02-12 18:21:51 -05:00
|
|
|
if (!checkRandom(test.name)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
// If there is nothing to derive, skip this portion of the test
|
|
|
|
if (test.hdnodes.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
it('Derives the HD nodes - ' + test.name, function () {
|
|
|
|
this.timeout(10000);
|
|
|
|
var rootNode = ethers_1.ethers.utils.HDNode.fromMnemonic(test.mnemonic, test.password || null);
|
|
|
|
test.hdnodes.forEach(function (nodeTest) {
|
|
|
|
var node = rootNode.derivePath(nodeTest.path);
|
|
|
|
assert_1.default.equal(node.privateKey, nodeTest.privateKey, 'Matches privateKey - ' + nodeTest.privateKey);
|
|
|
|
assert_1.default.equal(node.path, nodeTest.path, 'Matches path - ' + nodeTest.privateKey);
|
2020-01-18 21:48:12 -05:00
|
|
|
assert_1.default.equal(node.mnemonic.phrase, test.mnemonic, 'Matches mnemonic.phrase - ' + nodeTest.privateKey);
|
|
|
|
assert_1.default.equal(node.mnemonic.path, nodeTest.path, 'Matches mnemonic.path - ' + nodeTest.privateKey);
|
2019-05-14 18:48:48 -04:00
|
|
|
var wallet = new ethers_1.ethers.Wallet(node.privateKey);
|
|
|
|
assert_1.default.equal(wallet.address.toLowerCase(), nodeTest.address, 'Generates address - ' + nodeTest.privateKey);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('Test HD Mnemonic Phrases', function testMnemonic() {
|
|
|
|
var tests = testcases_1.loadTests('hdnode');
|
|
|
|
tests.forEach(function (test) {
|
2020-02-12 18:21:51 -05:00
|
|
|
if (!checkRandom(test.name)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
it(('converts mnemonic phrases - ' + test.name), function () {
|
|
|
|
this.timeout(1000000);
|
|
|
|
assert_1.default.equal(ethers_1.ethers.utils.mnemonicToSeed(test.mnemonic, test.password), test.seed, 'Converts mnemonic to seed - ' + test.mnemonic + ':' + test.password);
|
|
|
|
// Test default english
|
|
|
|
if (test.locale === "en") {
|
|
|
|
assert_1.default.equal(ethers_1.ethers.utils.entropyToMnemonic(test.entropy), test.mnemonic, "Converts entropy to mnemonic " + test.name + " (default en)");
|
|
|
|
assert_1.default.equal(ethers_1.ethers.utils.mnemonicToEntropy(test.mnemonic), test.entropy, "Converts mnemonic to entropy - " + test.mnemonic + " (default en)");
|
|
|
|
}
|
|
|
|
var wordlist = (ethers_1.ethers.wordlists)[test.locale];
|
|
|
|
var mnemonic = ethers_1.ethers.utils.entropyToMnemonic(test.entropy, wordlist);
|
|
|
|
assert_1.default.equal(mnemonic.normalize('NFKD'), test.mnemonic.normalize('NFKD'), 'Converts entropy to mnemonic ' + test.name);
|
|
|
|
assert_1.default.equal(ethers_1.ethers.utils.mnemonicToEntropy(test.mnemonic, wordlist), test.entropy, 'Converts mnemonic to entropy - ' + test.mnemonic);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|