Skip to content

Commit 5950725

Browse files
committed
limit checks for u64Hex
1 parent 702066e commit 5950725

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

cli/src/xrpl/onboarding.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,26 @@ function u8Hex(n: number): string {
5353
return n.toString(16).padStart(2, "0");
5454
}
5555

56-
/** Encode a non-negative integer as a big-endian 8-byte (16 hex char) value. */
57-
function u64Hex(n: number): string {
58-
if (!Number.isInteger(n) || n < 0) {
59-
throw new Error(`value ${n} must be a non-negative integer`);
56+
const U64_MAX = (1n << 64n) - 1n;
57+
58+
/**
59+
* Encode a non-negative integer that fits in a u64 as a big-endian 8-byte
60+
* (16 hex char) value. Accepts a number, bigint, or decimal string.
61+
*/
62+
function u64Hex(n: string | number | bigint): string {
63+
if (typeof n === "number" && !Number.isInteger(n)) {
64+
throw new Error(`value ${n} must be an integer`);
65+
}
66+
let v: bigint;
67+
try {
68+
v = BigInt(n);
69+
} catch {
70+
throw new Error(`value "${n}" is not a valid integer`);
71+
}
72+
if (v < 0n || v > U64_MAX) {
73+
throw new Error(`value ${n} must be in the range [0, ${U64_MAX}] (u64)`);
6074
}
61-
return BigInt(n).toString(16).padStart(16, "0");
75+
return v.toString(16).padStart(16, "0");
6276
}
6377

6478
/** Decode an XRPL r-address to its 20-byte account ID (40 hex chars). */
@@ -122,7 +136,7 @@ export function buildInitData(decimals: number, token: TokenInit): string {
122136
return (
123137
dec +
124138
padRight(
125-
TOKEN_TYPE_MPT + token.mptId.toLowerCase(),
139+
TOKEN_TYPE_MPT + token.mptId,
126140
TOKEN_ID_PADDED_BYTES
127141
)
128142
);

0 commit comments

Comments
 (0)