uvd-x402-sdk 2.0.3 → 2.2.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 +282 -15
- package/dist/adapters/index.d.mts +139 -0
- package/dist/adapters/index.d.ts +139 -0
- package/dist/adapters/index.js +724 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/index.mjs +720 -0
- package/dist/adapters/index.mjs.map +1 -0
- package/dist/{index-MTBgC_SL.d.mts → index-BHwtdJrt.d.mts} +45 -4
- package/dist/{index-MTBgC_SL.d.ts → index-BHwtdJrt.d.ts} +45 -4
- package/dist/{index-Db8dWNam.d.ts → index-CkDdnSNx.d.mts} +33 -2
- package/dist/{index-D0N_SYpK.d.mts → index-UTj85ZDJ.d.ts} +33 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +210 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +207 -1
- package/dist/index.mjs.map +1 -1
- package/dist/providers/evm/index.d.mts +19 -3
- package/dist/providers/evm/index.d.ts +19 -3
- package/dist/providers/evm/index.js +228 -12
- package/dist/providers/evm/index.js.map +1 -1
- package/dist/providers/evm/index.mjs +228 -12
- package/dist/providers/evm/index.mjs.map +1 -1
- package/dist/providers/near/index.d.mts +1 -1
- package/dist/providers/near/index.d.ts +1 -1
- package/dist/providers/near/index.js.map +1 -1
- package/dist/providers/near/index.mjs.map +1 -1
- package/dist/providers/solana/index.d.mts +1 -1
- package/dist/providers/solana/index.d.ts +1 -1
- package/dist/providers/solana/index.js +168 -0
- package/dist/providers/solana/index.js.map +1 -1
- package/dist/providers/solana/index.mjs +168 -0
- package/dist/providers/solana/index.mjs.map +1 -1
- package/dist/providers/stellar/index.d.mts +1 -1
- package/dist/providers/stellar/index.d.ts +1 -1
- package/dist/providers/stellar/index.js.map +1 -1
- package/dist/providers/stellar/index.mjs.map +1 -1
- package/dist/react/index.d.mts +3 -3
- package/dist/react/index.d.ts +3 -3
- package/dist/react/index.js +168 -0
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +168 -0
- package/dist/react/index.mjs.map +1 -1
- package/dist/utils/index.d.mts +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +168 -0
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +168 -0
- package/dist/utils/index.mjs.map +1 -1
- package/package.json +10 -2
- package/src/adapters/index.ts +13 -0
- package/src/adapters/wagmi.ts +294 -0
- package/src/chains/index.ts +255 -1
- package/src/index.ts +9 -0
- package/src/providers/evm/index.ts +64 -16
- package/src/types/index.ts +46 -3
|
@@ -27,9 +27,11 @@ import type {
|
|
|
27
27
|
PaymentInfo,
|
|
28
28
|
EVMPaymentPayload,
|
|
29
29
|
WalletAdapter,
|
|
30
|
+
TokenType,
|
|
31
|
+
TokenConfig,
|
|
30
32
|
} from '../../types';
|
|
31
33
|
import { X402Error } from '../../types';
|
|
32
|
-
import { getChainByName, getChainById } from '../../chains';
|
|
34
|
+
import { getChainByName, getChainById, getTokenConfig } from '../../chains';
|
|
33
35
|
|
|
34
36
|
/**
|
|
35
37
|
* Ethereum provider interface
|
|
@@ -197,22 +199,48 @@ export class EVMProvider implements WalletAdapter {
|
|
|
197
199
|
}
|
|
198
200
|
|
|
199
201
|
/**
|
|
200
|
-
* Get USDC
|
|
202
|
+
* Get token balance (defaults to USDC for backward compatibility)
|
|
203
|
+
*
|
|
204
|
+
* @param chainConfig - Chain configuration
|
|
205
|
+
* @param tokenType - Token type to check balance for (defaults to 'usdc')
|
|
206
|
+
* @returns Formatted balance string
|
|
201
207
|
*/
|
|
202
|
-
async getBalance(chainConfig: ChainConfig): Promise<string> {
|
|
208
|
+
async getBalance(chainConfig: ChainConfig, tokenType: TokenType = 'usdc'): Promise<string> {
|
|
203
209
|
if (!this.address) {
|
|
204
210
|
throw new X402Error('Wallet not connected', 'WALLET_NOT_CONNECTED');
|
|
205
211
|
}
|
|
206
212
|
|
|
213
|
+
// Get token config for the specified token type
|
|
214
|
+
const tokenConfig = getTokenConfig(chainConfig.name, tokenType);
|
|
215
|
+
if (!tokenConfig) {
|
|
216
|
+
// Fall back to USDC config for backward compatibility
|
|
217
|
+
const fallbackConfig = chainConfig.usdc;
|
|
218
|
+
if (!fallbackConfig) {
|
|
219
|
+
throw new X402Error(`Token ${tokenType} not supported on ${chainConfig.name}`, 'INVALID_CONFIG');
|
|
220
|
+
}
|
|
221
|
+
return this.getBalanceWithConfig(chainConfig.rpcUrl, fallbackConfig);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return this.getBalanceWithConfig(chainConfig.rpcUrl, tokenConfig);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Internal helper to get balance using a token config
|
|
229
|
+
*/
|
|
230
|
+
private async getBalanceWithConfig(rpcUrl: string, tokenConfig: TokenConfig): Promise<string> {
|
|
231
|
+
if (!this.address) {
|
|
232
|
+
return '0.00';
|
|
233
|
+
}
|
|
234
|
+
|
|
207
235
|
// Use public RPC for balance check
|
|
208
|
-
const publicProvider = new ethers.JsonRpcProvider(
|
|
236
|
+
const publicProvider = new ethers.JsonRpcProvider(rpcUrl);
|
|
209
237
|
|
|
210
|
-
const
|
|
211
|
-
const
|
|
238
|
+
const tokenAbi = ['function balanceOf(address owner) view returns (uint256)'];
|
|
239
|
+
const tokenContract = new ethers.Contract(tokenConfig.address, tokenAbi, publicProvider);
|
|
212
240
|
|
|
213
241
|
try {
|
|
214
|
-
const balance = await
|
|
215
|
-
const formatted = ethers.formatUnits(balance,
|
|
242
|
+
const balance = await tokenContract.balanceOf(this.address);
|
|
243
|
+
const formatted = ethers.formatUnits(balance, tokenConfig.decimals);
|
|
216
244
|
return parseFloat(formatted).toFixed(2);
|
|
217
245
|
} catch {
|
|
218
246
|
return '0.00';
|
|
@@ -221,12 +249,32 @@ export class EVMProvider implements WalletAdapter {
|
|
|
221
249
|
|
|
222
250
|
/**
|
|
223
251
|
* Create EVM payment (EIP-712 TransferWithAuthorization)
|
|
252
|
+
*
|
|
253
|
+
* Supports multi-token payments. If paymentInfo.tokenType is specified,
|
|
254
|
+
* it will use the appropriate token configuration. Defaults to USDC
|
|
255
|
+
* for backward compatibility.
|
|
256
|
+
*
|
|
257
|
+
* @param paymentInfo - Payment details including amount and recipient
|
|
258
|
+
* @param chainConfig - Chain configuration
|
|
259
|
+
* @returns JSON-encoded payment payload
|
|
224
260
|
*/
|
|
225
261
|
async signPayment(paymentInfo: PaymentInfo, chainConfig: ChainConfig): Promise<string> {
|
|
226
262
|
if (!this.signer || !this.address) {
|
|
227
263
|
throw new X402Error('Wallet not connected', 'WALLET_NOT_CONNECTED');
|
|
228
264
|
}
|
|
229
265
|
|
|
266
|
+
// Determine token type (default to 'usdc' for backward compatibility)
|
|
267
|
+
const tokenType: TokenType = paymentInfo.tokenType || 'usdc';
|
|
268
|
+
|
|
269
|
+
// Get token configuration for the specified token type
|
|
270
|
+
const tokenConfig = getTokenConfig(chainConfig.name, tokenType);
|
|
271
|
+
if (!tokenConfig) {
|
|
272
|
+
throw new X402Error(
|
|
273
|
+
`Token ${tokenType} not supported on ${chainConfig.name}`,
|
|
274
|
+
'CHAIN_NOT_SUPPORTED'
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
|
|
230
278
|
// Get recipient
|
|
231
279
|
const recipient = paymentInfo.recipients?.evm || paymentInfo.recipient;
|
|
232
280
|
|
|
@@ -246,12 +294,12 @@ export class EVMProvider implements WalletAdapter {
|
|
|
246
294
|
const validityWindowSeconds = chainConfig.name === 'base' ? 300 : 60;
|
|
247
295
|
const validBefore = Math.floor(Date.now() / 1000) + validityWindowSeconds;
|
|
248
296
|
|
|
249
|
-
// EIP-712 domain
|
|
297
|
+
// EIP-712 domain using the selected token's configuration
|
|
250
298
|
const domain = {
|
|
251
|
-
name:
|
|
252
|
-
version:
|
|
299
|
+
name: tokenConfig.name,
|
|
300
|
+
version: tokenConfig.version,
|
|
253
301
|
chainId: chainConfig.chainId,
|
|
254
|
-
verifyingContract:
|
|
302
|
+
verifyingContract: tokenConfig.address,
|
|
255
303
|
};
|
|
256
304
|
|
|
257
305
|
// EIP-712 types for TransferWithAuthorization (ERC-3009)
|
|
@@ -266,8 +314,8 @@ export class EVMProvider implements WalletAdapter {
|
|
|
266
314
|
],
|
|
267
315
|
};
|
|
268
316
|
|
|
269
|
-
// Parse amount
|
|
270
|
-
const value = ethers.parseUnits(paymentInfo.amount,
|
|
317
|
+
// Parse amount using the token's decimals
|
|
318
|
+
const value = ethers.parseUnits(paymentInfo.amount, tokenConfig.decimals);
|
|
271
319
|
const from = this.address;
|
|
272
320
|
const to = ethers.getAddress(recipient);
|
|
273
321
|
|
|
@@ -298,7 +346,7 @@ export class EVMProvider implements WalletAdapter {
|
|
|
298
346
|
|
|
299
347
|
const sig = ethers.Signature.from(signature);
|
|
300
348
|
|
|
301
|
-
// Construct payload
|
|
349
|
+
// Construct payload with the selected token address
|
|
302
350
|
const payload: EVMPaymentPayload = {
|
|
303
351
|
from,
|
|
304
352
|
to,
|
|
@@ -310,7 +358,7 @@ export class EVMProvider implements WalletAdapter {
|
|
|
310
358
|
r: sig.r,
|
|
311
359
|
s: sig.s,
|
|
312
360
|
chainId: chainConfig.chainId,
|
|
313
|
-
token:
|
|
361
|
+
token: tokenConfig.address,
|
|
314
362
|
};
|
|
315
363
|
|
|
316
364
|
return JSON.stringify(payload);
|
package/src/types/index.ts
CHANGED
|
@@ -20,8 +20,34 @@
|
|
|
20
20
|
*/
|
|
21
21
|
export type NetworkType = 'evm' | 'svm' | 'solana' | 'stellar' | 'near';
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Supported stablecoin token types
|
|
25
|
+
* - usdc: USD Coin (Circle) - 6 decimals
|
|
26
|
+
* - eurc: Euro Coin (Circle) - 6 decimals
|
|
27
|
+
* - ausd: Agora USD (Agora Finance) - 6 decimals
|
|
28
|
+
* - pyusd: PayPal USD (PayPal/Paxos) - 6 decimals
|
|
29
|
+
* - gho: GHO Stablecoin (Aave) - 18 decimals
|
|
30
|
+
* - crvusd: Curve USD (Curve Finance) - 18 decimals
|
|
31
|
+
*/
|
|
32
|
+
export type TokenType = 'usdc' | 'eurc' | 'ausd' | 'pyusd' | 'gho' | 'crvusd';
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Token configuration for EIP-712 signing and transfers
|
|
36
|
+
*/
|
|
37
|
+
export interface TokenConfig {
|
|
38
|
+
/** Contract/mint address */
|
|
39
|
+
address: string;
|
|
40
|
+
/** Token decimals (6 for most stablecoins, 18 for GHO/crvUSD) */
|
|
41
|
+
decimals: number;
|
|
42
|
+
/** Token name for EIP-712 domain (e.g., "USD Coin" or "USDC") */
|
|
43
|
+
name: string;
|
|
44
|
+
/** Token version for EIP-712 domain */
|
|
45
|
+
version: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
23
48
|
/**
|
|
24
49
|
* USDC token configuration for a specific chain
|
|
50
|
+
* @deprecated Use TokenConfig instead. This is kept for backward compatibility.
|
|
25
51
|
*/
|
|
26
52
|
export interface USDCConfig {
|
|
27
53
|
/** Contract/mint address */
|
|
@@ -65,6 +91,12 @@ export interface ChainConfig {
|
|
|
65
91
|
nativeCurrency: NativeCurrency;
|
|
66
92
|
/** USDC token configuration */
|
|
67
93
|
usdc: USDCConfig;
|
|
94
|
+
/**
|
|
95
|
+
* Multi-token configurations (EVM chains only)
|
|
96
|
+
* Maps token type to its configuration for this chain.
|
|
97
|
+
* Not all tokens are available on all chains.
|
|
98
|
+
*/
|
|
99
|
+
tokens?: Partial<Record<TokenType, TokenConfig>>;
|
|
68
100
|
/** x402 facilitator configuration */
|
|
69
101
|
x402: {
|
|
70
102
|
facilitatorUrl: string;
|
|
@@ -117,11 +149,17 @@ export interface WalletAdapter {
|
|
|
117
149
|
/** Switch to a different chain (EVM only) */
|
|
118
150
|
switchChain?(chainName: string): Promise<void>;
|
|
119
151
|
|
|
120
|
-
/**
|
|
152
|
+
/**
|
|
153
|
+
* Sign a payment payload
|
|
154
|
+
* For EVM chains, supports multi-token via paymentInfo.tokenType
|
|
155
|
+
*/
|
|
121
156
|
signPayment(paymentInfo: PaymentInfo, chainConfig: ChainConfig): Promise<string>;
|
|
122
157
|
|
|
123
|
-
/**
|
|
124
|
-
|
|
158
|
+
/**
|
|
159
|
+
* Check token balance (defaults to USDC for backward compatibility)
|
|
160
|
+
* EVM providers may accept optional tokenType parameter
|
|
161
|
+
*/
|
|
162
|
+
getBalance(chainConfig: ChainConfig, tokenType?: TokenType): Promise<string>;
|
|
125
163
|
|
|
126
164
|
/** Get current address */
|
|
127
165
|
getAddress(): string | null;
|
|
@@ -170,6 +208,11 @@ export interface PaymentInfo {
|
|
|
170
208
|
amount: string;
|
|
171
209
|
/** Token symbol (usually "USDC") */
|
|
172
210
|
token?: string;
|
|
211
|
+
/**
|
|
212
|
+
* Token type for multi-token support
|
|
213
|
+
* Defaults to 'usdc' if not specified for backward compatibility
|
|
214
|
+
*/
|
|
215
|
+
tokenType?: TokenType;
|
|
173
216
|
/** Network hint from backend */
|
|
174
217
|
network?: string;
|
|
175
218
|
/** Supported chain IDs */
|