@faremeter/types 0.15.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/env pnpm tsx
2
+ import t from "tap";
3
+ import { isValidationError } from "./validation.js";
4
+ import { x402PaymentHeaderToPayload, } from "./x402v2.js";
5
+ const makeValidRequirements = () => ({
6
+ scheme: "exact",
7
+ network: "eip155:84532",
8
+ amount: "1000",
9
+ asset: "0xUSDC",
10
+ payTo: "0x1234567890abcdef",
11
+ maxTimeoutSeconds: 60,
12
+ });
13
+ const makeValidResource = () => ({
14
+ url: "https://example.com/api",
15
+ });
16
+ const makeValidPayload = () => ({
17
+ x402Version: 2,
18
+ resource: makeValidResource(),
19
+ accepted: makeValidRequirements(),
20
+ payload: { signature: "0xabc123" },
21
+ });
22
+ await t.test("x402PaymentHeaderToPayload", async (t) => {
23
+ await t.test("parses valid base64-encoded JSON payload", (t) => {
24
+ const payload = makeValidPayload();
25
+ const encoded = btoa(JSON.stringify(payload));
26
+ const result = x402PaymentHeaderToPayload(encoded);
27
+ if (isValidationError(result)) {
28
+ t.fail("expected valid payload");
29
+ t.end();
30
+ return;
31
+ }
32
+ t.equal(result.x402Version, 2);
33
+ t.equal(result.accepted.scheme, "exact");
34
+ t.equal(result.accepted.amount, "1000");
35
+ t.matchOnly(result.resource, payload.resource);
36
+ t.end();
37
+ });
38
+ await t.test("rejects invalid base64 string", (t) => {
39
+ const result = x402PaymentHeaderToPayload("!!!not-valid-base64!!!");
40
+ t.ok(isValidationError(result));
41
+ t.end();
42
+ });
43
+ await t.test("rejects valid base64 with invalid JSON", (t) => {
44
+ const encoded = btoa("this is not valid json {{{");
45
+ const result = x402PaymentHeaderToPayload(encoded);
46
+ t.ok(isValidationError(result));
47
+ t.end();
48
+ });
49
+ await t.test("rejects valid JSON with missing required fields", (t) => {
50
+ const incompletePayload = { foo: "bar" };
51
+ const encoded = btoa(JSON.stringify(incompletePayload));
52
+ const result = x402PaymentHeaderToPayload(encoded);
53
+ t.ok(isValidationError(result));
54
+ t.end();
55
+ });
56
+ await t.test("rejects payload with wrong x402Version", (t) => {
57
+ const payload = { ...makeValidPayload(), x402Version: 1 };
58
+ const encoded = btoa(JSON.stringify(payload));
59
+ const result = x402PaymentHeaderToPayload(encoded);
60
+ t.ok(isValidationError(result));
61
+ t.end();
62
+ });
63
+ });