* @returns {Promise<string>} Private child key for the account\r
*/\r
export async function nanoCKD (seed: string, index: number): Promise<string> {\r
+ if (!Number.isSafeInteger(index) || index < 0 || index > 0x7fffffff) {\r
+ throw new RangeError(`Invalid child key index 0x${index.toString(16)}`)\r
+ }\r
+ return await ckd(seed, BIP44_COIN_NANO, index)\r
+}\r
+\r
+/**\r
+* Derives a private child key for a coin by following the specified BIP-32 and\r
+* BIP-44 derivation path. Purpose is always 44'. Only hardened child keys are\r
+* defined.\r
+*\r
+* @param {string} seed - Hexadecimal seed derived from mnemonic phrase\r
+* @param {number} coin - Number registered to a specific coin in SLIP-044\r
+* @param {number} index - Account number between 0 and 2^31-1\r
+* @returns {Promise<string>} Private child key for the account\r
+*/\r
+export async function ckd (seed: string, coin: number, index: number): Promise<string> {\r
if (!Number.isSafeInteger(index) || index < 0 || index > 0x7fffffff) {\r
throw new RangeError(`Invalid child key index 0x${index.toString(16)}`)\r
}\r
const masterKey = await slip10(SLIP10_ED25519, seed)\r
const purposeKey = await CKDpriv(masterKey, BIP44_PURPOSE + HARDENED_OFFSET)\r
- const coinKey = await CKDpriv(purposeKey, BIP44_COIN_NANO + HARDENED_OFFSET)\r
+ const coinKey = await CKDpriv(purposeKey, coin + HARDENED_OFFSET)\r
const accountKey = await CKDpriv(coinKey, index + HARDENED_OFFSET)\r
return accountKey.privateKey\r
}\r
*
* @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<string>} Private child key for the account
*/
async function nanoCKD (seed: string, index: number): Promise<string> {
+ 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<string>} Private child key for the account
+ */
+ async function ckd (seed: string, coin: number, index: number): Promise<string> {
if (seed.length < 32 || seed.length > 128) {
throw new RangeError(`Invalid seed length`)
}
}
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 = ''