]> zoso.dev Git - libnemo.git/commitdiff
Start eliminating imports from BIP-44 ckd worker since they are problematic for testi...
authorChris Duncan <chris@zoso.dev>
Fri, 22 Nov 2024 10:12:09 +0000 (02:12 -0800)
committerChris Duncan <chris@zoso.dev>
Fri, 22 Nov 2024 10:12:09 +0000 (02:12 -0800)
src/lib/workers/ckdBip44.ts

index 2f889ef42c08521b50835b0b17c44335d3dd5585..a05aef49a527f5f07713bf6e91b319ff96b59e7d 100644 (file)
@@ -7,21 +7,21 @@ type ExtendedKey = {
 }
 
 async function ckdBip44 () {
-       let BIP44_COIN_NANO: any, BIP44_PURPOSE: any, HARDENED_OFFSET: any, SLIP10_ED25519: any
-       let bytes: any, dec: any, hex: any, utf8: any
+       const BIP44_COIN_NANO = 165
+       const BIP44_PURPOSE = 44
+       const HARDENED_OFFSET = 0x80000000
+       const SLIP10_ED25519 = 'ed25519 seed'
+       let bytes: any
+       let hex: any
        let addEventListener = globalThis.addEventListener
        let postMessage = globalThis.postMessage
        if (addEventListener == null || postMessage == null) {
-               ({ BIP44_COIN_NANO, BIP44_PURPOSE, HARDENED_OFFSET, SLIP10_ED25519 } = await import(new URL('dist/lib/constants.js', import.meta.url).toString()));
-               ({ bytes, dec, hex, utf8 } = await import(new URL('./dist/lib/convert.js', import.meta.url).toString()))
+               ({ bytes, hex } = await import(new URL('./dist/lib/convert.js', import.meta.url).toString()))
                const { isMainThread, parentPort } = await import('node:worker_threads')
                if (!isMainThread && parentPort) {
                        addEventListener = Object.getPrototypeOf(parentPort).addListener.bind(parentPort)
                        postMessage = Object.getPrototypeOf(parentPort).postMessage.bind(parentPort)
                }
-       } else {
-               ({ BIP44_COIN_NANO, BIP44_PURPOSE, HARDENED_OFFSET, SLIP10_ED25519 } = await import('../constants.js'));
-               ({ bytes, dec, hex, utf8 } = await import('../convert.js'))
        }
 
        /**
@@ -52,7 +52,7 @@ async function ckdBip44 () {
        }
 
        async function slip10 (curve: string, S: string): Promise<ExtendedKey> {
-               const key = utf8.toBytes(curve)
+               const key = new TextEncoder().encode(curve)
                const data = hex.toBytes(S)
                const I = await hmac(key, data)
                const IL = I.slice(0, I.length / 2)
@@ -73,11 +73,12 @@ async function ckdBip44 () {
                if (typeof integer !== 'number') {
                        throw new TypeError(`Expected a number, received ${typeof integer}`)
                }
-               const bits = dec.toBin(integer)
-               if (bits.length > 32) {
-                       throw new RangeError(`Expected 32-bit integer, received ${bits.length}-bit value: ${integer}`)
+               if (integer > 0xffffffff) {
+                       throw new RangeError(`Expected 32-bit integer, received ${integer.toString(2).length}-bit value: ${integer}`)
                }
-               return dec.toBytes(integer, 4)
+               const view = new DataView(new ArrayBuffer(4))
+               view.setUint32(0, integer, false)
+               return new Uint8Array(view.buffer)
        }
 
        function ser256 (integer: string): Uint8Array {