Skip to content

Commit a34ca6b

Browse files
committed
Added test cases for case-agnostic mnemonics (ethers-io#557).
1 parent ef91dcd commit a34ca6b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

tests/test-hdnode.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,30 @@ describe('Testnet Extended Key (#553)', function testMnemonic() {
169169
});
170170
});
171171
});
172+
173+
describe("Test Mnemonic is Case Agnostic", function() {
174+
function randomCase(seed, text) {
175+
return text.split("").map(function(c, index) {
176+
if (utils.randomNumber(seed + "-" + index, 0, 1000) > 500) {
177+
return c.toUpperCase();
178+
}
179+
return c
180+
}).join("");
181+
}
182+
183+
function addTest(mnemonic, altMnemonic) {
184+
it(altMnemonic, function() {
185+
var node = ethers.utils.HDNode.fromMnemonic(mnemonic);
186+
var altNode = ethers.utils.HDNode.fromMnemonic(altMnemonic);
187+
assert.equal(node.privateKey, altNode.privateKey, altMnemonic);
188+
});
189+
}
190+
191+
for (var i = 0; i < 128; i++) {
192+
var seed = "test-" + i;
193+
var entropy = utils.randomBytes(seed, 16, 16);
194+
var mnemonic = ethers.utils.HDNode.entropyToMnemonic(entropy);
195+
var altMnemonic = randomCase(seed, mnemonic);
196+
addTest(mnemonic, altMnemonic);
197+
}
198+
});

0 commit comments

Comments
 (0)