From 469f5d3a4d1cb2da7a3479d5a1153bc165438e8c Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 22 Nov 2024 02:12:09 -0800 Subject: [PATCH] Start eliminating imports from BIP-44 ckd worker since they are problematic for testing with Node and also bundling for the browser. --- src/lib/workers/ckdBip44.ts | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/lib/workers/ckdBip44.ts b/src/lib/workers/ckdBip44.ts index 2f889ef..a05aef4 100644 --- a/src/lib/workers/ckdBip44.ts +++ b/src/lib/workers/ckdBip44.ts @@ -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 { - 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 { -- 2.34.1