From: Chris Duncan Date: Wed, 3 Sep 2025 13:17:59 +0000 (-0700) Subject: Move blake ckd into blake file out of vault. X-Git-Tag: v0.10.5~35^2~3 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=664344b502eea8a1ca16590881584011af791d2a;p=libnemo.git Move blake ckd into blake file out of vault. --- diff --git a/src/lib/crypto/blake2b.ts b/src/lib/crypto/blake2b.ts index c9bc75b..29fc4e7 100644 --- a/src/lib/crypto/blake2b.ts +++ b/src/lib/crypto/blake2b.ts @@ -10,8 +10,29 @@ * * 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 { + 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 } diff --git a/src/lib/vault/vault-worker.ts b/src/lib/vault/vault-worker.ts index fb49762..ef18f0a 100644 --- a/src/lib/vault/vault-worker.ts +++ b/src/lib/vault/vault-worker.ts @@ -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> { if (this.#type == null) { throw new Error('Invalid wallet type')