ethers.js/lib.commonjs/_tests/test-wallet-mnemonic.js

121 lines
5.2 KiB
JavaScript
Raw Permalink Normal View History

2022-09-05 16:57:11 -04:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
2023-04-25 20:04:48 +09:00
const tslib_1 = require("tslib");
const assert_1 = tslib_1.__importDefault(require("assert"));
2023-05-06 15:16:41 +09:00
const index_js_1 = require("../index.js");
2022-09-05 16:57:11 -04:00
const utils_js_1 = require("./utils.js");
const decoder = new TextDecoder();
function fromHex(hex) {
2023-05-06 15:16:41 +09:00
const data = (0, index_js_1.getBytes)(hex);
2022-09-05 16:57:11 -04:00
return decoder.decode(data);
}
function repeat(text, length) {
const result = [];
while (result.length < length) {
result.push(text);
}
return result;
}
describe("Tests Mnemonics", function () {
const tests = (0, utils_js_1.loadTests)("mnemonics");
function runTest(phrase, mnemonic, test) {
2023-05-06 15:16:41 +09:00
assert_1.default.ok(index_js_1.Mnemonic.isValidMnemonic(phrase, mnemonic.wordlist), "isValidMnemonic");
2022-09-05 16:57:11 -04:00
if (test.locale === "en") {
2023-05-06 15:16:41 +09:00
assert_1.default.ok(index_js_1.Mnemonic.isValidMnemonic(phrase), "isValidMnemonic (default)");
2022-09-05 16:57:11 -04:00
}
assert_1.default.equal(mnemonic.wordlist.locale, test.locale, "locale");
assert_1.default.equal(mnemonic.entropy, test.entropy, "entropy");
assert_1.default.equal(mnemonic.computeSeed(), test.seed, "seed");
2023-05-06 15:16:41 +09:00
assert_1.default.equal((0, index_js_1.sha256)((0, index_js_1.toUtf8Bytes)(phrase)), test.phraseHash, "phraseHash");
2022-09-05 16:57:11 -04:00
}
for (const test of tests) {
2023-05-06 15:16:41 +09:00
const wordlist = index_js_1.wordlists[test.locale];
2022-09-05 16:57:11 -04:00
it(`computes mnemonic from phrase: ${test.name}`, function () {
if (wordlist == null) {
this.skip();
return;
}
const phrase = fromHex(test.phrase);
const password = fromHex(test.password);
2023-05-06 15:16:41 +09:00
const mnemonic = index_js_1.Mnemonic.fromPhrase(phrase, password, wordlist);
2022-09-05 16:57:11 -04:00
runTest(phrase, mnemonic, test);
});
}
for (const test of tests) {
2023-05-06 15:16:41 +09:00
const wordlist = index_js_1.wordlists[test.locale];
2022-09-05 16:57:11 -04:00
it(`computes mnemonic from entropy: ${test.name}`, function () {
if (wordlist == null) {
this.skip();
return;
}
const phrase = fromHex(test.phrase);
const password = fromHex(test.password);
2023-05-06 15:16:41 +09:00
const mnemonic = index_js_1.Mnemonic.fromEntropy(test.entropy, password, wordlist);
2022-09-05 16:57:11 -04:00
runTest(phrase, mnemonic, test);
});
}
});
describe("Tests Bad Mnemonics Fail", function () {
const badLengths = [
repeat("abandon", 9),
repeat("abandon", 16),
repeat("abandon", 27), // 27 words; too long
];
for (const _phrase of badLengths) {
const phrase = _phrase.join(" ");
2022-11-30 15:44:23 -05:00
it(`correctly fails on invalid mnemonic length: ${_phrase.length}`, function () {
2023-05-06 15:16:41 +09:00
assert_1.default.ok(!index_js_1.Mnemonic.isValidMnemonic(phrase));
2022-09-05 16:57:11 -04:00
assert_1.default.throws(function () {
2023-05-06 15:16:41 +09:00
index_js_1.Mnemonic.fromPhrase(phrase);
2022-09-05 16:57:11 -04:00
}, function (error) {
return (error.code === "INVALID_ARGUMENT" &&
error.message.match(/^invalid mnemonic length/) &&
error.argument === "mnemonic" &&
error.value === "[ REDACTED ]");
});
});
}
2022-11-30 15:44:23 -05:00
it("correctly fails on invalid mnemonic word", function () {
2022-09-05 16:57:11 -04:00
const phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon wagmi";
2023-05-06 15:16:41 +09:00
assert_1.default.ok(!index_js_1.Mnemonic.isValidMnemonic(phrase));
2022-09-05 16:57:11 -04:00
assert_1.default.throws(function () {
2023-05-06 15:16:41 +09:00
index_js_1.Mnemonic.fromPhrase(phrase);
2022-09-05 16:57:11 -04:00
}, function (error) {
return (error.code === "INVALID_ARGUMENT" &&
error.message.match(/^invalid mnemonic word at index 11/) &&
error.argument === "mnemonic" &&
error.value === "[ REDACTED ]");
});
});
2022-11-30 15:44:23 -05:00
it("correctly fails on invalid mnemonic checksum", function () {
2022-09-05 16:57:11 -04:00
const phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon";
2023-05-06 15:16:41 +09:00
assert_1.default.ok(!index_js_1.Mnemonic.isValidMnemonic(phrase));
2022-09-05 16:57:11 -04:00
assert_1.default.throws(function () {
2023-05-06 15:16:41 +09:00
index_js_1.Mnemonic.fromPhrase(phrase);
2022-09-05 16:57:11 -04:00
}, function (error) {
return (error.code === "INVALID_ARGUMENT" &&
error.message.match(/^invalid mnemonic checksum/) &&
error.argument === "mnemonic" &&
error.value === "[ REDACTED ]");
});
});
const badEntropyLengths = [
repeat("42", 12),
repeat("42", 15),
repeat("42", 36), // 36 bytes; too long
];
for (const _entropy of badEntropyLengths) {
const entropy = "0x" + _entropy.join("");
2022-11-30 15:44:23 -05:00
it(`correctly fails on invalid entropy length: ${_entropy.length}`, function () {
2022-09-05 16:57:11 -04:00
assert_1.default.throws(function () {
2023-05-06 15:16:41 +09:00
index_js_1.Mnemonic.fromEntropy(entropy);
2022-09-05 16:57:11 -04:00
}, function (error) {
return (error.code === "INVALID_ARGUMENT" &&
error.message.match(/^invalid entropy size/) &&
error.argument === "entropy" &&
error.value === "[ REDACTED ]");
});
});
}
});
//# sourceMappingURL=test-wallet-mnemonic.js.map