From: Chris Duncan Date: Sat, 7 Dec 2024 21:32:48 +0000 (-0800) Subject: Extend bip44ckd to support any specified coin as an argument. nanoCKD remains as... X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=1d00af326555ae94d420013581f6e90a6301c023;p=libnemo.git Extend bip44ckd to support any specified coin as an argument. nanoCKD remains as a dedicated function for this wallet library. --- diff --git a/src/lib/bip32-key-derivation.ts b/src/lib/bip32-key-derivation.ts index f9014f9..886b623 100644 --- a/src/lib/bip32-key-derivation.ts +++ b/src/lib/bip32-key-derivation.ts @@ -18,12 +18,29 @@ type ExtendedKey = { * @returns {Promise} Private child key for the account */ export async function nanoCKD (seed: string, index: number): Promise { + if (!Number.isSafeInteger(index) || index < 0 || index > 0x7fffffff) { + throw new RangeError(`Invalid child key index 0x${index.toString(16)}`) + } + return await ckd(seed, 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. +* +* @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 +*/ +export async function ckd (seed: string, coin: number, index: number): Promise { if (!Number.isSafeInteger(index) || index < 0 || index > 0x7fffffff) { throw new RangeError(`Invalid child key index 0x${index.toString(16)}`) } const masterKey = await slip10(SLIP10_ED25519, seed) const purposeKey = await CKDpriv(masterKey, BIP44_PURPOSE + HARDENED_OFFSET) - const coinKey = await CKDpriv(purposeKey, BIP44_COIN_NANO + HARDENED_OFFSET) + const coinKey = await CKDpriv(purposeKey, coin + HARDENED_OFFSET) const accountKey = await CKDpriv(coinKey, index + HARDENED_OFFSET) return accountKey.privateKey } diff --git a/src/lib/bip44-ckd.ts b/src/lib/bip44-ckd.ts index c768e8b..ab1fdca 100644 --- a/src/lib/bip44-ckd.ts +++ b/src/lib/bip44-ckd.ts @@ -40,9 +40,26 @@ const b = () => { * * @param {string} seed - Hexadecimal seed derived from mnemonic phrase * @param {number} index - Account number between 0 and 2^31-1 - * @returns Private child key for the account + * @returns {Promise} Private child key for the account */ async function nanoCKD (seed: string, index: number): Promise { + if (!Number.isSafeInteger(index) || index < 0 || index > 0x7fffffff) { + throw new RangeError(`Invalid child key index 0x${index.toString(16)}`) + } + return await ckd(seed, 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. + * + * @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 + */ + async function ckd (seed: string, coin: number, index: number): Promise { if (seed.length < 32 || seed.length > 128) { throw new RangeError(`Invalid seed length`) } @@ -51,7 +68,7 @@ const b = () => { } const masterKey = await slip10(SLIP10_ED25519, seed) const purposeKey = await CKDpriv(masterKey, BIP44_PURPOSE + HARDENED_OFFSET) - const coinKey = await CKDpriv(purposeKey, BIP44_COIN_NANO + HARDENED_OFFSET) + const coinKey = await CKDpriv(purposeKey, coin + HARDENED_OFFSET) const accountKey = await CKDpriv(coinKey, index + HARDENED_OFFSET) const privateKey = new Uint8Array(accountKey.privateKey.buffer) let hex = ''