@atxp/worldchain 0.6.4

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/index.js ADDED
@@ -0,0 +1,869 @@
1
+ import { WORLD_CHAIN_MAINNET, USDC_CONTRACT_ADDRESS_WORLD_MAINNET, getWorldChainMainnetWithRPC } from '@atxp/client';
2
+ import { ConsoleLogger, constructEIP1271Message, createEIP1271AuthData, createEIP1271JWT, JsonCache, BrowserCache } from '@atxp/common';
3
+ export { BrowserCache, MemoryCache } from '@atxp/common';
4
+ import { createWalletClient, custom, encodeFunctionData, parseEther, createPublicClient, http, parseUnits } from 'viem';
5
+ import { privateKeyToAccount, generatePrivateKey } from 'viem/accounts';
6
+ import { toCoinbaseSmartAccount, createBundlerClient } from 'viem/account-abstraction';
7
+
8
+ /*
9
+ This shim replaces spend permission functionality with ERC20 approvals for World Chain.
10
+ This approach will work with any World Chain wallet/connector that supports ERC20 approvals.
11
+
12
+ The implementation is based on the Base shim but adapted for World Chain networks.
13
+ */
14
+ // Minimal ERC20 ABI for approve and transferFrom functions
15
+ const ERC20_ABI$2 = [
16
+ {
17
+ "constant": false,
18
+ "inputs": [
19
+ {
20
+ "name": "_spender",
21
+ "type": "address"
22
+ },
23
+ {
24
+ "name": "_value",
25
+ "type": "uint256"
26
+ }
27
+ ],
28
+ "name": "approve",
29
+ "outputs": [
30
+ {
31
+ "name": "",
32
+ "type": "bool"
33
+ }
34
+ ],
35
+ "payable": false,
36
+ "stateMutability": "nonpayable",
37
+ "type": "function"
38
+ },
39
+ {
40
+ "constant": false,
41
+ "inputs": [
42
+ {
43
+ "name": "_from",
44
+ "type": "address"
45
+ },
46
+ {
47
+ "name": "_to",
48
+ "type": "address"
49
+ },
50
+ {
51
+ "name": "_value",
52
+ "type": "uint256"
53
+ }
54
+ ],
55
+ "name": "transferFrom",
56
+ "outputs": [
57
+ {
58
+ "name": "",
59
+ "type": "bool"
60
+ }
61
+ ],
62
+ "payable": false,
63
+ "stateMutability": "nonpayable",
64
+ "type": "function"
65
+ }
66
+ ];
67
+ async function requestSpendPermission(params) {
68
+ // Support World Chain mainnet only
69
+ if (params.chainId !== WORLD_CHAIN_MAINNET.id) {
70
+ throw new Error(`Chain ID ${params.chainId} is not supported. Only World Chain mainnet (${WORLD_CHAIN_MAINNET.id}) is supported.`);
71
+ }
72
+ // Use World Chain mainnet config
73
+ const chainConfig = WORLD_CHAIN_MAINNET;
74
+ const client = createWalletClient({
75
+ chain: chainConfig,
76
+ transport: custom(params.provider)
77
+ });
78
+ // Send ERC20 approve transaction
79
+ const hash = await client.sendTransaction({
80
+ account: params.account,
81
+ to: params.token,
82
+ data: encodeFunctionData({
83
+ abi: ERC20_ABI$2,
84
+ functionName: "approve",
85
+ args: [params.spender, params.allowance]
86
+ })
87
+ });
88
+ return {
89
+ permission: {
90
+ account: params.account,
91
+ spender: params.spender,
92
+ token: params.token,
93
+ allowance: params.allowance.toString(),
94
+ period: params.periodInDays * 24 * 60 * 60,
95
+ start: Math.floor(Date.now() / 1000),
96
+ end: Math.floor(Date.now() / 1000) + params.periodInDays * 24 * 60 * 60,
97
+ salt: '0x0',
98
+ extraData: '0x0'
99
+ },
100
+ signature: hash
101
+ };
102
+ }
103
+ async function prepareSpendCallData(params) {
104
+ // Creates transferFrom call data to move tokens from user wallet to ephemeral wallet
105
+ return [
106
+ {
107
+ to: params.permission.permission.token,
108
+ data: encodeFunctionData({
109
+ abi: ERC20_ABI$2,
110
+ functionName: 'transferFrom',
111
+ args: [
112
+ params.permission.permission.account,
113
+ params.permission.permission.spender,
114
+ params.amount
115
+ ]
116
+ }),
117
+ value: BigInt(0)
118
+ }
119
+ ];
120
+ }
121
+
122
+ const USDC_DECIMALS$1 = 6;
123
+ // Minimal ERC20 ABI for transfer function
124
+ const ERC20_ABI$1 = [
125
+ {
126
+ inputs: [
127
+ { name: 'to', type: 'address' },
128
+ { name: 'amount', type: 'uint256' }
129
+ ],
130
+ name: 'transfer',
131
+ outputs: [{ name: '', type: 'bool' }],
132
+ stateMutability: 'nonpayable',
133
+ type: 'function'
134
+ }
135
+ ];
136
+ /**
137
+ * Validates confirmation delays configuration
138
+ */
139
+ function validateConfirmationDelays(delays) {
140
+ if (delays.networkPropagationMs < 0) {
141
+ throw new Error('networkPropagationMs must be non-negative');
142
+ }
143
+ if (delays.confirmationFailedMs < 0) {
144
+ throw new Error('confirmationFailedMs must be non-negative');
145
+ }
146
+ }
147
+ const DEFAULT_CONFIRMATION_DELAYS = {
148
+ networkPropagationMs: 5000, // 5 seconds for production
149
+ confirmationFailedMs: 15000 // 15 seconds for production
150
+ };
151
+ /**
152
+ * Gets default confirmation delays based on environment
153
+ */
154
+ const getDefaultConfirmationDelays = () => {
155
+ if (process.env.NODE_ENV === 'test') {
156
+ return { networkPropagationMs: 10, confirmationFailedMs: 20 };
157
+ }
158
+ return DEFAULT_CONFIRMATION_DELAYS;
159
+ };
160
+ async function waitForTransactionConfirmations(smartWallet, txHash, confirmations, logger, delays = DEFAULT_CONFIRMATION_DELAYS) {
161
+ try {
162
+ const publicClient = smartWallet.client.account?.client;
163
+ if (publicClient && 'waitForTransactionReceipt' in publicClient) {
164
+ logger.info(`Waiting for ${confirmations} confirmations...`);
165
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
166
+ await publicClient.waitForTransactionReceipt({
167
+ hash: txHash,
168
+ confirmations: 1, // Reduce confirmations to speed up
169
+ timeout: 60000 // 60 second timeout
170
+ });
171
+ logger.info(`Transaction confirmed with 1 confirmation`);
172
+ // Add extra delay for network propagation
173
+ logger.info(`Adding ${delays.networkPropagationMs}ms delay for network propagation...`);
174
+ await new Promise(resolve => setTimeout(resolve, delays.networkPropagationMs));
175
+ }
176
+ else {
177
+ logger.warn('Unable to wait for confirmations: client does not support waitForTransactionReceipt');
178
+ }
179
+ }
180
+ catch (error) {
181
+ logger.warn(`Could not wait for additional confirmations: ${error}`);
182
+ // Add longer delay if confirmation failed
183
+ logger.info(`Confirmation failed, adding ${delays.confirmationFailedMs}ms delay for transaction to propagate...`);
184
+ await new Promise(resolve => setTimeout(resolve, delays.confirmationFailedMs));
185
+ }
186
+ }
187
+ /**
188
+ * Payment maker for World Chain transactions using smart wallets and spend permissions
189
+ *
190
+ * @example
191
+ * ```typescript
192
+ * // For production use (default delays)
193
+ * const paymentMaker = new WorldchainPaymentMaker(permission, wallet);
194
+ *
195
+ * // For testing (fast delays)
196
+ * const paymentMaker = new WorldchainPaymentMaker(permission, wallet, {
197
+ * confirmationDelays: { networkPropagationMs: 10, confirmationFailedMs: 20 }
198
+ * });
199
+ *
200
+ * // With custom configuration
201
+ * const paymentMaker = new WorldchainPaymentMaker(permission, wallet, {
202
+ * chainId: 480, // World Chain Mainnet
203
+ * customRpcUrl: 'https://my-rpc.com',
204
+ * logger: myLogger
205
+ * });
206
+ * ```
207
+ */
208
+ class WorldchainPaymentMaker {
209
+ /**
210
+ * Creates a new WorldchainPaymentMaker instance
211
+ *
212
+ * @param spendPermission - The spend permission for transactions
213
+ * @param smartWallet - The smart wallet instance to use
214
+ * @param options - Optional configuration
215
+ */
216
+ constructor(spendPermission, smartWallet, options = {}) {
217
+ if (!spendPermission) {
218
+ throw new Error('Spend permission is required');
219
+ }
220
+ if (!smartWallet) {
221
+ throw new Error('Smart wallet is required');
222
+ }
223
+ // Extract and validate options
224
+ const { logger, chainId = WORLD_CHAIN_MAINNET.id, customRpcUrl, confirmationDelays } = options;
225
+ const finalDelays = confirmationDelays ?? getDefaultConfirmationDelays();
226
+ validateConfirmationDelays(finalDelays);
227
+ this.logger = logger ?? new ConsoleLogger();
228
+ this.spendPermission = spendPermission;
229
+ this.smartWallet = smartWallet;
230
+ this.chainId = chainId;
231
+ this.customRpcUrl = customRpcUrl;
232
+ this.confirmationDelays = finalDelays;
233
+ }
234
+ async generateJWT({ paymentRequestId, codeChallenge }) {
235
+ // Generate EIP-1271 auth data for smart wallet authentication
236
+ const timestamp = Math.floor(Date.now() / 1000);
237
+ const message = constructEIP1271Message({
238
+ walletAddress: this.smartWallet.account.address,
239
+ timestamp,
240
+ codeChallenge,
241
+ paymentRequestId
242
+ });
243
+ // Sign the message - this will return an ABI-encoded signature from the smart wallet
244
+ const signature = await this.smartWallet.account.signMessage({
245
+ message: message
246
+ });
247
+ const authData = createEIP1271AuthData({
248
+ walletAddress: this.smartWallet.account.address,
249
+ message,
250
+ signature,
251
+ timestamp,
252
+ codeChallenge,
253
+ paymentRequestId
254
+ });
255
+ return createEIP1271JWT(authData);
256
+ }
257
+ async makePayment(amount, currency, receiver, memo) {
258
+ if (currency !== 'USDC') {
259
+ throw new Error('Only usdc currency is supported; received ' + currency);
260
+ }
261
+ // Use World Chain Mainnet configuration
262
+ const usdcAddress = USDC_CONTRACT_ADDRESS_WORLD_MAINNET;
263
+ // Convert amount to USDC units (6 decimals) as BigInt for spendPermission
264
+ const amountInUSDCUnits = BigInt(amount.multipliedBy(10 ** USDC_DECIMALS$1).toFixed(0));
265
+ const spendCalls = await prepareSpendCallData({ permission: this.spendPermission, amount: amountInUSDCUnits });
266
+ // Add a second call to transfer USDC from the smart wallet to the receiver
267
+ let transferCallData = encodeFunctionData({
268
+ abi: ERC20_ABI$1,
269
+ functionName: "transfer",
270
+ args: [receiver, amountInUSDCUnits],
271
+ });
272
+ // Append memo to transfer call data if present
273
+ // This works because the EVM ignores extra calldata beyond what a function expects.
274
+ // The ERC20 transfer() function only reads the first 68 bytes (4-byte selector + 32-byte address + 32-byte amount).
275
+ // Any additional data appended after those 68 bytes is safely ignored by the USDC contract
276
+ // but remains accessible in the transaction data for payment verification.
277
+ // This is a well-established pattern used by OpenSea, Uniswap, and other major protocols.
278
+ if (memo && memo.trim()) {
279
+ const memoHex = Buffer.from(memo.trim(), 'utf8').toString('hex');
280
+ transferCallData = (transferCallData + memoHex);
281
+ this.logger.info(`Added memo "${memo.trim()}" to transfer call`);
282
+ }
283
+ const transferCall = {
284
+ to: usdcAddress,
285
+ data: transferCallData,
286
+ value: '0x0'
287
+ };
288
+ // Combine spend permission calls with the transfer call
289
+ const allCalls = [...spendCalls, transferCall];
290
+ this.logger.info(`Executing ${allCalls.length} calls (${spendCalls.length} spend permission + 1 transfer)`);
291
+ const hash = await this.smartWallet.client.sendUserOperation({
292
+ account: this.smartWallet.account,
293
+ calls: allCalls.map(call => {
294
+ return {
295
+ to: call.to,
296
+ data: call.data,
297
+ value: BigInt(call.value || '0x0')
298
+ };
299
+ }),
300
+ maxPriorityFeePerGas: parseEther('0.000000001')
301
+ });
302
+ const receipt = await this.smartWallet.client.waitForUserOperationReceipt({ hash });
303
+ if (!receipt) {
304
+ throw new Error('User operation failed');
305
+ }
306
+ // The receipt contains the actual transaction hash that was mined on chain
307
+ const txHash = receipt.receipt.transactionHash;
308
+ if (!txHash) {
309
+ throw new Error('User operation was executed but no transaction hash was returned. This should not happen.');
310
+ }
311
+ this.logger.info(`Spend permission executed successfully. UserOp: ${receipt.userOpHash}, TxHash: ${txHash}`);
312
+ // Wait for additional confirmations to ensure the transaction is well-propagated
313
+ // This helps avoid the "Transaction receipt could not be found" error
314
+ await waitForTransactionConfirmations(this.smartWallet, txHash, 2, this.logger, this.confirmationDelays);
315
+ // Return the actual transaction hash, not the user operation hash
316
+ // The payment verification system needs the on-chain transaction hash
317
+ return txHash;
318
+ }
319
+ }
320
+
321
+ const USDC_DECIMALS = 6;
322
+ // Minimal ERC20 ABI for transfer function
323
+ const ERC20_ABI = [
324
+ {
325
+ inputs: [
326
+ { name: 'to', type: 'address' },
327
+ { name: 'amount', type: 'uint256' }
328
+ ],
329
+ name: 'transfer',
330
+ outputs: [{ name: '', type: 'bool' }],
331
+ stateMutability: 'nonpayable',
332
+ type: 'function'
333
+ }
334
+ ];
335
+ class MainWalletPaymentMaker {
336
+ constructor(walletAddress, provider, logger, chainId = WORLD_CHAIN_MAINNET.id, customRpcUrl) {
337
+ this.walletAddress = walletAddress;
338
+ this.provider = provider;
339
+ this.logger = logger ?? new ConsoleLogger();
340
+ this.chainId = chainId;
341
+ this.customRpcUrl = customRpcUrl;
342
+ }
343
+ async generateJWT({ paymentRequestId, codeChallenge }) {
344
+ const timestamp = Math.floor(Date.now() / 1000);
345
+ // Create proper JWT header and payload for ES256K
346
+ const header = {
347
+ alg: 'ES256K',
348
+ typ: 'JWT'
349
+ };
350
+ const payload = {
351
+ sub: this.walletAddress,
352
+ iss: 'accounts.atxp.ai',
353
+ aud: 'https://auth.atxp.ai',
354
+ iat: timestamp,
355
+ exp: timestamp + 3600, // 1 hour expiration
356
+ payment_request_id: paymentRequestId,
357
+ code_challenge: codeChallenge,
358
+ chain_id: this.chainId
359
+ };
360
+ // Encode header and payload to base64url
361
+ const encodedHeader = Buffer.from(JSON.stringify(header)).toString('base64url');
362
+ const encodedPayload = Buffer.from(JSON.stringify(payload)).toString('base64url');
363
+ // Create the message that ES256K should actually sign (header.payload)
364
+ const messageToSign = `${encodedHeader}.${encodedPayload}`;
365
+ // Sign the actual JWT header.payload string using personal_sign
366
+ const signature = await this.provider.request({
367
+ method: 'personal_sign',
368
+ params: [messageToSign, this.walletAddress]
369
+ });
370
+ // Use the legacy format (base64url of hex string with 0x prefix)
371
+ // This matches what the ES256K verification expects
372
+ const encodedSignature = Buffer.from(signature).toString('base64url');
373
+ const jwt = `${encodedHeader}.${encodedPayload}.${encodedSignature}`;
374
+ this.logger.info(`Generated ES256K JWT for main wallet: ${this.walletAddress}`);
375
+ return jwt;
376
+ }
377
+ async makePayment(amount, currency, receiver, memo) {
378
+ if (currency !== 'USDC') {
379
+ throw new Error('Only USDC currency is supported; received ' + currency);
380
+ }
381
+ // Use World Chain Mainnet configuration
382
+ const usdcAddress = USDC_CONTRACT_ADDRESS_WORLD_MAINNET;
383
+ const chainConfig = this.customRpcUrl
384
+ ? getWorldChainMainnetWithRPC(this.customRpcUrl)
385
+ : WORLD_CHAIN_MAINNET;
386
+ const chainName = chainConfig.name;
387
+ this.logger.info(`Making direct wallet payment of ${amount} ${currency} to ${receiver} on ${chainName} with memo: ${memo}`);
388
+ // Convert amount to USDC units (6 decimals)
389
+ const amountInUSDCUnits = BigInt(amount.multipliedBy(10 ** USDC_DECIMALS).toFixed(0));
390
+ // Prepare transfer call data
391
+ let transferCallData = encodeFunctionData({
392
+ abi: ERC20_ABI,
393
+ functionName: 'transfer',
394
+ args: [receiver, amountInUSDCUnits]
395
+ });
396
+ // Append memo to call data if present
397
+ if (memo && memo.trim()) {
398
+ const memoHex = Buffer.from(memo.trim(), 'utf8').toString('hex');
399
+ transferCallData = (transferCallData + memoHex);
400
+ this.logger.info(`Added memo "${memo.trim()}" to transfer call`);
401
+ }
402
+ // Create wallet client
403
+ const walletClient = createWalletClient({
404
+ chain: chainConfig,
405
+ transport: custom(this.provider)
406
+ });
407
+ // Send transaction directly from main wallet
408
+ const txHash = await walletClient.sendTransaction({
409
+ account: this.walletAddress,
410
+ to: usdcAddress,
411
+ data: transferCallData,
412
+ value: 0n,
413
+ chain: chainConfig
414
+ });
415
+ this.logger.info(`Payment sent successfully. TxHash: ${txHash}`);
416
+ this.logger.info(`Transaction URL: https://worldscan.org/tx/${txHash}`);
417
+ // Wait for transaction confirmation to ensure it's mined
418
+ // This prevents "Transaction receipt could not be found" errors
419
+ try {
420
+ this.logger.info(`Waiting for transaction confirmation...`);
421
+ // Create a public client to wait for the transaction receipt
422
+ // Use custom RPC URL if provided for better consistency with auth server
423
+ const rpcUrl = this.customRpcUrl || chainConfig.rpcUrls.default.http[0];
424
+ const publicClient = createPublicClient({
425
+ chain: chainConfig,
426
+ transport: http(rpcUrl, {
427
+ timeout: 30000, // 30 second timeout for individual RPC calls
428
+ retryCount: 3, // Retry failed requests
429
+ retryDelay: 1000 // 1 second delay between retries
430
+ })
431
+ });
432
+ await publicClient.waitForTransactionReceipt({
433
+ hash: txHash,
434
+ confirmations: 1, // Reduce to 1 confirmation to speed up
435
+ timeout: 120000 // 2 minute timeout - World Chain can be slow
436
+ });
437
+ this.logger.info(`Transaction confirmed with 1 confirmation`);
438
+ // Add extra delay to ensure the transaction is well propagated across network
439
+ await new Promise(resolve => setTimeout(resolve, 5000));
440
+ }
441
+ catch (error) {
442
+ this.logger.warn(`Could not wait for confirmations: ${error}`);
443
+ // Add a much longer delay if confirmation failed - World Chain can be slow
444
+ this.logger.info('Confirmation failed, adding delay for transaction to propagate...');
445
+ await new Promise(resolve => setTimeout(resolve, 30000));
446
+ }
447
+ return txHash;
448
+ }
449
+ }
450
+
451
+ /**
452
+ * Type-safe cache wrapper for permission data
453
+ */
454
+ class IntermediaryCache extends JsonCache {
455
+ }
456
+
457
+ // For now, we'll use a generic approach for World Chain
458
+ // This may need to be updated when World Chain provides specific infrastructure
459
+ const DEFAULT_WORLD_CHAIN_RPC = 'https://worldchain-mainnet.g.alchemy.com/public';
460
+ /**
461
+ * Creates an ephemeral smart wallet for World Chain
462
+ * Note: This implementation uses Coinbase's smart wallet infrastructure
463
+ * adapted for World Chain. This may need updates when World Chain
464
+ * provides their own account abstraction infrastructure.
465
+ */
466
+ async function toEphemeralSmartWallet(privateKey, rpcUrl) {
467
+ const signer = privateKeyToAccount(privateKey);
468
+ const publicClient = createPublicClient({
469
+ chain: WORLD_CHAIN_MAINNET,
470
+ transport: http(DEFAULT_WORLD_CHAIN_RPC)
471
+ });
472
+ // Create the smart wallet using Coinbase's smart account SDK
473
+ // This will need to be adapted when World Chain provides their own solution
474
+ const account = await toCoinbaseSmartAccount({
475
+ client: publicClient,
476
+ owners: [signer],
477
+ version: '1'
478
+ });
479
+ // Create bundler client
480
+ // Note: World Chain may not have paymaster support initially
481
+ const bundlerClient = createBundlerClient({
482
+ account,
483
+ client: publicClient,
484
+ transport: http(DEFAULT_WORLD_CHAIN_RPC),
485
+ chain: WORLD_CHAIN_MAINNET
486
+ // Paymaster omitted - World Chain infrastructure may not support it yet
487
+ });
488
+ return {
489
+ address: account.address,
490
+ client: bundlerClient,
491
+ account,
492
+ signer,
493
+ };
494
+ }
495
+
496
+ const DEFAULT_ALLOWANCE = 10n;
497
+ const DEFAULT_PERIOD_IN_DAYS = 7;
498
+ class WorldchainAccount {
499
+ static toCacheKey(userWalletAddress) {
500
+ return `atxp-world-permission-${userWalletAddress}`;
501
+ }
502
+ static async initialize(config) {
503
+ const logger = config.logger || new ConsoleLogger();
504
+ const useEphemeralWallet = config.useEphemeralWallet ?? true;
505
+ const chainId = config.chainId || WORLD_CHAIN_MAINNET.id;
506
+ // Use World Chain Mainnet USDC address
507
+ const usdcAddress = USDC_CONTRACT_ADDRESS_WORLD_MAINNET;
508
+ // Some wallets don't support wallet_connect, so
509
+ // will just continue if it fails
510
+ try {
511
+ await config.provider.request({ method: 'wallet_connect' });
512
+ }
513
+ catch (error) {
514
+ logger.warn(`wallet_connect not supported, continuing with initialization. ${error}`);
515
+ }
516
+ // If using main wallet mode, return early with main wallet payment maker
517
+ if (!useEphemeralWallet) {
518
+ logger.info(`Using main wallet mode for address: ${config.walletAddress}`);
519
+ return new WorldchainAccount(null, // No spend permission in main wallet mode
520
+ null, // No ephemeral wallet in main wallet mode
521
+ logger, config.walletAddress, config.provider, chainId, config.customRpcUrl);
522
+ }
523
+ // Initialize cache
524
+ const baseCache = config?.cache || new BrowserCache();
525
+ const cache = new IntermediaryCache(baseCache);
526
+ const cacheKey = this.toCacheKey(config.walletAddress);
527
+ // Try to load existing permission
528
+ const existingData = this.loadSavedWalletAndPermission(cache, cacheKey);
529
+ if (existingData) {
530
+ const ephemeralSmartWallet = await toEphemeralSmartWallet(existingData.privateKey);
531
+ return new WorldchainAccount(existingData.permission, ephemeralSmartWallet, logger, undefined, undefined, chainId, config.customRpcUrl);
532
+ }
533
+ const privateKey = generatePrivateKey();
534
+ const smartWallet = await toEphemeralSmartWallet(privateKey);
535
+ logger.info(`Generated ephemeral wallet: ${smartWallet.address}`);
536
+ await this.deploySmartWallet(smartWallet);
537
+ logger.info(`Deployed smart wallet: ${smartWallet.address}`);
538
+ const permission = await requestSpendPermission({
539
+ account: config.walletAddress,
540
+ spender: smartWallet.address,
541
+ token: usdcAddress,
542
+ chainId: chainId,
543
+ allowance: config?.allowance ?? DEFAULT_ALLOWANCE,
544
+ periodInDays: config?.periodInDays ?? DEFAULT_PERIOD_IN_DAYS,
545
+ provider: config.provider,
546
+ });
547
+ // Save wallet and permission
548
+ cache.set(cacheKey, { privateKey, permission });
549
+ return new WorldchainAccount(permission, smartWallet, logger, undefined, undefined, chainId, config.customRpcUrl);
550
+ }
551
+ static loadSavedWalletAndPermission(permissionCache, cacheKey) {
552
+ const cachedData = permissionCache.get(cacheKey);
553
+ if (!cachedData)
554
+ return null;
555
+ // Check if permission is not expired
556
+ const now = Math.floor(Date.now() / 1000);
557
+ const permissionEnd = parseInt(cachedData.permission.permission.end.toString());
558
+ if (permissionEnd <= now) {
559
+ permissionCache.delete(cacheKey);
560
+ return null;
561
+ }
562
+ return cachedData;
563
+ }
564
+ static async deploySmartWallet(smartWallet) {
565
+ const deployTx = await smartWallet.client.sendUserOperation({
566
+ calls: [{
567
+ to: smartWallet.address,
568
+ value: 0n,
569
+ data: '0x'
570
+ }]
571
+ // Note: World Chain may not have paymaster support initially
572
+ // paymaster omitted
573
+ });
574
+ const receipt = await smartWallet.client.waitForUserOperationReceipt({
575
+ hash: deployTx
576
+ });
577
+ if (!receipt.success) {
578
+ throw new Error(`Smart wallet deployment failed. Receipt: ${JSON.stringify(receipt)}`);
579
+ }
580
+ }
581
+ constructor(spendPermission, ephemeralSmartWallet, logger, mainWalletAddress, provider, chainId = WORLD_CHAIN_MAINNET.id, customRpcUrl) {
582
+ if (ephemeralSmartWallet) {
583
+ // Ephemeral wallet mode
584
+ if (!spendPermission) {
585
+ throw new Error('Spend permission is required for ephemeral wallet mode');
586
+ }
587
+ this.accountId = ephemeralSmartWallet.address;
588
+ this.paymentMakers = {
589
+ 'world': new WorldchainPaymentMaker(spendPermission, ephemeralSmartWallet, {
590
+ logger,
591
+ chainId,
592
+ customRpcUrl
593
+ }),
594
+ };
595
+ }
596
+ else {
597
+ // Main wallet mode
598
+ if (!mainWalletAddress || !provider) {
599
+ throw new Error('Main wallet address and provider are required for main wallet mode');
600
+ }
601
+ this.accountId = mainWalletAddress;
602
+ this.paymentMakers = {
603
+ 'world': new MainWalletPaymentMaker(mainWalletAddress, provider, logger, chainId, customRpcUrl),
604
+ };
605
+ }
606
+ }
607
+ static clearAllCachedData(userWalletAddress, cache) {
608
+ // In non-browser environments, require an explicit cache parameter
609
+ if (!cache) {
610
+ const browserCache = new BrowserCache();
611
+ // Check if BrowserCache would work (i.e., we're in a browser)
612
+ if (typeof window === 'undefined') {
613
+ throw new Error('clearAllCachedData requires a cache to be provided outside of browser environments');
614
+ }
615
+ cache = browserCache;
616
+ }
617
+ cache.delete(this.toCacheKey(userWalletAddress));
618
+ }
619
+ }
620
+
621
+ /**
622
+ * Loads and initializes a Worldchain account with MiniKit integration
623
+ *
624
+ * This function creates a Worldchain account that can interact with the World Chain network
625
+ * using MiniKit for transaction signing and wallet operations. It sets up a custom provider
626
+ * that handles various Ethereum JSON-RPC methods through MiniKit's interface.
627
+ *
628
+ * @param walletAddress - The wallet address to use for the account
629
+ * @param logger - Optional logger instance for debugging and monitoring
630
+ * @param customRpcUrl - Optional custom RPC URL for Worldchain. If not provided, uses the public RPC endpoint
631
+ * @param miniKit - The MiniKit instance to use for transactions and signing
632
+ * @returns Promise that resolves to an initialized WorldchainAccount instance
633
+ *
634
+ * @example
635
+ * ```typescript
636
+ * // Using public RPC endpoint
637
+ * const account = await createMiniKitWorldchainAccount({
638
+ * walletAddress: "0x1234...",
639
+ * logger: new ConsoleLogger(),
640
+ * miniKit: MiniKit
641
+ * });
642
+ *
643
+ * // Using custom RPC endpoint
644
+ * const account = await createMiniKitWorldchainAccount({
645
+ * walletAddress: "0x1234...",
646
+ * logger: new ConsoleLogger(),
647
+ * customRpcUrl: "https://your-custom-rpc-endpoint.com",
648
+ * miniKit: MiniKit
649
+ * });
650
+ * ```
651
+ *
652
+ * @remarks
653
+ * The function creates a custom provider that supports:
654
+ * - `eth_accounts`: Returns the wallet address
655
+ * - `eth_chainId`: Returns Worldchain chain ID (0x1e0 / 480)
656
+ * - `eth_requestAccounts`: Returns the wallet address
657
+ * - `eth_sendTransaction`: Handles USDC transfers and other transactions via MiniKit
658
+ * - `personal_sign`: Signs messages using MiniKit
659
+ *
660
+ * The account is configured with:
661
+ * - 10 USDC allowance
662
+ * - 30-day period for permissions
663
+ * - Worldchain mainnet RPC endpoint (public or custom)
664
+ * - Regular wallet mode (no ephemeral wallet)
665
+ *
666
+ * @throws {Error} When MiniKit operations fail or unsupported transaction types are encountered
667
+ */
668
+ const createMiniKitWorldchainAccount = async ({ walletAddress, logger: loggerParam, customRpcUrl, miniKit }) => {
669
+ const logger = loggerParam || new ConsoleLogger();
670
+ // If no connector client from wagmi, create a simple MiniKit provider
671
+ const provider = {
672
+ request: async (args) => {
673
+ const { method, params } = args;
674
+ switch (method) {
675
+ case 'eth_accounts':
676
+ return [walletAddress];
677
+ case 'eth_chainId':
678
+ return '0x1e0'; // Worldchain chain ID (480)
679
+ case 'eth_requestAccounts':
680
+ return [walletAddress];
681
+ case 'eth_sendTransaction':
682
+ return await handleSendTransaction(params, logger, miniKit);
683
+ case 'personal_sign':
684
+ return await signMessageWithMiniKit(params, miniKit);
685
+ default:
686
+ throw new Error(`Method ${method} not supported in MiniKit context`);
687
+ }
688
+ },
689
+ };
690
+ const worldchainAccount = await WorldchainAccount.initialize({
691
+ walletAddress,
692
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
693
+ provider: provider, // Type cast needed for client compatibility
694
+ allowance: parseUnits("10", 6), // 10 USDC
695
+ useEphemeralWallet: false, // Regular wallet mode (smart wallet infrastructure not available on World Chain)
696
+ periodInDays: 30,
697
+ customRpcUrl: customRpcUrl || DEFAULT_WORLD_CHAIN_RPC // Public RPC URL as default
698
+ });
699
+ return worldchainAccount;
700
+ };
701
+ /**
702
+ * Handles eth_sendTransaction requests by processing different transaction types
703
+ * @param params Array containing the transaction object
704
+ * @param logger Logger instance for debugging
705
+ * @returns Transaction hash or throws error
706
+ */
707
+ async function handleSendTransaction(params, logger, miniKit) {
708
+ const transaction = params[0];
709
+ // Handle USDC transfer (ERC20 transfer function)
710
+ if (transaction.data && transaction.data.startsWith('0xa9059cbb')) {
711
+ // This is a transfer(address,uint256) call - decode the parameters
712
+ const data = transaction.data.slice(10); // Remove function selector
713
+ // Extract recipient address (first 32 bytes, last 20 bytes are the address)
714
+ const recipientHex = '0x' + data.slice(24, 64);
715
+ // Extract amount (next 32 bytes)
716
+ const amountHex = '0x' + data.slice(64, 128);
717
+ const amount = BigInt(amountHex).toString();
718
+ // Validate transaction parameter
719
+ // Check for memo data (any data after the standard 128 characters)
720
+ let memo = '';
721
+ if (data.length > 128) {
722
+ const memoHex = data.slice(128);
723
+ try {
724
+ memo = Buffer.from(memoHex, 'hex').toString('utf8');
725
+ }
726
+ catch (e) {
727
+ logger.warn(`[MiniKit] Failed to decode memo data: ${e}`);
728
+ }
729
+ }
730
+ // ERC20 ABI for transfer function
731
+ const ERC20_ABI = [
732
+ {
733
+ inputs: [
734
+ { name: 'to', type: 'address' },
735
+ { name: 'amount', type: 'uint256' }
736
+ ],
737
+ name: 'transfer',
738
+ outputs: [{ name: '', type: 'bool' }],
739
+ stateMutability: 'nonpayable',
740
+ type: 'function'
741
+ }
742
+ ];
743
+ const input = {
744
+ transaction: [
745
+ {
746
+ address: transaction.to, // USDC contract address
747
+ abi: ERC20_ABI,
748
+ functionName: 'transfer',
749
+ args: [recipientHex, amount],
750
+ value: transaction.value || "0"
751
+ }
752
+ ]
753
+ };
754
+ // TODO: MiniKit doesn't have a standard way to include memo data in ERC20 transfers
755
+ // The memo is extracted and logged but not included in the transaction
756
+ if (memo) {
757
+ logger.debug(`[MiniKit] Memo "${memo}" will be lost in MiniKit transaction - consider alternative approach`);
758
+ }
759
+ const sentResult = await miniKit.commandsAsync.sendTransaction(input);
760
+ if (sentResult.finalPayload?.status === 'success') {
761
+ const transactionId = sentResult.finalPayload.transaction_id;
762
+ // Wait for the transaction to be confirmed and get the actual transaction hash
763
+ const confirmed = await waitForTransactionConfirmation(transactionId, logger, 120000); // 2 minute timeout
764
+ if (confirmed && confirmed.transactionHash) {
765
+ logger.debug(`[MiniKit] Transaction confirmed with hash: ${confirmed.transactionHash}`);
766
+ return confirmed.transactionHash; // Return the actual blockchain transaction hash
767
+ }
768
+ else {
769
+ logger.error(`[MiniKit] Transaction confirmation failed for ID: ${transactionId}`);
770
+ throw new Error(`Transaction confirmation failed. Transaction may still be pending.`);
771
+ }
772
+ }
773
+ // Enhanced error logging for debugging
774
+ const errorCode = sentResult.finalPayload?.error_code;
775
+ const simulationError = sentResult.finalPayload?.details?.simulationError;
776
+ logger.error(`[MiniKit] Transaction failed: ${JSON.stringify({
777
+ errorCode,
778
+ simulationError,
779
+ fullPayload: sentResult.finalPayload
780
+ })}`);
781
+ // Provide more user-friendly error messages
782
+ let userFriendlyError = `MiniKit sendTransaction failed: ${errorCode}`;
783
+ if (simulationError?.includes('transfer amount exceeds balance')) {
784
+ const amountUSDC = (Number(amount) / 1000000).toFixed(6);
785
+ userFriendlyError = `💳 Insufficient USDC Balance\n\n` +
786
+ `You're trying to send ${amountUSDC} USDC, but your wallet doesn't have enough funds.\n\n` +
787
+ `To complete this payment:\n` +
788
+ `• Add USDC to your World App wallet\n` +
789
+ `• Bridge USDC from another chain\n` +
790
+ `• Buy USDC directly in World App\n\n` +
791
+ `Wallet: ${transaction.from?.slice(0, 6)}...${transaction.from?.slice(-4)}`;
792
+ }
793
+ else if (simulationError) {
794
+ userFriendlyError += ` - ${simulationError}`;
795
+ }
796
+ throw new Error(userFriendlyError);
797
+ }
798
+ // Handle simple ETH transfers (no data or empty data)
799
+ if (!transaction.data || transaction.data === '0x') {
800
+ // For ETH transfers, you'd need to use the Forward contract
801
+ throw new Error('ETH transfers require Forward contract - not implemented yet');
802
+ }
803
+ // For other transaction types
804
+ throw new Error(`Unsupported transaction type. Data: ${transaction.data.slice(0, 10)}`);
805
+ }
806
+ /**
807
+ * Signs a message using MiniKit
808
+ * @param params Array containing the message to sign
809
+ * @returns The signature string
810
+ * @throws Error if signing fails
811
+ */
812
+ async function signMessageWithMiniKit(params, miniKit) {
813
+ const [message] = params;
814
+ const signResult = await miniKit.commandsAsync.signMessage({ message: message });
815
+ if (signResult?.finalPayload?.status === 'success') {
816
+ return signResult.finalPayload.signature;
817
+ }
818
+ throw new Error(`MiniKit signing failed: ${signResult?.finalPayload?.error_code}`);
819
+ }
820
+ /**
821
+ * Resolves a MiniKit transaction ID to the actual blockchain transaction hash
822
+ * using the World API
823
+ */
824
+ async function resolveTransactionHash(transactionId, logger) {
825
+ try {
826
+ const response = await fetch('/api/resolve-transaction', {
827
+ method: 'POST',
828
+ headers: {
829
+ 'Content-Type': 'application/json',
830
+ },
831
+ body: JSON.stringify({ transactionId })
832
+ });
833
+ if (!response.ok) {
834
+ const error = await response.text();
835
+ logger.error(`[WorldTransaction] API error: ${response.status} ${error}`);
836
+ return null;
837
+ }
838
+ const transaction = await response.json();
839
+ return {
840
+ transactionHash: transaction.transactionHash,
841
+ status: transaction.transactionStatus
842
+ };
843
+ }
844
+ catch (error) {
845
+ logger.error(`[WorldTransaction] Error resolving transaction: ${error}`);
846
+ return null;
847
+ }
848
+ }
849
+ /**
850
+ * Waits for a MiniKit transaction to be confirmed and returns the transaction hash
851
+ * Polls the World API until the transaction is confirmed or times out
852
+ */
853
+ async function waitForTransactionConfirmation(transactionId, logger, timeoutMs = 120000, // 2 minutes
854
+ pollIntervalMs = 2000) {
855
+ const startTime = Date.now();
856
+ while (Date.now() - startTime < timeoutMs) {
857
+ const result = await resolveTransactionHash(transactionId, logger);
858
+ if (result && result.transactionHash && result.status !== 'pending') {
859
+ return result;
860
+ }
861
+ // Wait before next poll
862
+ await new Promise(resolve => setTimeout(resolve, pollIntervalMs));
863
+ }
864
+ logger.warn(`[WorldTransaction] Timeout waiting for transaction confirmation: ${transactionId}`);
865
+ return null;
866
+ }
867
+
868
+ export { MainWalletPaymentMaker, IntermediaryCache as PermissionCache, WorldchainAccount, WorldchainPaymentMaker, createMiniKitWorldchainAccount };
869
+ //# sourceMappingURL=index.js.map