Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions packages/sdk/src/ids.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,41 @@ describe("normalizeRoundId", () => {
assert.equal(normalizeRoundId("001"), 1n);
assert.equal(normalizeRoundId(7), 7n);
assert.equal(normalizeRoundId(7n), 7n);
assert.equal(normalizeRoundId(1), 1n);
assert.equal(normalizeRoundId(1n), 1n);
assert.equal(normalizeRoundId(Number.MAX_SAFE_INTEGER), BigInt(Number.MAX_SAFE_INTEGER));
assert.equal(normalizeRoundId(BigInt(Number.MAX_SAFE_INTEGER) + 100n), BigInt(Number.MAX_SAFE_INTEGER) + 100n);
});

it("rejects malformed values with explicit errors", () => {
assert.throws(() => normalizeRoundId(""), /roundId/);
assert.throws(() => normalizeRoundId(" "), /roundId/);
assert.throws(() => normalizeRoundId("0"), /positive integer/);
assert.throws(() => normalizeRoundId("-1"), /positive integer/);
assert.throws(() => normalizeRoundId("1.5"), /roundId/);
assert.throws(() => normalizeRoundId("abc"), /roundId/);
assert.throws(() => normalizeRoundId("1.5"), /positive integer/);
assert.throws(() => normalizeRoundId("abc"), /positive integer/);
assert.throws(() => normalizeRoundId("0x10"), /positive integer/);
assert.throws(() => normalizeRoundId("+5"), /positive integer/);
});

it("rejects zero and negative number or bigint inputs", () => {
assert.throws(() => normalizeRoundId(0), /positive integer/);
assert.throws(() => normalizeRoundId(-1), /positive integer/);
assert.throws(() => normalizeRoundId(-42), /positive integer/);
assert.throws(() => normalizeRoundId(0n), /positive integer/);
assert.throws(() => normalizeRoundId(-1n), /positive integer/);
assert.throws(() => normalizeRoundId(-42n), /positive integer/);
});

it("rejects unsafe, fractional, and non-finite numbers", () => {
assert.throws(() => normalizeRoundId(1.5), /positive integer/);
assert.throws(() => normalizeRoundId(0.1), /positive integer/);
assert.throws(() => normalizeRoundId(NaN), /positive integer/);
assert.throws(() => normalizeRoundId(Infinity), /positive integer/);
assert.throws(() => normalizeRoundId(-Infinity), /positive integer/);
assert.throws(() => normalizeRoundId(Number.MAX_SAFE_INTEGER + 1), /positive integer/);
assert.throws(() => normalizeRoundId(Number.MAX_SAFE_INTEGER + 2), /positive integer/);
assert.throws(() => normalizeRoundId(Number.MIN_SAFE_INTEGER), /positive integer/);
});
});

Expand Down
28 changes: 15 additions & 13 deletions packages/sdk/src/ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ function toTrimmedString(value: string | undefined): string | undefined {
}

export function normalizeRoundId(value: string | number | bigint): bigint {
if (typeof value === "bigint") return value;
if (typeof value === "bigint") {
if (value <= 0n) {
throw new Error(`roundId must be a positive integer, got ${value}`);
}
return value;
}

if (typeof value === "number") {
if (!Number.isInteger(value)) {
throw new Error(`roundId must be an integer, got ${JSON.stringify(value)}`);
if (!Number.isSafeInteger(value) || value <= 0) {
throw new Error(`roundId must be a positive integer, got ${value}`);
}
return BigInt(value);
}
Expand All @@ -21,18 +26,15 @@ export function normalizeRoundId(value: string | number | bigint): bigint {
throw new Error("roundId must be a non-empty decimal string");
}

try {
const parsed = BigInt(trimmed);
if (parsed < 1n) {
throw new Error(`roundId must be a positive integer, got ${JSON.stringify(trimmed)}`);
}
return parsed;
} catch (error) {
if (error instanceof Error && error.message.includes("roundId")) {
throw error;
}
if (!/^\d+$/.test(trimmed)) {
throw new Error(`roundId must be a positive integer, got ${JSON.stringify(trimmed)}`);
}

const parsed = BigInt(trimmed);
if (parsed <= 0n) {
throw new Error(`roundId must be a positive integer, got ${JSON.stringify(trimmed)}`);
}
return parsed;
}

export function normalizeSorobanContractId(value: string): string {
Expand Down
Loading