Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

relaySigning.ts

19 lines802 Bytes

1function isRecord(value: unknown): value is Record<string, unknown> {
2 return typeof value === "object" && value !== null && !Array.isArray(value);
3}
4​
5export function stableStringify(value: unknown): string {
6 if (Array.isArray(value)) {
7 return `[${value.map(stableStringify).join(",")}]`;
8 }
9 if (isRecord(value)) {
10 return `{${Object.entries(value)
11 .filter(([, entryValue]) => entryValue !== undefined)
12 // Code-unit order, not localeCompare: signatures and RFC 7638 thumbprints
13 // must match across machines whatever their OS locale.
14 .sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0))
15 .map(([key, entryValue]) => `${JSON.stringify(key)}:${stableStringify(entryValue)}`)
16 .join(",")}}`;
17 }
18 return JSON.stringify(value) ?? "null";