Canonical encoding for BigInt (<bigint: Xn>) and Uint8Array (hex or <Uint8Array: 0x...>) used in the relay protocol. Provides both serialization (sourceOutputToRelay, transactionToHex) and deserialization (parseExtendedJson, toUint8Array, toBigInt) so dapps and wallets don't have to implement this independently.
101 lines
3.1 KiB
TypeScript
101 lines
3.1 KiB
TypeScript
// Copyright (C) 2026 Whiterun LLC,
|
|
// This software is licensed under the GNU Lesser General Public License (LGPL), version 3.0 or later.
|
|
// A copy of the license can be found in the LICENSE file or at https://www.gnu.org/licenses/lgpl-3.0.html
|
|
|
|
import { describe, it, expect } from "vitest";
|
|
import { binToHex } from "@bitauth/libauth";
|
|
import {
|
|
parseExtendedJson,
|
|
parseExtendedJsonValue,
|
|
isExtendedJsonFormat,
|
|
toUint8Array,
|
|
toBigInt,
|
|
} from "./serialize.js";
|
|
|
|
describe("wc-serialize", () => {
|
|
describe("parseExtendedJsonValue", () => {
|
|
it("parses bigint format", () => {
|
|
expect(parseExtendedJsonValue("<bigint: 546n>")).toBe(546n);
|
|
expect(parseExtendedJsonValue("<bigint: 0n>")).toBe(0n);
|
|
expect(parseExtendedJsonValue("<bigint: 100000000n>")).toBe(100000000n);
|
|
});
|
|
|
|
it("parses Uint8Array format", () => {
|
|
const result = parseExtendedJsonValue("<Uint8Array: 0xabcd>");
|
|
expect(result).toBeInstanceOf(Uint8Array);
|
|
expect(binToHex(result as Uint8Array)).toBe("abcd");
|
|
});
|
|
|
|
it("returns original string for non-extended values", () => {
|
|
expect(parseExtendedJsonValue("hello")).toBe("hello");
|
|
expect(parseExtendedJsonValue("123")).toBe("123");
|
|
});
|
|
});
|
|
|
|
describe("isExtendedJsonFormat", () => {
|
|
it("detects bigint format", () => {
|
|
expect(isExtendedJsonFormat("<bigint: 100n>")).toBe(true);
|
|
});
|
|
|
|
it("detects Uint8Array format", () => {
|
|
expect(isExtendedJsonFormat("<Uint8Array: 0xabcd>")).toBe(true);
|
|
});
|
|
|
|
it("rejects plain strings", () => {
|
|
expect(isExtendedJsonFormat("hello")).toBe(false);
|
|
expect(isExtendedJsonFormat("123")).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("parseExtendedJson", () => {
|
|
it("parses JSON with mixed extended values", () => {
|
|
const json = JSON.stringify({
|
|
value: "<bigint: 200000n>",
|
|
data: "<Uint8Array: 0x76a9>",
|
|
name: "test",
|
|
count: 42,
|
|
});
|
|
const result = parseExtendedJson(json);
|
|
expect(result.value).toBe(200000n);
|
|
expect(result.data).toBeInstanceOf(Uint8Array);
|
|
expect(binToHex(result.data)).toBe("76a9");
|
|
expect(result.name).toBe("test");
|
|
expect(result.count).toBe(42);
|
|
});
|
|
});
|
|
|
|
describe("toUint8Array", () => {
|
|
it("passes through Uint8Array", () => {
|
|
const bytes = new Uint8Array([1, 2, 3]);
|
|
expect(toUint8Array(bytes)).toBe(bytes);
|
|
});
|
|
|
|
it("converts hex string", () => {
|
|
const result = toUint8Array("abcd");
|
|
expect(binToHex(result)).toBe("abcd");
|
|
});
|
|
|
|
it("converts extended JSON format", () => {
|
|
const result = toUint8Array("<Uint8Array: 0xabcd>");
|
|
expect(binToHex(result)).toBe("abcd");
|
|
});
|
|
});
|
|
|
|
describe("toBigInt", () => {
|
|
it("passes through bigint", () => {
|
|
expect(toBigInt(546n)).toBe(546n);
|
|
});
|
|
|
|
it("converts number", () => {
|
|
expect(toBigInt(546)).toBe(546n);
|
|
});
|
|
|
|
it("converts numeric string", () => {
|
|
expect(toBigInt("546")).toBe(546n);
|
|
});
|
|
|
|
it("converts extended JSON format", () => {
|
|
expect(toBigInt("<bigint: 546n>")).toBe(546n);
|
|
});
|
|
});
|
|
});
|