@agentbadge/circle-payments 0.1.11 → 0.1.12
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/gateway-probe.d.ts +15 -0
- package/dist/gateway-probe.js +62 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/middleware.d.ts +6 -1
- package/dist/middleware.js +26 -3
- package/dist/router.d.ts +4 -3
- package/dist/router.js +8 -3
- package/package.json +1 -1
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface GatewayProbeOptions {
|
|
2
|
+
apiUrl: string;
|
|
3
|
+
probePath?: string;
|
|
4
|
+
intervalMs?: number;
|
|
5
|
+
downMs?: number;
|
|
6
|
+
fetchImpl?: typeof fetch;
|
|
7
|
+
onChange?: (up: boolean, reason: string) => void;
|
|
8
|
+
}
|
|
9
|
+
export interface GatewayProbe {
|
|
10
|
+
isUp(): boolean;
|
|
11
|
+
markFailure(): void;
|
|
12
|
+
start(): void;
|
|
13
|
+
stop(): void;
|
|
14
|
+
}
|
|
15
|
+
export declare function createGatewayProbe(opts: GatewayProbeOptions): GatewayProbe;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export function createGatewayProbe(opts) {
|
|
2
|
+
const intervalMs = opts.intervalMs ?? 30_000;
|
|
3
|
+
const downMs = opts.downMs ?? 30_000;
|
|
4
|
+
const probePath = opts.probePath ?? "/x402/transfers?pageSize=1";
|
|
5
|
+
const fetchImpl = opts.fetchImpl ?? fetch;
|
|
6
|
+
let downUntil = 0;
|
|
7
|
+
let downNotified = false;
|
|
8
|
+
let timer;
|
|
9
|
+
const url = `${opts.apiUrl.replace(/\/+$/, "")}${probePath}`;
|
|
10
|
+
function setDown(reason) {
|
|
11
|
+
downUntil = Date.now() + downMs;
|
|
12
|
+
if (!downNotified) {
|
|
13
|
+
downNotified = true;
|
|
14
|
+
opts.onChange?.(false, reason);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function setUp(reason) {
|
|
18
|
+
downUntil = 0;
|
|
19
|
+
if (downNotified) {
|
|
20
|
+
downNotified = false;
|
|
21
|
+
opts.onChange?.(true, reason);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
async function probe() {
|
|
25
|
+
try {
|
|
26
|
+
const res = await fetchImpl(url);
|
|
27
|
+
if (res.ok) {
|
|
28
|
+
setUp("probe ok");
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
setDown(`probe HTTP ${res.status}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
catch (err) {
|
|
35
|
+
setDown(`probe ${err instanceof Error ? err.message : String(err)}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
isUp() {
|
|
40
|
+
if (downUntil === 0)
|
|
41
|
+
return true;
|
|
42
|
+
return Date.now() >= downUntil;
|
|
43
|
+
},
|
|
44
|
+
markFailure() {
|
|
45
|
+
setDown("handle failure");
|
|
46
|
+
},
|
|
47
|
+
start() {
|
|
48
|
+
if (timer)
|
|
49
|
+
return;
|
|
50
|
+
timer = setInterval(() => {
|
|
51
|
+
void probe();
|
|
52
|
+
}, intervalMs);
|
|
53
|
+
timer.unref?.();
|
|
54
|
+
},
|
|
55
|
+
stop() {
|
|
56
|
+
if (timer) {
|
|
57
|
+
clearInterval(timer);
|
|
58
|
+
timer = undefined;
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export * from "./schemes/exact.js";
|
|
|
5
5
|
export * from "./schemes/arc-self-settle.js";
|
|
6
6
|
export * from "./router.js";
|
|
7
7
|
export * from "./middleware.js";
|
|
8
|
+
export * from "./gateway-probe.js";
|
|
8
9
|
export * from "./identity.js";
|
|
9
10
|
export * from "./ledger.js";
|
|
10
11
|
export * from "./pricing.js";
|
package/dist/index.js
CHANGED
|
@@ -5,6 +5,7 @@ export * from "./schemes/exact.js";
|
|
|
5
5
|
export * from "./schemes/arc-self-settle.js";
|
|
6
6
|
export * from "./router.js";
|
|
7
7
|
export * from "./middleware.js";
|
|
8
|
+
export * from "./gateway-probe.js";
|
|
8
9
|
export * from "./identity.js";
|
|
9
10
|
export * from "./ledger.js";
|
|
10
11
|
export * from "./pricing.js";
|
package/dist/middleware.d.ts
CHANGED
|
@@ -6,6 +6,10 @@ export interface RequirePaymentOptions extends PaymentRouterConfig {
|
|
|
6
6
|
router?: PaymentRouter;
|
|
7
7
|
description?: string;
|
|
8
8
|
mimeType?: string;
|
|
9
|
+
methods?: string[];
|
|
10
|
+
onBeforeChallenge?: (c: Context) => Promise<true | Response | void> | true | Response | void;
|
|
11
|
+
unpaidBody?: (c: Context) => Promise<unknown> | unknown;
|
|
12
|
+
resourceUrl?: (c: Context) => string;
|
|
9
13
|
extensions?: Record<string, unknown> | (() => Promise<Record<string, unknown> | undefined>);
|
|
10
14
|
failureStore?: FailureStore;
|
|
11
15
|
onFailure?: FailureAlert;
|
|
@@ -35,4 +39,5 @@ export interface PaymentPayload {
|
|
|
35
39
|
accepted?: PaymentRequirements;
|
|
36
40
|
payload?: Record<string, unknown>;
|
|
37
41
|
}
|
|
38
|
-
export
|
|
42
|
+
export type PriceResolver = string | ((c: Context) => string | Promise<string>);
|
|
43
|
+
export declare function requirePayment(price: PriceResolver, opts: RequirePaymentOptions): MiddlewareHandler;
|
package/dist/middleware.js
CHANGED
|
@@ -36,19 +36,42 @@ async function paymentRequired(c, accepts, opts, error) {
|
|
|
36
36
|
x402Version: 2,
|
|
37
37
|
...(error ? { error } : {}),
|
|
38
38
|
resource: {
|
|
39
|
-
url: c.req.url,
|
|
39
|
+
url: opts.resourceUrl?.(c) ?? c.req.url,
|
|
40
40
|
description: opts.description ?? "Paid resource",
|
|
41
41
|
mimeType: opts.mimeType ?? "application/json",
|
|
42
42
|
},
|
|
43
43
|
accepts,
|
|
44
44
|
...(extensions ? { extensions } : {}),
|
|
45
45
|
};
|
|
46
|
-
|
|
46
|
+
const responseBody = opts.unpaidBody
|
|
47
|
+
? await opts.unpaidBody(c)
|
|
48
|
+
: { error: error ?? "Payment required" };
|
|
49
|
+
return c.json(responseBody, 402, { "PAYMENT-REQUIRED": b64encode(body) });
|
|
47
50
|
}
|
|
48
51
|
export function requirePayment(price, opts) {
|
|
49
|
-
const
|
|
52
|
+
const staticAmount = typeof price === "string" ? priceToAmount(price) : undefined;
|
|
50
53
|
const router = opts.router ?? createPaymentRouter(opts);
|
|
54
|
+
const methods = opts.methods?.map((m) => m.toUpperCase());
|
|
51
55
|
return async (c, next) => {
|
|
56
|
+
if (methods && !methods.includes(c.req.method.toUpperCase())) {
|
|
57
|
+
return next();
|
|
58
|
+
}
|
|
59
|
+
if (opts.onBeforeChallenge) {
|
|
60
|
+
const bypass = await opts.onBeforeChallenge(c);
|
|
61
|
+
if (bypass === true)
|
|
62
|
+
return next();
|
|
63
|
+
if (bypass instanceof Response)
|
|
64
|
+
return bypass;
|
|
65
|
+
}
|
|
66
|
+
let amount = staticAmount;
|
|
67
|
+
if (amount === undefined) {
|
|
68
|
+
try {
|
|
69
|
+
amount = priceToAmount(await price(c));
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return c.json({ error: "Invalid price" }, 402);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
52
75
|
const accepts = router.acceptsFor(amount);
|
|
53
76
|
const signatureHeader = c.req.header("payment-signature");
|
|
54
77
|
if (!signatureHeader) {
|
package/dist/router.d.ts
CHANGED
|
@@ -3,10 +3,11 @@ export interface SchemeHandle {
|
|
|
3
3
|
verify(payload: unknown, requirements: PaymentRequirements): Promise<VerifyResult>;
|
|
4
4
|
settle(payload: unknown, requirements: PaymentRequirements): Promise<SettleResult>;
|
|
5
5
|
}
|
|
6
|
+
export type DynamicFlag = boolean | (() => boolean);
|
|
6
7
|
export interface RouterFlags {
|
|
7
|
-
gateway?:
|
|
8
|
-
exact?:
|
|
9
|
-
arc?:
|
|
8
|
+
gateway?: DynamicFlag;
|
|
9
|
+
exact?: DynamicFlag;
|
|
10
|
+
arc?: DynamicFlag;
|
|
10
11
|
}
|
|
11
12
|
export interface RouterConfig extends RouterFlags {
|
|
12
13
|
sellerAddress: string;
|
package/dist/router.js
CHANGED
|
@@ -2,11 +2,16 @@ import { ARC_TESTNET, BASE_SEPOLIA, } from "./chains.js";
|
|
|
2
2
|
import { buildGatewayRequirements } from "./schemes/gateway.js";
|
|
3
3
|
import { buildExactRequirements } from "./schemes/exact.js";
|
|
4
4
|
import { ARC_SELF_SETTLE_SCHEME, buildArcSelfSettleRequirements, } from "./schemes/arc-self-settle.js";
|
|
5
|
+
function flagOn(flag) {
|
|
6
|
+
if (flag === undefined)
|
|
7
|
+
return true;
|
|
8
|
+
return typeof flag === "function" ? flag() !== false : flag !== false;
|
|
9
|
+
}
|
|
5
10
|
export function buildAccepts(amount, config) {
|
|
6
11
|
const accepts = [];
|
|
7
|
-
const gatewayOn = config.gateway
|
|
8
|
-
const exactOn = config.exact
|
|
9
|
-
const arcOn = config.arc
|
|
12
|
+
const gatewayOn = flagOn(config.gateway);
|
|
13
|
+
const exactOn = flagOn(config.exact);
|
|
14
|
+
const arcOn = flagOn(config.arc);
|
|
10
15
|
if (gatewayOn) {
|
|
11
16
|
const chains = config.gatewayChains ?? [BASE_SEPOLIA, ARC_TESTNET];
|
|
12
17
|
for (const chain of chains) {
|
package/package.json
CHANGED