]> git.codecow.com Git - libnemo.git/commitdiff
Fix null coin and use Nano as default for BIP-44 ckd.
authorChris Duncan <chris@zoso.dev>
Tue, 15 Jul 2025 18:50:47 +0000 (11:50 -0700)
committerChris Duncan <chris@zoso.dev>
Tue, 15 Jul 2025 18:50:47 +0000 (11:50 -0700)
src/lib/workers/bip44-ckd.ts

index afe3bf8ac91ce538fee8b1dc0a971630ec778775..9b212c0f04d359181750c1456cdbd4d50911aeff 100644 (file)
@@ -22,7 +22,7 @@ export class Bip44Ckd extends WorkerInterface {
        static async work (headers: Headers, data: Data): Promise<any[]> {
                let { coin, indexes } = headers
                let { seed } = data
-               if (typeof coin !== 'number' || !Number.isInteger(coin)) {
+               if (coin != null && (typeof coin !== 'number' || !Number.isInteger(coin))) {
                        throw new TypeError('BIP-44 coin derivation level must be an integer')
                }
                if (!Array.isArray(indexes)) indexes = [indexes]
@@ -32,9 +32,7 @@ export class Bip44Ckd extends WorkerInterface {
                                throw new TypeError('BIP-44 account derivation level must be an integer')
                        }
                        try {
-                               const pk = (coin !== this.BIP44_PURPOSE)
-                                       ? await this.ckd(seed, coin, i)
-                                       : await this.nanoCKD(seed, i)
+                               const pk = await this.ckd(seed, coin, i)
                                privateKeys.push(pk)
                        } catch (err) {
                                console.log(err)
@@ -52,23 +50,20 @@ export class Bip44Ckd extends WorkerInterface {
        * @returns {Promise<string>} Private child key for the account
        */
        static async nanoCKD (seed: ArrayBuffer, index: number): Promise<ArrayBuffer> {
-               if (!Number.isSafeInteger(index) || index < 0 || index > 0x7fffffff) {
-                       throw new RangeError(`Invalid child key index 0x${index.toString(16)}`)
-               }
                return await this.ckd(seed, this.BIP44_COIN_NANO, index)
        }
 
        /**
        * Derives a private child key for a coin by following the specified BIP-32 and
-       * BIP-44 derivation path. Purpose is always 44'. Only hardened child keys are
-       * defined.
+       * BIP-44 derivation path. Purpose is always 44'. Coin defaults to Nano. Only
+       * hardened child keys are defined.
        *
        * @param {string} seed - Hexadecimal seed derived from mnemonic phrase
        * @param {number} coin - Number registered to a specific coin in SLIP-044
        * @param {number} index - Account number between 0 and 2^31-1
        * @returns {Promise<string>} Private child key for the account
        */
-       static async ckd (seed: ArrayBuffer, coin: number, index: number): Promise<ArrayBuffer> {
+       static async ckd (seed: ArrayBuffer, coin: number = this.BIP44_COIN_NANO, index: number): Promise<ArrayBuffer> {
                if (seed.byteLength < 16 || seed.byteLength > 64) {
                        throw new RangeError(`Invalid seed length`)
                }
@@ -80,12 +75,6 @@ export class Bip44Ckd extends WorkerInterface {
                const coinKey = await this.CKDpriv(purposeKey, coin + this.HARDENED_OFFSET)
                const accountKey = await this.CKDpriv(coinKey, index + this.HARDENED_OFFSET)
                return accountKey.privateKey.buffer
-               // const privateKey = new Uint8Array(accountKey.privateKey.buffer)
-               // let hex = ''
-               // for (let i = 0; i < privateKey.length; i++) {
-               //      hex += privateKey[i].toString(16).padStart(2, '0')
-               // }
-               // return hex
        }
 
        static async slip10 (curve: string, S: ArrayBuffer): Promise<ExtendedKey> {