@agentspend/sdk 0.3.5 → 0.3.7
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/dist/index.js +10 -7
- package/package.json +3 -2
- package/src/index.ts +10 -7
package/dist/index.js
CHANGED
|
@@ -10,6 +10,7 @@ exports.createAgentSpend = createAgentSpend;
|
|
|
10
10
|
// x402 imports – server-side only (HTTP calls to facilitator, no crypto deps)
|
|
11
11
|
// ---------------------------------------------------------------------------
|
|
12
12
|
const server_1 = require("@x402/core/server");
|
|
13
|
+
const server_2 = require("@x402/evm/exact/server");
|
|
13
14
|
class AgentSpendChargeError extends Error {
|
|
14
15
|
statusCode;
|
|
15
16
|
details;
|
|
@@ -74,6 +75,7 @@ function createAgentSpend(options) {
|
|
|
74
75
|
const facilitatorUrl = options.crypto?.facilitatorUrl ?? "https://x402.org/facilitator";
|
|
75
76
|
facilitator = new server_1.HTTPFacilitatorClient({ url: facilitatorUrl });
|
|
76
77
|
resourceServer = new server_1.x402ResourceServer(facilitator);
|
|
78
|
+
(0, server_2.registerExactEvmScheme)(resourceServer);
|
|
77
79
|
}
|
|
78
80
|
// -------------------------------------------------------------------
|
|
79
81
|
// charge() — card-only, unchanged
|
|
@@ -141,8 +143,9 @@ function createAgentSpend(options) {
|
|
|
141
143
|
return c.json({ error: "Could not determine payment amount from request" }, 400);
|
|
142
144
|
}
|
|
143
145
|
const currency = opts.currency ?? "usd";
|
|
144
|
-
// Step 3: Check for
|
|
145
|
-
|
|
146
|
+
// Step 3: Check for payment header → crypto payment
|
|
147
|
+
// x402 v2 uses "Payment-Signature", v1 uses "X-Payment"
|
|
148
|
+
const paymentHeader = c.req.header("payment-signature") ?? c.req.header("x-payment");
|
|
146
149
|
if (paymentHeader) {
|
|
147
150
|
return handleCryptoPayment(c, next, paymentHeader, effectiveAmount, currency, body, opts);
|
|
148
151
|
}
|
|
@@ -205,7 +208,7 @@ function createAgentSpend(options) {
|
|
|
205
208
|
// handleCryptoPayment — x402 verify + settle via facilitator
|
|
206
209
|
// -------------------------------------------------------------------
|
|
207
210
|
async function handleCryptoPayment(c, next, paymentHeader, amountCents, currency, _body, _opts) {
|
|
208
|
-
if (!
|
|
211
|
+
if (!resourceServer) {
|
|
209
212
|
return c.json({ error: "Crypto payments not configured" }, 500);
|
|
210
213
|
}
|
|
211
214
|
try {
|
|
@@ -236,13 +239,13 @@ function createAgentSpend(options) {
|
|
|
236
239
|
maxTimeoutSeconds: 300,
|
|
237
240
|
extra: { name: "USD Coin", version: "2" }
|
|
238
241
|
};
|
|
239
|
-
// Verify payment via facilitator
|
|
240
|
-
const verifyResult = await
|
|
242
|
+
// Verify payment via resource server (delegates to facilitator)
|
|
243
|
+
const verifyResult = await resourceServer.verifyPayment(paymentPayload, paymentRequirements);
|
|
241
244
|
if (!verifyResult.isValid) {
|
|
242
245
|
return c.json({ error: "Payment verification failed", details: verifyResult.invalidReason }, 402);
|
|
243
246
|
}
|
|
244
|
-
// Settle payment via facilitator
|
|
245
|
-
const settleResult = await
|
|
247
|
+
// Settle payment via resource server (delegates to facilitator)
|
|
248
|
+
const settleResult = await resourceServer.settlePayment(paymentPayload, paymentRequirements);
|
|
246
249
|
if (!settleResult.success) {
|
|
247
250
|
return c.json({ error: "Payment settlement failed", details: settleResult.errorReason }, 402);
|
|
248
251
|
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentspend/sdk",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.7",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@x402/core": "^2.3.1"
|
|
10
|
+
"@x402/core": "^2.3.1",
|
|
11
|
+
"@x402/evm": "^2.3.1"
|
|
11
12
|
},
|
|
12
13
|
"scripts": {
|
|
13
14
|
"build": "tsc",
|
package/src/index.ts
CHANGED
|
@@ -43,6 +43,7 @@ export interface PaywallPaymentContext {
|
|
|
43
43
|
// x402 imports – server-side only (HTTP calls to facilitator, no crypto deps)
|
|
44
44
|
// ---------------------------------------------------------------------------
|
|
45
45
|
import { HTTPFacilitatorClient, x402ResourceServer } from "@x402/core/server";
|
|
46
|
+
import { registerExactEvmScheme } from "@x402/evm/exact/server";
|
|
46
47
|
import type {
|
|
47
48
|
PaymentRequirements,
|
|
48
49
|
PaymentPayload,
|
|
@@ -204,6 +205,7 @@ export function createAgentSpend(options: AgentSpendOptions): AgentSpend {
|
|
|
204
205
|
options.crypto?.facilitatorUrl ?? "https://x402.org/facilitator";
|
|
205
206
|
facilitator = new HTTPFacilitatorClient({ url: facilitatorUrl });
|
|
206
207
|
resourceServer = new x402ResourceServer(facilitator);
|
|
208
|
+
registerExactEvmScheme(resourceServer);
|
|
207
209
|
}
|
|
208
210
|
|
|
209
211
|
// -------------------------------------------------------------------
|
|
@@ -293,8 +295,9 @@ export function createAgentSpend(options: AgentSpendOptions): AgentSpend {
|
|
|
293
295
|
|
|
294
296
|
const currency = opts.currency ?? "usd";
|
|
295
297
|
|
|
296
|
-
// Step 3: Check for
|
|
297
|
-
|
|
298
|
+
// Step 3: Check for payment header → crypto payment
|
|
299
|
+
// x402 v2 uses "Payment-Signature", v1 uses "X-Payment"
|
|
300
|
+
const paymentHeader = c.req.header("payment-signature") ?? c.req.header("x-payment");
|
|
298
301
|
if (paymentHeader) {
|
|
299
302
|
return handleCryptoPayment(c, next, paymentHeader, effectiveAmount, currency, body, opts);
|
|
300
303
|
}
|
|
@@ -384,7 +387,7 @@ export function createAgentSpend(options: AgentSpendOptions): AgentSpend {
|
|
|
384
387
|
_body: unknown,
|
|
385
388
|
_opts: PaywallOptions
|
|
386
389
|
): Promise<Response | void> {
|
|
387
|
-
if (!
|
|
390
|
+
if (!resourceServer) {
|
|
388
391
|
return c.json({ error: "Crypto payments not configured" }, 500);
|
|
389
392
|
}
|
|
390
393
|
|
|
@@ -420,8 +423,8 @@ export function createAgentSpend(options: AgentSpendOptions): AgentSpend {
|
|
|
420
423
|
extra: { name: "USD Coin", version: "2" }
|
|
421
424
|
};
|
|
422
425
|
|
|
423
|
-
// Verify payment via facilitator
|
|
424
|
-
const verifyResult: VerifyResponse = await
|
|
426
|
+
// Verify payment via resource server (delegates to facilitator)
|
|
427
|
+
const verifyResult: VerifyResponse = await resourceServer!.verifyPayment(
|
|
425
428
|
paymentPayload,
|
|
426
429
|
paymentRequirements
|
|
427
430
|
);
|
|
@@ -433,8 +436,8 @@ export function createAgentSpend(options: AgentSpendOptions): AgentSpend {
|
|
|
433
436
|
);
|
|
434
437
|
}
|
|
435
438
|
|
|
436
|
-
// Settle payment via facilitator
|
|
437
|
-
const settleResult: SettleResponse = await
|
|
439
|
+
// Settle payment via resource server (delegates to facilitator)
|
|
440
|
+
const settleResult: SettleResponse = await resourceServer!.settlePayment(
|
|
438
441
|
paymentPayload,
|
|
439
442
|
paymentRequirements
|
|
440
443
|
);
|