diff --git a/packages/hdnode/src.ts/index.ts b/packages/hdnode/src.ts/index.ts index 848f471af9..4dadf220aa 100644 --- a/packages/hdnode/src.ts/index.ts +++ b/packages/hdnode/src.ts/index.ts @@ -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); } diff --git a/packages/tests/src.ts/test-hdnode.ts b/packages/tests/src.ts/test-hdnode.ts index ad63158ac0..01d4eb29b1 100644 --- a/packages/tests/src.ts/test-hdnode.ts +++ b/packages/tests/src.ts/test-hdnode.ts @@ -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 = 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() {