//! 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
* 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
}
}
- 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
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)