Skip to content

Commit 54f1b7c

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

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

cli/src/__tests__/xrpl-onboarding.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe("buildInitData", () => {
5252
test("mpt: decimals + 0x02 + mpt_id, right-padded to 43 bytes total", () => {
5353
const out = buildInitData(9, { type: "mpt", mptId: MPT_ID });
5454
expect(out.length).toBe(86);
55-
expect(out.startsWith("09" + "02" + MPT_ID.toLowerCase())).toBe(true);
55+
expect(out.startsWith("09" + "02" + MPT_ID)).toBe(true);
5656
// 1 + 2 + 48 = 51 hex of data, padded to 84 -> ends in zeros
5757
expect(out.endsWith("0000")).toBe(true);
5858
});

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)