}
/**
-* Derives BIP-44 Nano account private keys.
-*
-* @param {number} index - Index of the account
-* @returns {Promise<Account>}
+* Listens for messages from a calling function.
*/
addEventListener('message', (message) => {
const { seed, index } = message.data ?? message
- ckd(seed, index).then(postMessage)
+ nanoCKD(seed, index).then(key => postMessage({ index, key }))
})
-/**
-* Derives a private child key following the BIP-32 and BIP-44 derivation path
-* registered to the Nano block lattice. Only hardened child keys are defined.
-*
-* @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
-*/
-async function ckd (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)}`)
- }
- 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 accountKey = await CKDpriv(coinKey, index + HARDENED_OFFSET)
- return accountKey.privateKey
-}
-
/**
* Derives a private child key following the BIP-32 and BIP-44 derivation path
* registered to the Nano block lattice. Only hardened child keys are defined.