All files / ethers.js/src.ts/_tests test-wallet-mnemonic.ts

95.8% Statements 137/143
92.59% Branches 25/27
100% Functions 3/3
95.8% Lines 137/143

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 1441x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1600x 1600x 1600x 1600x 1x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 800x 800x 80x 80x 800x 800x 800x 800x 800x 800x 800x 1x 1x 400x 400x 400x 400x       400x 400x 400x 400x 400x 400x 400x 1x 1x 400x 400x 400x 400x       400x 400x 400x 400x 400x 400x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x  
 
import assert from "assert";
 
import {
    getBytes, sha256, toUtf8Bytes,
    Mnemonic, wordlists,
} from "../index.js";
 
import { loadTests } from "./utils.js";
 
import type { TestCaseMnemonic } from "./types.js";
 
 
const decoder = new TextDecoder();
function fromHex(hex: string): string {
    const data = getBytes(hex);
    return decoder.decode(data);
}
 
function repeat(text: string, length: number): Array<string> {
    const result: Array<string> = [ ];
    while (result.length < length) { result.push(text); }
    return result;
}
 
describe("Tests Mnemonics", function() {
    const tests = loadTests<TestCaseMnemonic>("mnemonics");
 
    function runTest(phrase: string, mnemonic: Mnemonic, test: TestCaseMnemonic): void {
        assert.ok(Mnemonic.isValidMnemonic(phrase, mnemonic.wordlist), "isValidMnemonic");
        if (test.locale === "en") {
            assert.ok(Mnemonic.isValidMnemonic(phrase), "isValidMnemonic (default)");
        }
 
        assert.equal(mnemonic.wordlist.locale, test.locale, "locale");
 
        assert.equal(mnemonic.entropy, test.entropy, "entropy");
        assert.equal(mnemonic.computeSeed(), test.seed, "seed");
        assert.equal(sha256(toUtf8Bytes(phrase)), test.phraseHash, "phraseHash")
    }
 
    for (const test of tests) {
        const wordlist = wordlists[test.locale];
 
        it(`computes mnemonic from phrase: ${ test.name }`, function() {
            if (wordlist == null) {
                this.skip();
                return;
            }
 
            const phrase = fromHex(test.phrase);
            const password = fromHex(test.password);
            const mnemonic = Mnemonic.fromPhrase(phrase, password, wordlist);
            runTest(phrase, mnemonic, test);
        });
    }
 
    for (const test of tests) {
        const wordlist = wordlists[test.locale];
 
        it(`computes mnemonic from entropy: ${ test.name }`, function() {
            if (wordlist == null) {
                this.skip();
                return;
            }
            const phrase = fromHex(test.phrase);
            const password = fromHex(test.password);
            const mnemonic = Mnemonic.fromEntropy(test.entropy, password, wordlist);
            runTest(phrase, mnemonic, test);
        });
    }
});
 
describe("Tests Bad Mnemonics Fail", function() {
 
    const badLengths = [
        repeat("abandon", 9),   // 9 words; too short
        repeat("abandon", 16),  // 16 words; not congruent to 0 mod 3
        repeat("abandon", 27),  // 27 words; too long
    ];
 
    for (const _phrase of badLengths) {
        const phrase = _phrase.join(" ");
        it(`correctly fails on invalid mnemonic length: ${ _phrase.length }`, function() {
            assert.ok(!Mnemonic.isValidMnemonic(phrase));
            assert.throws(function() {
                Mnemonic.fromPhrase(phrase);
            }, function(error: any) {
                return (error.code === "INVALID_ARGUMENT" &&
                    error.message.match(/^invalid mnemonic length/) &&
                    error.argument === "mnemonic" &&
                    error.value === "[ REDACTED ]");
            });
        });
    }
 
    it("correctly fails on invalid mnemonic word", function() {
        const phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon wagmi";
        assert.ok(!Mnemonic.isValidMnemonic(phrase));
        assert.throws(function() {
            Mnemonic.fromPhrase(phrase);
        }, function(error: any) {
            return (error.code === "INVALID_ARGUMENT" &&
                error.message.match(/^invalid mnemonic word at index 11/) &&
                error.argument === "mnemonic" &&
                error.value === "[ REDACTED ]");
        });
    });
 
    it("correctly fails on invalid mnemonic checksum", function() {
        const phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon";
        assert.ok(!Mnemonic.isValidMnemonic(phrase));
        assert.throws(function() {
            Mnemonic.fromPhrase(phrase);
        }, function(error: any) {
            return (error.code === "INVALID_ARGUMENT" &&
                error.message.match(/^invalid mnemonic checksum/) &&
                error.argument === "mnemonic" &&
                error.value === "[ REDACTED ]");
        });
    });
 
 
    const badEntropyLengths = [
        repeat("42", 12),   //12 bytes; too short
        repeat("42", 15),  // 16 bytes; not congruent to 0 mod 4
        repeat("42", 36),  // 36 bytes; too long
    ];
 
    for (const _entropy of badEntropyLengths) {
        const entropy = "0x" + _entropy.join("");
        it(`correctly fails on invalid entropy length: ${ _entropy.length }`, function() {
            assert.throws(function() {
                Mnemonic.fromEntropy(entropy);
            }, function(error: any) {
                return (error.code === "INVALID_ARGUMENT" &&
                    error.message.match(/^invalid entropy size/) &&
                    error.argument === "entropy" &&
                    error.value === "[ REDACTED ]");
            });
        });
    }
})