]> git.codecow.com Git - libnemo.git/commitdiff
Implement entropy in safe.
authorChris Duncan <chris@zoso.dev>
Mon, 4 Aug 2025 18:23:23 +0000 (11:23 -0700)
committerChris Duncan <chris@zoso.dev>
Mon, 4 Aug 2025 18:23:23 +0000 (11:23 -0700)
src/lib/safe.ts

index de5657b5ba4dea5a9574df118fedf3bf5a749bd6..76a6c2fc7151fe874564540f4c523f4f2d474fa0 100644 (file)
@@ -11,6 +11,7 @@ import { Blake2b } from './blake2b'
 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'
 
@@ -112,8 +113,8 @@ export class Safe {
        */
        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')
@@ -369,7 +370,7 @@ export class Safe {
                        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 }
@@ -416,7 +417,7 @@ export class Safe {
                }
                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)) {
@@ -424,7 +425,7 @@ export class Safe {
                }
                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
@@ -515,6 +516,7 @@ export default `
        const Bip44Ckd = ${Bip44Ckd}
        const Blake2b = ${Blake2b}
        const Blake2bCkd = ${Blake2bCkd}
+       const Entropy = ${Entropy}
        const NanoNaCl = ${NanoNaCl}
        const Safe = ${Safe}
 `