Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Аdded NFKD normalization for mnemonic passphrase #1335

Merged
merged 1 commit into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});