]> git.codecow.com Git - libnemo.git/commitdiff
Extract child key algorithm steps 2-6 to its own function.
authorChris Duncan <chris@codecow.com>
Fri, 15 May 2026 08:05:42 +0000 (01:05 -0700)
committerChris Duncan <chris@codecow.com>
Fri, 15 May 2026 08:05:42 +0000 (01:05 -0700)
src/lib/crypto/bip44.ts

index 922b7d106418af37cb8c87a9993a81a4f05ff58a..040bdc614eeb31430e0637ce082ec9efebdd4e69 100644 (file)
@@ -103,24 +103,26 @@ function CKDpriv (curve: Curve, { privateKey, chainCode }: ExtendedKey, index?:
        const key = chainCode
        const data = t.buffer
        return hmac(key, data)
-               .then(I => {
-                       const IL = I.slice(0, I.byteLength / 2)
-                       const IR = I.slice(I.byteLength / 2)
-                       if (curve === 'ed25519 seed') {
-                               return ({ privateKey: IL, chainCode: IR })
-                       } else {
-                               const ILparsed = parse256(new Uint8Array(IL))
-                               if (ILparsed >= Point.CURVE().n) {
-                                       throw new Error('Invalid child key is greater than the order of the curve')
-                               }
-                               const pkParsed = parse256(pk)
-                               const childKey = (ILparsed + pkParsed) % Point.CURVE().n
-                               if (childKey === 0n) {
-                                       throw new Error('Invalid child key is zero')
-                               }
-                               return ({ privateKey: ser256(childKey).buffer, chainCode: IR })
-                       }
-               })
+               .then(I => childKey(curve, pk, I))
+}
+
+function childKey (curve: Curve, pk: Bytes, I: ArrayBuffer): ExtendedKey {
+       const IL = I.slice(0, I.byteLength / 2)
+       const IR = I.slice(I.byteLength / 2)
+       if (curve === 'ed25519 seed') {
+               return ({ privateKey: IL, chainCode: IR })
+       } else {
+               const ILparsed = parse256(new Uint8Array(IL))
+               if (ILparsed >= Point.CURVE().n) {
+                       throw new Error('Invalid child key is greater than the order of the curve')
+               }
+               const pkParsed = parse256(pk)
+               const childKey = (ILparsed + pkParsed) % Point.CURVE().n
+               if (childKey === 0n) {
+                       throw new Error('Invalid child key is zero')
+               }
+               return ({ privateKey: ser256(childKey).buffer, chainCode: IR })
+       }
 }
 
 function ser32 (integer: number): Bytes {