@dynamic-labs/ethereum-aa 0.0.0-exp20240808.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.
@@ -0,0 +1,349 @@
1
+ 'use client'
2
+ import { __awaiter } from '../_virtual/_tslib.js';
3
+ import { toMultiChainECDSAValidator } from '@zerodev/multi-chain-validator';
4
+ import { publicActions, custom, createWalletClient, isHex, fromHex, formatEther } from 'viem';
5
+ import { ENTRYPOINT_ADDRESS_V07, ENTRYPOINT_ADDRESS_V06, walletClientToSmartAccountSigner } from 'permissionless/utils';
6
+ import { KERNEL_V3_1, KERNEL_V3_0, KERNEL_V2_4 } from '@zerodev/sdk/constants';
7
+ import { signerToEcdsaValidator } from '@zerodev/ecdsa-validator';
8
+ import { ProviderEntryPointVersionEnum, ProviderKernelVersionEnum } from '@dynamic-labs/sdk-api-core';
9
+ import { WalletConnectorBase } from '@dynamic-labs/wallet-connector-core';
10
+ import { confirmationTransport, chainsMap, unFormatTransaction, createViemUiTransaction } from '@dynamic-labs/viem-utils';
11
+ import { parseEvmNetworks, DynamicError, DeferredPromise, wrapMethodWithCallback, TransactionGasCannotBeSponsoredError, InsufficientFundsError } from '@dynamic-labs/utils';
12
+ import { EthereumWallet, isEthWalletConnector } from '@dynamic-labs/ethereum';
13
+ import { cachedChainIdKey, fetchChain } from './utils/fetchChain.js';
14
+ import { logger } from './utils/logger.js';
15
+ import { createEcdsaKernelAccountClient } from './utils/createEcsdaKernelAccountClient.js';
16
+
17
+ class ZeroDevConnector extends WalletConnectorBase {
18
+ constructor(opts) {
19
+ var _a, _b, _c, _d;
20
+ super(opts);
21
+ this.ChainWallet = EthereumWallet;
22
+ this.connectedChain = 'EVM';
23
+ this.supportedChains = ['ETH', 'EVM'];
24
+ this.isGasSponsorshipDisabled = false;
25
+ this.isEmbeddedWallet = true;
26
+ this.name = 'ZeroDev';
27
+ this.overrideKey = 'zerodev';
28
+ this.walletFallback = {
29
+ brand: {
30
+ alt: 'Smart Wallet',
31
+ spriteId: 'smartwallet',
32
+ },
33
+ name: 'ZeroDev',
34
+ };
35
+ const zeroDevProjectId = (_a = opts.apiProviders.zerodev) === null || _a === void 0 ? void 0 : _a.clientId;
36
+ if (!zeroDevProjectId) {
37
+ throw new Error('Missing ZeroDev project ID provided. Add your ZeroDev project id to your project configuration via the Dynamic Labs dashboard.');
38
+ }
39
+ this.projectId = zeroDevProjectId;
40
+ this._walletUiUtils = opts.walletUiUtils;
41
+ this.evmNetworks = parseEvmNetworks(opts.evmNetworks);
42
+ this.entryPoint = this.getEntryPoint((_b = opts.apiProviders.zerodev) === null || _b === void 0 ? void 0 : _b.entryPointVersion);
43
+ this.kernelVersion = this.getKernelVersion((_c = opts.apiProviders.zerodev) === null || _c === void 0 ? void 0 : _c.kernelVersion);
44
+ this.ecdsaProviderType = (_d = opts.apiProviders.zerodev) === null || _d === void 0 ? void 0 : _d.ecdsaProviderType;
45
+ }
46
+ getEntryPoint(entryPoint) {
47
+ if (entryPoint === undefined ||
48
+ entryPoint === ProviderEntryPointVersionEnum.V7) {
49
+ return ENTRYPOINT_ADDRESS_V07;
50
+ }
51
+ else if (entryPoint === ProviderEntryPointVersionEnum.V6) {
52
+ return ENTRYPOINT_ADDRESS_V06;
53
+ }
54
+ else {
55
+ logger.error('Invalid entry point version', entryPoint);
56
+ return ENTRYPOINT_ADDRESS_V07;
57
+ }
58
+ }
59
+ getKernelVersion(kernelVersion) {
60
+ if (kernelVersion === ProviderKernelVersionEnum.V31 ||
61
+ (kernelVersion === undefined &&
62
+ this.entryPoint === ENTRYPOINT_ADDRESS_V07)) {
63
+ return KERNEL_V3_1;
64
+ }
65
+ else if (kernelVersion === ProviderKernelVersionEnum.V30) {
66
+ return KERNEL_V3_0;
67
+ }
68
+ else if (kernelVersion === ProviderKernelVersionEnum.V24 ||
69
+ this.entryPoint === ENTRYPOINT_ADDRESS_V06 // v0.6 only supports v2.4
70
+ ) {
71
+ return KERNEL_V2_4;
72
+ }
73
+ else {
74
+ logger.error('Invalid kernel version', kernelVersion);
75
+ return KERNEL_V3_1;
76
+ }
77
+ }
78
+ getEcdsaValidator(ecdsaProviderType) {
79
+ if (ecdsaProviderType === 'zerodev_signer_to_ecdsa') {
80
+ return signerToEcdsaValidator;
81
+ }
82
+ else if (ecdsaProviderType === 'zerodev_multi_chain') {
83
+ return toMultiChainECDSAValidator;
84
+ }
85
+ else {
86
+ return signerToEcdsaValidator;
87
+ }
88
+ }
89
+ getEOAConnector() {
90
+ return this.eoaConnector;
91
+ }
92
+ getAccountAbstractionProvider({ withSponsorship, } = {}) {
93
+ if (withSponsorship && this.kernelClientWithSponsorship) {
94
+ return this.kernelClientWithSponsorship;
95
+ }
96
+ return this.kernelClient;
97
+ }
98
+ endSession() {
99
+ return __awaiter(this, void 0, void 0, function* () {
100
+ localStorage.removeItem(cachedChainIdKey);
101
+ });
102
+ }
103
+ setEoaConnector(connector) {
104
+ return __awaiter(this, void 0, void 0, function* () {
105
+ if (!connector) {
106
+ logger.error('No EOA connector provided');
107
+ return;
108
+ }
109
+ this.kernelClientDeferredPromise = new DeferredPromise();
110
+ this.eoaConnector = connector;
111
+ if (isEthWalletConnector(this.eoaConnector) &&
112
+ !this.eoaConnector.getActiveAccount()) {
113
+ yield this.eoaConnector.getConnectedAccounts();
114
+ }
115
+ const signer = yield this.eoaConnector.getSigner();
116
+ if (!(signer === null || signer === void 0 ? void 0 : signer.account)) {
117
+ logger.error('No signer account found');
118
+ return;
119
+ }
120
+ const kernelClient = yield createEcdsaKernelAccountClient({
121
+ bundlerRpc: ZeroDevConnector.bundlerRpc,
122
+ chain: yield fetchChain(this.projectId),
123
+ ecdsaValidator: this.getEcdsaValidator(this.ecdsaProviderType),
124
+ entryPointAddress: this.entryPoint,
125
+ kernelVersion: this.kernelVersion,
126
+ paymaster: 'NONE',
127
+ paymasterRpc: ZeroDevConnector.paymasterRpc,
128
+ projectId: this.projectId,
129
+ provider: ZeroDevConnector.bundlerProvider,
130
+ signer: walletClientToSmartAccountSigner(signer),
131
+ });
132
+ wrapMethodWithCallback(kernelClient, 'sendUserOperation', (original, ...args) => {
133
+ this._walletUiUtils.disabledConfirmationOnce();
134
+ return original(...args);
135
+ });
136
+ this.kernelClient = kernelClient;
137
+ // set the provider with sponsorship enabled
138
+ yield this.getKernelClientWithSponsorshipValidation();
139
+ yield this.checkIsProjectChainEnabled();
140
+ this.kernelClientDeferredPromise.resolve();
141
+ });
142
+ }
143
+ checkIsProjectChainEnabled() {
144
+ return __awaiter(this, void 0, void 0, function* () {
145
+ var _a, _b;
146
+ const client = yield this.getPublicClient();
147
+ const chainName = (_a = client === null || client === void 0 ? void 0 : client.chain) === null || _a === void 0 ? void 0 : _a.name;
148
+ const chainId = (_b = client === null || client === void 0 ? void 0 : client.chain) === null || _b === void 0 ? void 0 : _b.id;
149
+ const isProjectChainEnabled = this.evmNetworks.some((network) => network.chainId === chainId);
150
+ if (!isProjectChainEnabled) {
151
+ logger.error(`Please make sure to enable ${chainName} (${chainId}) in the Dynamic dashboard (https://app.dynamic.xyz/dashboard/configurations#evm).`);
152
+ }
153
+ });
154
+ }
155
+ getKernelClientWithSponsorshipValidation() {
156
+ return __awaiter(this, void 0, void 0, function* () {
157
+ if (this.kernelClientWithSponsorship) {
158
+ return this.kernelClientWithSponsorship;
159
+ }
160
+ const { eoaConnector } = this;
161
+ if (!eoaConnector)
162
+ throw new DynamicError('No EOA connector');
163
+ const signer = yield eoaConnector.getSigner();
164
+ this.kernelClientWithSponsorship = yield createEcdsaKernelAccountClient({
165
+ bundlerRpc: ZeroDevConnector.bundlerRpc,
166
+ chain: yield fetchChain(this.projectId),
167
+ ecdsaValidator: this.getEcdsaValidator(this.ecdsaProviderType),
168
+ entryPointAddress: this.entryPoint,
169
+ kernelVersion: this.kernelVersion,
170
+ paymaster: 'SPONSOR',
171
+ paymasterRpc: ZeroDevConnector.paymasterRpc,
172
+ projectId: this.projectId,
173
+ provider: ZeroDevConnector.bundlerProvider,
174
+ signer: walletClientToSmartAccountSigner(signer),
175
+ });
176
+ wrapMethodWithCallback(this.kernelClientWithSponsorship, 'sendUserOperation', (original, ...args) => {
177
+ this._walletUiUtils.disabledConfirmationOnce();
178
+ return original(...args);
179
+ });
180
+ return this.kernelClientWithSponsorship;
181
+ });
182
+ }
183
+ getAddress() {
184
+ return __awaiter(this, void 0, void 0, function* () {
185
+ var _a, _b, _c;
186
+ yield ((_a = this.kernelClientDeferredPromise) === null || _a === void 0 ? void 0 : _a.promise);
187
+ return (_c = (_b = this.kernelClient) === null || _b === void 0 ? void 0 : _b.account) === null || _c === void 0 ? void 0 : _c.address;
188
+ });
189
+ }
190
+ getConnectedAccounts() {
191
+ return __awaiter(this, void 0, void 0, function* () {
192
+ var _a, _b, _c;
193
+ yield ((_a = this.kernelClientDeferredPromise) === null || _a === void 0 ? void 0 : _a.promise);
194
+ return ((_c = (_b = this.kernelClient) === null || _b === void 0 ? void 0 : _b.account) === null || _c === void 0 ? void 0 : _c.address)
195
+ ? [yield this.kernelClient.account.address]
196
+ : [];
197
+ });
198
+ }
199
+ getNetwork() {
200
+ return __awaiter(this, void 0, void 0, function* () {
201
+ var _a, _b, _c;
202
+ yield ((_a = this.kernelClientDeferredPromise) === null || _a === void 0 ? void 0 : _a.promise);
203
+ return (_c = (_b = this.kernelClient) === null || _b === void 0 ? void 0 : _b.chain) === null || _c === void 0 ? void 0 : _c.id;
204
+ });
205
+ }
206
+ getTransport(provider) {
207
+ const transport = confirmationTransport({
208
+ getAccounts: () => __awaiter(this, void 0, void 0, function* () { return [provider.account.address]; }),
209
+ onPersonalSign: (_a) => __awaiter(this, [_a], void 0, function* ({ message }) {
210
+ this._walletUiUtils.disabledConfirmationOnce();
211
+ return provider.signMessage({
212
+ account: provider.account,
213
+ message,
214
+ });
215
+ }),
216
+ onSendTransaction: (_b) => __awaiter(this, [_b], void 0, function* ({ transaction }) {
217
+ try {
218
+ this._walletUiUtils.disabledConfirmationOnce();
219
+ const effectiveProvider = this.isGasSponsorshipDisabled
220
+ ? provider
221
+ : yield this.getKernelClientWithSponsorshipValidation();
222
+ const response = yield effectiveProvider.sendTransaction(unFormatTransaction(transaction));
223
+ return response;
224
+ }
225
+ catch (err) {
226
+ logger.debug(err);
227
+ if (TransactionGasCannotBeSponsoredError.isErrorMessage(err)) {
228
+ throw new TransactionGasCannotBeSponsoredError();
229
+ }
230
+ if (InsufficientFundsError.isErrorMessage(err)) {
231
+ throw new InsufficientFundsError();
232
+ }
233
+ throw err;
234
+ }
235
+ }),
236
+ onSignTypedData: (_c) => __awaiter(this, [_c], void 0, function* ({ message }) {
237
+ this._walletUiUtils.disabledConfirmationOnce();
238
+ const signTypedData = JSON.parse(message);
239
+ return provider.signTypedData({
240
+ account: provider.account,
241
+ domain: signTypedData.domain,
242
+ message: signTypedData.message,
243
+ primaryType: signTypedData.primaryType,
244
+ types: signTypedData.types,
245
+ });
246
+ }),
247
+ provider: provider.extend(publicActions),
248
+ transport: custom(provider),
249
+ walletConnector: this,
250
+ walletUiUtils: this._walletUiUtils,
251
+ });
252
+ return transport;
253
+ }
254
+ getWalletClient(chainId) {
255
+ var _a;
256
+ const provider = this.kernelClient;
257
+ if (!provider)
258
+ throw new DynamicError('No provider');
259
+ const transport = this.getTransport(provider);
260
+ const address = (_a = provider.account) === null || _a === void 0 ? void 0 : _a.address;
261
+ const walletClient = createWalletClient({
262
+ account: address,
263
+ chain: chainId ? chainsMap[chainId] : provider.chain,
264
+ transport,
265
+ });
266
+ return walletClient;
267
+ }
268
+ getPublicClient() {
269
+ return __awaiter(this, void 0, void 0, function* () {
270
+ var _a;
271
+ const provider = (_a = this.kernelClient) === null || _a === void 0 ? void 0 : _a.extend(publicActions);
272
+ if (!provider)
273
+ throw new DynamicError('No provider');
274
+ return provider;
275
+ });
276
+ }
277
+ getSigner() {
278
+ return __awaiter(this, void 0, void 0, function* () {
279
+ return this.getWalletClient();
280
+ });
281
+ }
282
+ canSponsorTransactionGas(transaction) {
283
+ return __awaiter(this, void 0, void 0, function* () {
284
+ try {
285
+ const kernelClientWithSponsorship = yield this.getKernelClientWithSponsorshipValidation();
286
+ const userOperation = {
287
+ callData: yield kernelClientWithSponsorship.account.encodeCallData({
288
+ data: transaction.data ? transaction.data : '0x',
289
+ to: transaction.to,
290
+ /**
291
+ * if value is undefined, it will be set to 0x0 so the sponsorship
292
+ * check does not fail
293
+ */
294
+ value: transaction.value && isHex(transaction.value)
295
+ ? fromHex(transaction.value, 'bigint')
296
+ : fromHex('0x0', 'bigint'),
297
+ }),
298
+ };
299
+ const result = yield kernelClientWithSponsorship.prepareUserOperationRequest({
300
+ account: kernelClientWithSponsorship.account,
301
+ userOperation,
302
+ });
303
+ if (!result)
304
+ return false;
305
+ return result.paymasterAndData !== '0x';
306
+ }
307
+ catch (err) {
308
+ logger.debug(err);
309
+ return false;
310
+ }
311
+ });
312
+ }
313
+ disableGasSponsorshipOnce() {
314
+ this.isGasSponsorshipDisabled = true;
315
+ }
316
+ getBalance(address) {
317
+ return __awaiter(this, void 0, void 0, function* () {
318
+ var _a;
319
+ const rpcClient = (_a = this.kernelClient) === null || _a === void 0 ? void 0 : _a.extend(publicActions);
320
+ if (!rpcClient)
321
+ throw new DynamicError('No RPC client');
322
+ return formatEther(yield rpcClient.getBalance({ address: address }));
323
+ });
324
+ }
325
+ signMessage(messageToSign) {
326
+ return __awaiter(this, void 0, void 0, function* () {
327
+ const client = yield this.getSigner();
328
+ const account = yield this.getAddress();
329
+ if (!account || !client)
330
+ return;
331
+ return client.signMessage({
332
+ account: account,
333
+ message: messageToSign,
334
+ });
335
+ });
336
+ }
337
+ createUiTransaction(from) {
338
+ return __awaiter(this, void 0, void 0, function* () {
339
+ const walletClient = this.getWalletClient();
340
+ const publicClient = yield this.getPublicClient();
341
+ if (!publicClient || !walletClient) {
342
+ throw new DynamicError('No public client available');
343
+ }
344
+ return createViemUiTransaction({ from, publicClient, walletClient });
345
+ });
346
+ }
347
+ }
348
+
349
+ export { ZeroDevConnector };
@@ -0,0 +1,9 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ class EthereumAaConnector {
7
+ }
8
+
9
+ exports.EthereumAaConnector = EthereumAaConnector;
@@ -0,0 +1,2 @@
1
+ export declare class EthereumAaConnector {
2
+ }
@@ -0,0 +1,5 @@
1
+ 'use client'
2
+ class EthereumAaConnector {
3
+ }
4
+
5
+ export { EthereumAaConnector };
package/src/index.cjs ADDED
@@ -0,0 +1,27 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var ZeroDevConnector = require('./ZeroDevConnector.cjs');
7
+ var ethereumAa = require('./ethereum-aa.cjs');
8
+ var isZeroDevConnector = require('./utils/isZeroDevConnector.cjs');
9
+
10
+ const ZeroDevSmartWalletConnectorsWithConfig = ({ bundlerProvider, bundlerRpc, paymasterRpc, } = {}) => {
11
+ ZeroDevConnector.ZeroDevConnector.bundlerProvider = bundlerProvider;
12
+ ZeroDevConnector.ZeroDevConnector.bundlerRpc = bundlerRpc;
13
+ ZeroDevConnector.ZeroDevConnector.paymasterRpc = paymasterRpc;
14
+ return ZeroDevSmartWalletConnectors;
15
+ };
16
+ const ZeroDevSmartWalletConnectors = (props) => {
17
+ var _a;
18
+ if ((_a = props.apiProviders) === null || _a === void 0 ? void 0 : _a.zerodev) {
19
+ return [ZeroDevConnector.ZeroDevConnector];
20
+ }
21
+ return [];
22
+ };
23
+
24
+ exports.EthereumAaConnector = ethereumAa.EthereumAaConnector;
25
+ exports.isZeroDevConnector = isZeroDevConnector.isZeroDevConnector;
26
+ exports.ZeroDevSmartWalletConnectors = ZeroDevSmartWalletConnectors;
27
+ exports.ZeroDevSmartWalletConnectorsWithConfig = ZeroDevSmartWalletConnectorsWithConfig;
package/src/index.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { WalletConnectorConstructor } from '@dynamic-labs/wallet-connector-core';
2
+ import { BundlerProvider } from './utils';
3
+ export * from './ethereum-aa';
4
+ export { isZeroDevConnector } from './utils/isZeroDevConnector';
5
+ export declare const ZeroDevSmartWalletConnectorsWithConfig: ({ bundlerProvider, bundlerRpc, paymasterRpc, }?: {
6
+ bundlerProvider?: BundlerProvider;
7
+ bundlerRpc?: string;
8
+ paymasterRpc?: string;
9
+ }) => (props: any) => WalletConnectorConstructor[];
10
+ export declare const ZeroDevSmartWalletConnectors: (props: any) => WalletConnectorConstructor[];
package/src/index.js ADDED
@@ -0,0 +1,20 @@
1
+ 'use client'
2
+ import { ZeroDevConnector } from './ZeroDevConnector.js';
3
+ export { EthereumAaConnector } from './ethereum-aa.js';
4
+ export { isZeroDevConnector } from './utils/isZeroDevConnector.js';
5
+
6
+ const ZeroDevSmartWalletConnectorsWithConfig = ({ bundlerProvider, bundlerRpc, paymasterRpc, } = {}) => {
7
+ ZeroDevConnector.bundlerProvider = bundlerProvider;
8
+ ZeroDevConnector.bundlerRpc = bundlerRpc;
9
+ ZeroDevConnector.paymasterRpc = paymasterRpc;
10
+ return ZeroDevSmartWalletConnectors;
11
+ };
12
+ const ZeroDevSmartWalletConnectors = (props) => {
13
+ var _a;
14
+ if ((_a = props.apiProviders) === null || _a === void 0 ? void 0 : _a.zerodev) {
15
+ return [ZeroDevConnector];
16
+ }
17
+ return [];
18
+ };
19
+
20
+ export { ZeroDevSmartWalletConnectors, ZeroDevSmartWalletConnectorsWithConfig };
@@ -0,0 +1,79 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var _tslib = require('../../_virtual/_tslib.cjs');
7
+ var accounts = require('@zerodev/sdk/accounts');
8
+ var clients = require('@zerodev/sdk/clients');
9
+ var viem = require('viem');
10
+
11
+ const getZeroDevBundlerRPC = (projectId, provider) => {
12
+ let rpc = `https://rpc.zerodev.app/api/v2/bundler/${projectId}`;
13
+ if (provider) {
14
+ rpc += `?bundlerProvider=${provider}`;
15
+ }
16
+ return rpc;
17
+ };
18
+ const getZeroDevPaymasterRPC = (projectId, provider) => {
19
+ let rpc = `https://rpc.zerodev.app/api/v2/paymaster/${projectId}`;
20
+ if (provider) {
21
+ rpc += `?paymasterProvider=${provider}`;
22
+ }
23
+ return rpc;
24
+ };
25
+ const isERC20 = (value) => viem.isAddress(value);
26
+ const createEcdsaKernelAccountClient = (_a) => _tslib.__awaiter(void 0, [_a], void 0, function* ({ bundlerRpc, chain, paymasterRpc, projectId, signer, provider, index, paymaster = 'SPONSOR', entryPointAddress, kernelVersion, ecdsaValidator, }) {
27
+ const resolvedBundlerRpc = bundlerRpc !== null && bundlerRpc !== void 0 ? bundlerRpc : getZeroDevBundlerRPC(projectId, provider);
28
+ const resolvedPaymasterRpc = paymasterRpc !== null && paymasterRpc !== void 0 ? paymasterRpc : getZeroDevPaymasterRPC(projectId, provider);
29
+ const publicClient = viem.createPublicClient({
30
+ transport: viem.http(resolvedBundlerRpc),
31
+ });
32
+ const validator = yield ecdsaValidator(publicClient, {
33
+ entryPoint: entryPointAddress,
34
+ kernelVersion,
35
+ signer,
36
+ });
37
+ const account = yield accounts.createKernelAccount(publicClient, {
38
+ entryPoint: entryPointAddress,
39
+ index,
40
+ kernelVersion,
41
+ plugins: {
42
+ sudo: validator,
43
+ },
44
+ });
45
+ const zerodevPaymaster = clients.createZeroDevPaymasterClient({
46
+ chain: chain,
47
+ entryPoint: entryPointAddress,
48
+ transport: viem.http(resolvedPaymasterRpc),
49
+ });
50
+ const kernelClient = clients.createKernelAccountClient({
51
+ account,
52
+ bundlerTransport: viem.http(resolvedBundlerRpc),
53
+ chain,
54
+ entryPoint: entryPointAddress,
55
+ middleware: paymaster !== 'NONE'
56
+ ? {
57
+ sponsorUserOperation: (_b) => _tslib.__awaiter(void 0, [_b], void 0, function* ({ userOperation }) {
58
+ const _userOperation = userOperation;
59
+ if (isERC20(paymaster)) {
60
+ return zerodevPaymaster.sponsorUserOperation({
61
+ entryPoint: entryPointAddress,
62
+ gasToken: paymaster,
63
+ userOperation: _userOperation,
64
+ });
65
+ }
66
+ return zerodevPaymaster.sponsorUserOperation({
67
+ entryPoint: entryPointAddress,
68
+ userOperation: _userOperation,
69
+ });
70
+ }),
71
+ }
72
+ : undefined,
73
+ });
74
+ return kernelClient;
75
+ });
76
+
77
+ exports.createEcdsaKernelAccountClient = createEcdsaKernelAccountClient;
78
+ exports.getZeroDevBundlerRPC = getZeroDevBundlerRPC;
79
+ exports.getZeroDevPaymasterRPC = getZeroDevPaymasterRPC;
@@ -0,0 +1,24 @@
1
+ import { KernelSmartAccount } from '@zerodev/sdk/accounts';
2
+ import { KernelAccountClient } from '@zerodev/sdk/clients';
3
+ import { SmartAccountSigner } from 'permissionless/accounts';
4
+ import { EntryPoint } from 'permissionless/types';
5
+ import { Address, HttpTransport, Chain } from 'viem';
6
+ import { GetKernelVersion } from '@zerodev/sdk/types';
7
+ export type BundlerProvider = 'STACKUP' | 'PIMLICO' | 'ALCHEMY' | 'GELATO';
8
+ export type ERC20Paymaster = Address;
9
+ export type PaymasterType = 'NONE' | 'SPONSOR' | ERC20Paymaster;
10
+ export declare const getZeroDevBundlerRPC: (projectId: string, provider?: BundlerProvider) => string;
11
+ export declare const getZeroDevPaymasterRPC: (projectId: string, provider?: BundlerProvider) => string;
12
+ export declare const createEcdsaKernelAccountClient: <entryPoint extends EntryPoint, TChain extends Chain | undefined = Chain | undefined, TSource extends string = "custom", TAddress extends `0x${string}` = `0x${string}`>({ bundlerRpc, chain, paymasterRpc, projectId, signer, provider, index, paymaster, entryPointAddress, kernelVersion, ecdsaValidator, }: {
13
+ bundlerRpc?: string;
14
+ chain: TChain;
15
+ projectId: string;
16
+ signer: SmartAccountSigner<TSource, TAddress>;
17
+ paymaster: PaymasterType;
18
+ paymasterRpc?: string;
19
+ entryPointAddress: entryPoint;
20
+ provider?: BundlerProvider;
21
+ index?: bigint;
22
+ kernelVersion: GetKernelVersion<entryPoint>;
23
+ ecdsaValidator: any;
24
+ }) => Promise<KernelAccountClient<entryPoint, HttpTransport, TChain, KernelSmartAccount<entryPoint, HttpTransport, TChain>>>;
@@ -0,0 +1,73 @@
1
+ 'use client'
2
+ import { __awaiter } from '../../_virtual/_tslib.js';
3
+ import { createKernelAccount } from '@zerodev/sdk/accounts';
4
+ import { createZeroDevPaymasterClient, createKernelAccountClient } from '@zerodev/sdk/clients';
5
+ import { createPublicClient, http, isAddress } from 'viem';
6
+
7
+ const getZeroDevBundlerRPC = (projectId, provider) => {
8
+ let rpc = `https://rpc.zerodev.app/api/v2/bundler/${projectId}`;
9
+ if (provider) {
10
+ rpc += `?bundlerProvider=${provider}`;
11
+ }
12
+ return rpc;
13
+ };
14
+ const getZeroDevPaymasterRPC = (projectId, provider) => {
15
+ let rpc = `https://rpc.zerodev.app/api/v2/paymaster/${projectId}`;
16
+ if (provider) {
17
+ rpc += `?paymasterProvider=${provider}`;
18
+ }
19
+ return rpc;
20
+ };
21
+ const isERC20 = (value) => isAddress(value);
22
+ const createEcdsaKernelAccountClient = (_a) => __awaiter(void 0, [_a], void 0, function* ({ bundlerRpc, chain, paymasterRpc, projectId, signer, provider, index, paymaster = 'SPONSOR', entryPointAddress, kernelVersion, ecdsaValidator, }) {
23
+ const resolvedBundlerRpc = bundlerRpc !== null && bundlerRpc !== void 0 ? bundlerRpc : getZeroDevBundlerRPC(projectId, provider);
24
+ const resolvedPaymasterRpc = paymasterRpc !== null && paymasterRpc !== void 0 ? paymasterRpc : getZeroDevPaymasterRPC(projectId, provider);
25
+ const publicClient = createPublicClient({
26
+ transport: http(resolvedBundlerRpc),
27
+ });
28
+ const validator = yield ecdsaValidator(publicClient, {
29
+ entryPoint: entryPointAddress,
30
+ kernelVersion,
31
+ signer,
32
+ });
33
+ const account = yield createKernelAccount(publicClient, {
34
+ entryPoint: entryPointAddress,
35
+ index,
36
+ kernelVersion,
37
+ plugins: {
38
+ sudo: validator,
39
+ },
40
+ });
41
+ const zerodevPaymaster = createZeroDevPaymasterClient({
42
+ chain: chain,
43
+ entryPoint: entryPointAddress,
44
+ transport: http(resolvedPaymasterRpc),
45
+ });
46
+ const kernelClient = createKernelAccountClient({
47
+ account,
48
+ bundlerTransport: http(resolvedBundlerRpc),
49
+ chain,
50
+ entryPoint: entryPointAddress,
51
+ middleware: paymaster !== 'NONE'
52
+ ? {
53
+ sponsorUserOperation: (_b) => __awaiter(void 0, [_b], void 0, function* ({ userOperation }) {
54
+ const _userOperation = userOperation;
55
+ if (isERC20(paymaster)) {
56
+ return zerodevPaymaster.sponsorUserOperation({
57
+ entryPoint: entryPointAddress,
58
+ gasToken: paymaster,
59
+ userOperation: _userOperation,
60
+ });
61
+ }
62
+ return zerodevPaymaster.sponsorUserOperation({
63
+ entryPoint: entryPointAddress,
64
+ userOperation: _userOperation,
65
+ });
66
+ }),
67
+ }
68
+ : undefined,
69
+ });
70
+ return kernelClient;
71
+ });
72
+
73
+ export { createEcdsaKernelAccountClient, getZeroDevBundlerRPC, getZeroDevPaymasterRPC };
@@ -0,0 +1,29 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var _tslib = require('../../_virtual/_tslib.cjs');
7
+ var viemUtils = require('@dynamic-labs/viem-utils');
8
+ var logger = require('./logger.cjs');
9
+
10
+ const cachedChainIdKey = 'dynamic_zd_chain_id';
11
+ const fetchChain = (projectId) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
12
+ try {
13
+ const cachedChainId = localStorage.getItem(cachedChainIdKey);
14
+ if (cachedChainId) {
15
+ return viemUtils.chainsMap[cachedChainId];
16
+ }
17
+ const response = yield (yield fetch(`https://prod-api.zerodev.app/projects/${projectId}`)).json();
18
+ localStorage.setItem(cachedChainIdKey, response.chainId);
19
+ return viemUtils.chainsMap[response.chainId];
20
+ }
21
+ catch (err) {
22
+ logger.logger.error('Failed to fetch ZeroDev project settings from ZeroDev API ' +
23
+ 'This is needed to fetch the chain id so that we know which chain the smart contract is on');
24
+ throw err;
25
+ }
26
+ });
27
+
28
+ exports.cachedChainIdKey = cachedChainIdKey;
29
+ exports.fetchChain = fetchChain;
@@ -0,0 +1,3 @@
1
+ import { type Chain } from 'viem/chains';
2
+ export declare const cachedChainIdKey = "dynamic_zd_chain_id";
3
+ export declare const fetchChain: (projectId: string) => Promise<Chain>;
@@ -0,0 +1,24 @@
1
+ 'use client'
2
+ import { __awaiter } from '../../_virtual/_tslib.js';
3
+ import { chainsMap } from '@dynamic-labs/viem-utils';
4
+ import { logger } from './logger.js';
5
+
6
+ const cachedChainIdKey = 'dynamic_zd_chain_id';
7
+ const fetchChain = (projectId) => __awaiter(void 0, void 0, void 0, function* () {
8
+ try {
9
+ const cachedChainId = localStorage.getItem(cachedChainIdKey);
10
+ if (cachedChainId) {
11
+ return chainsMap[cachedChainId];
12
+ }
13
+ const response = yield (yield fetch(`https://prod-api.zerodev.app/projects/${projectId}`)).json();
14
+ localStorage.setItem(cachedChainIdKey, response.chainId);
15
+ return chainsMap[response.chainId];
16
+ }
17
+ catch (err) {
18
+ logger.error('Failed to fetch ZeroDev project settings from ZeroDev API ' +
19
+ 'This is needed to fetch the chain id so that we know which chain the smart contract is on');
20
+ throw err;
21
+ }
22
+ });
23
+
24
+ export { cachedChainIdKey, fetchChain };
@@ -0,0 +1,4 @@
1
+ export * from './fetchChain';
2
+ export * from './logger';
3
+ export * from './isZeroDevConnector';
4
+ export * from './createEcsdaKernelAccountClient';