@dynamic-labs/ethereum-aa-zksync 4.14.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,520 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var _tslib = require('../../_virtual/_tslib.cjs');
7
+ var viem = require('viem');
8
+ var zksync = require('viem/zksync');
9
+ var passkey = require('zksync-sso/client/passkey');
10
+ var utils$1 = require('zksync-sso/utils');
11
+ var client = require('zksync-sso/client');
12
+ var ecdsa = require('zksync-sso/client/ecdsa');
13
+ var ethereumCore = require('@dynamic-labs/ethereum-core');
14
+ var utils = require('@dynamic-labs/utils');
15
+ var ethereumAaCore = require('@dynamic-labs/ethereum-aa-core');
16
+ var getSalt = require('../utils/getSalt.cjs');
17
+ var passkeys = require('../utils/passkeys.cjs');
18
+ var deployment = require('../utils/deployment.cjs');
19
+ var network = require('../utils/network.cjs');
20
+
21
+ class ZKsyncConnector extends ethereumAaCore.AccountAbstractionBaseConnector {
22
+ constructor(props) {
23
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
24
+ super(props);
25
+ // Core wallet properties
26
+ this.ChainWallet = ethereumCore.EthereumWallet;
27
+ this.name = 'ZKsync';
28
+ this.overrideKey = 'zksync';
29
+ // Chain configuration
30
+ this.connectedChain = 'EVM';
31
+ this.evmNetworks = [];
32
+ this.supportedChains = ['ETH', 'EVM'];
33
+ this._isSmartAccountDeployed = false;
34
+ this.enablePasskeys = true;
35
+ this.enableEIP7702Mode = false;
36
+ this.walletFallback = {
37
+ brand: {
38
+ alt: 'Smart Wallet',
39
+ spriteId: 'smartwallet',
40
+ },
41
+ name: 'ZKsync',
42
+ };
43
+ this._walletUiUtils = props.walletUiUtils;
44
+ this.providersConfig = (_a = props.providersConfig) !== null && _a !== void 0 ? _a : {};
45
+ this.chainId = (_b = props.apiProviders.zksync) === null || _b === void 0 ? void 0 : _b.defaultChainId;
46
+ this.evmNetworks = utils.parseEvmNetworks(props.evmNetworks).filter((network) => network.chainId === this.chainId);
47
+ this.factoryAddress = (_c = props.apiProviders.zksync) === null || _c === void 0 ? void 0 : _c.factoryAddress; // Fallback if not provided
48
+ this.paymasterAddress = (_d = props.apiProviders.zksync) === null || _d === void 0 ? void 0 : _d.paymasterAddress; // Fallback if not provided
49
+ this.passkeyAddress = (_e = props.apiProviders.zksync) === null || _e === void 0 ? void 0 : _e.passkeyAddress; // Fallback if not provided
50
+ this.sessionKeyAddress = (_f = props.apiProviders.zksync) === null || _f === void 0 ? void 0 : _f.sessionAddress; // Fallback if not provided
51
+ this.saltText = (_g = props.apiProviders.zksync) === null || _g === void 0 ? void 0 : _g.salt;
52
+ this.enablePasskeys =
53
+ (_j = (_h = props.settings.sdk.accountAbstraction) === null || _h === void 0 ? void 0 : _h.enablePasskeys) !== null && _j !== void 0 ? _j : false;
54
+ }
55
+ getEnabledNetworks() {
56
+ return this.evmNetworks;
57
+ }
58
+ getConnectedAccounts() {
59
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
60
+ var _a;
61
+ yield ((_a = this.initializedDeferredPromise) === null || _a === void 0 ? void 0 : _a.promise);
62
+ if (!this.smartAccountAddress) {
63
+ throw new Error('Smart account address is not initialized');
64
+ }
65
+ return [this.smartAccountAddress];
66
+ });
67
+ }
68
+ getNetwork() {
69
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
70
+ var _a;
71
+ yield ((_a = this.initializedDeferredPromise) === null || _a === void 0 ? void 0 : _a.promise);
72
+ if (!this.smartAccountAddress) {
73
+ throw new Error('Smart account address is not initialized');
74
+ }
75
+ return this.chainId;
76
+ });
77
+ }
78
+ getPublicClient() {
79
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
80
+ return viem.createPublicClient({
81
+ chain: ethereumCore.getOrMapViemChain(this.evmNetworks[0]),
82
+ transport: viem.http(),
83
+ });
84
+ });
85
+ }
86
+ canSponsorTransactionGas() {
87
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
88
+ return false;
89
+ });
90
+ }
91
+ getAccountAbstractionProvider() {
92
+ return this.smartAccount;
93
+ }
94
+ getWalletClient(chainId) {
95
+ const smartAccountClient = this.smartAccount;
96
+ if (!smartAccountClient) {
97
+ return undefined;
98
+ }
99
+ const transport = this.getWalletClientTransport(smartAccountClient);
100
+ return viem.createWalletClient({
101
+ account: smartAccountClient.account,
102
+ chain: chainId ? ethereumCore.chainsMap[chainId] : smartAccountClient.chain,
103
+ transport,
104
+ });
105
+ }
106
+ getWalletClientTransport(smartAccountClient) {
107
+ return viem.custom({
108
+ request: (_a) => _tslib.__awaiter(this, [_a], void 0, function* ({ method, params, }) {
109
+ const handleRequest = () => _tslib.__awaiter(this, void 0, void 0, function* () {
110
+ var _b;
111
+ switch (method) {
112
+ case 'personal_sign':
113
+ // Delegate signing to the smart account client instance
114
+ return smartAccountClient.signMessage({ message: params[0] });
115
+ case 'eth_signTypedData_v4': {
116
+ // Delegate typed data signing to the smart account client instance
117
+ // Note: ZKsync usually expects JSON string for EIP712, but viem standard passes object.
118
+ // The smartAccountClient's signTypedData should handle the expected format internally.
119
+ const signTypedDataParams = params[1]; // Assuming standard viem format
120
+ return smartAccountClient.signTypedData(signTypedDataParams);
121
+ }
122
+ case 'eth_sendTransaction':
123
+ // Delegate transaction sending to the smart account client instance.
124
+ // This ensures deployment checks, paymaster logic, etc., are handled.
125
+ return smartAccountClient.sendTransaction(params[0]);
126
+ case 'eth_accounts':
127
+ case 'eth_requestAccounts':
128
+ // Return the smart account address
129
+ return ((_b = smartAccountClient.account) === null || _b === void 0 ? void 0 : _b.address)
130
+ ? [smartAccountClient.account.address]
131
+ : [];
132
+ case 'eth_chainId': {
133
+ // Return chainId from the smart account client
134
+ const chainId = yield smartAccountClient.getChainId();
135
+ return viem.toHex(chainId);
136
+ }
137
+ default: {
138
+ // For other read-only methods, delegate to a basic public client transport.
139
+ // Avoid creating a new client/transport on every call if possible,
140
+ // but for simplicity here, we create it transiently.
141
+ // Consider caching the provided client if performance becomes an issue.
142
+ return smartAccountClient.request({ method, params });
143
+ }
144
+ }
145
+ });
146
+ // The internal confirmationTransport used by smartAccountClient via its 'owner'
147
+ // should handle the UI prompts. We don't need to add extra prompts here.
148
+ return handleRequest();
149
+ }),
150
+ });
151
+ }
152
+ getTransport(provider) {
153
+ const transport = ethereumCore.confirmationTransport({
154
+ getAccounts: () => _tslib.__awaiter(this, void 0, void 0, function* () { return [provider.account.address]; }),
155
+ onPersonalSign: (_a) => _tslib.__awaiter(this, [_a], void 0, function* ({ message }) {
156
+ this._walletUiUtils.disabledConfirmationOnce();
157
+ if (!this._isSmartAccountDeployed) {
158
+ yield this.deploySmartAccount({
159
+ withPaymaster: true,
160
+ });
161
+ }
162
+ this._walletUiUtils.disabledConfirmationOnce();
163
+ return provider.signMessage({
164
+ message,
165
+ });
166
+ }),
167
+ onSignTypedData: (_b) => _tslib.__awaiter(this, [_b], void 0, function* ({ message }) {
168
+ this._walletUiUtils.disabledConfirmationOnce();
169
+ const signTypedData = JSON.parse(message);
170
+ if (!this._isSmartAccountDeployed) {
171
+ yield this.deploySmartAccount({
172
+ withPaymaster: true,
173
+ });
174
+ }
175
+ this._walletUiUtils.disabledConfirmationOnce();
176
+ return provider.signTypedData({
177
+ domain: signTypedData.domain,
178
+ message: signTypedData.message,
179
+ primaryType: signTypedData.primaryType,
180
+ types: signTypedData.types,
181
+ });
182
+ }),
183
+ provider: provider.extend(viem.publicActions),
184
+ transport: viem.custom(provider, this.providersConfig.httpTransportConfig),
185
+ walletConnector: this,
186
+ walletUiUtils: this._walletUiUtils,
187
+ });
188
+ return transport;
189
+ }
190
+ createEcdsaClient(eoaConnector) {
191
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
192
+ const eoaSigner = yield (eoaConnector === null || eoaConnector === void 0 ? void 0 : eoaConnector.getSigner());
193
+ if (!eoaSigner) {
194
+ throw new Error('EOA wallet client not found');
195
+ }
196
+ if (!this.smartAccountAddress) {
197
+ throw new Error('Smart account address is not initialized');
198
+ }
199
+ const transport = this.getTransport(eoaSigner);
200
+ const ownerWithTransport = viem.createWalletClient(Object.assign(Object.assign({}, eoaSigner), { transport }));
201
+ const walletClient = yield ecdsa.createZksyncEcdsaClient({
202
+ address: this.smartAccountAddress,
203
+ chain: ethereumCore.getOrMapViemChain(this.evmNetworks[0]),
204
+ contracts: {
205
+ accountFactory: this.factoryAddress,
206
+ session: this.sessionKeyAddress,
207
+ },
208
+ owner: ownerWithTransport,
209
+ transport: viem.custom(ownerWithTransport),
210
+ });
211
+ return walletClient;
212
+ });
213
+ }
214
+ /**
215
+ * Implementation of the interface method required by IAccountAbstractionWalletConnector
216
+ * @param eoaConnector - The EOA connector to use for signing transactions
217
+ * @param smartAccountAddress - The address of the smart account
218
+ */
219
+ initialize(_a) {
220
+ return _tslib.__awaiter(this, arguments, void 0, function* ({ eoaConnector, smartWalletAddress, }) {
221
+ this.initializedDeferredPromise = this.initializedDeferredPromise
222
+ ? this.initializedDeferredPromise
223
+ : new utils.DeferredPromise();
224
+ this.eoaConnector = eoaConnector;
225
+ this.eoaAddress = (yield eoaConnector.getAddress());
226
+ this.smartAccountAddress = smartWalletAddress;
227
+ this.smartAccount = (yield this.createEcdsaClient(eoaConnector));
228
+ yield this.checkIsDeployed();
229
+ this.initializedDeferredPromise.resolve();
230
+ });
231
+ }
232
+ createSessionClient(sessionKey, sessionConfig) {
233
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
234
+ if (!this.smartAccountAddress) {
235
+ throw new Error('Smart account address is not initialized');
236
+ }
237
+ if (!this.sessionKeyAddress) {
238
+ throw new Error('Session key address is not initialized');
239
+ }
240
+ return client.createZksyncSessionClient({
241
+ address: this.smartAccountAddress,
242
+ chain: ethereumCore.getOrMapViemChain(this.evmNetworks[0]),
243
+ contracts: {
244
+ session: this.sessionKeyAddress,
245
+ },
246
+ sessionConfig,
247
+ sessionKey,
248
+ transport: viem.http(),
249
+ });
250
+ });
251
+ }
252
+ /**
253
+ * Creates a passkey client for ZKsync authentication
254
+ *
255
+ * This method initializes a passkey client using stored credentials and
256
+ * configures it with the necessary contract addresses and chain information.
257
+ *
258
+ */
259
+ createPasskeyClient() {
260
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
261
+ if (!this.smartAccountAddress) {
262
+ throw new Error('Smart account address is not initialized');
263
+ }
264
+ if (!this.passkeyAddress) {
265
+ throw new Error('Passkey address is not initialized');
266
+ }
267
+ const storedCredentialPublicKey = utils.StorageService.getItem('credentialPublicKey');
268
+ if (!storedCredentialPublicKey) {
269
+ throw new Error('Credential public key not found');
270
+ }
271
+ const credentialPublicKey = new Uint8Array(Object.values(storedCredentialPublicKey));
272
+ const publicClient = yield this.getTypedPublicClient();
273
+ const { chain } = publicClient;
274
+ if (!chain) {
275
+ throw new Error('Chain information not available');
276
+ }
277
+ const contractAddresses = {
278
+ accountFactory: this.factoryAddress,
279
+ passkey: this.passkeyAddress,
280
+ recovery: this.recoveryAddress,
281
+ session: this.sessionKeyAddress,
282
+ };
283
+ this.passkeyClient = passkey.createZksyncPasskeyClient({
284
+ address: this.smartAccountAddress,
285
+ chain,
286
+ contracts: contractAddresses,
287
+ credentialPublicKey,
288
+ transport: viem.http(),
289
+ userDisplayName: this.smartAccountAddress,
290
+ userName: this.smartAccountAddress,
291
+ });
292
+ });
293
+ }
294
+ registerPasskeyAndDeploy() {
295
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
296
+ if (!this.smartAccountAddress) {
297
+ throw new Error('Smart account address is not initialized');
298
+ }
299
+ const { credentialPublicKey, credentialId } = yield passkey.registerNewPasskey({
300
+ attestationType: 'none',
301
+ authenticatorSelection: {
302
+ authenticatorAttachment: 'platform',
303
+ requireResidentKey: false,
304
+ userVerification: 'required',
305
+ },
306
+ userDisplayName: this.smartAccountAddress,
307
+ userName: this.smartAccountAddress,
308
+ });
309
+ utils.StorageService.setItem('credentialPublicKey', credentialPublicKey);
310
+ utils.StorageService.setItem('credentialId', credentialId);
311
+ yield this.deploySmartAccount({
312
+ withPaymaster: true,
313
+ });
314
+ });
315
+ }
316
+ deploySmartAccount(_a) {
317
+ return _tslib.__awaiter(this, arguments, void 0, function* ({ withPaymaster, }) {
318
+ if (!this.chainId) {
319
+ throw new Error('Chain ID is not initialized');
320
+ }
321
+ if (!this.eoaConnector) {
322
+ throw new Error('EOA connector is not initialized');
323
+ }
324
+ yield network.ensureEoaConnectorNetwork(this.eoaConnector, this.chainId);
325
+ if (withPaymaster && this.paymasterAddress) {
326
+ yield this.deploySmartAccountWithPaymaster();
327
+ }
328
+ else {
329
+ yield this.deploySmartAccountWithoutPaymaster();
330
+ }
331
+ });
332
+ }
333
+ deploySmartAccountWithPaymaster() {
334
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
335
+ if (!this.paymasterAddress) {
336
+ throw new Error('Paymaster address is not initialized');
337
+ }
338
+ if (!this.smartAccountAddress) {
339
+ throw new Error('Smart account address is not initialized');
340
+ }
341
+ if (!this.eoaAddress) {
342
+ throw new Error('EOA address is not initialized');
343
+ }
344
+ const paymaster = this.paymasterAddress;
345
+ const publicClient = yield this.getTypedPublicClient();
346
+ const walletClient = yield this.getTypedWalletClient();
347
+ const deploymentParams = yield deployment.getDeploymentParameters({
348
+ enablePasskeys: this.enablePasskeys,
349
+ eoaAddress: this.eoaAddress,
350
+ passkeyAddress: this.passkeyAddress,
351
+ saltText: this.saltText,
352
+ sessionKeyAddress: this.sessionKeyAddress,
353
+ });
354
+ const { request } = yield deployment.simulateAccountDeployment(publicClient, walletClient, deploymentParams, this.factoryAddress, false);
355
+ const paymasterInput = zksync.getGeneralPaymasterInput({
356
+ innerInput: new Uint8Array(),
357
+ });
358
+ const requestWithPaymaster = Object.assign(Object.assign({}, request), { paymaster,
359
+ paymasterInput });
360
+ yield deployment.executeAccountDeployment(walletClient, publicClient, requestWithPaymaster, this.smartAccountAddress);
361
+ this._isSmartAccountDeployed = true;
362
+ });
363
+ }
364
+ /**
365
+ * Deploys a new smart account contract
366
+ * @throws {ZkSyncConnectorError} If deployment fails
367
+ * @testable This method can be mocked in tests
368
+ */
369
+ deploySmartAccountWithoutPaymaster() {
370
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
371
+ if (!this.eoaConnector) {
372
+ throw new Error('EOA connector is not initialized');
373
+ }
374
+ if (!this.smartAccountAddress) {
375
+ throw new Error('Smart account address is not initialized');
376
+ }
377
+ const walletClient = yield this.getTypedWalletClient();
378
+ const publicClient = yield this.getTypedPublicClient();
379
+ const deploymentParams = yield this.getDeploymentParameters();
380
+ // Simulate the contract deployment first
381
+ const { request } = yield deployment.simulateAccountDeployment(publicClient, walletClient, deploymentParams, this.factoryAddress, false);
382
+ // Execute the deployment transaction
383
+ yield deployment.executeAccountDeployment(walletClient, publicClient, request, this.smartAccountAddress);
384
+ });
385
+ }
386
+ /**
387
+ * Gets typed public client from EOA connector
388
+ * @throws {ZkSyncConnectorError} If public client cannot be obtained
389
+ * @testable This method can be mocked in tests
390
+ */
391
+ getTypedPublicClient() {
392
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
393
+ if (!this.eoaConnector) {
394
+ throw new Error('EOA connector is not initialized');
395
+ }
396
+ const publicClient = yield this.eoaConnector.getPublicClient();
397
+ if (!publicClient) {
398
+ throw new Error('Failed to get public client from EOA connector');
399
+ }
400
+ return publicClient;
401
+ });
402
+ }
403
+ /**
404
+ * Gets typed wallet client from EOA connector on the correct chain
405
+ * @throws {ZkSyncConnectorError} If wallet client cannot be obtained
406
+ * @testable This method can be mocked in tests
407
+ */
408
+ getTypedWalletClient() {
409
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
410
+ if (!this.eoaConnector) {
411
+ throw new Error('EOA connector is not initialized');
412
+ }
413
+ const walletClient = yield this.eoaConnector.getWalletClient();
414
+ if (!walletClient) {
415
+ throw new Error('Failed to get wallet client from EOA connector');
416
+ }
417
+ return walletClient
418
+ .extend(viem.publicActions)
419
+ .extend(viem.walletActions)
420
+ .extend(zksync.eip712WalletActions())
421
+ .extend(ecdsa.zksyncSsoEcdsaWalletActions);
422
+ });
423
+ }
424
+ /**
425
+ * Returns the parameters needed for deploying the smart account
426
+ * @returns Deployment parameters object
427
+ * @testable This method can be overridden in tests
428
+ */
429
+ getDeploymentParameters() {
430
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
431
+ if (!this.eoaAddress) {
432
+ throw new Error('EOA address is not initialized');
433
+ }
434
+ const initialValidators = [];
435
+ const initialK1Owners = [this.eoaAddress];
436
+ // TODO: revisit this logic, maybe we should always add the passkey module data,
437
+ // otherwise if people active passkeys later, old accounts will not be able to use passkey module
438
+ if (this.enablePasskeys && this.passkeyAddress) {
439
+ const encodedPasskeyModuleData = yield passkeys.getEncodedPasskeyModuleData({
440
+ passkeyAddress: this.passkeyAddress,
441
+ });
442
+ initialValidators.push(encodedPasskeyModuleData);
443
+ }
444
+ if (this.sessionKeyAddress) {
445
+ const encodedSessionKeyModuleData = utils$1.encodeModuleData({
446
+ address: this.sessionKeyAddress,
447
+ parameters: '0x',
448
+ });
449
+ initialValidators.push(encodedSessionKeyModuleData);
450
+ }
451
+ return {
452
+ initialK1Owners,
453
+ initialValidators,
454
+ salt: getSalt.getSalt(this.saltText),
455
+ };
456
+ });
457
+ }
458
+ signMessage(message) {
459
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
460
+ const walletClient = this.getWalletClient();
461
+ if (!walletClient) {
462
+ throw new Error('Error fetching signer');
463
+ }
464
+ return walletClient.signMessage({
465
+ account: walletClient.account,
466
+ message,
467
+ });
468
+ });
469
+ }
470
+ /**
471
+ * Checks if a smart account is deployed
472
+ * @throws {ZkSyncConnectorError} If the check fails
473
+ * @returns Promise that resolves when the check is complete
474
+ */
475
+ checkIsDeployed() {
476
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
477
+ if (!this.smartAccountAddress) {
478
+ throw new Error('Smart account address is not initialized');
479
+ }
480
+ const client = yield this.getPublicClient();
481
+ if (!client) {
482
+ throw new Error('Failed to get public client from EOA connector');
483
+ }
484
+ const code = yield client.getCode({
485
+ address: this.smartAccountAddress,
486
+ });
487
+ const isDeployed = Boolean(code);
488
+ this._isSmartAccountDeployed = isDeployed;
489
+ return isDeployed;
490
+ });
491
+ }
492
+ getAddress() {
493
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
494
+ var _a;
495
+ yield ((_a = this.initializedDeferredPromise) === null || _a === void 0 ? void 0 : _a.promise);
496
+ if (!this.smartAccount) {
497
+ throw new Error('Smart account is not initialized');
498
+ }
499
+ return this.smartAccountAddress;
500
+ });
501
+ }
502
+ validateActiveWallet(expectedAddress) {
503
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
504
+ const currentAddress = yield this.getAddress();
505
+ if (currentAddress !== expectedAddress) {
506
+ throw new utils.DynamicError('Invalid active wallet');
507
+ }
508
+ });
509
+ }
510
+ isSmartAccountDeployed() {
511
+ return _tslib.__awaiter(this, arguments, void 0, function* (refetch = false) {
512
+ if (refetch) {
513
+ yield this.checkIsDeployed();
514
+ }
515
+ return this._isSmartAccountDeployed;
516
+ });
517
+ }
518
+ }
519
+
520
+ exports.ZKsyncConnector = ZKsyncConnector;