@three-ws/x402-payment-modal 1.1.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/CHANGELOG.md +57 -0
- package/LICENSE +180 -0
- package/README.md +347 -0
- package/TUTORIAL.md +188 -0
- package/dist/index.d.ts +141 -0
- package/dist/x402.js +1777 -0
- package/dist/x402.min.js +375 -0
- package/docs/api-reference.md +227 -0
- package/docs/architecture.md +171 -0
- package/docs/server-setup.md +239 -0
- package/docs/siwx.md +116 -0
- package/docs/spending-caps.md +92 -0
- package/docs/theming.md +124 -0
- package/examples/README.md +22 -0
- package/examples/plain-html/index.html +229 -0
- package/examples/react/README.md +69 -0
- package/examples/react/X402Button.jsx +84 -0
- package/examples/server-express/package.json +16 -0
- package/examples/server-express/public/index.html +89 -0
- package/examples/server-express/server.js +89 -0
- package/package.json +113 -0
- package/server/README.md +68 -0
- package/server/checkout.js +392 -0
- package/server/express.js +44 -0
- package/server/vercel.js +54 -0
- package/src/index.js +1776 -0
- package/types/index.d.ts +141 -0
- package/types/server.d.ts +109 -0
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// Type definitions for @three-ws/x402-payment-modal
|
|
2
|
+
|
|
3
|
+
/** Client-side spending caps, enforced in localStorage before each payment. */
|
|
4
|
+
export interface SpendingCaps {
|
|
5
|
+
/** Max atomic micro-USD per single call. */
|
|
6
|
+
maxPerCall?: string | number;
|
|
7
|
+
/** Max atomic micro-USD per rolling UTC hour. */
|
|
8
|
+
maxPerHour?: string | number;
|
|
9
|
+
/** Max atomic micro-USD per rolling UTC day. */
|
|
10
|
+
maxPerDay?: string | number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Options for {@link pay}. */
|
|
14
|
+
export interface PayOptions {
|
|
15
|
+
/** URL of the paid (x402) endpoint. Required. */
|
|
16
|
+
endpoint: string;
|
|
17
|
+
/** HTTP method to call the endpoint with. Defaults to GET, or POST if `body` is set. */
|
|
18
|
+
method?: string;
|
|
19
|
+
/** Request body. Objects are JSON-stringified; strings are sent as-is. */
|
|
20
|
+
body?: unknown;
|
|
21
|
+
/** Extra request headers merged into the paid call. */
|
|
22
|
+
headers?: Record<string, string>;
|
|
23
|
+
/** Merchant name shown in the modal header. */
|
|
24
|
+
merchant?: string;
|
|
25
|
+
/** Action label shown in the modal header. */
|
|
26
|
+
action?: string;
|
|
27
|
+
/** Skip the wallet picker and open the wallet directly when exactly one supported wallet is detected. */
|
|
28
|
+
autoConnect?: boolean;
|
|
29
|
+
/** Client-side spending caps (stablecoin assets only in the browser). */
|
|
30
|
+
caps?: SpendingCaps;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Settlement details returned by the facilitator on a paid call. */
|
|
34
|
+
export interface PaymentReceipt {
|
|
35
|
+
network?: string;
|
|
36
|
+
transaction?: string;
|
|
37
|
+
payer?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** SIWX re-entry details when the wallet signed in instead of paying. */
|
|
41
|
+
export interface SiwxReceipt {
|
|
42
|
+
address: string;
|
|
43
|
+
network: string | number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Resolved value of {@link pay}. */
|
|
47
|
+
export interface PayResult {
|
|
48
|
+
ok: true;
|
|
49
|
+
/** Parsed JSON (or text) the paid endpoint returned. */
|
|
50
|
+
result: unknown;
|
|
51
|
+
/** Present on a paid call. */
|
|
52
|
+
payment?: PaymentReceipt;
|
|
53
|
+
/** Present when re-entry happened via SIWX instead of a payment. */
|
|
54
|
+
siwx?: SiwxReceipt;
|
|
55
|
+
response: {
|
|
56
|
+
status: number;
|
|
57
|
+
headers: Record<string, string>;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Branding shown in the modal footer. */
|
|
62
|
+
export interface BrandConfig {
|
|
63
|
+
name?: string;
|
|
64
|
+
url?: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** ERC-8021 builder-code self-attribution echoed when the 402 challenge declares one. */
|
|
68
|
+
export interface BuilderCodeConfig {
|
|
69
|
+
wallet?: string;
|
|
70
|
+
service?: string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** CDN URLs for the crypto helpers loaded on demand. */
|
|
74
|
+
export interface EsmConfig {
|
|
75
|
+
solanaWeb3?: string;
|
|
76
|
+
nobleHashesSha3?: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Host configuration. See {@link configure}. */
|
|
80
|
+
export interface X402Config {
|
|
81
|
+
checkoutOrigin?: string | null;
|
|
82
|
+
checkoutPath?: string;
|
|
83
|
+
brand?: BrandConfig;
|
|
84
|
+
footerNote?: string;
|
|
85
|
+
builderCode?: BuilderCodeConfig;
|
|
86
|
+
esm?: EsmConfig;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Override host configuration (Solana checkout origin, branding, builder-code,
|
|
91
|
+
* esm.sh CDN URLs). Shallow-merges nested objects. Call before the first `pay()`.
|
|
92
|
+
* Returns the resolved config.
|
|
93
|
+
*/
|
|
94
|
+
export function configure(opts?: X402Config): Required<X402Config>;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Open the payment modal for an x402 endpoint and resolve when the user completes
|
|
98
|
+
* (or rejects with `{ code: 'cancelled' }` if dismissed).
|
|
99
|
+
*/
|
|
100
|
+
export function pay(opts: PayOptions): Promise<PayResult>;
|
|
101
|
+
|
|
102
|
+
/** Bind all `[data-x402-endpoint]` elements on the page (called automatically on load). */
|
|
103
|
+
export function init(): void;
|
|
104
|
+
|
|
105
|
+
/** Library version. */
|
|
106
|
+
export const version: string;
|
|
107
|
+
|
|
108
|
+
/** Solana USDC mint (mainnet). */
|
|
109
|
+
export const USDC_MINT_SOLANA: string;
|
|
110
|
+
/** $THREE — the three.ws utility token mint. Recognized by the modal so a 402
|
|
111
|
+
* `accept` using it renders as THREE without merchant-supplied metadata. */
|
|
112
|
+
export const THREE_MINT: string;
|
|
113
|
+
|
|
114
|
+
export interface KnownSolanaToken {
|
|
115
|
+
symbol: string;
|
|
116
|
+
name: string;
|
|
117
|
+
decimals: number;
|
|
118
|
+
stable?: boolean;
|
|
119
|
+
accent?: string;
|
|
120
|
+
glyph?: string;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Well-known Solana settlement assets, keyed by mint address. */
|
|
124
|
+
export const KNOWN_SOLANA_TOKENS: Readonly<Record<string, KnownSolanaToken>>;
|
|
125
|
+
|
|
126
|
+
/** Global exposed for non-module / inline-script usage. */
|
|
127
|
+
declare global {
|
|
128
|
+
interface Window {
|
|
129
|
+
X402?: {
|
|
130
|
+
pay: typeof pay;
|
|
131
|
+
init: typeof init;
|
|
132
|
+
configure: typeof configure;
|
|
133
|
+
version: string;
|
|
134
|
+
tokens: {
|
|
135
|
+
USDC_MINT_SOLANA: string;
|
|
136
|
+
THREE_MINT: string;
|
|
137
|
+
KNOWN_SOLANA_TOKENS: Readonly<Record<string, KnownSolanaToken>>;
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// Type definitions for @three-ws/x402-payment-modal/server
|
|
2
|
+
|
|
3
|
+
/** One x402 `accept` entry (scheme "exact", Solana network). */
|
|
4
|
+
export interface SolanaAccept {
|
|
5
|
+
scheme: 'exact';
|
|
6
|
+
network: string;
|
|
7
|
+
/** Atomic integer amount as a string. */
|
|
8
|
+
amount: string;
|
|
9
|
+
/** Mint address (base58). */
|
|
10
|
+
asset: string;
|
|
11
|
+
/** Recipient address (base58). */
|
|
12
|
+
payTo: string;
|
|
13
|
+
maxTimeoutSeconds?: number;
|
|
14
|
+
extra: {
|
|
15
|
+
name?: string;
|
|
16
|
+
decimals?: number;
|
|
17
|
+
/** Facilitator sponsor account that pays the SOL network fee (base58). */
|
|
18
|
+
feePayer: string;
|
|
19
|
+
[key: string]: unknown;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface CheckoutOptions {
|
|
24
|
+
/** Solana mainnet RPC URL. */
|
|
25
|
+
rpcUrl?: string;
|
|
26
|
+
/** Solana devnet RPC URL. */
|
|
27
|
+
devnetRpcUrl?: string;
|
|
28
|
+
/** Access-Control-Allow-Origin for the adapters. Default '*'. */
|
|
29
|
+
origin?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface PrepareResult {
|
|
33
|
+
network: string;
|
|
34
|
+
tx_base64: string;
|
|
35
|
+
recent_blockhash: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface BuilderCodeEcho {
|
|
39
|
+
a: string;
|
|
40
|
+
w?: string;
|
|
41
|
+
s?: string[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const X402_VERSION: number;
|
|
45
|
+
export const NETWORK_SOLANA_MAINNET: string;
|
|
46
|
+
export const NETWORK_SOLANA_DEVNET: string;
|
|
47
|
+
|
|
48
|
+
/** Solana USDC mint (mainnet). */
|
|
49
|
+
export const USDC_MINT_SOLANA: string;
|
|
50
|
+
/** $THREE — the three.ws utility token mint. */
|
|
51
|
+
export const THREE_MINT: string;
|
|
52
|
+
|
|
53
|
+
export interface WellKnownToken {
|
|
54
|
+
mint: string;
|
|
55
|
+
symbol: string;
|
|
56
|
+
name: string;
|
|
57
|
+
decimals: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Well-known Solana settlement assets, keyed by lowercase shortcut. */
|
|
61
|
+
export const WELL_KNOWN_SOLANA_TOKENS: {
|
|
62
|
+
usdc: WellKnownToken;
|
|
63
|
+
three: WellKnownToken;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Build one x402 Solana `accept` entry. Pass `token: 'usdc' | 'three'` for a
|
|
68
|
+
* well-known asset or an explicit `mint`, and the price as atomic `amount` or
|
|
69
|
+
* human `uiAmount`.
|
|
70
|
+
*/
|
|
71
|
+
export function solanaAccept(args: {
|
|
72
|
+
token?: 'usdc' | 'three';
|
|
73
|
+
mint?: string;
|
|
74
|
+
payTo: string;
|
|
75
|
+
feePayer: string;
|
|
76
|
+
amount?: string | number | bigint;
|
|
77
|
+
uiAmount?: string | number;
|
|
78
|
+
decimals?: number;
|
|
79
|
+
name?: string;
|
|
80
|
+
network?: string;
|
|
81
|
+
maxTimeoutSeconds?: number;
|
|
82
|
+
}): SolanaAccept;
|
|
83
|
+
|
|
84
|
+
export class CheckoutError extends Error {
|
|
85
|
+
status: number;
|
|
86
|
+
code: string;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function isSolanaNetwork(network: string): boolean;
|
|
90
|
+
|
|
91
|
+
export function prepareSolanaCheckout(args: {
|
|
92
|
+
accept: SolanaAccept;
|
|
93
|
+
buyer: string;
|
|
94
|
+
rpcUrl?: string;
|
|
95
|
+
devnetRpcUrl?: string;
|
|
96
|
+
}): Promise<PrepareResult>;
|
|
97
|
+
|
|
98
|
+
export function encodeX402Payment(args: {
|
|
99
|
+
accept: SolanaAccept;
|
|
100
|
+
signedTxBase64: string;
|
|
101
|
+
resourceUrl: string;
|
|
102
|
+
builderCode?: BuilderCodeEcho;
|
|
103
|
+
}): { x_payment: string };
|
|
104
|
+
|
|
105
|
+
export function handleCheckout(args: {
|
|
106
|
+
action: 'prepare' | 'encode' | string;
|
|
107
|
+
body?: Record<string, unknown>;
|
|
108
|
+
options?: CheckoutOptions;
|
|
109
|
+
}): Promise<{ status: number; body: Record<string, unknown> }>;
|