xytara 3.1.5 → 3.1.6
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.
package/RELEASE_NOTES.md
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
`xytara` 3.1.
|
|
1
|
+
`xytara` 3.1.6 fixes the x402 facilitator request so strict facilitators like Coinbase CDP accept it.
|
|
2
2
|
|
|
3
|
-
The
|
|
3
|
+
The facilitator client was adding a `resource` field to the `paymentRequirements` it sends to `/verify` and `/settle`. The x402 v2 `PaymentRequirements` shape has no `resource` field (resource belongs on the payload), so strict facilitators such as the CDP-hosted facilitator rejected the request with HTTP 400, which blocked Base mainnet settlement through CDP. Lenient facilitators ignored the extra field, so the default Base Sepolia path was unaffected.
|
|
4
4
|
|
|
5
5
|
Highlights:
|
|
6
6
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
- adds `npm run verify:cdp-facilitator`, which checks the per-request token minting, claim shape, and signature against a mock facilitator, and documents the Base mainnet setup in `REAL_PAYMENT_SETUP.md`
|
|
7
|
+
- sends a clean x402 v2 `paymentRequirements` (`scheme`, `network`, `asset`, `amount`, `payTo`, `maxTimeoutSeconds`, `extra`) with no extra `resource` field, so it matches `paymentPayload.accepted` and passes strict facilitator validation
|
|
8
|
+
- this unblocks the CDP facilitator path added in the previous release, including Base mainnet
|
|
9
|
+
- adds a regression check that the facilitator receives `paymentRequirements` without a `resource` field and equal to the payment payload's `accepted`
|
|
10
|
+
- no change to payment semantics, settlement, or any route; lenient-facilitator and Base Sepolia behavior is unchanged
|
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
"source_ref": "spine.job.commitment",
|
|
7
7
|
"committed_ref": "transaction.job.commitment",
|
|
8
8
|
"precommit_pack_ref": "precommit.job.commitment.readiness",
|
|
9
|
-
"transaction_id": "
|
|
10
|
-
"receipt_id": "
|
|
11
|
-
"proof_ref": "
|
|
9
|
+
"transaction_id": "txn_2061435e6b7b",
|
|
10
|
+
"receipt_id": "rcpt_4451d9ab898f",
|
|
11
|
+
"proof_ref": "proof_rcpt_4451d9ab898f",
|
|
12
12
|
"payment_protocol": "x402",
|
|
13
13
|
"settlement_mode": "evm_payment",
|
|
14
14
|
"treasury_destination_id": "treasury_acct_demo",
|
package/lib/x402_facilitator.js
CHANGED
|
@@ -127,7 +127,13 @@ async function verifyX402Payment({ paymentHeader, accepts, resourceUrl, facilita
|
|
|
127
127
|
if (!paymentRequirements) {
|
|
128
128
|
return { ok: false, status: 402, reason: "payment_requirements_mismatch", detail: "accepted requirements do not match any advertised payment option" };
|
|
129
129
|
}
|
|
130
|
-
|
|
130
|
+
// The x402 v2 PaymentRequirements shape is exactly {scheme, network, asset, amount, payTo,
|
|
131
|
+
// maxTimeoutSeconds, extra} with no `resource` field (resource lives on the payload in v2).
|
|
132
|
+
// Adding one makes strict facilitators (e.g. Coinbase CDP) reject the request with HTTP 400
|
|
133
|
+
// and makes paymentRequirements diverge from paymentPayload.accepted, so send it clean.
|
|
134
|
+
// resourceUrl is accepted for API compatibility but intentionally not sent to the facilitator.
|
|
135
|
+
void resourceUrl;
|
|
136
|
+
const requirementsForFacilitator = { ...paymentRequirements };
|
|
131
137
|
const verification = await facilitator.verify(paymentPayload, requirementsForFacilitator);
|
|
132
138
|
if (!verification.ok || !verification.json) {
|
|
133
139
|
return { ok: false, status: 402, reason: "facilitator_unreachable", detail: verification.error || `facilitator verify returned HTTP ${verification.status}` };
|
package/package.json
CHANGED
|
@@ -1,194 +1,202 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
// Verifies the x402_facilitator payment mode end to end against a mock facilitator:
|
|
4
|
-
// - an unpaid x402 execute returns 402 and advertises the configured accepts
|
|
5
|
-
// - a well-formed payment is verified and settled, the command runs, and the
|
|
6
|
-
// response carries a PAYMENT-RESPONSE settlement receipt
|
|
7
|
-
// - malformed, mismatched, facilitator-rejected, and unsettled payments all 402
|
|
8
|
-
//
|
|
9
|
-
// No real network or chain is used; the mock facilitator stands in for x402.org.
|
|
10
|
-
|
|
11
|
-
const assert = require("assert");
|
|
12
|
-
const http = require("http");
|
|
13
|
-
|
|
14
|
-
const ACCEPTS = [
|
|
15
|
-
{
|
|
16
|
-
scheme: "exact",
|
|
17
|
-
network: "eip155:84532",
|
|
18
|
-
asset: "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
|
|
19
|
-
amount: "1000",
|
|
20
|
-
payTo: "0x7cd2E0Ed2721319B20FAac4109774d030833F84D",
|
|
21
|
-
extra: { name: "USDC", version: "2" },
|
|
22
|
-
maxTimeoutSeconds: 60
|
|
23
|
-
}
|
|
24
|
-
];
|
|
25
|
-
|
|
26
|
-
function startMockFacilitator() {
|
|
27
|
-
const calls = { verify: 0, settle: 0 };
|
|
28
|
-
const server = http.createServer((req, res) => {
|
|
29
|
-
const chunks = [];
|
|
30
|
-
req.on("data", (c) => chunks.push(c));
|
|
31
|
-
req.on("end", () => {
|
|
32
|
-
let body = {};
|
|
33
|
-
try { body = JSON.parse(Buffer.concat(chunks).toString("utf8") || "{}"); } catch (_) { body = {}; }
|
|
34
|
-
const sig = body && body.paymentPayload && body.paymentPayload.payload ? String(body.paymentPayload.payload.signature || "") : "";
|
|
35
|
-
const reply = (payload) => {
|
|
36
|
-
const text = JSON.stringify(payload);
|
|
37
|
-
res.writeHead(200, { "content-type": "application/json", "content-length": Buffer.byteLength(text) });
|
|
38
|
-
res.end(text);
|
|
39
|
-
};
|
|
40
|
-
if (req.method === "POST" && req.url === "/verify") {
|
|
41
|
-
calls.verify += 1;
|
|
42
|
-
|
|
43
|
-
return reply({ isValid:
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
return reply({ success:
|
|
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
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
process.env.
|
|
95
|
-
process.env.
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
assert.
|
|
111
|
-
assert.
|
|
112
|
-
assert.
|
|
113
|
-
assert.strictEqual(
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
assert.strictEqual(bogus.
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
assert.strictEqual(mismatch.
|
|
125
|
-
assert.strictEqual(
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
assert.strictEqual(rejected.
|
|
131
|
-
assert.strictEqual(rejected.json.
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
assert.strictEqual(settleFails.
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
const
|
|
141
|
-
|
|
142
|
-
assert.strictEqual(
|
|
143
|
-
assert.strictEqual(paid.json.transaction.
|
|
144
|
-
assert.strictEqual(paid.json.transaction.
|
|
145
|
-
assert.strictEqual(
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
assert.strictEqual(receipt.
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
//
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
//
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
assert.strictEqual(
|
|
172
|
-
|
|
173
|
-
assert.
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
assert.strictEqual(
|
|
179
|
-
assert.strictEqual(
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Verifies the x402_facilitator payment mode end to end against a mock facilitator:
|
|
4
|
+
// - an unpaid x402 execute returns 402 and advertises the configured accepts
|
|
5
|
+
// - a well-formed payment is verified and settled, the command runs, and the
|
|
6
|
+
// response carries a PAYMENT-RESPONSE settlement receipt
|
|
7
|
+
// - malformed, mismatched, facilitator-rejected, and unsettled payments all 402
|
|
8
|
+
//
|
|
9
|
+
// No real network or chain is used; the mock facilitator stands in for x402.org.
|
|
10
|
+
|
|
11
|
+
const assert = require("assert");
|
|
12
|
+
const http = require("http");
|
|
13
|
+
|
|
14
|
+
const ACCEPTS = [
|
|
15
|
+
{
|
|
16
|
+
scheme: "exact",
|
|
17
|
+
network: "eip155:84532",
|
|
18
|
+
asset: "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
|
|
19
|
+
amount: "1000",
|
|
20
|
+
payTo: "0x7cd2E0Ed2721319B20FAac4109774d030833F84D",
|
|
21
|
+
extra: { name: "USDC", version: "2" },
|
|
22
|
+
maxTimeoutSeconds: 60
|
|
23
|
+
}
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
function startMockFacilitator() {
|
|
27
|
+
const calls = { verify: 0, settle: 0 };
|
|
28
|
+
const server = http.createServer((req, res) => {
|
|
29
|
+
const chunks = [];
|
|
30
|
+
req.on("data", (c) => chunks.push(c));
|
|
31
|
+
req.on("end", () => {
|
|
32
|
+
let body = {};
|
|
33
|
+
try { body = JSON.parse(Buffer.concat(chunks).toString("utf8") || "{}"); } catch (_) { body = {}; }
|
|
34
|
+
const sig = body && body.paymentPayload && body.paymentPayload.payload ? String(body.paymentPayload.payload.signature || "") : "";
|
|
35
|
+
const reply = (payload) => {
|
|
36
|
+
const text = JSON.stringify(payload);
|
|
37
|
+
res.writeHead(200, { "content-type": "application/json", "content-length": Buffer.byteLength(text) });
|
|
38
|
+
res.end(text);
|
|
39
|
+
};
|
|
40
|
+
if (req.method === "POST" && req.url === "/verify") {
|
|
41
|
+
calls.verify += 1;
|
|
42
|
+
calls.lastVerifyBody = body;
|
|
43
|
+
if (sig === "reject-me") return reply({ isValid: false, invalidReason: "invalid_signature" });
|
|
44
|
+
return reply({ isValid: true, payer: "0x1111111111111111111111111111111111111111" });
|
|
45
|
+
}
|
|
46
|
+
if (req.method === "POST" && req.url === "/settle") {
|
|
47
|
+
calls.settle += 1;
|
|
48
|
+
if (sig === "settle-fails") return reply({ success: false, errorReason: "insufficient_funds" });
|
|
49
|
+
return reply({ success: true, transaction: "0xmocktx", network: "eip155:84532", payer: "0x1111111111111111111111111111111111111111" });
|
|
50
|
+
}
|
|
51
|
+
res.writeHead(404); res.end();
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
return new Promise((resolve) => server.listen(0, "127.0.0.1", () => resolve({ server, calls, port: server.address().port })));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function paymentHeader(signature, overrides = {}) {
|
|
58
|
+
const accepted = ACCEPTS[0];
|
|
59
|
+
return Buffer.from(JSON.stringify({
|
|
60
|
+
x402Version: 2,
|
|
61
|
+
accepted: { ...accepted, ...overrides.accepted },
|
|
62
|
+
payload: {
|
|
63
|
+
signature,
|
|
64
|
+
authorization: {
|
|
65
|
+
from: "0x1111111111111111111111111111111111111111",
|
|
66
|
+
to: accepted.payTo,
|
|
67
|
+
value: accepted.amount,
|
|
68
|
+
validAfter: "0",
|
|
69
|
+
validBefore: String(Math.floor(Date.now() / 1000) + 600),
|
|
70
|
+
nonce: `0x${"ab".repeat(32)}`
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}), "utf8").toString("base64");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function post(baseUrl, path, body, headers = {}) {
|
|
77
|
+
const res = await fetch(`${baseUrl}${path}`, {
|
|
78
|
+
method: "POST",
|
|
79
|
+
headers: { "content-type": "application/json", ...headers },
|
|
80
|
+
body: JSON.stringify(body)
|
|
81
|
+
});
|
|
82
|
+
let json = null;
|
|
83
|
+
try { json = await res.json(); } catch (_) { json = null; }
|
|
84
|
+
return { status: res.status, json, headers: res.headers };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function main() {
|
|
88
|
+
const facilitator = await startMockFacilitator();
|
|
89
|
+
const previous = {
|
|
90
|
+
mode: process.env.XYTARA_PAYMENT_VERIFICATION_MODE,
|
|
91
|
+
accepts: process.env.XYTARA_X402_ACCEPTS_JSON,
|
|
92
|
+
facUrl: process.env.XYTARA_X402_FACILITATOR_URL
|
|
93
|
+
};
|
|
94
|
+
process.env.XYTARA_PAYMENT_VERIFICATION_MODE = "x402_facilitator";
|
|
95
|
+
process.env.XYTARA_X402_ACCEPTS_JSON = JSON.stringify(ACCEPTS);
|
|
96
|
+
process.env.XYTARA_X402_FACILITATOR_URL = `http://127.0.0.1:${facilitator.port}`;
|
|
97
|
+
|
|
98
|
+
delete require.cache[require.resolve("../server")];
|
|
99
|
+
const { createServer } = require("../server");
|
|
100
|
+
const server = createServer();
|
|
101
|
+
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
102
|
+
const baseUrl = `http://127.0.0.1:${server.address().port}`;
|
|
103
|
+
const execBody = { account_id: "acct_x402", command: "x402 facilitator demo", task_ref: "trust.verify", body: { subject_id: "s-1" } };
|
|
104
|
+
|
|
105
|
+
try {
|
|
106
|
+
// 1. Unpaid -> 402 advertising accepts.
|
|
107
|
+
const unpaid = await post(baseUrl, "/x402/commands/execute", execBody);
|
|
108
|
+
assert.strictEqual(unpaid.status, 402, "unpaid x402 execute must return 402");
|
|
109
|
+
const required = unpaid.json && unpaid.json.payment_required;
|
|
110
|
+
assert.ok(required, "402 must carry payment_required");
|
|
111
|
+
assert.strictEqual(required.x402Version, 2, "challenge must be x402 v2");
|
|
112
|
+
assert.ok(Array.isArray(required.accepts) && required.accepts.length === 1, "challenge must advertise accepts");
|
|
113
|
+
assert.strictEqual(required.accepts[0].network, "eip155:84532", "advertised network mismatch");
|
|
114
|
+
assert.strictEqual(typeof unpaid.headers.get("payment-required"), "string", "PAYMENT-REQUIRED header missing");
|
|
115
|
+
|
|
116
|
+
// 2. Bogus header -> 402 payment_signature_invalid.
|
|
117
|
+
const bogus = await post(baseUrl, "/x402/commands/execute", execBody, { "payment-signature": "not-a-payment" });
|
|
118
|
+
assert.strictEqual(bogus.status, 402, "bogus payment header must 402");
|
|
119
|
+
assert.strictEqual(bogus.json.reason, "payment_signature_invalid", "bogus reason mismatch");
|
|
120
|
+
|
|
121
|
+
// 3. Mismatched requirements -> 402 before the facilitator.
|
|
122
|
+
const verifyBefore = facilitator.calls.verify;
|
|
123
|
+
const mismatch = await post(baseUrl, "/x402/commands/execute", execBody, { "payment-signature": paymentHeader("ok", { accepted: { amount: "9" } }) });
|
|
124
|
+
assert.strictEqual(mismatch.status, 402, "mismatched requirements must 402");
|
|
125
|
+
assert.strictEqual(mismatch.json.reason, "payment_requirements_mismatch", "mismatch reason");
|
|
126
|
+
assert.strictEqual(facilitator.calls.verify, verifyBefore, "facilitator must not be called on a requirements mismatch");
|
|
127
|
+
|
|
128
|
+
// 4. Facilitator rejects -> 402 payment_verification_failed.
|
|
129
|
+
const rejected = await post(baseUrl, "/x402/commands/execute", execBody, { "payment-signature": paymentHeader("reject-me") });
|
|
130
|
+
assert.strictEqual(rejected.status, 402, "facilitator-rejected payment must 402");
|
|
131
|
+
assert.strictEqual(rejected.json.reason, "payment_verification_failed", "rejection reason");
|
|
132
|
+
assert.strictEqual(rejected.json.detail, "invalid_signature", "rejection detail");
|
|
133
|
+
|
|
134
|
+
// 5. Settlement fails -> 402, no tool output.
|
|
135
|
+
const settleFails = await post(baseUrl, "/x402/commands/execute", execBody, { "payment-signature": paymentHeader("settle-fails") });
|
|
136
|
+
assert.strictEqual(settleFails.status, 402, "failed settlement must 402");
|
|
137
|
+
assert.strictEqual(settleFails.json.reason, "payment_settlement_failed", "settlement failure reason");
|
|
138
|
+
|
|
139
|
+
// 6. Verified + settled -> 200 with a result and a PAYMENT-RESPONSE receipt.
|
|
140
|
+
const settleBefore = facilitator.calls.settle;
|
|
141
|
+
const paid = await post(baseUrl, "/x402/commands/execute", execBody, { "payment-signature": paymentHeader("ok") });
|
|
142
|
+
assert.strictEqual(paid.status, 200, "verified+settled payment must 200");
|
|
143
|
+
assert.strictEqual(typeof paid.json.transaction.transaction_id, "string", "paid result missing transaction");
|
|
144
|
+
assert.strictEqual(paid.json.transaction.status, "completed", "paid transaction must be completed");
|
|
145
|
+
assert.strictEqual(paid.json.transaction.payment.verification_mode, "x402_facilitator", "verification mode not recorded");
|
|
146
|
+
assert.strictEqual(facilitator.calls.settle, settleBefore + 1, "facilitator settle must be called once");
|
|
147
|
+
const receiptHeader = paid.headers.get("payment-response");
|
|
148
|
+
assert.strictEqual(typeof receiptHeader, "string", "PAYMENT-RESPONSE header missing on a settled 200");
|
|
149
|
+
const receipt = JSON.parse(Buffer.from(receiptHeader, "base64").toString("utf8"));
|
|
150
|
+
assert.strictEqual(receipt.success, true, "settlement receipt must report success");
|
|
151
|
+
assert.strictEqual(receipt.transaction, "0xmocktx", "settlement receipt transaction mismatch");
|
|
152
|
+
|
|
153
|
+
// The facilitator must receive a clean v2 PaymentRequirements: no `resource` field (which
|
|
154
|
+
// strict facilitators like CDP reject), and it must equal the payment payload's `accepted`.
|
|
155
|
+
const verifyBody = facilitator.calls.lastVerifyBody;
|
|
156
|
+
assert.ok(verifyBody && verifyBody.paymentRequirements, "facilitator must receive paymentRequirements");
|
|
157
|
+
assert.strictEqual(verifyBody.paymentRequirements.resource, undefined, "paymentRequirements must not carry a resource field (v2)");
|
|
158
|
+
assert.deepStrictEqual(verifyBody.paymentRequirements, verifyBody.paymentPayload.accepted, "paymentRequirements must equal paymentPayload.accepted");
|
|
159
|
+
|
|
160
|
+
// 7. Native routes must not be disabled by x402_facilitator: a native paid attempt
|
|
161
|
+
// falls back to local_signed semantics, never payment_verification_mode_unsupported.
|
|
162
|
+
const nativeAttempt = await post(baseUrl, "/v1/commands/execute", execBody, {
|
|
163
|
+
"x-wallet-id": "wallet-not-registered",
|
|
164
|
+
"x-payment": "encoded",
|
|
165
|
+
"x-payment-signature": "sig"
|
|
166
|
+
});
|
|
167
|
+
assert.notStrictEqual(nativeAttempt.json && nativeAttempt.json.reason, "payment_verification_mode_unsupported", "native route must fall back to local_signed under x402_facilitator");
|
|
168
|
+
|
|
169
|
+
// 8. Self-serve credits top-up: unpaid -> 402 advertising accepts + credits_per_topup.
|
|
170
|
+
const topupUnpaid = await post(baseUrl, "/x402/credits/topup", { account_id: "acct_topup" });
|
|
171
|
+
assert.strictEqual(topupUnpaid.status, 402, "unpaid topup must 402");
|
|
172
|
+
assert.ok(Array.isArray(topupUnpaid.json.payment_required.accepts), "topup 402 must advertise accepts");
|
|
173
|
+
assert.strictEqual(typeof topupUnpaid.json.payment_required.credits_per_topup, "number", "topup 402 must state credits_per_topup");
|
|
174
|
+
|
|
175
|
+
// 9. Paid top-up -> 200, credits minted to the account, with a settlement receipt.
|
|
176
|
+
const topupPaid = await post(baseUrl, "/x402/credits/topup", { account_id: "acct_topup" }, { "payment-signature": paymentHeader("ok") });
|
|
177
|
+
assert.strictEqual(topupPaid.status, 200, "paid topup must 200");
|
|
178
|
+
assert.strictEqual(topupPaid.json.ok, true, "paid topup must be ok");
|
|
179
|
+
assert.strictEqual(typeof topupPaid.json.credit_grant, "object", "paid topup must return a credit grant");
|
|
180
|
+
const grantedUnits = Number(topupPaid.json.balance.available_units);
|
|
181
|
+
assert.ok(grantedUnits > 0, "paid topup must leave a positive credit balance");
|
|
182
|
+
assert.strictEqual(typeof topupPaid.headers.get("payment-response"), "string", "paid topup must carry a settlement receipt");
|
|
183
|
+
|
|
184
|
+
// 10. Replayed top-up (same payment nonce) is idempotent: no double-mint.
|
|
185
|
+
const topupReplay = await post(baseUrl, "/x402/credits/topup", { account_id: "acct_topup" }, { "payment-signature": paymentHeader("ok") });
|
|
186
|
+
assert.strictEqual(topupReplay.status, 200, "replayed topup must still 200");
|
|
187
|
+
assert.strictEqual(Number(topupReplay.json.balance.available_units), grantedUnits, "replayed topup must not mint credits twice");
|
|
188
|
+
|
|
189
|
+
console.log("xytara verify:x402-facilitator passed");
|
|
190
|
+
} finally {
|
|
191
|
+
await new Promise((resolve) => server.close(resolve));
|
|
192
|
+
await new Promise((resolve) => facilitator.server.close(resolve));
|
|
193
|
+
if (previous.mode === undefined) delete process.env.XYTARA_PAYMENT_VERIFICATION_MODE; else process.env.XYTARA_PAYMENT_VERIFICATION_MODE = previous.mode;
|
|
194
|
+
if (previous.accepts === undefined) delete process.env.XYTARA_X402_ACCEPTS_JSON; else process.env.XYTARA_X402_ACCEPTS_JSON = previous.accepts;
|
|
195
|
+
if (previous.facUrl === undefined) delete process.env.XYTARA_X402_FACILITATOR_URL; else process.env.XYTARA_X402_FACILITATOR_URL = previous.facUrl;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
main().catch((error) => {
|
|
200
|
+
console.error(error && error.message ? error.message : error);
|
|
201
|
+
process.exitCode = 1;
|
|
202
|
+
});
|