Skip to content

Commit 8377186

Browse files
committed
hdkey: Improve toU32
1 parent 18e07b4 commit 8377186

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/hdkey.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ export interface Versions {
4040
const hash160 = (data: Uint8Array) => ripemd160(sha256(data));
4141
const fromU32 = (data: Uint8Array) => createView(data).getUint32(0, false);
4242
const toU32 = (n: number) => {
43+
if (!Number.isSafeInteger(n) || n < 0 || n >= 2 ** 32) {
44+
throw new Error(`Invalid number=${n}. Should be [0, 2**32)`);
45+
}
4346
const buf = new Uint8Array(4);
4447
createView(buf).setUint32(0, n, false);
4548
return buf;
@@ -224,8 +227,7 @@ export class HDKey {
224227
if (!this.pubKey || !this.chainCode) {
225228
throw new Error("No publicKey or chainCode set");
226229
}
227-
let data = new Uint8Array(4);
228-
createView(data).setUint32(0, index, false);
230+
let data = toU32(index);
229231
if (index >= HARDENED_OFFSET) {
230232
// Hardened
231233
const priv = this.privateKey;

0 commit comments

Comments
 (0)