Merge pull request #305 from CosmWasm/219-pbkdf2-uint8array

Use pbkdf2 Uint8Array support
This commit is contained in:
Will Clark
2020-07-21 13:42:41 +02:00
committed by GitHub
3 changed files with 15 additions and 16 deletions
+2 -2
View File
@@ -51,7 +51,7 @@
"elliptic": "^6.4.0",
"js-sha3": "^0.8.0",
"libsodium-wrappers": "^0.7.6",
"pbkdf2": "^3.0.16",
"pbkdf2": "^3.1.1",
"ripemd160": "^2.0.2",
"sha.js": "^2.4.11",
"type-tagger": "^1.0.0",
@@ -61,7 +61,7 @@
"@types/bn.js": "^4.11.6",
"@types/elliptic": "^6.4.12",
"@types/libsodium-wrappers": "^0.7.7",
"@types/pbkdf2": "^3.0.0",
"@types/pbkdf2": "^3.1.0",
"@types/ripemd160": "^2.0.0",
"@types/sha.js": "^2.4.0",
"@types/unorm": "^1.3.27"
+5 -6
View File
@@ -1,4 +1,4 @@
import { fromHex, toHex } from "@cosmjs/encoding";
import { fromHex, toHex, toUtf8 } from "@cosmjs/encoding";
import * as bip39 from "bip39";
import { pbkdf2 } from "pbkdf2";
import * as unorm from "unorm";
@@ -23,13 +23,13 @@ export class Bip39 {
public static async mnemonicToSeed(mnemonic: EnglishMnemonic, password?: string): Promise<Uint8Array> {
// reimplementation of bip39.mnemonicToSeed using the asynchronous
// interface of https://www.npmjs.com/package/pbkdf2
const mnemonicBytes = Buffer.from(unorm.nfkd(mnemonic.toString()), "utf8");
const mnemonicBytes = toUtf8(unorm.nfkd(mnemonic.toString()));
const salt = "mnemonic" + (password ? unorm.nfkd(password) : "");
const saltBytes = Buffer.from(salt, "utf8");
const saltBytes = toUtf8(salt);
return this.pbkdf2(mnemonicBytes, saltBytes, 2048, 64, "sha512");
}
// convert pbkdf2's calllback interface to Promise interface
// convert pbkdf2's callback interface to Promise interface
private static async pbkdf2(
secret: Uint8Array,
salt: Uint8Array,
@@ -38,8 +38,7 @@ export class Bip39 {
digest: string,
): Promise<Uint8Array> {
return new Promise<any>((resolve, reject) => {
// TODO: Patch @types/pbkdf2 to allow Uint8Array as secret and salt argument
pbkdf2(Buffer.from(secret), Buffer.from(salt), iterations, keylen, digest, (err, derivedKey) => {
pbkdf2(secret, salt, iterations, keylen, digest, (err, derivedKey) => {
if (err) {
reject(err);
} else {