Pull out and fix getSubtle()

This commit is contained in:
Simon Warta 2021-12-16 23:21:32 +01:00
parent 956b5379a3
commit 9603a1da46

View File

@ -2177,6 +2177,18 @@ export class EnglishMnemonic {
}
}
async function getSubtle(): Promise<any | undefined> {
const g: any = globalThis;
let subtle = g.crypto && g.crypto.subtle;
if (!subtle) {
const crypto: any = await import("crypto");
if (crypto.webcrypto && crypto.webcrypto.subtle) {
subtle = crypto.webcrypto.subtle;
}
}
return subtle;
}
export class Bip39 {
/**
* Encodes raw entropy of length 16, 20, 24, 28 or 32 bytes as an English mnemonic between 12 and 24 words.
@ -2215,13 +2227,7 @@ export class Bip39 {
iterations: number,
keylen: number,
): Promise<Uint8Array> {
const g: any = globalThis;
let subtle = g.crypto && g.crypto.subtle;
if (!subtle) {
const crypto = await import("crypto");
subtle = (crypto as any).webcrypto.subtle;
}
const subtle = await getSubtle();
if (subtle) {
return subtle
.importKey("raw", secret, { name: "PBKDF2" }, false, ["deriveBits"])