@agirails/sdk 2.5.3 → 2.5.5
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/ACTPClient.d.ts +18 -0
- package/dist/ACTPClient.d.ts.map +1 -1
- package/dist/ACTPClient.js +72 -23
- package/dist/ACTPClient.js.map +1 -1
- package/dist/adapters/BasicAdapter.d.ts +15 -0
- package/dist/adapters/BasicAdapter.d.ts.map +1 -1
- package/dist/adapters/BasicAdapter.js +33 -4
- package/dist/adapters/BasicAdapter.js.map +1 -1
- package/dist/adapters/StandardAdapter.d.ts +20 -3
- package/dist/adapters/StandardAdapter.d.ts.map +1 -1
- package/dist/adapters/StandardAdapter.js +90 -12
- package/dist/adapters/StandardAdapter.js.map +1 -1
- package/dist/cli/commands/publish.js +16 -4
- package/dist/cli/commands/publish.js.map +1 -1
- package/dist/cli/commands/register.js +16 -4
- package/dist/cli/commands/register.js.map +1 -1
- package/dist/cli/commands/tx.js +31 -3
- package/dist/cli/commands/tx.js.map +1 -1
- package/dist/config/networks.d.ts +10 -2
- package/dist/config/networks.d.ts.map +1 -1
- package/dist/config/networks.js +31 -22
- package/dist/config/networks.js.map +1 -1
- package/dist/level0/request.d.ts.map +1 -1
- package/dist/level0/request.js +2 -1
- package/dist/level0/request.js.map +1 -1
- package/dist/runtime/BlockchainRuntime.d.ts.map +1 -1
- package/dist/runtime/BlockchainRuntime.js +11 -5
- package/dist/runtime/BlockchainRuntime.js.map +1 -1
- package/dist/utils/IPFSClient.d.ts +3 -1
- package/dist/utils/IPFSClient.d.ts.map +1 -1
- package/dist/utils/IPFSClient.js +27 -7
- package/dist/utils/IPFSClient.js.map +1 -1
- package/dist/wallet/AutoWalletProvider.d.ts +11 -1
- package/dist/wallet/AutoWalletProvider.d.ts.map +1 -1
- package/dist/wallet/AutoWalletProvider.js +84 -19
- package/dist/wallet/AutoWalletProvider.js.map +1 -1
- package/dist/wallet/IWalletProvider.d.ts +34 -0
- package/dist/wallet/IWalletProvider.d.ts.map +1 -1
- package/dist/wallet/SmartWalletRouter.d.ts +128 -0
- package/dist/wallet/SmartWalletRouter.d.ts.map +1 -0
- package/dist/wallet/SmartWalletRouter.js +248 -0
- package/dist/wallet/SmartWalletRouter.js.map +1 -0
- package/dist/wallet/aa/DualNonceManager.d.ts +26 -1
- package/dist/wallet/aa/DualNonceManager.d.ts.map +1 -1
- package/dist/wallet/aa/DualNonceManager.js +140 -6
- package/dist/wallet/aa/DualNonceManager.js.map +1 -1
- package/package.json +3 -6
- package/src/ACTPClient.ts +0 -1579
- package/src/abi/ACTPKernel.json +0 -1356
- package/src/abi/AgentRegistry.json +0 -915
- package/src/abi/ERC20.json +0 -40
- package/src/abi/EscrowVault.json +0 -134
- package/src/abi/IdentityRegistry.json +0 -316
- package/src/adapters/AdapterRegistry.ts +0 -173
- package/src/adapters/AdapterRouter.ts +0 -416
- package/src/adapters/BaseAdapter.ts +0 -498
- package/src/adapters/BasicAdapter.ts +0 -514
- package/src/adapters/IAdapter.ts +0 -292
- package/src/adapters/StandardAdapter.ts +0 -555
- package/src/adapters/X402Adapter.ts +0 -731
- package/src/adapters/index.ts +0 -60
- package/src/builders/DeliveryProofBuilder.ts +0 -327
- package/src/builders/QuoteBuilder.ts +0 -483
- package/src/builders/index.ts +0 -17
- package/src/cli/commands/balance.ts +0 -110
- package/src/cli/commands/batch.ts +0 -487
- package/src/cli/commands/config.ts +0 -231
- package/src/cli/commands/deploy-check.ts +0 -364
- package/src/cli/commands/deploy-env.ts +0 -120
- package/src/cli/commands/diff.ts +0 -141
- package/src/cli/commands/init.ts +0 -469
- package/src/cli/commands/mint.ts +0 -116
- package/src/cli/commands/pay.ts +0 -113
- package/src/cli/commands/publish.ts +0 -475
- package/src/cli/commands/pull.ts +0 -124
- package/src/cli/commands/register.ts +0 -247
- package/src/cli/commands/simulate.ts +0 -345
- package/src/cli/commands/time.ts +0 -302
- package/src/cli/commands/tx.ts +0 -448
- package/src/cli/commands/watch.ts +0 -211
- package/src/cli/index.ts +0 -134
- package/src/cli/utils/client.ts +0 -252
- package/src/cli/utils/config.ts +0 -389
- package/src/cli/utils/output.ts +0 -465
- package/src/cli/utils/wallet.ts +0 -109
- package/src/config/agirailsmd.ts +0 -262
- package/src/config/networks.ts +0 -275
- package/src/config/pendingPublish.ts +0 -237
- package/src/config/publishPipeline.ts +0 -359
- package/src/config/syncOperations.ts +0 -279
- package/src/erc8004/ERC8004Bridge.ts +0 -462
- package/src/erc8004/ReputationReporter.ts +0 -468
- package/src/erc8004/index.ts +0 -61
- package/src/errors/index.ts +0 -427
- package/src/index.ts +0 -364
- package/src/level0/Provider.ts +0 -117
- package/src/level0/ServiceDirectory.ts +0 -131
- package/src/level0/index.ts +0 -10
- package/src/level0/provide.ts +0 -132
- package/src/level0/request.ts +0 -432
- package/src/level1/Agent.ts +0 -1426
- package/src/level1/index.ts +0 -10
- package/src/level1/pricing/PriceCalculator.ts +0 -255
- package/src/level1/pricing/PricingStrategy.ts +0 -198
- package/src/level1/types/Job.ts +0 -179
- package/src/level1/types/Options.ts +0 -291
- package/src/level1/types/index.ts +0 -8
- package/src/protocol/ACTPKernel.ts +0 -808
- package/src/protocol/AgentRegistry.ts +0 -559
- package/src/protocol/DIDManager.ts +0 -629
- package/src/protocol/DIDResolver.ts +0 -554
- package/src/protocol/EASHelper.ts +0 -378
- package/src/protocol/EscrowVault.ts +0 -255
- package/src/protocol/EventMonitor.ts +0 -204
- package/src/protocol/MessageSigner.ts +0 -510
- package/src/protocol/ProofGenerator.ts +0 -339
- package/src/protocol/QuoteBuilder.ts +0 -15
- package/src/registry/AgentRegistryClient.ts +0 -202
- package/src/runtime/BlockchainRuntime.ts +0 -1015
- package/src/runtime/IACTPRuntime.ts +0 -306
- package/src/runtime/MockRuntime.ts +0 -1298
- package/src/runtime/MockStateManager.ts +0 -577
- package/src/runtime/index.ts +0 -25
- package/src/runtime/types/MockState.ts +0 -237
- package/src/storage/ArchiveBundleBuilder.ts +0 -561
- package/src/storage/ArweaveClient.ts +0 -946
- package/src/storage/FilebaseClient.ts +0 -790
- package/src/storage/index.ts +0 -96
- package/src/storage/types.ts +0 -348
- package/src/types/adapter.ts +0 -310
- package/src/types/agent.ts +0 -79
- package/src/types/did.ts +0 -223
- package/src/types/eip712.ts +0 -175
- package/src/types/erc8004.ts +0 -293
- package/src/types/escrow.ts +0 -27
- package/src/types/index.ts +0 -17
- package/src/types/message.ts +0 -145
- package/src/types/state.ts +0 -87
- package/src/types/transaction.ts +0 -69
- package/src/types/x402.ts +0 -251
- package/src/utils/ErrorRecoveryGuide.ts +0 -676
- package/src/utils/Helpers.ts +0 -688
- package/src/utils/IPFSClient.ts +0 -368
- package/src/utils/Logger.ts +0 -484
- package/src/utils/NonceManager.ts +0 -591
- package/src/utils/RateLimiter.ts +0 -534
- package/src/utils/ReceivedNonceTracker.ts +0 -567
- package/src/utils/SDKLifecycle.ts +0 -416
- package/src/utils/SecureNonce.ts +0 -78
- package/src/utils/Semaphore.ts +0 -276
- package/src/utils/UsedAttestationTracker.ts +0 -385
- package/src/utils/canonicalJson.ts +0 -38
- package/src/utils/circuitBreaker.ts +0 -324
- package/src/utils/computeTypeHash.ts +0 -48
- package/src/utils/fsSafe.ts +0 -80
- package/src/utils/index.ts +0 -80
- package/src/utils/retry.ts +0 -364
- package/src/utils/security.ts +0 -418
- package/src/utils/validation.ts +0 -540
- package/src/wallet/AutoWalletProvider.ts +0 -299
- package/src/wallet/EOAWalletProvider.ts +0 -69
- package/src/wallet/IWalletProvider.ts +0 -135
- package/src/wallet/aa/BundlerClient.ts +0 -274
- package/src/wallet/aa/DualNonceManager.ts +0 -173
- package/src/wallet/aa/PaymasterClient.ts +0 -174
- package/src/wallet/aa/TransactionBatcher.ts +0 -353
- package/src/wallet/aa/UserOpBuilder.ts +0 -246
- package/src/wallet/aa/constants.ts +0 -60
- package/src/wallet/keystore.ts +0 -240
|
@@ -1,274 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* BundlerClient — JSON-RPC client for ERC-4337 bundlers.
|
|
3
|
-
*
|
|
4
|
-
* Supports Coinbase (primary) and Pimlico (backup) bundlers.
|
|
5
|
-
* Handles gas estimation, UserOp submission, and receipt polling.
|
|
6
|
-
* Retry with exponential backoff on transient failures.
|
|
7
|
-
*
|
|
8
|
-
* @module wallet/aa/BundlerClient
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import { UserOperationV06 } from './constants';
|
|
12
|
-
import { serializeUserOp } from './UserOpBuilder';
|
|
13
|
-
import { ENTRYPOINT_V06 } from './constants';
|
|
14
|
-
import { sdkLogger } from '../../utils/Logger';
|
|
15
|
-
|
|
16
|
-
// ============================================================================
|
|
17
|
-
// Types
|
|
18
|
-
// ============================================================================
|
|
19
|
-
|
|
20
|
-
export interface BundlerConfig {
|
|
21
|
-
/** Primary bundler URL (Coinbase CDP) */
|
|
22
|
-
primaryUrl: string;
|
|
23
|
-
/** Backup bundler URL (Pimlico) */
|
|
24
|
-
backupUrl?: string;
|
|
25
|
-
/** Max retry attempts per endpoint */
|
|
26
|
-
maxRetries?: number;
|
|
27
|
-
/** Base delay for exponential backoff (ms) */
|
|
28
|
-
baseDelayMs?: number;
|
|
29
|
-
/** Timeout for individual requests (ms) */
|
|
30
|
-
timeoutMs?: number;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export interface UserOpReceipt {
|
|
34
|
-
/** UserOp hash */
|
|
35
|
-
userOpHash: string;
|
|
36
|
-
/** Transaction hash on-chain */
|
|
37
|
-
transactionHash: string;
|
|
38
|
-
/** Block number */
|
|
39
|
-
blockNumber: number;
|
|
40
|
-
/** Whether the UserOp execution succeeded */
|
|
41
|
-
success: boolean;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
interface JsonRpcResponse<T = unknown> {
|
|
45
|
-
jsonrpc: string;
|
|
46
|
-
id: number;
|
|
47
|
-
result?: T;
|
|
48
|
-
error?: { code: number; message: string; data?: unknown };
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// ============================================================================
|
|
52
|
-
// BundlerClient
|
|
53
|
-
// ============================================================================
|
|
54
|
-
|
|
55
|
-
export class BundlerClient {
|
|
56
|
-
private readonly primaryUrl: string;
|
|
57
|
-
private readonly backupUrl: string | undefined;
|
|
58
|
-
private readonly maxRetries: number;
|
|
59
|
-
private readonly baseDelayMs: number;
|
|
60
|
-
private readonly timeoutMs: number;
|
|
61
|
-
private requestId = 1;
|
|
62
|
-
|
|
63
|
-
constructor(config: BundlerConfig) {
|
|
64
|
-
this.primaryUrl = config.primaryUrl;
|
|
65
|
-
this.backupUrl = config.backupUrl;
|
|
66
|
-
this.maxRetries = config.maxRetries ?? 2;
|
|
67
|
-
this.baseDelayMs = config.baseDelayMs ?? 1000;
|
|
68
|
-
this.timeoutMs = config.timeoutMs ?? 30_000;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Estimate gas for a UserOp.
|
|
73
|
-
*/
|
|
74
|
-
async estimateUserOperationGas(
|
|
75
|
-
userOp: UserOperationV06
|
|
76
|
-
): Promise<{ callGasLimit: bigint; verificationGasLimit: bigint; preVerificationGas: bigint }> {
|
|
77
|
-
const result = await this.callWithFallback<{
|
|
78
|
-
callGasLimit: string;
|
|
79
|
-
verificationGasLimit: string;
|
|
80
|
-
preVerificationGas: string;
|
|
81
|
-
}>('eth_estimateUserOperationGas', [
|
|
82
|
-
serializeUserOp(userOp),
|
|
83
|
-
ENTRYPOINT_V06,
|
|
84
|
-
]);
|
|
85
|
-
|
|
86
|
-
return {
|
|
87
|
-
callGasLimit: BigInt(result.callGasLimit),
|
|
88
|
-
verificationGasLimit: BigInt(result.verificationGasLimit),
|
|
89
|
-
preVerificationGas: BigInt(result.preVerificationGas),
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Submit a signed UserOp to the bundler.
|
|
95
|
-
* Returns the UserOp hash.
|
|
96
|
-
*/
|
|
97
|
-
async sendUserOperation(userOp: UserOperationV06): Promise<string> {
|
|
98
|
-
return this.callWithFallback<string>('eth_sendUserOperation', [
|
|
99
|
-
serializeUserOp(userOp),
|
|
100
|
-
ENTRYPOINT_V06,
|
|
101
|
-
]);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Get the receipt for a submitted UserOp.
|
|
106
|
-
* Returns null if not yet mined.
|
|
107
|
-
*/
|
|
108
|
-
async getUserOperationReceipt(
|
|
109
|
-
userOpHash: string
|
|
110
|
-
): Promise<UserOpReceipt | null> {
|
|
111
|
-
const result = await this.callWithFallback<{
|
|
112
|
-
userOpHash: string;
|
|
113
|
-
receipt: {
|
|
114
|
-
transactionHash: string;
|
|
115
|
-
blockNumber: string;
|
|
116
|
-
status: string;
|
|
117
|
-
};
|
|
118
|
-
success: boolean;
|
|
119
|
-
} | null>('eth_getUserOperationReceipt', [userOpHash]);
|
|
120
|
-
|
|
121
|
-
if (!result) return null;
|
|
122
|
-
|
|
123
|
-
return {
|
|
124
|
-
userOpHash: result.userOpHash,
|
|
125
|
-
transactionHash: result.receipt.transactionHash,
|
|
126
|
-
blockNumber: parseInt(result.receipt.blockNumber, 16),
|
|
127
|
-
success: result.success,
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Wait for UserOp receipt with polling.
|
|
133
|
-
*/
|
|
134
|
-
async waitForReceipt(
|
|
135
|
-
userOpHash: string,
|
|
136
|
-
timeoutMs: number = 60_000,
|
|
137
|
-
pollIntervalMs: number = 2_000
|
|
138
|
-
): Promise<UserOpReceipt> {
|
|
139
|
-
const start = Date.now();
|
|
140
|
-
while (Date.now() - start < timeoutMs) {
|
|
141
|
-
const receipt = await this.getUserOperationReceipt(userOpHash);
|
|
142
|
-
if (receipt) return receipt;
|
|
143
|
-
await sleep(pollIntervalMs);
|
|
144
|
-
}
|
|
145
|
-
throw new Error(
|
|
146
|
-
`UserOp ${userOpHash} not mined after ${timeoutMs}ms. ` +
|
|
147
|
-
'The transaction may still be pending — check the bundler or block explorer.'
|
|
148
|
-
);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// ==========================================================================
|
|
152
|
-
// Internal
|
|
153
|
-
// ==========================================================================
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Call primary, fall back to backup, with retries and backoff.
|
|
157
|
-
*/
|
|
158
|
-
private async callWithFallback<T>(
|
|
159
|
-
method: string,
|
|
160
|
-
params: unknown[]
|
|
161
|
-
): Promise<T> {
|
|
162
|
-
// Try primary
|
|
163
|
-
try {
|
|
164
|
-
return await this.callWithRetry<T>(this.primaryUrl, method, params);
|
|
165
|
-
} catch (primaryError) {
|
|
166
|
-
if (!this.backupUrl) {
|
|
167
|
-
throw primaryError;
|
|
168
|
-
}
|
|
169
|
-
sdkLogger.warn('Primary bundler failed, trying backup', {
|
|
170
|
-
method,
|
|
171
|
-
error: primaryError instanceof Error ? primaryError.message : String(primaryError),
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// Try backup
|
|
176
|
-
return this.callWithRetry<T>(this.backupUrl!, method, params);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
private async callWithRetry<T>(
|
|
180
|
-
url: string,
|
|
181
|
-
method: string,
|
|
182
|
-
params: unknown[]
|
|
183
|
-
): Promise<T> {
|
|
184
|
-
let lastError: Error | undefined;
|
|
185
|
-
for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
|
|
186
|
-
try {
|
|
187
|
-
return await this.jsonRpc<T>(url, method, params);
|
|
188
|
-
} catch (error) {
|
|
189
|
-
lastError = error instanceof Error ? error : new Error(String(error));
|
|
190
|
-
// Don't retry non-transient errors
|
|
191
|
-
if (isNonTransient(lastError)) throw lastError;
|
|
192
|
-
if (attempt < this.maxRetries) {
|
|
193
|
-
const delay = this.baseDelayMs * Math.pow(2, attempt);
|
|
194
|
-
sdkLogger.warn('Bundler request failed, retrying', {
|
|
195
|
-
attempt: attempt + 1,
|
|
196
|
-
method,
|
|
197
|
-
delayMs: delay,
|
|
198
|
-
});
|
|
199
|
-
await sleep(delay);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
throw lastError!;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
private async jsonRpc<T>(
|
|
207
|
-
url: string,
|
|
208
|
-
method: string,
|
|
209
|
-
params: unknown[]
|
|
210
|
-
): Promise<T> {
|
|
211
|
-
const body = JSON.stringify({
|
|
212
|
-
jsonrpc: '2.0',
|
|
213
|
-
id: this.requestId++,
|
|
214
|
-
method,
|
|
215
|
-
params,
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
const controller = new AbortController();
|
|
219
|
-
const timeout = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
220
|
-
|
|
221
|
-
try {
|
|
222
|
-
const response = await fetch(url, {
|
|
223
|
-
method: 'POST',
|
|
224
|
-
headers: { 'Content-Type': 'application/json' },
|
|
225
|
-
body,
|
|
226
|
-
signal: controller.signal,
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
if (!response.ok) {
|
|
230
|
-
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
const json = (await response.json()) as JsonRpcResponse<T>;
|
|
234
|
-
|
|
235
|
-
if (json.error) {
|
|
236
|
-
const dataStr = json.error.data ? ` | data: ${JSON.stringify(json.error.data)}` : '';
|
|
237
|
-
const err = new Error(
|
|
238
|
-
`Bundler RPC error ${json.error.code}: ${json.error.message}${dataStr}`
|
|
239
|
-
);
|
|
240
|
-
(err as any).code = json.error.code;
|
|
241
|
-
(err as any).data = json.error.data;
|
|
242
|
-
throw err;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
return json.result as T;
|
|
246
|
-
} finally {
|
|
247
|
-
clearTimeout(timeout);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// ============================================================================
|
|
253
|
-
// Helpers
|
|
254
|
-
// ============================================================================
|
|
255
|
-
|
|
256
|
-
function sleep(ms: number): Promise<void> {
|
|
257
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* Detect non-transient errors that shouldn't be retried.
|
|
262
|
-
*/
|
|
263
|
-
function isNonTransient(error: Error): boolean {
|
|
264
|
-
const code = (error as any).code;
|
|
265
|
-
// AA errors from bundler (invalid signature, insufficient funds, etc.)
|
|
266
|
-
if (typeof code === 'number' && code >= -32700 && code <= -32600) {
|
|
267
|
-
return true; // JSON-RPC parse/invalid request errors
|
|
268
|
-
}
|
|
269
|
-
const msg = error.message.toLowerCase();
|
|
270
|
-
if (msg.includes('aa') && (msg.includes('invalid') || msg.includes('rejected'))) {
|
|
271
|
-
return true; // AA validation errors
|
|
272
|
-
}
|
|
273
|
-
return false;
|
|
274
|
-
}
|
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DualNonceManager — Manages both EntryPoint and ACTP nonces.
|
|
3
|
-
*
|
|
4
|
-
* ERC-4337 UserOps need two independent nonces:
|
|
5
|
-
* 1. EntryPoint nonce — anti-replay for the UserOp itself
|
|
6
|
-
* 2. ACTP nonce — used to compute deterministic txId
|
|
7
|
-
*
|
|
8
|
-
* This manager uses a sequential mutex queue to ensure:
|
|
9
|
-
* - Only one UserOp is in-flight at a time
|
|
10
|
-
* - ACTP nonce increments only on confirmed receipt
|
|
11
|
-
* - On failure, next call re-reads from chain
|
|
12
|
-
*
|
|
13
|
-
* @module wallet/aa/DualNonceManager
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import { ethers } from 'ethers';
|
|
17
|
-
import { ENTRYPOINT_V06 } from './constants';
|
|
18
|
-
import { sdkLogger } from '../../utils/Logger';
|
|
19
|
-
|
|
20
|
-
// ============================================================================
|
|
21
|
-
// ABI fragments
|
|
22
|
-
// ============================================================================
|
|
23
|
-
|
|
24
|
-
const ENTRYPOINT_NONCE_ABI = [
|
|
25
|
-
'function getNonce(address sender, uint192 key) view returns (uint256)',
|
|
26
|
-
];
|
|
27
|
-
|
|
28
|
-
const ACTP_KERNEL_NONCE_ABI = [
|
|
29
|
-
'function requesterNonces(address requester) view returns (uint256)',
|
|
30
|
-
];
|
|
31
|
-
|
|
32
|
-
// ============================================================================
|
|
33
|
-
// Mutex
|
|
34
|
-
// ============================================================================
|
|
35
|
-
|
|
36
|
-
class Mutex {
|
|
37
|
-
private locked = false;
|
|
38
|
-
private queue: (() => void)[] = [];
|
|
39
|
-
|
|
40
|
-
async acquire(): Promise<void> {
|
|
41
|
-
if (!this.locked) {
|
|
42
|
-
this.locked = true;
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
return new Promise<void>((resolve) => {
|
|
46
|
-
this.queue.push(resolve);
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
release(): void {
|
|
51
|
-
if (this.queue.length > 0) {
|
|
52
|
-
const next = this.queue.shift()!;
|
|
53
|
-
next();
|
|
54
|
-
} else {
|
|
55
|
-
this.locked = false;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// ============================================================================
|
|
61
|
-
// DualNonceManager
|
|
62
|
-
// ============================================================================
|
|
63
|
-
|
|
64
|
-
export class DualNonceManager {
|
|
65
|
-
private readonly provider: ethers.JsonRpcProvider;
|
|
66
|
-
private readonly senderAddress: string;
|
|
67
|
-
private readonly actpKernelAddress: string;
|
|
68
|
-
private readonly mutex = new Mutex();
|
|
69
|
-
|
|
70
|
-
/** Locally cached ACTP nonce — undefined means "re-read from chain" */
|
|
71
|
-
private cachedActpNonce: bigint | undefined;
|
|
72
|
-
|
|
73
|
-
constructor(
|
|
74
|
-
provider: ethers.JsonRpcProvider,
|
|
75
|
-
senderAddress: string,
|
|
76
|
-
actpKernelAddress: string
|
|
77
|
-
) {
|
|
78
|
-
this.provider = provider;
|
|
79
|
-
this.senderAddress = senderAddress;
|
|
80
|
-
this.actpKernelAddress = actpKernelAddress;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Execute a callback while holding the nonce mutex.
|
|
85
|
-
*
|
|
86
|
-
* The callback receives current nonces and must return whether
|
|
87
|
-
* the operation succeeded (to decide ACTP nonce increment).
|
|
88
|
-
*
|
|
89
|
-
* @param fn Callback receiving { entryPointNonce, actpNonce }
|
|
90
|
-
* @param incrementsActpNonce Whether success increments the ACTP nonce
|
|
91
|
-
*/
|
|
92
|
-
async enqueue<T>(
|
|
93
|
-
fn: (nonces: { entryPointNonce: bigint; actpNonce: bigint }) => Promise<{
|
|
94
|
-
result: T;
|
|
95
|
-
success: boolean;
|
|
96
|
-
}>,
|
|
97
|
-
incrementsActpNonce: boolean = true
|
|
98
|
-
): Promise<T> {
|
|
99
|
-
await this.mutex.acquire();
|
|
100
|
-
try {
|
|
101
|
-
// Read nonces
|
|
102
|
-
const entryPointNonce = await this.readEntryPointNonce();
|
|
103
|
-
const actpNonce = this.cachedActpNonce ?? await this.readActpNonce();
|
|
104
|
-
|
|
105
|
-
sdkLogger.info('Nonces acquired', {
|
|
106
|
-
entryPointNonce: entryPointNonce.toString(),
|
|
107
|
-
actpNonce: actpNonce.toString(),
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
const { result, success } = await fn({ entryPointNonce, actpNonce });
|
|
111
|
-
|
|
112
|
-
if (success && incrementsActpNonce) {
|
|
113
|
-
this.cachedActpNonce = actpNonce + 1n;
|
|
114
|
-
} else if (!success) {
|
|
115
|
-
// Reset cache on failure — next call re-reads from chain
|
|
116
|
-
this.cachedActpNonce = undefined;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return result;
|
|
120
|
-
} catch (error) {
|
|
121
|
-
// Reset on error
|
|
122
|
-
this.cachedActpNonce = undefined;
|
|
123
|
-
throw error;
|
|
124
|
-
} finally {
|
|
125
|
-
this.mutex.release();
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Read current EntryPoint nonce for the sender.
|
|
131
|
-
* Key 0 is the default key for CoinbaseSmartWallet.
|
|
132
|
-
*/
|
|
133
|
-
private async readEntryPointNonce(): Promise<bigint> {
|
|
134
|
-
const entryPoint = new ethers.Contract(
|
|
135
|
-
ENTRYPOINT_V06,
|
|
136
|
-
ENTRYPOINT_NONCE_ABI,
|
|
137
|
-
this.provider
|
|
138
|
-
);
|
|
139
|
-
return await entryPoint.getNonce(this.senderAddress, 0);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Read current ACTP nonce for the requester.
|
|
144
|
-
* requesterNonces is public on ACTPKernel (added in v2).
|
|
145
|
-
* Older deployments may not expose this — fall back to 0n.
|
|
146
|
-
*/
|
|
147
|
-
private async readActpNonce(): Promise<bigint> {
|
|
148
|
-
try {
|
|
149
|
-
const kernel = new ethers.Contract(
|
|
150
|
-
this.actpKernelAddress,
|
|
151
|
-
ACTP_KERNEL_NONCE_ABI,
|
|
152
|
-
this.provider
|
|
153
|
-
);
|
|
154
|
-
const nonce = await kernel.requesterNonces(this.senderAddress);
|
|
155
|
-
this.cachedActpNonce = nonce;
|
|
156
|
-
return nonce;
|
|
157
|
-
} catch {
|
|
158
|
-
// Older ACTPKernel deployments don't expose requesterNonces.
|
|
159
|
-
// Return 0n — registration doesn't need it, and payment batches
|
|
160
|
-
// will fail at the contract level anyway if nonces are wrong.
|
|
161
|
-
sdkLogger.warn('requesterNonces not available on ACTPKernel — using 0 (older deployment?)');
|
|
162
|
-
this.cachedActpNonce = 0n;
|
|
163
|
-
return 0n;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Invalidate cached ACTP nonce (forces re-read on next operation).
|
|
169
|
-
*/
|
|
170
|
-
invalidateCache(): void {
|
|
171
|
-
this.cachedActpNonce = undefined;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* PaymasterClient — Gas sponsorship via ERC-7677 paymasters.
|
|
3
|
-
*
|
|
4
|
-
* Fallback chain: Coinbase CDP (primary) → Pimlico (backup).
|
|
5
|
-
* Both implement ERC-7677 pm_getPaymasterStubData / pm_getPaymasterData.
|
|
6
|
-
*
|
|
7
|
-
* The paymaster fills the `paymasterAndData` field of the UserOp,
|
|
8
|
-
* which the EntryPoint uses to debit gas from the paymaster instead
|
|
9
|
-
* of the sender's ETH balance.
|
|
10
|
-
*
|
|
11
|
-
* @module wallet/aa/PaymasterClient
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import { UserOperationV06 } from './constants';
|
|
15
|
-
import { serializeUserOp } from './UserOpBuilder';
|
|
16
|
-
import { ENTRYPOINT_V06 } from './constants';
|
|
17
|
-
import { sdkLogger } from '../../utils/Logger';
|
|
18
|
-
|
|
19
|
-
// ============================================================================
|
|
20
|
-
// Types
|
|
21
|
-
// ============================================================================
|
|
22
|
-
|
|
23
|
-
export interface PaymasterConfig {
|
|
24
|
-
/** Primary paymaster URL (Coinbase CDP) */
|
|
25
|
-
primaryUrl: string;
|
|
26
|
-
/** Backup paymaster URL (Pimlico) */
|
|
27
|
-
backupUrl?: string;
|
|
28
|
-
/** Chain ID (8453 for Base Mainnet, 84532 for Sepolia) */
|
|
29
|
-
chainId: number;
|
|
30
|
-
/** Request timeout (ms) */
|
|
31
|
-
timeoutMs?: number;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
interface JsonRpcResponse<T = unknown> {
|
|
35
|
-
jsonrpc: string;
|
|
36
|
-
id: number;
|
|
37
|
-
result?: T;
|
|
38
|
-
error?: { code: number; message: string; data?: unknown };
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// ============================================================================
|
|
42
|
-
// PaymasterClient
|
|
43
|
-
// ============================================================================
|
|
44
|
-
|
|
45
|
-
export class PaymasterClient {
|
|
46
|
-
private readonly primaryUrl: string;
|
|
47
|
-
private readonly backupUrl: string | undefined;
|
|
48
|
-
private readonly chainId: number;
|
|
49
|
-
private readonly timeoutMs: number;
|
|
50
|
-
private requestId = 1;
|
|
51
|
-
|
|
52
|
-
constructor(config: PaymasterConfig) {
|
|
53
|
-
this.primaryUrl = config.primaryUrl;
|
|
54
|
-
this.backupUrl = config.backupUrl;
|
|
55
|
-
this.chainId = config.chainId;
|
|
56
|
-
this.timeoutMs = config.timeoutMs ?? 15_000;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Get stub paymaster data for gas estimation.
|
|
61
|
-
*
|
|
62
|
-
* Returns approximate paymasterAndData that the bundler can use
|
|
63
|
-
* for gas estimation (exact values come from getPaymasterData).
|
|
64
|
-
*/
|
|
65
|
-
async getPaymasterStubData(
|
|
66
|
-
userOp: UserOperationV06
|
|
67
|
-
): Promise<{ paymasterAndData: string }> {
|
|
68
|
-
return this.callWithFallback<{ paymasterAndData: string }>(
|
|
69
|
-
'pm_getPaymasterStubData',
|
|
70
|
-
[
|
|
71
|
-
serializeUserOp(userOp),
|
|
72
|
-
ENTRYPOINT_V06,
|
|
73
|
-
'0x' + this.chainId.toString(16),
|
|
74
|
-
{}, // context
|
|
75
|
-
]
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Get final paymaster data for the signed UserOp.
|
|
81
|
-
*
|
|
82
|
-
* This is called after gas estimation with final gas values.
|
|
83
|
-
* Returns the paymaster signature that goes into paymasterAndData.
|
|
84
|
-
*/
|
|
85
|
-
async getPaymasterData(
|
|
86
|
-
userOp: UserOperationV06
|
|
87
|
-
): Promise<{ paymasterAndData: string }> {
|
|
88
|
-
return this.callWithFallback<{ paymasterAndData: string }>(
|
|
89
|
-
'pm_getPaymasterData',
|
|
90
|
-
[
|
|
91
|
-
serializeUserOp(userOp),
|
|
92
|
-
ENTRYPOINT_V06,
|
|
93
|
-
'0x' + this.chainId.toString(16),
|
|
94
|
-
{}, // context
|
|
95
|
-
]
|
|
96
|
-
);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// ==========================================================================
|
|
100
|
-
// Internal
|
|
101
|
-
// ==========================================================================
|
|
102
|
-
|
|
103
|
-
private async callWithFallback<T>(
|
|
104
|
-
method: string,
|
|
105
|
-
params: unknown[]
|
|
106
|
-
): Promise<T> {
|
|
107
|
-
try {
|
|
108
|
-
return await this.jsonRpc<T>(this.primaryUrl, method, params);
|
|
109
|
-
} catch (primaryError) {
|
|
110
|
-
if (!this.backupUrl) {
|
|
111
|
-
throw new Error(
|
|
112
|
-
`Gas sponsorship unavailable: ${primaryError instanceof Error ? primaryError.message : String(primaryError)}. ` +
|
|
113
|
-
'No backup paymaster configured.'
|
|
114
|
-
);
|
|
115
|
-
}
|
|
116
|
-
sdkLogger.warn('Primary paymaster failed, trying backup', {
|
|
117
|
-
method,
|
|
118
|
-
error: primaryError instanceof Error ? primaryError.message : String(primaryError),
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
try {
|
|
123
|
-
return await this.jsonRpc<T>(this.backupUrl!, method, params);
|
|
124
|
-
} catch (backupError) {
|
|
125
|
-
throw new Error(
|
|
126
|
-
'Gas sponsorship temporarily unavailable — both Coinbase and Pimlico paymasters failed. ' +
|
|
127
|
-
'Please retry later. ' +
|
|
128
|
-
`Primary: ${backupError instanceof Error ? backupError.message : String(backupError)}`
|
|
129
|
-
);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
private async jsonRpc<T>(
|
|
134
|
-
url: string,
|
|
135
|
-
method: string,
|
|
136
|
-
params: unknown[]
|
|
137
|
-
): Promise<T> {
|
|
138
|
-
const body = JSON.stringify({
|
|
139
|
-
jsonrpc: '2.0',
|
|
140
|
-
id: this.requestId++,
|
|
141
|
-
method,
|
|
142
|
-
params,
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
const controller = new AbortController();
|
|
146
|
-
const timeout = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
147
|
-
|
|
148
|
-
try {
|
|
149
|
-
const response = await fetch(url, {
|
|
150
|
-
method: 'POST',
|
|
151
|
-
headers: { 'Content-Type': 'application/json' },
|
|
152
|
-
body,
|
|
153
|
-
signal: controller.signal,
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
if (!response.ok) {
|
|
157
|
-
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
const json = (await response.json()) as JsonRpcResponse<T>;
|
|
161
|
-
|
|
162
|
-
if (json.error) {
|
|
163
|
-
const dataStr = json.error.data ? ` | data: ${JSON.stringify(json.error.data)}` : '';
|
|
164
|
-
throw new Error(
|
|
165
|
-
`Paymaster RPC error ${json.error.code}: ${json.error.message}${dataStr}`
|
|
166
|
-
);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
return json.result as T;
|
|
170
|
-
} finally {
|
|
171
|
-
clearTimeout(timeout);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
}
|