import { Blake2bCkd } from './blake2b-ckd'
import { BIP39_ITERATIONS } from './constants'
import { default as Convert, bytes, hex, utf8 } from './convert.js'
+import { Entropy } from './entropy'
import { NanoNaCl } from './nano-nacl'
import { NamedData } from '#types'
*/
static async create (type?: 'BIP-44' | 'BLAKE2b', key?: CryptoKey, keySalt?: ArrayBuffer, mnemonicSalt?: string): Promise<NamedData<ArrayBuffer>> {
try {
- const entropy = crypto.getRandomValues(new Uint8Array(32))
- const mnemonicPhrase = (await Bip39Mnemonic.fromEntropy(entropy)).phrase
+ const entropy = new Entropy()
+ const { phrase: mnemonicPhrase } = await Bip39Mnemonic.fromEntropy(entropy.bytes)
const record = await this.import(type, key, keySalt, mnemonicPhrase, mnemonicSalt)
if (this.#seed == null || this.#mnemonic?.phrase == null) {
throw new Error('Failed to generate seed and mnemonic')
seed: bytes.toHex(new Uint8Array(this.#seed))
}
if (this.#mnemonic?.phrase != null) data.mnemonic = this.#mnemonic.phrase
- const iv = crypto.getRandomValues(new Uint8Array(32)).buffer
+ const iv = new Entropy().buffer
const encoded = utf8.toBytes(JSON.stringify(data))
const encrypted = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, encoded)
return { iv, encrypted }
}
const iv: ArrayBuffer = action === 'unlock' && messageData.iv instanceof ArrayBuffer
? messageData.iv
- : crypto.getRandomValues(new Uint8Array(32)).buffer
+ : new Entropy().buffer
// Salt for decryption key to unlock
if (action === 'unlock' && !(messageData.keySalt instanceof ArrayBuffer)) {
}
const keySalt: ArrayBuffer = action === 'unlock' && messageData.keySalt instanceof ArrayBuffer
? messageData.keySalt
- : crypto.getRandomValues(new Uint8Array(32)).buffer
+ : new Entropy().buffer
// CryptoKey from password, decryption key if unlocking else encryption key
const key = password instanceof ArrayBuffer
const Bip44Ckd = ${Bip44Ckd}
const Blake2b = ${Blake2b}
const Blake2bCkd = ${Blake2bCkd}
+ const Entropy = ${Entropy}
const NanoNaCl = ${NanoNaCl}
const Safe = ${Safe}
`