Skip to content

Commit

Permalink
added NFKD normalization for mnemonic passphrase (hiero-ledger#1335)
Browse files Browse the repository at this point in the history
  • Loading branch information
petreze authored Nov 28, 2022
1 parent 00299da commit 68c24c4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/cryptography/src/primitive/bip39.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as pbkdf2 from "./pbkdf2.js";
*/
export async function toSeed(words, passphrase) {
const input = words.join(" ");
const salt = `mnemonic${passphrase}`;
const salt = (`mnemonic${passphrase}`).normalize("NFKD");

return pbkdf2.deriveKey(hmac.HashAlgorithm.Sha512, input, salt, 2048, 64);
}
37 changes: 37 additions & 0 deletions packages/cryptography/test/unit/bip39.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//import Mnemonic from "../../src/Mnemonic.js";
import * as bip39 from "../../src/primitive/bip39.js";
import * as hex from "../../src/encoding/hex.js";

Expand All @@ -24,4 +25,40 @@ describe("bip39", function () {
"ed37b3442b3d550d0fbb6f01f20aac041c245d4911e13452cac7b1676a070eda66771b71c0083b34cc57ca9c327c459a0ec3600dbaf7f238ff27626c8430a806"
);
});

it("mnemonic passphrase NFKD normalization compliant test", async function (){
const words = [
"inmate",
"flip",
"alley",
"wear",
"offer",
"often",
"piece",
"magnet",
"surge",
"toddler",
"submit",
"right",
"radio",
"absent",
"pear",
"floor",
"belt",
"raven",
"price",
"stove",
"replace",
"reduce",
"plate",
"home"
];

const unicodePassphrase = "\u0070\u0061\u0073\u0073\u0070\u0068\u0072\u0061\u0073\u0065";
const expectedPrivateKey = "1ed95521b3406aa1e34db78be696db32f09d9f8ec3115fc12314082a44a3e8d6d4551a1905758b45bc315430f7d9c095da93645f1b0004c393370e0a878dfd4c";

const seed = await bip39.toSeed(words, unicodePassphrase)

expect(hex.encode(seed)).to.be.equal(expectedPrivateKey);
});
});

0 comments on commit 68c24c4

Please sign in to comment.