]> git.codecow.com Git - libnemo.git/commitdiff
Convert index to bytes with buffers instead of string manipulation.
authorChris Duncan <chris@zoso.dev>
Sun, 3 Aug 2025 05:40:12 +0000 (22:40 -0700)
committerChris Duncan <chris@zoso.dev>
Sun, 3 Aug 2025 05:40:12 +0000 (22:40 -0700)
src/lib/blake2b-ckd.ts
src/lib/blake2b.ts

index b347a5e75ee16cd16cc19d224f41a3811c820472..7f75f0714bf8e95374f9515f78db2717838477cf 100644 (file)
@@ -2,7 +2,6 @@
 //! SPDX-License-Identifier: GPL-3.0-or-later\r
 \r
 import { Blake2b } from '#src/lib/blake2b.js'\r
-import { hex } from '#src/lib/convert.js'\r
 \r
 /**\r
 * Derives account private keys from a wallet seed using the BLAKE2b hashing\r
@@ -14,15 +13,15 @@ import { hex } from '#src/lib/convert.js'
 * https://docs.nano.org/integration-guides/the-basics/\r
 *\r
 * @param {ArrayBuffer} seed - 32-byte secret seed of the wallet\r
-* @param {number} index - Account to derive\r
+* @param {number} index - 4-byte index of account to derive\r
 * @returns {ArrayBuffer} Private key for the account\r
 */\r
 export class Blake2bCkd {\r
        static ckd (seed: ArrayBuffer, index: number): ArrayBuffer {\r
-               return new Blake2b(32)\r
-                       .update(new Uint8Array(seed))\r
-                       .update(hex.toBytes(index.toString(16).padStart(8, '0')))\r
-                       .digest()\r
-                       .buffer\r
+               const b = new ArrayBuffer(4)\r
+               new DataView(b).setUint32(0, index, false)\r
+               const s = new Uint8Array(seed)\r
+               const i = new Uint8Array(b)\r
+               return new Blake2b(32).update(s).update(i).digest().buffer\r
        }\r
 }\r
index 47c1297d40347548b3827ed7bb9f9045b52f697a..319e73b25f765bf69ef572b894ba35372f1ad507 100644 (file)
@@ -256,12 +256,12 @@ export class Blake2b {
                }
        }
 
-       update (input: Uint8Array<ArrayBuffer>): Blake2b {
+       update (input: Uint8Array): Blake2b {
                if (input instanceof ArrayBuffer) {
                        input = new Uint8Array(input)
                }
                if (!(input instanceof Uint8Array)) {
-                       throw new TypeError(`input must be Uint8Array or Buffer`)
+                       throw new TypeError('Input must be Uint8Array or Buffer')
                }
                this.#blake2bUpdate(input)
                return this
@@ -272,8 +272,8 @@ export class Blake2b {
        digest (out: 'binary' | Uint8Array<ArrayBuffer>): Uint8Array<ArrayBuffer>
        digest (out?: 'binary' | 'hex' | Uint8Array<ArrayBuffer>): string | Uint8Array<ArrayBuffer> {
                const buf = (!out || out === 'binary' || out === 'hex') ? new Uint8Array(this.#outlen) : out
-               if (!(buf instanceof Uint8Array)) throw new TypeError(`out must be "binary", "hex", Uint8Array, or Buffer`)
-               if (buf.length < this.#outlen) throw new RangeError(`out must have at least outlen bytes of space`)
+               if (!(buf instanceof Uint8Array)) throw new TypeError('out must be "binary", "hex", Uint8Array, or Buffer')
+               if (buf.length < this.#outlen) throw new RangeError('out must have at least outlen bytes of space')
                this.#blake2bFinal(buf)
                return (out === 'hex')
                        ? Blake2b.#toHex(buf)