-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircle-gateway-route.test.mjs
More file actions
160 lines (154 loc) · 5.99 KB
/
Copy pathcircle-gateway-route.test.mjs
File metadata and controls
160 lines (154 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import test from "node:test";
import assert from "node:assert/strict";
import {
CIRCLE_GATEWAY_FACILITATOR,
CIRCLE_GATEWAY_NAME,
CIRCLE_GATEWAY_PATH,
buildCircleGatewayRoute,
} from "./circle-gateway-route.mjs";
const SELLER = "0x8904dF3DE6DFEe6a7C8cc38619d2f17806213Cee";
const RESOURCE_METADATA = {
serviceName: "SameDayDesk",
tags: ["x402", "payment-preflight"],
iconUrl: "https://samedaydesk.com/favicon.svg",
};
test("builds a Base representative contract and official middleware", () => {
let config;
let requiredPrice;
const integration = buildCircleGatewayRoute({
sellerAddress: SELLER,
resourceMetadata: RESOURCE_METADATA,
middlewareFactory(input) {
config = input;
return { require(price) { requiredPrice = price; return () => {}; } };
},
});
assert.equal(integration.enabled, true);
assert.equal(integration.resource.urlPath, CIRCLE_GATEWAY_PATH);
assert.equal(integration.resource.amount, "5000");
assert.equal(integration.resource.accepts[0].network, "eip155:8453");
assert.equal(integration.resource.accepts[0].amount, "5000");
assert.equal(integration.resource.accepts[0].payTo, SELLER);
assert.equal(integration.resource.accepts[0].extra.name, CIRCLE_GATEWAY_NAME);
assert.equal(integration.resource.accepts[0].extra.verifyingContract.toLowerCase(), "0x77777777dcc4d5a8b6e418fd04d8997ef11000ee");
assert.equal(integration.resource.accepts[0].maxTimeoutSeconds, 604900);
assert.equal(integration.resource.serviceName, RESOURCE_METADATA.serviceName);
assert.deepEqual(integration.resource.tags, RESOURCE_METADATA.tags);
assert.equal(integration.resource.iconUrl, RESOURCE_METADATA.iconUrl);
assert.equal(config.facilitatorUrl, CIRCLE_GATEWAY_FACILITATOR);
assert.equal(config.sellerAddress, SELLER);
assert.deepEqual(config.networks, ["eip155:8453"]);
assert.equal(requiredPrice, "$0.005");
});
test("projects exact service metadata into the direct payment-required resource", () => {
let challengeHeader;
const integration = buildCircleGatewayRoute({
sellerAddress: SELLER,
resourceMetadata: RESOURCE_METADATA,
middlewareFactory() {
return {
require() {
return (_req, res) => res.setHeader("PAYMENT-REQUIRED", Buffer.from(JSON.stringify({
x402Version: 2,
resource: { url: CIRCLE_GATEWAY_PATH, description: "fixture", mimeType: "application/json" },
accepts: [{ scheme: "exact", network: "eip155:8453" }],
})).toString("base64"));
},
};
},
});
integration.middleware({}, {
setHeader(name, value) {
if (String(name).toLowerCase() === "payment-required") challengeHeader = value;
},
}, () => {});
const challenge = JSON.parse(Buffer.from(challengeHeader, "base64").toString("utf8"));
assert.deepEqual(challenge.resource, {
url: CIRCLE_GATEWAY_PATH,
description: "fixture",
mimeType: "application/json",
...RESOURCE_METADATA,
});
});
test("projects AgentCash-readable bazaar schemas into the payment-required payload", () => {
let challengeHeader;
const bazaarExtensions = {
bazaar: {
info: {
input: { type: "http", method: "GET", queryParams: { url: "https://example.com" } },
output: { type: "json", example: { ok: true } },
},
schema: {
type: "object",
properties: {
input: {
type: "object",
properties: {
queryParams: {
type: "object",
properties: { url: { type: "string" } },
required: ["url"],
},
},
},
output: {
type: "object",
properties: {
example: {
type: "object",
properties: { ok: { type: "boolean" } },
required: ["ok"],
},
},
},
},
required: ["input", "output"],
},
},
};
const integration = buildCircleGatewayRoute({
sellerAddress: SELLER,
resourceMetadata: RESOURCE_METADATA,
extensions: bazaarExtensions,
middlewareFactory() {
return {
require() {
return (_req, res) => res.setHeader("PAYMENT-REQUIRED", Buffer.from(JSON.stringify({
x402Version: 2,
resource: { url: CIRCLE_GATEWAY_PATH, description: "fixture", mimeType: "application/json" },
accepts: [{ scheme: "exact", network: "eip155:8453", asset: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913", amount: "5000", payTo: SELLER }],
})).toString("base64"));
},
};
},
});
integration.middleware({}, {
setHeader(name, value) {
if (String(name).toLowerCase() === "payment-required") challengeHeader = value;
},
}, () => {});
const challenge = JSON.parse(Buffer.from(challengeHeader, "base64").toString("utf8"));
assert.deepEqual(challenge.extensions.bazaar.schema.properties.input.properties.queryParams.required, ["url"]);
assert.equal(challenge.extensions.bazaar.schema.properties.output.properties.example.properties.ok.type, "boolean");
});
test("kill switch preserves discovery metadata without constructing middleware", () => {
const integration = buildCircleGatewayRoute({
sellerAddress: SELLER,
enabled: false,
middlewareFactory() {
throw new Error("must not run");
},
});
assert.equal(integration.enabled, false);
assert.equal(integration.middleware, null);
assert.equal(integration.resource.amount, "5000");
});
test("fails closed on invalid seller, price, or middleware", () => {
assert.throws(() => buildCircleGatewayRoute({ sellerAddress: "0x123" }), /sellerAddress/);
assert.throws(() => buildCircleGatewayRoute({ sellerAddress: SELLER, price: "$0.0000001" }), /six decimals/);
assert.throws(() => buildCircleGatewayRoute({ sellerAddress: SELLER, middlewareFactory: () => ({}) }), /invalid gateway/);
assert.throws(() => buildCircleGatewayRoute({
sellerAddress: SELLER,
resourceMetadata: { ...RESOURCE_METADATA, iconUrl: "javascript:alert(1)" },
}), /iconUrl is invalid/);
});