Skip to content

Commit 90e9e37

Browse files
dynstwebmaster128
authored andcommitted
number range assertions
1 parent b4ce2be commit 90e9e37

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

packages/crypto/src/slip10.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { fromHex, toAscii, toHex } from "@cosmjs/encoding";
22
import { Uint32, Uint53 } from "@cosmjs/math";
3+
import { assert } from "@cosmjs/utils";
34
import { secp256k1 } from "@noble/curves/secp256k1";
45

56
import { Hmac } from "./hmac";
@@ -25,6 +26,8 @@ function bytesToUnsignedBigInt(a: Uint8Array): bigint {
2526
}
2627

2728
function intTo32be(n: bigint): Uint8Array {
29+
assert(n >= 0n);
30+
assert(n < 2n ** (32n * 8n));
2831
// 32 bytes is 64 hexadecimal characters
2932
const hex = n.toString(16).padStart(64, "0");
3033
return fromHex(hex);

packages/math/src/integers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,10 @@ export class Uint64 implements Integer, WithByteConverters {
272272
}
273273

274274
public toNumber(): number {
275-
const num = Number(this.data);
276-
if (!Number.isSafeInteger(num)) {
275+
if (this.data > BigInt(Number.MAX_SAFE_INTEGER)) {
277276
throw new Error("number can only safely store up to 53 bits");
278277
}
278+
const num = Number(this.data);
279279
return num;
280280
}
281281
}

0 commit comments

Comments
 (0)