]> git.codecow.com Git - libnemo.git/commitdiff
Move blake ckd into blake file out of vault.
authorChris Duncan <chris@zoso.dev>
Wed, 3 Sep 2025 13:17:59 +0000 (06:17 -0700)
committerChris Duncan <chris@zoso.dev>
Wed, 3 Sep 2025 13:17:59 +0000 (06:17 -0700)
src/lib/crypto/blake2b.ts
src/lib/vault/vault-worker.ts

index c9bc75b140a5baf42aed9af32d59d0c3aa15877d..29fc4e742bcfc77ece79102d7929a85e799c1357 100644 (file)
 *
 * Original source commit: https://github.com/emilbayes/blake2b/blob/1f63e02e3f226642959506cdaa67c8819ff145cd/index.js
 */
-
 export class Blake2b {
+       /**
+       * Derives account private keys from a wallet seed using the BLAKE2b hashing
+       * algorithm.
+       *
+       * Separately, account public keys are derived from the private key using the
+       * Ed25519 key algorithm, and account addresses are derived from the public key
+       * as described in the Nano documentation.
+       * https://docs.nano.org/integration-guides/the-basics/
+       *
+       * @param {ArrayBuffer} seed - 32-byte secret seed of the wallet
+       * @param {number} index - 4-byte index of account to derive
+       * @returns {ArrayBuffer} Private key for the account
+       */
+       static ckd (seed: ArrayBuffer, index: number): Promise<ArrayBuffer> {
+               const b = new ArrayBuffer(4)
+               new DataView(b).setUint32(0, index, false)
+               const s = new Uint8Array(seed)
+               const i = new Uint8Array(b)
+               const sk = new Blake2b(32).update(s).update(i).digest()
+               return Promise.resolve(sk.buffer)
+       }
+
        static get OUTBYTES_MIN (): 1 { return 1 }
        static get OUTBYTES_MAX (): 64 { return 64 }
        static get KEYBYTES_MIN (): 1 { return 1 }
index fb49762965d64c27df9277eb92d006b76e1613c3..ef18f0a0acc61010d35e046f14fc3f52ab63a170 100644 (file)
@@ -144,7 +144,7 @@ export class VaultWorker {
                        }
                        const derive = this.#type === 'BIP-44'
                                ? Bip44.ckd(this.#seed, BIP44_COIN_NANO, index)
-                               : Promise.resolve(this.#deriveBlake2bPrivateKey(this.#seed, index))
+                               : Blake2b.ckd(this.#seed, index)
                        return derive.then(prv => {
                                const pub = NanoNaCl.convert(new Uint8Array(prv))
                                this.#timeout = new VaultTimer(() => this.lock(), 120000)
@@ -205,7 +205,7 @@ export class VaultWorker {
                        }
                        const derive = this.#type === 'BIP-44'
                                ? Bip44.ckd(this.#seed, BIP44_COIN_NANO, index)
-                               : Promise.resolve(this.#deriveBlake2bPrivateKey(this.#seed, index))
+                               : Blake2b.ckd(this.#seed, index)
                        return derive.then(prv => {
                                const sig = NanoNaCl.detached(new Uint8Array(data), new Uint8Array(prv))
                                this.#timeout = new VaultTimer(() => this.lock(), 120000)
@@ -382,27 +382,6 @@ export class VaultWorker {
                        })
        }
 
-       /**
-       * Derives account private keys from a wallet seed using the BLAKE2b hashing
-       * algorithm.
-       *
-       * Separately, account public keys are derived from the private key using the
-       * Ed25519 key algorithm, and account addresses are derived from the public key
-       * as described in the Nano documentation.
-       * https://docs.nano.org/integration-guides/the-basics/
-       *
-       * @param {ArrayBuffer} seed - 32-byte secret seed of the wallet
-       * @param {number} index - 4-byte index of account to derive
-       * @returns {ArrayBuffer} Private key for the account
-       */
-       #deriveBlake2bPrivateKey (seed: ArrayBuffer, index: number): ArrayBuffer {
-               const b = new ArrayBuffer(4)
-               new DataView(b).setUint32(0, index, false)
-               const s = new Uint8Array(seed)
-               const i = new Uint8Array(b)
-               return new Blake2b(32).update(s).update(i).digest().buffer
-       }
-
        #encryptWallet (key: CryptoKey): Promise<NamedData<ArrayBuffer>> {
                if (this.#type == null) {
                        throw new Error('Invalid wallet type')