From: Chris Duncan Date: Tue, 15 Jul 2025 18:50:47 +0000 (-0700) Subject: Fix null coin and use Nano as default for BIP-44 ckd. X-Git-Tag: v0.10.5~57^2~12 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=5057f39c9d5db529635c059bbdaec1c4ca130fbd;p=libnemo.git Fix null coin and use Nano as default for BIP-44 ckd. --- diff --git a/src/lib/workers/bip44-ckd.ts b/src/lib/workers/bip44-ckd.ts index afe3bf8..9b212c0 100644 --- a/src/lib/workers/bip44-ckd.ts +++ b/src/lib/workers/bip44-ckd.ts @@ -22,7 +22,7 @@ export class Bip44Ckd extends WorkerInterface { static async work (headers: Headers, data: Data): Promise { 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} Private child key for the account */ static async nanoCKD (seed: ArrayBuffer, index: number): Promise { - 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} Private child key for the account */ - static async ckd (seed: ArrayBuffer, coin: number, index: number): Promise { + static async ckd (seed: ArrayBuffer, coin: number = this.BIP44_COIN_NANO, index: number): Promise { 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 {