@dexterai/x402 3.3.0 → 3.4.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.
- package/README.md +66 -8
- package/dist/adapters/index.d.cts +2 -3
- package/dist/adapters/index.d.ts +2 -3
- package/dist/batch-settlement/index.cjs +1 -1
- package/dist/batch-settlement/index.d.cts +5 -81
- package/dist/batch-settlement/index.d.ts +5 -81
- package/dist/batch-settlement/index.js +1 -1
- package/dist/batch-settlement/seller/index.cjs +1 -0
- package/dist/batch-settlement/seller/index.d.cts +17 -0
- package/dist/batch-settlement/seller/index.d.ts +17 -0
- package/dist/batch-settlement/seller/index.js +1 -0
- package/dist/client/index.cjs +1 -1
- package/dist/client/index.d.cts +3 -5
- package/dist/client/index.d.ts +3 -5
- package/dist/client/index.js +1 -1
- package/dist/react/index.cjs +1 -1
- package/dist/react/index.d.cts +4 -5
- package/dist/react/index.d.ts +4 -5
- package/dist/react/index.js +1 -1
- package/dist/server/index.cjs +16 -16
- package/dist/server/index.d.cts +20 -30
- package/dist/server/index.d.ts +20 -30
- package/dist/server/index.js +16 -16
- package/dist/{sponsored-access-BEufXQ6c.d.cts → sponsored-access-DC2x5LQ-.d.cts} +12 -2
- package/dist/{sponsored-access-4wVjCYrH.d.ts → sponsored-access-DSgmtjhF.d.ts} +12 -2
- package/dist/types-BBhXpBwI.d.cts +60 -0
- package/dist/types-BIINnfB4.d.ts +142 -0
- package/dist/types-Bbt_SDZE.d.cts +142 -0
- package/dist/types-CwK4mPWV.d.ts +60 -0
- package/dist/{types-HFS_C6RX.d.ts → types-IqnDBsjL.d.cts} +226 -2
- package/dist/{types-B3kS1y_t.d.cts → types-IqnDBsjL.d.ts} +226 -2
- package/package.json +8 -2
- package/dist/types-Bcsi5jQb.d.cts +0 -227
- package/dist/types-Bcsi5jQb.d.ts +0 -227
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { ClientChannelStorage } from '@x402/evm/batch-settlement/client';
|
|
2
|
+
import { E as EvmWallet } from './types-IqnDBsjL.cjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Buyer withdrawal escape hatch for batch-settlement escrow channels.
|
|
6
|
+
*
|
|
7
|
+
* Normally a buyer's unspent escrow returns via the seller's
|
|
8
|
+
* `refundWithSignature`. If the seller goes dark and never settles, the buyer
|
|
9
|
+
* must be able to reclaim the unspent escrow directly on-chain. The
|
|
10
|
+
* `x402BatchSettlement` contract provides a two-step timed withdrawal:
|
|
11
|
+
*
|
|
12
|
+
* 1. `initiateWithdraw(config, amount)` — starts the `withdrawDelay` timer.
|
|
13
|
+
* 2. `finalizeWithdraw(config)` — after the delay, returns the funds.
|
|
14
|
+
*
|
|
15
|
+
* Both are buyer-submitted and cost the buyer gas. There is deliberately no
|
|
16
|
+
* facilitator-relayed variant: the escape cannot be sponsored by the party
|
|
17
|
+
* being escaped from.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** Thrown by finalizeWithdraw when called before the withdrawDelay elapses. */
|
|
21
|
+
declare class WithdrawNotReadyError extends Error {
|
|
22
|
+
constructor(message: string);
|
|
23
|
+
}
|
|
24
|
+
/** Result of forceWithdraw — the initiate tx and when finalize becomes possible. */
|
|
25
|
+
interface ForceWithdrawResult {
|
|
26
|
+
/** initiateWithdraw transaction hash. */
|
|
27
|
+
initiateTx: string;
|
|
28
|
+
/** Unix seconds at which finalizeWithdraw becomes callable. */
|
|
29
|
+
finalizableAt: number;
|
|
30
|
+
}
|
|
31
|
+
/** Result of finalizeWithdraw. */
|
|
32
|
+
interface FinalizeWithdrawResult {
|
|
33
|
+
/** finalizeWithdraw transaction hash. */
|
|
34
|
+
finalizeTx: string;
|
|
35
|
+
/** Amount returned to the buyer, USDC human units. */
|
|
36
|
+
withdrawnAmount: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Channel persistence. This is the upstream `ClientChannelStorage` interface
|
|
41
|
+
* (`get` / `set` / `delete`, keyed by lowercased channelId). The SDK ships
|
|
42
|
+
* file-backed and localStorage-backed implementations; a consumer may pass any
|
|
43
|
+
* `ClientChannelStorage` (e.g. one backed by their own database).
|
|
44
|
+
*/
|
|
45
|
+
type ChannelStore = ClientChannelStorage;
|
|
46
|
+
/** Channel accounting, all amounts USDC human units (e.g. "0.30"). */
|
|
47
|
+
interface ChannelState {
|
|
48
|
+
/** Total escrowed into the channel. */
|
|
49
|
+
deposited: string;
|
|
50
|
+
/** Cumulative amount spent via vouchers. */
|
|
51
|
+
spent: string;
|
|
52
|
+
/** deposited - spent. */
|
|
53
|
+
remaining: string;
|
|
54
|
+
}
|
|
55
|
+
/** Options for opening a fresh (or auto-resumed) channel. */
|
|
56
|
+
interface OpenBatchChannelOptions {
|
|
57
|
+
/** EVM wallet — any { address, signTypedData }. The same EvmWallet the exact scheme uses. */
|
|
58
|
+
wallet: EvmWallet;
|
|
59
|
+
/** CAIP-2 network: eip155:8453 (Base), eip155:42161 (Arbitrum), eip155:137 (Polygon). */
|
|
60
|
+
network: string;
|
|
61
|
+
/** Total escrow for the channel, USDC human units, e.g. "0.30". */
|
|
62
|
+
deposit: string;
|
|
63
|
+
/** Facilitator base URL. Default: https://x402.dexter.cash */
|
|
64
|
+
facilitatorUrl?: string;
|
|
65
|
+
/** RPC URL override. Default: per-network. */
|
|
66
|
+
rpcUrl?: string;
|
|
67
|
+
/** Channel persistence. Default: localStorage (browser) / file (Node). */
|
|
68
|
+
store?: ChannelStore;
|
|
69
|
+
}
|
|
70
|
+
/** Options for explicitly resuming an existing channel. */
|
|
71
|
+
interface ResumeBatchChannelOptions {
|
|
72
|
+
wallet: EvmWallet;
|
|
73
|
+
network: string;
|
|
74
|
+
facilitatorUrl?: string;
|
|
75
|
+
rpcUrl?: string;
|
|
76
|
+
store?: ChannelStore;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Result of the seller runtime's claim -> settle -> refund — the three
|
|
80
|
+
* settlement tx hashes and final amounts. Used by the SELLER module; the
|
|
81
|
+
* buyer's channel.close() returns {@link CloseResult} instead.
|
|
82
|
+
*/
|
|
83
|
+
interface CloseReceipt {
|
|
84
|
+
/** claimWithSignature tx hash. */
|
|
85
|
+
claimTx: string;
|
|
86
|
+
/** settle tx hash. */
|
|
87
|
+
settleTx: string;
|
|
88
|
+
/** refundWithSignature tx hash. */
|
|
89
|
+
refundTx: string;
|
|
90
|
+
/** Amount paid to the seller, USDC human units. */
|
|
91
|
+
settledAmount: string;
|
|
92
|
+
/** Unspent amount returned to the buyer, USDC human units. */
|
|
93
|
+
refundedAmount: string;
|
|
94
|
+
}
|
|
95
|
+
/** Result of the buyer's channel.close() — an intent signal, not a settlement. */
|
|
96
|
+
interface CloseResult {
|
|
97
|
+
/** Always true — the channel was marked done in the buyer's local store. */
|
|
98
|
+
closed: true;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* A live escrow channel. Returned by openBatchChannel / resumeBatchChannel.
|
|
102
|
+
* Hold this handle for the lifetime of a batching session.
|
|
103
|
+
*/
|
|
104
|
+
interface BatchSettlementChannel {
|
|
105
|
+
/** On-chain channel id (bytes32 hex). Empty until the first fetch resolves it. */
|
|
106
|
+
readonly channelId: string;
|
|
107
|
+
/** CAIP-2 network. */
|
|
108
|
+
readonly network: string;
|
|
109
|
+
/** Live channel accounting; updated after each fetch. */
|
|
110
|
+
readonly state: ChannelState;
|
|
111
|
+
/** Drop-in fetch. On a batch-settlement 402, signs a voucher and retries. */
|
|
112
|
+
fetch(input: string | URL | Request, init?: RequestInit): Promise<Response>;
|
|
113
|
+
/**
|
|
114
|
+
* Marks the channel done in the buyer's local store (an intent signal).
|
|
115
|
+
* The buyer does not settle — the seller's runtime claims accumulated
|
|
116
|
+
* vouchers and refunds the buyer's unspent escrow. Returns { closed: true }.
|
|
117
|
+
*/
|
|
118
|
+
close(): Promise<CloseResult>;
|
|
119
|
+
/**
|
|
120
|
+
* Escape hatch — initiates the contract's on-chain withdrawal of this
|
|
121
|
+
* channel's unspent escrow, starting the withdrawDelay timer. Use only if
|
|
122
|
+
* the seller never settles. COSTS THE BUYER GAS — the buyer's wallet must
|
|
123
|
+
* hold the chain's native token; there is no facilitator-relayed variant.
|
|
124
|
+
*/
|
|
125
|
+
forceWithdraw(): Promise<ForceWithdrawResult>;
|
|
126
|
+
/**
|
|
127
|
+
* Escape hatch — finalizes a withdrawal started by forceWithdraw, after the
|
|
128
|
+
* withdrawDelay has elapsed; returns the funds. Throws WithdrawNotReadyError
|
|
129
|
+
* if called too early. COSTS THE BUYER GAS.
|
|
130
|
+
*/
|
|
131
|
+
finalizeWithdraw(): Promise<FinalizeWithdrawResult>;
|
|
132
|
+
}
|
|
133
|
+
/** Thrown by openBatchChannel when the buyer wallet lacks USDC for the deposit. */
|
|
134
|
+
declare class InsufficientBalanceError extends Error {
|
|
135
|
+
constructor(message: string);
|
|
136
|
+
}
|
|
137
|
+
/** Thrown when a network has no deployed x402BatchSettlement contract. */
|
|
138
|
+
declare class UnsupportedNetworkError extends Error {
|
|
139
|
+
constructor(message: string);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export { type BatchSettlementChannel as B, type ChannelStore as C, type ForceWithdrawResult as F, InsufficientBalanceError as I, type OpenBatchChannelOptions as O, type ResumeBatchChannelOptions as R, UnsupportedNetworkError as U, WithdrawNotReadyError as W, type ChannelState as a, type CloseReceipt as b, type CloseResult as c, type FinalizeWithdrawResult as d };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { RequestHandler } from 'express';
|
|
2
|
+
import { ChannelStorage } from '@x402/evm/batch-settlement/server';
|
|
3
|
+
import { b as CloseReceipt } from './types-BIINnfB4.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Result of closing one channel from the seller side. Either a settlement
|
|
7
|
+
* receipt, or an error entry when that channel's claim/settle/refund failed.
|
|
8
|
+
*/
|
|
9
|
+
type SellerCloseResult = (CloseReceipt & {
|
|
10
|
+
channelId: string;
|
|
11
|
+
}) | {
|
|
12
|
+
channelId: string;
|
|
13
|
+
error: string;
|
|
14
|
+
};
|
|
15
|
+
/** Configuration for createBatchSettlementSeller. */
|
|
16
|
+
interface BatchSettlementSellerConfig {
|
|
17
|
+
/** Seller's payout address; also the channel receiver. */
|
|
18
|
+
payTo: string;
|
|
19
|
+
/** CAIP-2 network: eip155:8453 (Base), eip155:42161 (Arbitrum), eip155:137 (Polygon). */
|
|
20
|
+
network: string;
|
|
21
|
+
/** USDC charged per request, human units, e.g. "0.08". */
|
|
22
|
+
price: string;
|
|
23
|
+
/** Facilitator base URL. Default: https://x402.dexter.cash */
|
|
24
|
+
facilitatorUrl?: string;
|
|
25
|
+
/** The protected route, e.g. "GET /api/data". Default: "GET /". */
|
|
26
|
+
route?: string;
|
|
27
|
+
/**
|
|
28
|
+
* Persistent server-side channel storage. Default: file-backed at
|
|
29
|
+
* ~/.dexter-x402/seller-channels/. Pass RedisChannelStorage or a custom
|
|
30
|
+
* server-side ChannelStorage for multi-instance deployments.
|
|
31
|
+
*/
|
|
32
|
+
channelStore?: ChannelStorage;
|
|
33
|
+
/**
|
|
34
|
+
* Auto-settlement loop. Default true (loop on, default intervals). Pass an
|
|
35
|
+
* object to tune; pass false to disable (settle only via closeChannel/closeAll).
|
|
36
|
+
*/
|
|
37
|
+
autoSettle?: boolean | {
|
|
38
|
+
claimIntervalSecs?: number;
|
|
39
|
+
settleIntervalSecs?: number;
|
|
40
|
+
refundIntervalSecs?: number;
|
|
41
|
+
};
|
|
42
|
+
/** Verbose logging. */
|
|
43
|
+
verbose?: boolean;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The seller runtime. It is callable — usable directly as an Express
|
|
47
|
+
* RequestHandler — and exposes lifecycle + settlement methods.
|
|
48
|
+
*/
|
|
49
|
+
interface BatchSettlementSeller extends RequestHandler {
|
|
50
|
+
/** Returns the Express request handler (same handler the object itself is). */
|
|
51
|
+
middleware(): RequestHandler;
|
|
52
|
+
/** Claim -> settle -> refund one channel. */
|
|
53
|
+
closeChannel(channelId: string): Promise<CloseReceipt>;
|
|
54
|
+
/** Settle every channel currently in storage; one result per channel. */
|
|
55
|
+
closeAll(): Promise<SellerCloseResult[]>;
|
|
56
|
+
/** Halt the auto-settle loop and flush a final claimAndSettle. */
|
|
57
|
+
stop(): Promise<void>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type { BatchSettlementSeller as B, SellerCloseResult as S, BatchSettlementSellerConfig as a };
|
|
@@ -1,4 +1,228 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* x402 v2 SDK — Shared Types
|
|
3
|
+
*
|
|
4
|
+
* Chain-agnostic types for x402 v2 payments.
|
|
5
|
+
* Works with Solana, Base, and any future x402-compatible networks.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Context passed to a PayToProvider function.
|
|
10
|
+
* Contains request-scoped information for dynamic address resolution.
|
|
11
|
+
*/
|
|
12
|
+
interface PayToContext {
|
|
13
|
+
/** The PAYMENT-SIGNATURE header value (present on retry/verify, undefined on initial 402) */
|
|
14
|
+
paymentHeader?: string;
|
|
15
|
+
/** Amount in atomic units (e.g., '10000' for 0.01 USDC) */
|
|
16
|
+
amountAtomic?: string;
|
|
17
|
+
/** The resource URL being accessed */
|
|
18
|
+
resourceUrl?: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Optional defaults a PayToProvider can advertise for auto-configuration.
|
|
22
|
+
* Attached as `_x402Defaults` on the provider function.
|
|
23
|
+
*/
|
|
24
|
+
interface PayToProviderDefaults {
|
|
25
|
+
/** Default CAIP-2 network (e.g., 'eip155:8453' for Base) */
|
|
26
|
+
network?: string;
|
|
27
|
+
/** Default facilitator URL */
|
|
28
|
+
facilitatorUrl?: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A function that dynamically resolves a payment address.
|
|
32
|
+
* Used for providers like Stripe that generate per-request deposit addresses.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* import { stripePayTo } from '@dexterai/x402/server';
|
|
37
|
+
*
|
|
38
|
+
* const provider = stripePayTo(process.env.STRIPE_SECRET_KEY);
|
|
39
|
+
* const address = await provider({ amountAtomic: '10000' });
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
type PayToProvider = ((context: PayToContext) => Promise<string>) & {
|
|
43
|
+
/** Auto-configuration defaults (set by provider factories like stripePayTo) */
|
|
44
|
+
_x402Defaults?: PayToProviderDefaults;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Resource info included in payment requirements
|
|
48
|
+
*/
|
|
49
|
+
interface ResourceInfo {
|
|
50
|
+
/** Resource URL */
|
|
51
|
+
url: string;
|
|
52
|
+
/** Human-readable description */
|
|
53
|
+
description?: string;
|
|
54
|
+
/** MIME type of the resource */
|
|
55
|
+
mimeType?: string;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Extra fields in payment requirements
|
|
59
|
+
* Chain-specific fields may vary
|
|
60
|
+
*/
|
|
61
|
+
interface AcceptsExtra {
|
|
62
|
+
/** Facilitator address that pays tx fees (required for Solana) */
|
|
63
|
+
feePayer?: string;
|
|
64
|
+
/** Token decimals (optional - defaults to 6 for USDC) */
|
|
65
|
+
decimals?: number;
|
|
66
|
+
/** EIP-712: Token name (EVM only) */
|
|
67
|
+
name?: string;
|
|
68
|
+
/** EIP-712: Token version (EVM only) */
|
|
69
|
+
version?: string;
|
|
70
|
+
/**
|
|
71
|
+
* batch-settlement: on-chain authorizer that the escrow channel pays into.
|
|
72
|
+
* Provided by the facilitator's batch-settlement kind. EVM only.
|
|
73
|
+
*/
|
|
74
|
+
receiverAuthorizer?: string;
|
|
75
|
+
/** Additional chain-specific fields */
|
|
76
|
+
[key: string]: unknown;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* A single payment option in the accepts array
|
|
80
|
+
*/
|
|
81
|
+
interface PaymentAccept {
|
|
82
|
+
/** x402 version (1 or 2, defaults to 2 if not specified) */
|
|
83
|
+
x402Version?: 1 | 2;
|
|
84
|
+
/**
|
|
85
|
+
* Payment scheme: 'exact' for EIP-3009 chains, 'exact-approval' for
|
|
86
|
+
* approval-based chains like BSC, 'batch-settlement' for the EVM
|
|
87
|
+
* escrow-channel batching scheme (discrete API purchases, gas-amortized).
|
|
88
|
+
*/
|
|
89
|
+
scheme: 'exact' | 'exact-approval' | 'batch-settlement';
|
|
90
|
+
/** CAIP-2 network identifier (v1: 'solana', v2: 'solana:5eykt...') */
|
|
91
|
+
network: string;
|
|
92
|
+
/** Payment amount in atomic units (x402 v2 spec field) */
|
|
93
|
+
amount: string;
|
|
94
|
+
/** @deprecated v1 field — use `amount` instead. Kept for backwards compatibility with v1 data. */
|
|
95
|
+
maxAmountRequired?: string;
|
|
96
|
+
/** Token address */
|
|
97
|
+
asset: string;
|
|
98
|
+
/** Seller's address to receive payment */
|
|
99
|
+
payTo: string;
|
|
100
|
+
/** Maximum seconds until payment expires */
|
|
101
|
+
maxTimeoutSeconds: number;
|
|
102
|
+
/** Chain-specific extra data */
|
|
103
|
+
extra?: AcceptsExtra;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Full PaymentRequired structure (sent in PAYMENT-REQUIRED header)
|
|
107
|
+
*/
|
|
108
|
+
interface PaymentRequired {
|
|
109
|
+
/** x402 version (always 2) */
|
|
110
|
+
x402Version: 2;
|
|
111
|
+
/** Resource being accessed */
|
|
112
|
+
resource: ResourceInfo;
|
|
113
|
+
/** Available payment options */
|
|
114
|
+
accepts: PaymentAccept[];
|
|
115
|
+
/** Optional error message */
|
|
116
|
+
error?: string;
|
|
117
|
+
/** Protocol extensions */
|
|
118
|
+
extensions?: Record<string, unknown>;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Response from /verify endpoint
|
|
122
|
+
*/
|
|
123
|
+
interface VerifyResponse {
|
|
124
|
+
/** Whether the payment is valid */
|
|
125
|
+
isValid: boolean;
|
|
126
|
+
/** Reason for invalidity (if invalid) */
|
|
127
|
+
invalidReason?: string;
|
|
128
|
+
/** Payer address */
|
|
129
|
+
payer?: string;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Response from /settle endpoint
|
|
133
|
+
*/
|
|
134
|
+
interface SettleResponse {
|
|
135
|
+
/** Whether settlement succeeded */
|
|
136
|
+
success: boolean;
|
|
137
|
+
/** Transaction signature/hash */
|
|
138
|
+
transaction?: string;
|
|
139
|
+
/** Network the payment was made on */
|
|
140
|
+
network: string;
|
|
141
|
+
/** Error reason (if failed) */
|
|
142
|
+
errorReason?: string;
|
|
143
|
+
/** Error code (if failed) */
|
|
144
|
+
errorCode?: string;
|
|
145
|
+
/** Payer address */
|
|
146
|
+
payer?: string;
|
|
147
|
+
/** Protocol extensions returned by the facilitator (e.g., sponsored-access recommendations) */
|
|
148
|
+
extensions?: Record<string, unknown>;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* A single access pass tier offered by a seller
|
|
152
|
+
*/
|
|
153
|
+
interface AccessPassTier {
|
|
154
|
+
/** Tier ID (e.g., '1h', '24h') */
|
|
155
|
+
id: string;
|
|
156
|
+
/** Human-readable label (e.g., '1 hour') */
|
|
157
|
+
label: string;
|
|
158
|
+
/** Duration in seconds */
|
|
159
|
+
seconds: number;
|
|
160
|
+
/** Price in USD (e.g., '0.50') */
|
|
161
|
+
price: string;
|
|
162
|
+
/** Price in atomic units (e.g., '500000') */
|
|
163
|
+
priceAtomic: string;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Access pass info returned in X-ACCESS-PASS-TIERS header
|
|
167
|
+
*/
|
|
168
|
+
interface AccessPassInfo {
|
|
169
|
+
/** Available tiers (if tier-based pricing) */
|
|
170
|
+
tiers?: AccessPassTier[];
|
|
171
|
+
/** Rate per hour in USD (if custom duration pricing) */
|
|
172
|
+
ratePerHour?: string;
|
|
173
|
+
/** Pass issuer identifier */
|
|
174
|
+
issuer?: string;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* JWT claims inside an access pass token
|
|
178
|
+
*/
|
|
179
|
+
interface AccessPassClaims {
|
|
180
|
+
/** Subject — always 'x402-access-pass' */
|
|
181
|
+
sub: string;
|
|
182
|
+
/** Tier ID or 'custom' */
|
|
183
|
+
tier: string;
|
|
184
|
+
/** Duration in seconds */
|
|
185
|
+
duration: number;
|
|
186
|
+
/** Issued at (unix seconds) */
|
|
187
|
+
iat: number;
|
|
188
|
+
/** Expires at (unix seconds) */
|
|
189
|
+
exp: number;
|
|
190
|
+
/** Payer wallet address */
|
|
191
|
+
payer: string;
|
|
192
|
+
/** Network used for payment */
|
|
193
|
+
network: string;
|
|
194
|
+
/** Issuer identifier */
|
|
195
|
+
iss: string;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Client-side access pass configuration
|
|
199
|
+
*/
|
|
200
|
+
interface AccessPassClientConfig {
|
|
201
|
+
/** Enable access pass mode (default: true when this config is present) */
|
|
202
|
+
enabled?: boolean;
|
|
203
|
+
/** Preferred tier ID (e.g., '1h') — pick this tier if available */
|
|
204
|
+
preferTier?: string;
|
|
205
|
+
/** Preferred custom duration in seconds (e.g., 3600) */
|
|
206
|
+
preferDuration?: number;
|
|
207
|
+
/** Maximum amount willing to spend in USD (e.g., '2.00') */
|
|
208
|
+
maxSpend?: string;
|
|
209
|
+
/** Auto-renew expired passes (default: true) */
|
|
210
|
+
autoRenew?: boolean;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* SDK error codes
|
|
214
|
+
*/
|
|
215
|
+
type X402ErrorCode = 'missing_payment_required_header' | 'invalid_payment_required' | 'unsupported_network' | 'no_matching_payment_option' | 'missing_fee_payer' | 'missing_decimals' | 'missing_amount' | 'amount_exceeds_max' | 'insufficient_balance' | 'wallet_missing_sign_transaction' | 'wallet_not_connected' | 'wallet_disconnected' | 'user_rejected_signature' | 'transaction_build_failed' | 'payment_rejected' | 'rpc_timeout' | 'facilitator_timeout' | 'invalid_payment_signature' | 'facilitator_verify_failed' | 'facilitator_settle_failed' | 'facilitator_request_failed' | 'no_matching_requirement' | 'access_pass_expired' | 'access_pass_invalid' | 'access_pass_tier_not_found' | 'access_pass_exceeds_max_spend';
|
|
216
|
+
/**
|
|
217
|
+
* Custom error class for x402 operations
|
|
218
|
+
*/
|
|
219
|
+
declare class X402Error extends Error {
|
|
220
|
+
/** Error code for programmatic handling */
|
|
221
|
+
code: X402ErrorCode;
|
|
222
|
+
/** Additional error details */
|
|
223
|
+
details?: unknown;
|
|
224
|
+
constructor(code: X402ErrorCode, message: string, details?: unknown);
|
|
225
|
+
}
|
|
2
226
|
|
|
3
227
|
/**
|
|
4
228
|
* EVM Chain Adapter
|
|
@@ -281,4 +505,4 @@ interface BalanceInfo {
|
|
|
281
505
|
asset: string;
|
|
282
506
|
}
|
|
283
507
|
|
|
284
|
-
export { type
|
|
508
|
+
export { type AccessPassClientConfig as A, type BalanceInfo as B, type ChainAdapter as C, type EvmWallet as E, type GenericWallet as G, type PaymentAccept as P, type SolanaWallet as S, type VerifyResponse as V, type WalletSet as W, X402Error as X, createEvmAdapter as a, type AccessPassTier as b, createSolanaAdapter as c, type AccessPassInfo as d, type SettleResponse as e, type PayToProvider as f, type PaymentRequired as g, type PayToContext as h, type PayToProviderDefaults as i, type AccessPassClaims as j, SolanaAdapter as k, EvmAdapter as l, type AdapterConfig as m, type SignedTransaction as n, isSolanaWallet as o, isEvmWallet as p };
|
|
@@ -1,4 +1,228 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* x402 v2 SDK — Shared Types
|
|
3
|
+
*
|
|
4
|
+
* Chain-agnostic types for x402 v2 payments.
|
|
5
|
+
* Works with Solana, Base, and any future x402-compatible networks.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Context passed to a PayToProvider function.
|
|
10
|
+
* Contains request-scoped information for dynamic address resolution.
|
|
11
|
+
*/
|
|
12
|
+
interface PayToContext {
|
|
13
|
+
/** The PAYMENT-SIGNATURE header value (present on retry/verify, undefined on initial 402) */
|
|
14
|
+
paymentHeader?: string;
|
|
15
|
+
/** Amount in atomic units (e.g., '10000' for 0.01 USDC) */
|
|
16
|
+
amountAtomic?: string;
|
|
17
|
+
/** The resource URL being accessed */
|
|
18
|
+
resourceUrl?: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Optional defaults a PayToProvider can advertise for auto-configuration.
|
|
22
|
+
* Attached as `_x402Defaults` on the provider function.
|
|
23
|
+
*/
|
|
24
|
+
interface PayToProviderDefaults {
|
|
25
|
+
/** Default CAIP-2 network (e.g., 'eip155:8453' for Base) */
|
|
26
|
+
network?: string;
|
|
27
|
+
/** Default facilitator URL */
|
|
28
|
+
facilitatorUrl?: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A function that dynamically resolves a payment address.
|
|
32
|
+
* Used for providers like Stripe that generate per-request deposit addresses.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* import { stripePayTo } from '@dexterai/x402/server';
|
|
37
|
+
*
|
|
38
|
+
* const provider = stripePayTo(process.env.STRIPE_SECRET_KEY);
|
|
39
|
+
* const address = await provider({ amountAtomic: '10000' });
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
type PayToProvider = ((context: PayToContext) => Promise<string>) & {
|
|
43
|
+
/** Auto-configuration defaults (set by provider factories like stripePayTo) */
|
|
44
|
+
_x402Defaults?: PayToProviderDefaults;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Resource info included in payment requirements
|
|
48
|
+
*/
|
|
49
|
+
interface ResourceInfo {
|
|
50
|
+
/** Resource URL */
|
|
51
|
+
url: string;
|
|
52
|
+
/** Human-readable description */
|
|
53
|
+
description?: string;
|
|
54
|
+
/** MIME type of the resource */
|
|
55
|
+
mimeType?: string;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Extra fields in payment requirements
|
|
59
|
+
* Chain-specific fields may vary
|
|
60
|
+
*/
|
|
61
|
+
interface AcceptsExtra {
|
|
62
|
+
/** Facilitator address that pays tx fees (required for Solana) */
|
|
63
|
+
feePayer?: string;
|
|
64
|
+
/** Token decimals (optional - defaults to 6 for USDC) */
|
|
65
|
+
decimals?: number;
|
|
66
|
+
/** EIP-712: Token name (EVM only) */
|
|
67
|
+
name?: string;
|
|
68
|
+
/** EIP-712: Token version (EVM only) */
|
|
69
|
+
version?: string;
|
|
70
|
+
/**
|
|
71
|
+
* batch-settlement: on-chain authorizer that the escrow channel pays into.
|
|
72
|
+
* Provided by the facilitator's batch-settlement kind. EVM only.
|
|
73
|
+
*/
|
|
74
|
+
receiverAuthorizer?: string;
|
|
75
|
+
/** Additional chain-specific fields */
|
|
76
|
+
[key: string]: unknown;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* A single payment option in the accepts array
|
|
80
|
+
*/
|
|
81
|
+
interface PaymentAccept {
|
|
82
|
+
/** x402 version (1 or 2, defaults to 2 if not specified) */
|
|
83
|
+
x402Version?: 1 | 2;
|
|
84
|
+
/**
|
|
85
|
+
* Payment scheme: 'exact' for EIP-3009 chains, 'exact-approval' for
|
|
86
|
+
* approval-based chains like BSC, 'batch-settlement' for the EVM
|
|
87
|
+
* escrow-channel batching scheme (discrete API purchases, gas-amortized).
|
|
88
|
+
*/
|
|
89
|
+
scheme: 'exact' | 'exact-approval' | 'batch-settlement';
|
|
90
|
+
/** CAIP-2 network identifier (v1: 'solana', v2: 'solana:5eykt...') */
|
|
91
|
+
network: string;
|
|
92
|
+
/** Payment amount in atomic units (x402 v2 spec field) */
|
|
93
|
+
amount: string;
|
|
94
|
+
/** @deprecated v1 field — use `amount` instead. Kept for backwards compatibility with v1 data. */
|
|
95
|
+
maxAmountRequired?: string;
|
|
96
|
+
/** Token address */
|
|
97
|
+
asset: string;
|
|
98
|
+
/** Seller's address to receive payment */
|
|
99
|
+
payTo: string;
|
|
100
|
+
/** Maximum seconds until payment expires */
|
|
101
|
+
maxTimeoutSeconds: number;
|
|
102
|
+
/** Chain-specific extra data */
|
|
103
|
+
extra?: AcceptsExtra;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Full PaymentRequired structure (sent in PAYMENT-REQUIRED header)
|
|
107
|
+
*/
|
|
108
|
+
interface PaymentRequired {
|
|
109
|
+
/** x402 version (always 2) */
|
|
110
|
+
x402Version: 2;
|
|
111
|
+
/** Resource being accessed */
|
|
112
|
+
resource: ResourceInfo;
|
|
113
|
+
/** Available payment options */
|
|
114
|
+
accepts: PaymentAccept[];
|
|
115
|
+
/** Optional error message */
|
|
116
|
+
error?: string;
|
|
117
|
+
/** Protocol extensions */
|
|
118
|
+
extensions?: Record<string, unknown>;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Response from /verify endpoint
|
|
122
|
+
*/
|
|
123
|
+
interface VerifyResponse {
|
|
124
|
+
/** Whether the payment is valid */
|
|
125
|
+
isValid: boolean;
|
|
126
|
+
/** Reason for invalidity (if invalid) */
|
|
127
|
+
invalidReason?: string;
|
|
128
|
+
/** Payer address */
|
|
129
|
+
payer?: string;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Response from /settle endpoint
|
|
133
|
+
*/
|
|
134
|
+
interface SettleResponse {
|
|
135
|
+
/** Whether settlement succeeded */
|
|
136
|
+
success: boolean;
|
|
137
|
+
/** Transaction signature/hash */
|
|
138
|
+
transaction?: string;
|
|
139
|
+
/** Network the payment was made on */
|
|
140
|
+
network: string;
|
|
141
|
+
/** Error reason (if failed) */
|
|
142
|
+
errorReason?: string;
|
|
143
|
+
/** Error code (if failed) */
|
|
144
|
+
errorCode?: string;
|
|
145
|
+
/** Payer address */
|
|
146
|
+
payer?: string;
|
|
147
|
+
/** Protocol extensions returned by the facilitator (e.g., sponsored-access recommendations) */
|
|
148
|
+
extensions?: Record<string, unknown>;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* A single access pass tier offered by a seller
|
|
152
|
+
*/
|
|
153
|
+
interface AccessPassTier {
|
|
154
|
+
/** Tier ID (e.g., '1h', '24h') */
|
|
155
|
+
id: string;
|
|
156
|
+
/** Human-readable label (e.g., '1 hour') */
|
|
157
|
+
label: string;
|
|
158
|
+
/** Duration in seconds */
|
|
159
|
+
seconds: number;
|
|
160
|
+
/** Price in USD (e.g., '0.50') */
|
|
161
|
+
price: string;
|
|
162
|
+
/** Price in atomic units (e.g., '500000') */
|
|
163
|
+
priceAtomic: string;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Access pass info returned in X-ACCESS-PASS-TIERS header
|
|
167
|
+
*/
|
|
168
|
+
interface AccessPassInfo {
|
|
169
|
+
/** Available tiers (if tier-based pricing) */
|
|
170
|
+
tiers?: AccessPassTier[];
|
|
171
|
+
/** Rate per hour in USD (if custom duration pricing) */
|
|
172
|
+
ratePerHour?: string;
|
|
173
|
+
/** Pass issuer identifier */
|
|
174
|
+
issuer?: string;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* JWT claims inside an access pass token
|
|
178
|
+
*/
|
|
179
|
+
interface AccessPassClaims {
|
|
180
|
+
/** Subject — always 'x402-access-pass' */
|
|
181
|
+
sub: string;
|
|
182
|
+
/** Tier ID or 'custom' */
|
|
183
|
+
tier: string;
|
|
184
|
+
/** Duration in seconds */
|
|
185
|
+
duration: number;
|
|
186
|
+
/** Issued at (unix seconds) */
|
|
187
|
+
iat: number;
|
|
188
|
+
/** Expires at (unix seconds) */
|
|
189
|
+
exp: number;
|
|
190
|
+
/** Payer wallet address */
|
|
191
|
+
payer: string;
|
|
192
|
+
/** Network used for payment */
|
|
193
|
+
network: string;
|
|
194
|
+
/** Issuer identifier */
|
|
195
|
+
iss: string;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Client-side access pass configuration
|
|
199
|
+
*/
|
|
200
|
+
interface AccessPassClientConfig {
|
|
201
|
+
/** Enable access pass mode (default: true when this config is present) */
|
|
202
|
+
enabled?: boolean;
|
|
203
|
+
/** Preferred tier ID (e.g., '1h') — pick this tier if available */
|
|
204
|
+
preferTier?: string;
|
|
205
|
+
/** Preferred custom duration in seconds (e.g., 3600) */
|
|
206
|
+
preferDuration?: number;
|
|
207
|
+
/** Maximum amount willing to spend in USD (e.g., '2.00') */
|
|
208
|
+
maxSpend?: string;
|
|
209
|
+
/** Auto-renew expired passes (default: true) */
|
|
210
|
+
autoRenew?: boolean;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* SDK error codes
|
|
214
|
+
*/
|
|
215
|
+
type X402ErrorCode = 'missing_payment_required_header' | 'invalid_payment_required' | 'unsupported_network' | 'no_matching_payment_option' | 'missing_fee_payer' | 'missing_decimals' | 'missing_amount' | 'amount_exceeds_max' | 'insufficient_balance' | 'wallet_missing_sign_transaction' | 'wallet_not_connected' | 'wallet_disconnected' | 'user_rejected_signature' | 'transaction_build_failed' | 'payment_rejected' | 'rpc_timeout' | 'facilitator_timeout' | 'invalid_payment_signature' | 'facilitator_verify_failed' | 'facilitator_settle_failed' | 'facilitator_request_failed' | 'no_matching_requirement' | 'access_pass_expired' | 'access_pass_invalid' | 'access_pass_tier_not_found' | 'access_pass_exceeds_max_spend';
|
|
216
|
+
/**
|
|
217
|
+
* Custom error class for x402 operations
|
|
218
|
+
*/
|
|
219
|
+
declare class X402Error extends Error {
|
|
220
|
+
/** Error code for programmatic handling */
|
|
221
|
+
code: X402ErrorCode;
|
|
222
|
+
/** Additional error details */
|
|
223
|
+
details?: unknown;
|
|
224
|
+
constructor(code: X402ErrorCode, message: string, details?: unknown);
|
|
225
|
+
}
|
|
2
226
|
|
|
3
227
|
/**
|
|
4
228
|
* EVM Chain Adapter
|
|
@@ -281,4 +505,4 @@ interface BalanceInfo {
|
|
|
281
505
|
asset: string;
|
|
282
506
|
}
|
|
283
507
|
|
|
284
|
-
export { type
|
|
508
|
+
export { type AccessPassClientConfig as A, type BalanceInfo as B, type ChainAdapter as C, type EvmWallet as E, type GenericWallet as G, type PaymentAccept as P, type SolanaWallet as S, type VerifyResponse as V, type WalletSet as W, X402Error as X, createEvmAdapter as a, type AccessPassTier as b, createSolanaAdapter as c, type AccessPassInfo as d, type SettleResponse as e, type PayToProvider as f, type PaymentRequired as g, type PayToContext as h, type PayToProviderDefaults as i, type AccessPassClaims as j, SolanaAdapter as k, EvmAdapter as l, type AdapterConfig as m, type SignedTransaction as n, isSolanaWallet as o, isEvmWallet as p };
|