Skip to content

Commit

Permalink
Make mnemonic phrases case agnostic (#557).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Jul 9, 2019
1 parent ab5f9e2 commit e4423b7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/hdnode/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ export class HDNode implements ExternallyOwnedAccount {
}

static fromMnemonic(mnemonic: string, password?: string, wordlist?: Wordlist): HDNode {
// Check that the checksum s valid (will throw an error)
mnemonicToEntropy(mnemonic, wordlist);
// Normalize the case and spacing in the mnemonic (throws if the mnemonic is invalid)
mnemonic = entropyToMnemonic(mnemonicToEntropy(mnemonic, wordlist), wordlist);

return HDNode._fromSeed(mnemonicToSeed(mnemonic, password), mnemonic);
}
Expand Down
28 changes: 27 additions & 1 deletion packages/tests/src.ts/test-hdnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,33 @@
import assert from "assert";

import { ethers } from "ethers";
import { loadTests, TestCase } from "@ethersproject/testcases";
import { loadTests, randomNumber, TestCase } from "@ethersproject/testcases";

function randomCase(seed: string, text: string): string {
return text.split("").map(function(c, index) {
if (randomNumber(seed + "-" + index, 0, 2)) {
return c.toUpperCase();
}
return c
}).join("");
}

describe('Test HD Node Derivation is Case Agnostic', function() {
let tests: Array<TestCase.HDWallet> = loadTests('hdnode');
tests.forEach((test) => {
it("Normalizes case - " + test.name, function() {
this.timeout(10000);
let wordlist = (<{ [ locale: string ]: ethers.Wordlist }>(ethers.wordlists))[test.locale];

let rootNode = ethers.utils.HDNode.fromMnemonic(test.mnemonic, test.password || null, wordlist);

let altMnemonic = randomCase(test.name, test.mnemonic);
let altNode = ethers.utils.HDNode.fromMnemonic(altMnemonic, test.password || null, wordlist);

assert.equal(altNode.privateKey, rootNode.privateKey, altMnemonic);
});
});
});

describe('Test HD Node Derivation from Seed', function() {

Expand Down

0 comments on commit e4423b7

Please sign in to comment.