@wtflabs/x402-server 0.0.1-beta.2
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 +313 -0
- package/dist/index.d.mts +221 -0
- package/dist/index.d.ts +221 -0
- package/dist/index.js +548 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +518 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +52 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { Facilitator } from '@wtflabs/x402-facilitator';
|
|
2
|
+
import { X402PaymentSchema } from '@wtflabs/x402-schema';
|
|
3
|
+
import { PublicClient } from 'viem';
|
|
4
|
+
import { PaymentPayload, PaymentRequirements } from '@wtflabs/x402/types';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* X402Server 配置选项
|
|
8
|
+
*/
|
|
9
|
+
interface X402ServerConfig {
|
|
10
|
+
/**
|
|
11
|
+
* Facilitator 实例
|
|
12
|
+
*/
|
|
13
|
+
facilitator: Facilitator;
|
|
14
|
+
/**
|
|
15
|
+
* Payment Schema 实例
|
|
16
|
+
*/
|
|
17
|
+
schema: X402PaymentSchema;
|
|
18
|
+
/**
|
|
19
|
+
* Viem Public Client 实例
|
|
20
|
+
*/
|
|
21
|
+
client: PublicClient;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 初始化结果
|
|
25
|
+
*/
|
|
26
|
+
interface InitializeResult {
|
|
27
|
+
success: boolean;
|
|
28
|
+
error?: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* 验证结果
|
|
32
|
+
*/
|
|
33
|
+
interface VerifyResult {
|
|
34
|
+
success: boolean;
|
|
35
|
+
errors?: string[];
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 结算结果
|
|
39
|
+
*/
|
|
40
|
+
interface SettleResult {
|
|
41
|
+
success: boolean;
|
|
42
|
+
transactionHash?: string;
|
|
43
|
+
error?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* X402Server 类
|
|
48
|
+
* 集成 facilitator, schema 和 client,提供完整的服务端支付处理
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* import { X402Server } from "@wtflabs/x402-server";
|
|
53
|
+
* import { Facilitator } from "@wtflabs/x402-facilitator";
|
|
54
|
+
* import { X402PaymentSchema } from "@wtflabs/x402-schema";
|
|
55
|
+
* import { createPublicClient, http } from "viem";
|
|
56
|
+
* import { bscTestnet } from "viem/chains";
|
|
57
|
+
*
|
|
58
|
+
* const facilitator = new Facilitator({
|
|
59
|
+
* recipientAddress: "0x1234...",
|
|
60
|
+
* });
|
|
61
|
+
*
|
|
62
|
+
* const schema = new X402PaymentSchema({
|
|
63
|
+
* scheme: "exact",
|
|
64
|
+
* network: "bsc-testnet",
|
|
65
|
+
* maxAmountRequired: "100000",
|
|
66
|
+
* resource: "http://localhost:3000/protected-resource",
|
|
67
|
+
* description: "Access to protected resource",
|
|
68
|
+
* mimeType: "application/json",
|
|
69
|
+
* payTo: "0x1234...",
|
|
70
|
+
* maxTimeoutSeconds: 3600,
|
|
71
|
+
* asset: "0x5678...",
|
|
72
|
+
* });
|
|
73
|
+
*
|
|
74
|
+
* const client = createPublicClient({
|
|
75
|
+
* chain: bscTestnet,
|
|
76
|
+
* transport: http(),
|
|
77
|
+
* });
|
|
78
|
+
*
|
|
79
|
+
* const server = new X402Server({
|
|
80
|
+
* facilitator,
|
|
81
|
+
* schema,
|
|
82
|
+
* client,
|
|
83
|
+
* });
|
|
84
|
+
*
|
|
85
|
+
* // 初始化和校验
|
|
86
|
+
* await server.initialize();
|
|
87
|
+
*
|
|
88
|
+
* // 验证
|
|
89
|
+
* const verifyResult = await server.verify();
|
|
90
|
+
*
|
|
91
|
+
* // 结算
|
|
92
|
+
* const settleResult = await server.settle(paymentPayload, paymentRequirements);
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
declare class X402Server {
|
|
96
|
+
private facilitator;
|
|
97
|
+
private schema;
|
|
98
|
+
private client;
|
|
99
|
+
private initialized;
|
|
100
|
+
constructor(config: X402ServerConfig);
|
|
101
|
+
/**
|
|
102
|
+
* 初始化服务器
|
|
103
|
+
* 初始化和校验 schema 等数据,对 schema 增加 facilitator 数据 extra: {relayer}
|
|
104
|
+
* 并获取 token 的 name 和 version 信息
|
|
105
|
+
*/
|
|
106
|
+
initialize(): Promise<InitializeResult>;
|
|
107
|
+
/**
|
|
108
|
+
* 验证配置
|
|
109
|
+
* 1. 验证 client network 是否和 schema 的 network 匹配
|
|
110
|
+
* 2. 验证 facilitator recipientAddress 和 schema payTo 是否一致
|
|
111
|
+
*/
|
|
112
|
+
_verify(): Promise<VerifyResult>;
|
|
113
|
+
/**
|
|
114
|
+
* 结算支付
|
|
115
|
+
* @param paymentPayload 支付负载
|
|
116
|
+
* @param paymentRequirements 支付要求
|
|
117
|
+
*/
|
|
118
|
+
settle(paymentPayload: any, paymentRequirements: any): Promise<SettleResult>;
|
|
119
|
+
/**
|
|
120
|
+
* 验证支付负载
|
|
121
|
+
* @param paymentPayload 支付负载
|
|
122
|
+
* @param paymentRequirements 支付要求
|
|
123
|
+
*/
|
|
124
|
+
verifyPayment(paymentPayload: any, paymentRequirements: any): Promise<{
|
|
125
|
+
success: boolean;
|
|
126
|
+
data?: string;
|
|
127
|
+
error?: string;
|
|
128
|
+
}>;
|
|
129
|
+
/**
|
|
130
|
+
* 获取 facilitator
|
|
131
|
+
*/
|
|
132
|
+
getFacilitator(): Facilitator;
|
|
133
|
+
/**
|
|
134
|
+
* 获取 schema
|
|
135
|
+
*/
|
|
136
|
+
getSchema(): X402PaymentSchema;
|
|
137
|
+
/**
|
|
138
|
+
* 获取 client
|
|
139
|
+
*/
|
|
140
|
+
getClient(): PublicClient;
|
|
141
|
+
/**
|
|
142
|
+
* 检查是否已初始化
|
|
143
|
+
*/
|
|
144
|
+
isInitialized(): boolean;
|
|
145
|
+
/**
|
|
146
|
+
* 验证网络是否匹配
|
|
147
|
+
* @param schemaNetwork schema 中的 network
|
|
148
|
+
* @param clientChainId client 的 chainId
|
|
149
|
+
* @returns 是否匹配
|
|
150
|
+
*/
|
|
151
|
+
private validateNetwork;
|
|
152
|
+
/**
|
|
153
|
+
* 解析支付 header
|
|
154
|
+
* @param paymentHeaderBase64 Base64 编码的支付 header
|
|
155
|
+
* @returns 解析结果,成功时返回 paymentPayload 和 paymentRequirements,失败时返回服务端的支付要求
|
|
156
|
+
*/
|
|
157
|
+
parsePaymentHeader(paymentHeaderBase64: string): {
|
|
158
|
+
success: true;
|
|
159
|
+
data: {
|
|
160
|
+
paymentPayload: PaymentPayload;
|
|
161
|
+
paymentRequirements: PaymentRequirements;
|
|
162
|
+
};
|
|
163
|
+
} | {
|
|
164
|
+
success: false;
|
|
165
|
+
data: PaymentRequirements;
|
|
166
|
+
error: string;
|
|
167
|
+
};
|
|
168
|
+
/**
|
|
169
|
+
* 验证客户端的支付数据是否与服务端要求一致
|
|
170
|
+
* @param paymentPayload 客户端的支付负载
|
|
171
|
+
* @param paymentRequirements 服务端的支付要求
|
|
172
|
+
* @returns 错误信息,如果验证通过则返回 null
|
|
173
|
+
*/
|
|
174
|
+
private validatePaymentPayload;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Token 信息
|
|
179
|
+
*/
|
|
180
|
+
interface TokenInfo {
|
|
181
|
+
name: string;
|
|
182
|
+
version: string;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* 支持的支付方式
|
|
186
|
+
*/
|
|
187
|
+
type PaymentMethod = "eip3009" | "permit" | "permit2" | "permit2-witness";
|
|
188
|
+
/**
|
|
189
|
+
* 检测结果
|
|
190
|
+
*/
|
|
191
|
+
interface TokenPaymentCapabilities {
|
|
192
|
+
address: string;
|
|
193
|
+
supportedMethods: PaymentMethod[];
|
|
194
|
+
details: {
|
|
195
|
+
hasEIP3009: boolean;
|
|
196
|
+
hasPermit: boolean;
|
|
197
|
+
hasPermit2Approval: boolean;
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* 检测代币支持的支付方式
|
|
202
|
+
* @param tokenAddress 代币地址
|
|
203
|
+
* @param client viem PublicClient
|
|
204
|
+
* @returns 检测结果
|
|
205
|
+
*/
|
|
206
|
+
declare function detectTokenPaymentMethods(tokenAddress: string, client: PublicClient): Promise<TokenPaymentCapabilities>;
|
|
207
|
+
/**
|
|
208
|
+
* 获取推荐的支付方式(仅返回 schema 支持的类型)
|
|
209
|
+
* 按优先级排序:eip3009 > permit > permit2
|
|
210
|
+
* 注意:permit2-witness 会被映射为 permit2,因为它们在 schema 中是同一种支付类型
|
|
211
|
+
*/
|
|
212
|
+
declare function getRecommendedPaymentMethod(capabilities: TokenPaymentCapabilities): "eip3009" | "permit2" | "permit" | null;
|
|
213
|
+
/**
|
|
214
|
+
* 获取 token 的 name 和 version 信息(用于 EIP-712 签名)
|
|
215
|
+
* @param tokenAddress 代币地址
|
|
216
|
+
* @param client viem PublicClient
|
|
217
|
+
* @returns Token 的 name 和 version
|
|
218
|
+
*/
|
|
219
|
+
declare function getTokenInfo(tokenAddress: string, client: PublicClient): Promise<TokenInfo>;
|
|
220
|
+
|
|
221
|
+
export { type InitializeResult, type PaymentMethod, type SettleResult, type TokenInfo, type TokenPaymentCapabilities, type VerifyResult, X402Server, type X402ServerConfig, detectTokenPaymentMethods, getRecommendedPaymentMethod, getTokenInfo };
|