chore: use built in bip39

This commit is contained in:
Alex Harley 2021-05-14 23:12:25 +02:00
parent dab008f2f5
commit c3ab89f6a9
No known key found for this signature in database
GPG Key ID: 36DCBCB317F919C3

View File

@ -1,8 +1,5 @@
import { fromHex, toHex, toUtf8 } from "@cosmjs/encoding";
import { fromHex, toHex } from "@cosmjs/encoding";
import * as bip39 from "bip39";
import { pbkdf2 } from "pbkdf2";
import * as unorm from "unorm";
import { EnglishMnemonic } from "./englishmnemonic";
export class Bip39 {
@ -36,30 +33,6 @@ 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 = toUtf8(unorm.nfkd(mnemonic.toString()));
const salt = "mnemonic" + (password ? unorm.nfkd(password) : "");
const saltBytes = toUtf8(salt);
return this.pbkdf2(mnemonicBytes, saltBytes, 2048, 64, "sha512");
}
// convert pbkdf2's callback interface to Promise interface
private static async pbkdf2(
secret: Uint8Array,
salt: Uint8Array,
iterations: number,
keylen: number,
digest: string,
): Promise<Uint8Array> {
return new Promise<any>((resolve, reject) => {
pbkdf2(secret, salt, iterations, keylen, digest, (err, derivedKey) => {
if (err) {
reject(err);
} else {
resolve(new Uint8Array(derivedKey));
}
});
});
return new Uint8Array(await bip39.mnemonicToSeed(mnemonic.toString(), password));
}
}