@atxp/polygon 0.8.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 +497 -0
- package/dist/cache.d.ts +19 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/cache.js +11 -0
- package/dist/cache.js.map +1 -0
- package/dist/directWalletPaymentMaker.d.ts +35 -0
- package/dist/directWalletPaymentMaker.d.ts.map +1 -0
- package/dist/directWalletPaymentMaker.js +143 -0
- package/dist/directWalletPaymentMaker.js.map +1 -0
- package/dist/index.cjs +458 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +145 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +446 -0
- package/dist/index.js.map +1 -0
- package/dist/polygonBrowserAccount.d.ts +40 -0
- package/dist/polygonBrowserAccount.d.ts.map +1 -0
- package/dist/polygonBrowserAccount.js +70 -0
- package/dist/polygonBrowserAccount.js.map +1 -0
- package/dist/polygonServerAccount.d.ts +35 -0
- package/dist/polygonServerAccount.d.ts.map +1 -0
- package/dist/polygonServerAccount.js +78 -0
- package/dist/polygonServerAccount.js.map +1 -0
- package/dist/serverPaymentMaker.d.ts +29 -0
- package/dist/serverPaymentMaker.d.ts.map +1 -0
- package/dist/serverPaymentMaker.js +173 -0
- package/dist/serverPaymentMaker.js.map +1 -0
- package/dist/smartWalletHelpers.d.ts +18 -0
- package/dist/smartWalletHelpers.d.ts.map +1 -0
- package/dist/smartWalletHelpers.js +93 -0
- package/dist/smartWalletHelpers.js.map +1 -0
- package/dist/smartWalletPaymentMaker.d.ts +29 -0
- package/dist/smartWalletPaymentMaker.d.ts.map +1 -0
- package/dist/smartWalletPaymentMaker.js +172 -0
- package/dist/smartWalletPaymentMaker.js.map +1 -0
- package/dist/spendPermissionShim.d.ts +19 -0
- package/dist/spendPermissionShim.d.ts.map +1 -0
- package/dist/spendPermissionShim.js +129 -0
- package/dist/spendPermissionShim.js.map +1 -0
- package/dist/testHelpers.d.ts +17 -0
- package/dist/testHelpers.d.ts.map +1 -0
- package/dist/types.d.ts +11 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +57 -0
package/README.md
ADDED
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
# @atxp/polygon
|
|
2
|
+
|
|
3
|
+
ATXP for Polygon - Enable seamless payments in Polygon applications with smart wallet and direct wallet support.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
`@atxp/polygon` provides a complete solution for integrating ATXP (Autonomous Transaction eXecution Protocol) payments into Polygon applications. It handles Polygon-specific wallet interactions, USDC transfers, and smart wallet integration while abstracting away the complexity of blockchain transactions.
|
|
8
|
+
|
|
9
|
+
The package supports three account types:
|
|
10
|
+
|
|
11
|
+
### Browser-Based Accounts (`PolygonBrowserAccount`)
|
|
12
|
+
- **Smart Wallet Mode** (`SmartWalletPaymentMaker`): Uses Coinbase CDP for ephemeral smart wallets with account abstraction and gasless transactions (default, best UX)
|
|
13
|
+
- **Direct Wallet Mode** (`DirectWalletPaymentMaker`): Direct wallet integration where users sign each transaction
|
|
14
|
+
|
|
15
|
+
### Server/CLI Accounts (`PolygonServerAccount`)
|
|
16
|
+
- **Server Mode** (`ServerPaymentMaker`): For backend services and CLI tools using private keys directly
|
|
17
|
+
|
|
18
|
+
## Support
|
|
19
|
+
|
|
20
|
+
For detailed API documentation, configuration options, and advanced usage patterns, please refer to our [complete documentation](https://docs.atxp.ai/).
|
|
21
|
+
|
|
22
|
+
Have questions or need help? Join our [Discord community](https://discord.gg/FuJXHhe9aW) - we're happy to help!
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm install @atxp/polygon
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Peer Dependencies
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
npm install viem
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Quick Start
|
|
37
|
+
|
|
38
|
+
### Browser Usage
|
|
39
|
+
|
|
40
|
+
#### Option 1: Smart Wallet Mode (Recommended - Best UX)
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { PolygonBrowserAccount } from '@atxp/polygon';
|
|
44
|
+
|
|
45
|
+
const account = await PolygonBrowserAccount.initialize({
|
|
46
|
+
provider: window.ethereum, // or any EIP-1193 provider
|
|
47
|
+
walletAddress: '0x1234...', // User's wallet address
|
|
48
|
+
useEphemeralWallet: true, // Smart wallet with gasless transactions
|
|
49
|
+
|
|
50
|
+
// Spend permission parameters
|
|
51
|
+
allowance: BigInt('10000000'), // 10 USDC (in 6 decimals)
|
|
52
|
+
periodInDays: 30, // Permission valid for 30 days
|
|
53
|
+
periodStart: Math.floor(Date.now() / 1000), // Start time in seconds
|
|
54
|
+
|
|
55
|
+
// Optional configuration
|
|
56
|
+
customRpcUrl: 'https://polygon-rpc.com', // Custom RPC endpoint
|
|
57
|
+
logger: console // Logger instance
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
#### Option 2: Direct Wallet Mode
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
import { PolygonBrowserAccount } from '@atxp/polygon';
|
|
65
|
+
|
|
66
|
+
const account = await PolygonBrowserAccount.initialize({
|
|
67
|
+
provider: window.ethereum,
|
|
68
|
+
walletAddress: '0x1234...',
|
|
69
|
+
useEphemeralWallet: false, // User signs each transaction
|
|
70
|
+
allowance: BigInt('10000000'),
|
|
71
|
+
periodInDays: 30,
|
|
72
|
+
periodStart: Math.floor(Date.now() / 1000)
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Server/CLI Usage
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
import { PolygonServerAccount } from '@atxp/polygon';
|
|
80
|
+
|
|
81
|
+
const account = new PolygonServerAccount(
|
|
82
|
+
'https://polygon-rpc.com', // RPC URL
|
|
83
|
+
'0x_your_private_key', // Private key
|
|
84
|
+
137 // Chain ID (137 = Polygon mainnet)
|
|
85
|
+
);
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### 2. Set up ATXP Client
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { atxpClient } from '@atxp/client';
|
|
92
|
+
|
|
93
|
+
const client = await atxpClient({
|
|
94
|
+
account,
|
|
95
|
+
mcpServer: 'https://your-mcp-server.com',
|
|
96
|
+
onPayment: async ({ payment }) => {
|
|
97
|
+
console.log('Payment successful:', payment);
|
|
98
|
+
},
|
|
99
|
+
onPaymentFailure: async ({ payment, error }) => {
|
|
100
|
+
console.error('Payment failed:', payment, error);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### 3. Make MCP Tool Calls
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
const result = await client.callTool({
|
|
109
|
+
name: 'your_tool_name',
|
|
110
|
+
arguments: { prompt: 'Generate an image' }
|
|
111
|
+
});
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## React Integration Example
|
|
115
|
+
|
|
116
|
+
Here's how to integrate ATXP Polygon into a React application:
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
import { PolygonBrowserAccount } from '@atxp/polygon';
|
|
120
|
+
import { atxpClient } from '@atxp/client';
|
|
121
|
+
import { useCallback, useEffect, useState } from 'react';
|
|
122
|
+
|
|
123
|
+
export const AtxpProvider = ({ children }) => {
|
|
124
|
+
const [atxpAccount, setAtxpAccount] = useState<PolygonBrowserAccount | null>(null);
|
|
125
|
+
const [client, setClient] = useState(null);
|
|
126
|
+
|
|
127
|
+
const loadAccount = useCallback(async (walletAddress: string) => {
|
|
128
|
+
const account = await PolygonBrowserAccount.initialize({
|
|
129
|
+
provider: window.ethereum,
|
|
130
|
+
walletAddress,
|
|
131
|
+
useEphemeralWallet: true,
|
|
132
|
+
allowance: BigInt('10000000'), // 10 USDC
|
|
133
|
+
periodInDays: 30,
|
|
134
|
+
periodStart: Math.floor(Date.now() / 1000)
|
|
135
|
+
});
|
|
136
|
+
setAtxpAccount(account);
|
|
137
|
+
|
|
138
|
+
const atxpClient = await atxpClient({
|
|
139
|
+
account,
|
|
140
|
+
mcpServer: 'https://your-mcp-server.com',
|
|
141
|
+
onPayment: async ({ payment }) => {
|
|
142
|
+
console.log('Payment successful:', payment);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
setClient(atxpClient);
|
|
146
|
+
}, []);
|
|
147
|
+
|
|
148
|
+
// Initialize when wallet connects
|
|
149
|
+
useEffect(() => {
|
|
150
|
+
if (walletAddress && !atxpAccount) {
|
|
151
|
+
loadAccount(walletAddress);
|
|
152
|
+
}
|
|
153
|
+
}, [walletAddress, atxpAccount, loadAccount]);
|
|
154
|
+
|
|
155
|
+
const callMcpTool = useCallback(async (name: string, args: any) => {
|
|
156
|
+
if (!client) return null;
|
|
157
|
+
|
|
158
|
+
const response = await client.callTool({
|
|
159
|
+
name,
|
|
160
|
+
arguments: args
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
return response;
|
|
164
|
+
}, [client]);
|
|
165
|
+
|
|
166
|
+
return (
|
|
167
|
+
<AtxpContext.Provider value={{ atxpAccount, callMcpTool }}>
|
|
168
|
+
{children}
|
|
169
|
+
</AtxpContext.Provider>
|
|
170
|
+
);
|
|
171
|
+
};
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## API Reference
|
|
175
|
+
|
|
176
|
+
### `PolygonBrowserAccount.initialize(options)`
|
|
177
|
+
|
|
178
|
+
Creates and initializes a browser-based Polygon account with smart wallet or direct wallet support.
|
|
179
|
+
|
|
180
|
+
#### Parameters
|
|
181
|
+
|
|
182
|
+
- `provider: Eip1193Provider` - EIP-1193 compatible provider (e.g., window.ethereum)
|
|
183
|
+
- `walletAddress: string` - The user's wallet address
|
|
184
|
+
- `useEphemeralWallet?: boolean` - Whether to use smart wallet (true) or direct wallet (false). Default: true
|
|
185
|
+
- `allowance: bigint` - Maximum USDC amount that can be spent (in 6 decimals)
|
|
186
|
+
- `periodInDays: number` - Permission validity period in days
|
|
187
|
+
- `periodStart: number` - Permission start time in Unix seconds
|
|
188
|
+
- `customRpcUrl?: string` - Optional custom RPC URL (defaults to public Polygon RPC)
|
|
189
|
+
- `chainId?: number` - Chain ID (137 for mainnet, 80002 for Amoy testnet)
|
|
190
|
+
- `logger?: Logger` - Optional logger for debugging
|
|
191
|
+
|
|
192
|
+
#### Returns
|
|
193
|
+
|
|
194
|
+
`Promise<PolygonBrowserAccount>` - Initialized browser Polygon account
|
|
195
|
+
|
|
196
|
+
### `PolygonServerAccount`
|
|
197
|
+
|
|
198
|
+
Server-side/CLI account for backend services.
|
|
199
|
+
|
|
200
|
+
#### Constructor
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
new PolygonServerAccount(
|
|
204
|
+
rpcUrl: string,
|
|
205
|
+
privateKey: string,
|
|
206
|
+
chainId: number = 137
|
|
207
|
+
)
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
#### Key Features
|
|
211
|
+
|
|
212
|
+
- Direct private key signing (no browser required)
|
|
213
|
+
- ES256K JWT authentication
|
|
214
|
+
- Simple USDC transfers
|
|
215
|
+
- Works in Node.js, CLI tools, and backend services
|
|
216
|
+
|
|
217
|
+
### `PolygonBrowserAccount`
|
|
218
|
+
|
|
219
|
+
Browser-based account class that handles Polygon wallet interactions.
|
|
220
|
+
|
|
221
|
+
#### Key Methods
|
|
222
|
+
|
|
223
|
+
- `getSources(): Promise<Source[]>` - Get wallet addresses and their sources
|
|
224
|
+
- Payment makers handle the actual payment processing
|
|
225
|
+
|
|
226
|
+
#### Key Features
|
|
227
|
+
|
|
228
|
+
- **USDC Transfers**: Handles native USDC transfers on Polygon (0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359)
|
|
229
|
+
- **Message Signing**: Signs JWTs using EIP-1271 for smart wallets or standard signing for direct wallets
|
|
230
|
+
- **Smart Wallet Integration**: Uses Coinbase CDP for account abstraction and gasless transactions
|
|
231
|
+
- **Spend Permissions**: Manages recurring payment permissions with ERC-20 approvals
|
|
232
|
+
- **Automatic Deployment**: Deploys smart wallet before first use (smart wallet mode only)
|
|
233
|
+
|
|
234
|
+
### `SmartWalletPaymentMaker`
|
|
235
|
+
|
|
236
|
+
Browser-based payment maker using ephemeral smart wallets with account abstraction.
|
|
237
|
+
|
|
238
|
+
#### Features
|
|
239
|
+
|
|
240
|
+
- Gasless transactions via Coinbase CDP bundler
|
|
241
|
+
- Batched transaction support
|
|
242
|
+
- Automatic memo appending to transfers
|
|
243
|
+
- User operation handling
|
|
244
|
+
- **Best for**: Production browser apps (best UX)
|
|
245
|
+
|
|
246
|
+
### `DirectWalletPaymentMaker`
|
|
247
|
+
|
|
248
|
+
Browser-based payment maker for direct wallet signing.
|
|
249
|
+
|
|
250
|
+
#### Features
|
|
251
|
+
|
|
252
|
+
- Direct wallet signing for each transaction
|
|
253
|
+
- Support for Coinbase Wallet and standard wallets
|
|
254
|
+
- Transaction confirmation tracking
|
|
255
|
+
- Flexible message signing for different wallet providers
|
|
256
|
+
- **Best for**: Users who want full control, compatibility mode
|
|
257
|
+
|
|
258
|
+
### `ServerPaymentMaker`
|
|
259
|
+
|
|
260
|
+
Server-side payment maker using direct private key signing.
|
|
261
|
+
|
|
262
|
+
#### Features
|
|
263
|
+
|
|
264
|
+
- ES256K JWT generation
|
|
265
|
+
- Direct USDC ERC-20 transfers
|
|
266
|
+
- Balance checking
|
|
267
|
+
- Transaction confirmation
|
|
268
|
+
- **Best for**: Backend services, CLI tools, testing
|
|
269
|
+
|
|
270
|
+
### Caching System
|
|
271
|
+
|
|
272
|
+
```typescript
|
|
273
|
+
import { IntermediaryCache } from '@atxp/polygon';
|
|
274
|
+
|
|
275
|
+
// Browser-based cache for persistent permission storage
|
|
276
|
+
const cache = new IntermediaryCache();
|
|
277
|
+
|
|
278
|
+
// Store permission
|
|
279
|
+
await cache.set('cache-key', {
|
|
280
|
+
privateKey: '0x...',
|
|
281
|
+
permission: { /* spend permission data */ }
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
// Retrieve permission
|
|
285
|
+
const intermediary = await cache.get('cache-key');
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
## Configuration
|
|
289
|
+
|
|
290
|
+
### Default Configuration
|
|
291
|
+
|
|
292
|
+
The package comes with sensible defaults:
|
|
293
|
+
|
|
294
|
+
- **Chain**: Polygon Mainnet (Chain ID: 137)
|
|
295
|
+
- **RPC**: Public Polygon RPC endpoint (https://polygon-rpc.com)
|
|
296
|
+
- **USDC Address**: Native USDC on Polygon (0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359)
|
|
297
|
+
- **Bundler**: Coinbase CDP Polygon bundler
|
|
298
|
+
- **Paymaster**: Coinbase CDP paymaster (gasless transactions)
|
|
299
|
+
|
|
300
|
+
### Smart Wallet Mode (Browser - Default, Recommended)
|
|
301
|
+
|
|
302
|
+
```typescript
|
|
303
|
+
const account = await PolygonBrowserAccount.initialize({
|
|
304
|
+
provider: window.ethereum,
|
|
305
|
+
walletAddress: '0x1234...',
|
|
306
|
+
useEphemeralWallet: true, // default - uses SmartWalletPaymentMaker
|
|
307
|
+
allowance: BigInt('10000000'),
|
|
308
|
+
periodInDays: 30,
|
|
309
|
+
periodStart: Math.floor(Date.now() / 1000)
|
|
310
|
+
});
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
**How it works:**
|
|
314
|
+
1. Creates a new ephemeral smart wallet using Coinbase CDP
|
|
315
|
+
2. Requests ERC-20 approval for the smart wallet to spend USDC
|
|
316
|
+
3. Deploys the smart wallet on first use
|
|
317
|
+
4. Stores permission and wallet in cache for subsequent use
|
|
318
|
+
5. All transactions are gasless via Coinbase paymaster
|
|
319
|
+
|
|
320
|
+
**Benefits:**
|
|
321
|
+
- Single approval for multiple transactions
|
|
322
|
+
- Gasless transactions (no native POL needed)
|
|
323
|
+
- Improved UX with minimal user prompts
|
|
324
|
+
|
|
325
|
+
### Direct Wallet Mode (Browser)
|
|
326
|
+
|
|
327
|
+
```typescript
|
|
328
|
+
const account = await PolygonBrowserAccount.initialize({
|
|
329
|
+
provider: window.ethereum,
|
|
330
|
+
walletAddress: '0x1234...',
|
|
331
|
+
useEphemeralWallet: false, // uses DirectWalletPaymentMaker
|
|
332
|
+
allowance: BigInt('10000000'),
|
|
333
|
+
periodInDays: 30,
|
|
334
|
+
periodStart: Math.floor(Date.now() / 1000)
|
|
335
|
+
});
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
**How it works:**
|
|
339
|
+
1. Each transaction requires user approval in their wallet
|
|
340
|
+
2. JWT signing also requires user approval
|
|
341
|
+
3. Direct USDC transfers from user's wallet
|
|
342
|
+
4. User pays gas fees in POL
|
|
343
|
+
|
|
344
|
+
**When to use:**
|
|
345
|
+
- Users want full control over each transaction
|
|
346
|
+
- Development/testing environments
|
|
347
|
+
- Compatibility with wallets that don't support smart contracts
|
|
348
|
+
|
|
349
|
+
### Server/CLI Mode
|
|
350
|
+
|
|
351
|
+
```typescript
|
|
352
|
+
import { PolygonServerAccount } from '@atxp/polygon';
|
|
353
|
+
|
|
354
|
+
const account = new PolygonServerAccount(
|
|
355
|
+
'https://polygon-rpc.com', // RPC URL
|
|
356
|
+
'0x_your_private_key', // Private key
|
|
357
|
+
137 // 137 = Polygon mainnet, 80002 = Amoy testnet
|
|
358
|
+
);
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
**How it works:**
|
|
362
|
+
1. Direct private key signing (no browser or wallet provider needed)
|
|
363
|
+
2. ES256K JWT authentication
|
|
364
|
+
3. Simple USDC ERC-20 transfers
|
|
365
|
+
4. Account pays gas fees in POL
|
|
366
|
+
|
|
367
|
+
**When to use:**
|
|
368
|
+
- Backend services and APIs
|
|
369
|
+
- CLI tools and scripts
|
|
370
|
+
- Testing and automation
|
|
371
|
+
- Server-side payment processing
|
|
372
|
+
|
|
373
|
+
### Custom RPC Endpoint
|
|
374
|
+
|
|
375
|
+
#### Browser
|
|
376
|
+
|
|
377
|
+
```typescript
|
|
378
|
+
const account = await PolygonBrowserAccount.initialize({
|
|
379
|
+
provider: window.ethereum,
|
|
380
|
+
walletAddress: '0x1234...',
|
|
381
|
+
customRpcUrl: 'https://your-polygon-rpc.com',
|
|
382
|
+
useEphemeralWallet: true,
|
|
383
|
+
allowance: BigInt('10000000'),
|
|
384
|
+
periodInDays: 30,
|
|
385
|
+
periodStart: Math.floor(Date.now() / 1000)
|
|
386
|
+
});
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
#### Server/CLI
|
|
390
|
+
|
|
391
|
+
```typescript
|
|
392
|
+
const account = new PolygonServerAccount(
|
|
393
|
+
'https://your-polygon-rpc.com', // Custom RPC
|
|
394
|
+
'0x_your_private_key',
|
|
395
|
+
137
|
|
396
|
+
);
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
## Error Handling
|
|
400
|
+
|
|
401
|
+
The library provides detailed error handling for common scenarios:
|
|
402
|
+
|
|
403
|
+
### Insufficient Balance
|
|
404
|
+
|
|
405
|
+
```typescript
|
|
406
|
+
try {
|
|
407
|
+
await client.callTool({ name: 'expensive_tool', arguments: {} });
|
|
408
|
+
} catch (error) {
|
|
409
|
+
if (error.message.includes('insufficient funds') ||
|
|
410
|
+
error.message.includes('transfer amount exceeds balance')) {
|
|
411
|
+
// Handle insufficient USDC balance
|
|
412
|
+
console.log('Please add USDC to your wallet');
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
### Transaction Failures
|
|
418
|
+
|
|
419
|
+
```typescript
|
|
420
|
+
const client = await atxpClient({
|
|
421
|
+
account,
|
|
422
|
+
mcpServer: 'https://your-server.com',
|
|
423
|
+
onPaymentFailure: async ({ payment, error }) => {
|
|
424
|
+
if (error.message.includes('Transaction receipt')) {
|
|
425
|
+
// Payment verification failed - transaction may still be pending
|
|
426
|
+
console.log('Payment verification failed, please wait and try again');
|
|
427
|
+
} else if (error.message.includes('User rejected')) {
|
|
428
|
+
// User rejected the transaction in their wallet
|
|
429
|
+
console.log('Transaction was cancelled');
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
### Approval/Permission Errors
|
|
436
|
+
|
|
437
|
+
```typescript
|
|
438
|
+
try {
|
|
439
|
+
const account = await PolygonAccount.initialize({
|
|
440
|
+
provider: window.ethereum,
|
|
441
|
+
walletAddress: '0x1234...',
|
|
442
|
+
useEphemeralWallet: true,
|
|
443
|
+
allowance: BigInt('10000000'),
|
|
444
|
+
periodInDays: 30,
|
|
445
|
+
periodStart: Math.floor(Date.now() / 1000)
|
|
446
|
+
});
|
|
447
|
+
} catch (error) {
|
|
448
|
+
if (error.message.includes('User rejected')) {
|
|
449
|
+
// User rejected the ERC-20 approval
|
|
450
|
+
console.log('Please approve USDC spending to continue');
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
## Supported Networks
|
|
456
|
+
|
|
457
|
+
- **Polygon Mainnet** (Chain ID: 137) - Production
|
|
458
|
+
- **Polygon Amoy** (Chain ID: 80002) - Testnet
|
|
459
|
+
|
|
460
|
+
## Requirements
|
|
461
|
+
|
|
462
|
+
### Browser Usage
|
|
463
|
+
- Modern browser environment
|
|
464
|
+
- EIP-1193 compatible wallet provider (e.g., MetaMask, Coinbase Wallet, WalletConnect)
|
|
465
|
+
- USDC balance on Polygon
|
|
466
|
+
- POL balance for gas fees (only in direct wallet mode; smart wallet mode is gasless)
|
|
467
|
+
|
|
468
|
+
### Server/CLI Usage
|
|
469
|
+
- Node.js 16+
|
|
470
|
+
- Private key with USDC and POL balance
|
|
471
|
+
|
|
472
|
+
## Technical Details
|
|
473
|
+
|
|
474
|
+
### Smart Wallet Architecture
|
|
475
|
+
|
|
476
|
+
The ephemeral wallet implementation uses:
|
|
477
|
+
- **Coinbase CDP SDK** for smart wallet creation and management
|
|
478
|
+
- **ERC-4337 Account Abstraction** for gasless transactions
|
|
479
|
+
- **Coinbase Bundler** for user operation submission
|
|
480
|
+
- **Coinbase Paymaster** for gas sponsorship
|
|
481
|
+
|
|
482
|
+
### Spend Permission System
|
|
483
|
+
|
|
484
|
+
Instead of native spend permissions (which Polygon doesn't support), this package uses:
|
|
485
|
+
- **ERC-20 Approve/TransferFrom** pattern for permission management
|
|
486
|
+
- **Approval caching** to avoid repeated approval requests
|
|
487
|
+
- **Permission parameters** stored in cache for validation
|
|
488
|
+
|
|
489
|
+
### Authentication
|
|
490
|
+
|
|
491
|
+
- **Smart Wallet Mode** (`SmartWalletPaymentMaker`): Uses EIP-1271 smart contract signature verification
|
|
492
|
+
- **Direct Wallet Mode** (`DirectWalletPaymentMaker`): Uses standard Ethereum message signing with special handling for Coinbase Wallet
|
|
493
|
+
- **Server Mode** (`ServerPaymentMaker`): Uses ES256K JWT with direct private key signing
|
|
494
|
+
|
|
495
|
+
## License
|
|
496
|
+
|
|
497
|
+
See the main ATXP SDK repository for license information.
|
package/dist/cache.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { SpendPermission } from './types.js';
|
|
2
|
+
import { Hex } from '@atxp/client';
|
|
3
|
+
import { type ICache, JsonCache, BrowserCache, MemoryCache } from '@atxp/common';
|
|
4
|
+
/**
|
|
5
|
+
* Stored permission data structure
|
|
6
|
+
*/
|
|
7
|
+
export interface Intermediary {
|
|
8
|
+
/** Ephemeral wallet private key */
|
|
9
|
+
privateKey: Hex;
|
|
10
|
+
/** Spend permission from Polygon */
|
|
11
|
+
permission: SpendPermission;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Type-safe cache wrapper for permission data
|
|
15
|
+
*/
|
|
16
|
+
export declare class IntermediaryCache extends JsonCache<Intermediary> {
|
|
17
|
+
}
|
|
18
|
+
export { type ICache, BrowserCache, MemoryCache };
|
|
19
|
+
//# sourceMappingURL=cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AACnC,OAAO,EAAE,KAAK,MAAM,EAAE,SAAS,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAEjF;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,mCAAmC;IACnC,UAAU,EAAE,GAAG,CAAC;IAChB,oCAAoC;IACpC,UAAU,EAAE,eAAe,CAAC;CAC7B;AAED;;GAEG;AACH,qBAAa,iBAAkB,SAAQ,SAAS,CAAC,YAAY,CAAC;CAAG;AAGjE,OAAO,EAAE,KAAK,MAAM,EAAE,YAAY,EAAE,WAAW,EAAE,CAAC"}
|
package/dist/cache.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { JsonCache } from '@atxp/common';
|
|
2
|
+
export { BrowserCache, MemoryCache } from '@atxp/common';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Type-safe cache wrapper for permission data
|
|
6
|
+
*/
|
|
7
|
+
class IntermediaryCache extends JsonCache {
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export { IntermediaryCache };
|
|
11
|
+
//# sourceMappingURL=cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.js","sources":["../src/cache.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAcA;;AAEG;AACG,MAAO,iBAAkB,SAAQ,SAAuB,CAAA;AAAG;;;;"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import BigNumber from 'bignumber.js';
|
|
2
|
+
import { Logger, Currency, PaymentMaker, AccountId, PaymentIdentifier, Destination } from '@atxp/common';
|
|
3
|
+
export type MainWalletProvider = {
|
|
4
|
+
request: (params: {
|
|
5
|
+
method: string;
|
|
6
|
+
params?: any[];
|
|
7
|
+
}) => Promise<any>;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Browser-based payment maker using direct wallet signing.
|
|
11
|
+
* Each transaction requires user approval in their wallet.
|
|
12
|
+
* User pays gas fees in POL.
|
|
13
|
+
*/
|
|
14
|
+
export declare class DirectWalletPaymentMaker implements PaymentMaker {
|
|
15
|
+
private walletAddress;
|
|
16
|
+
private provider;
|
|
17
|
+
private logger;
|
|
18
|
+
private chainId;
|
|
19
|
+
private usdcAddress;
|
|
20
|
+
constructor(walletAddress: string, provider: MainWalletProvider, logger?: Logger, chainId?: number);
|
|
21
|
+
getSourceAddress(_params: {
|
|
22
|
+
amount: BigNumber;
|
|
23
|
+
currency: Currency;
|
|
24
|
+
receiver: string;
|
|
25
|
+
memo: string;
|
|
26
|
+
}): string;
|
|
27
|
+
generateJWT(payload: {
|
|
28
|
+
paymentRequestId: string;
|
|
29
|
+
codeChallenge: string;
|
|
30
|
+
accountId?: AccountId | null;
|
|
31
|
+
}): Promise<string>;
|
|
32
|
+
makePayment(destinations: Destination[], _memo: string, _paymentRequestId?: string): Promise<PaymentIdentifier | null>;
|
|
33
|
+
private waitForTransactionConfirmations;
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=directWalletPaymentMaker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"directWalletPaymentMaker.d.ts","sourceRoot":"","sources":["../src/directWalletPaymentMaker.ts"],"names":[],"mappings":"AAGA,OAAO,SAAS,MAAM,cAAc,CAAC;AACrC,OAAO,EAAiB,MAAM,EAAE,QAAQ,EAAE,YAAY,EAAE,SAAS,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAQxH,MAAM,MAAM,kBAAkB,GAAG;IAC/B,OAAO,EAAE,CAAC,MAAM,EAAE;QAChB,MAAM,EAAE,MAAM,CAAC;QACf,MAAM,CAAC,EAAE,GAAG,EAAE,CAAC;KAChB,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;CACpB,CAAC;AAEF;;;;GAIG;AACH,qBAAa,wBAAyB,YAAW,YAAY;IAMzD,OAAO,CAAC,aAAa;IACrB,OAAO,CAAC,QAAQ;IANlB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,WAAW,CAAS;gBAGlB,aAAa,EAAE,MAAM,EACrB,QAAQ,EAAE,kBAAkB,EACpC,MAAM,CAAC,EAAE,MAAM,EACf,OAAO,GAAE,MAAmB;IAO9B,gBAAgB,CAAC,OAAO,EAAE;QAAC,MAAM,EAAE,SAAS,CAAC;QAAC,QAAQ,EAAE,QAAQ,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAC,GAAG,MAAM;IAIpG,WAAW,CAAC,OAAO,EAAE;QACzB,gBAAgB,EAAE,MAAM,CAAC;QACzB,aAAa,EAAE,MAAM,CAAC;QACtB,SAAS,CAAC,EAAE,SAAS,GAAG,IAAI,CAAC;KAC9B,GAAG,OAAO,CAAC,MAAM,CAAC;IAoCb,WAAW,CACf,YAAY,EAAE,WAAW,EAAE,EAC3B,KAAK,EAAE,MAAM,EACb,iBAAiB,CAAC,EAAE,MAAM,GACzB,OAAO,CAAC,iBAAiB,GAAG,IAAI,CAAC;YAgEtB,+BAA+B;CA4C9C"}
|