\r
async function CKDpriv ({ privateKey, chainCode }: ExtendedKey, index: number): Promise<ExtendedKey> {\r
const key = hex.toBytes(chainCode)\r
- const data = hex.toBytes(`00${bytes.toHex(await ser256(privateKey))}${bytes.toHex(await ser32(index))}`)\r
+ const data = hex.toBytes(`00${bytes.toHex(ser256(privateKey))}${bytes.toHex(ser32(index))}`)\r
const I = await hmac(key, data)\r
const IL = I.slice(0, I.length / 2)\r
const IR = I.slice(I.length / 2)\r
return ({ privateKey: IL, chainCode: IR })\r
}\r
\r
-async function ser32 (integer: number): Promise<Uint8Array> {\r
- const bits = integer.toString(2)\r
+function ser32 (integer: number): Uint8Array {\r
+ if (typeof integer !== 'number') {\r
+ throw new TypeError(`Expected a number, received ${typeof integer}`)\r
+ }\r
+ const bits = dec.toBin(integer)\r
if (bits.length > 32) {\r
- throw new RangeError(`Expected 32-bit integer, received ${bits.length} bits: ${bits}`)\r
+ throw new RangeError(`Expected 32-bit integer, received ${bits.length}-bit value: ${integer}`)\r
}\r
- const bytes = dec.toBytes(integer)\r
- const result = new Uint8Array(4)\r
- result.set(bytes, 4 - bytes.length)\r
- return result\r
+ return dec.toBytes(integer, 4)\r
}\r
\r
-async function ser256 (integer: string): Promise<Uint8Array> {\r
+function ser256 (integer: string): Uint8Array {\r
+ if (typeof integer !== 'string') {\r
+ throw new TypeError(`Expected string, received ${typeof integer}`)\r
+ }\r
const bits = hex.toBin(integer)\r
if (bits.length > 256) {\r
- throw new RangeError(`Expected 256-bit integer, received ${bits.length} bits: ${bits}`)\r
+ throw new RangeError(`Expected 256-bit integer, received ${bits.length}-bit value: ${integer}`)\r
}\r
- const bytes = hex.toBytes(integer)\r
- const result = new Uint8Array(32)\r
- result.set(bytes, 32 - bytes.length)\r
- return result\r
+ return hex.toBytes(integer, 32)\r
}\r
\r
async function hmac (key: Uint8Array, data: Uint8Array): Promise<string> {\r