@dynamic-labs/ethereum-aa 4.11.1 → 4.12.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,91 @@
1
+ 'use client'
2
+ import { __awaiter } from '../../../_virtual/_tslib.js';
3
+ import { encodeModuleData } from 'zksync-sso/utils';
4
+ import { keccak256, toHex } from 'viem';
5
+ import { AAFactoryABI } from '../abi.js';
6
+ import { getEncodedPasskeyModuleData } from './passkeys.js';
7
+ import { getSalt } from './getSalt.js';
8
+
9
+ /**
10
+ * Returns the parameters needed for deploying the smart account
11
+ * @returns Deployment parameters object
12
+ * @testable This method can be overridden in tests
13
+ */
14
+ const getDeploymentParameters = (_a) => __awaiter(void 0, [_a], void 0, function* ({ eoaAddress, enablePasskeys, passkeyAddress, sessionKeyAddress, saltText, }) {
15
+ const initialValidators = [];
16
+ const initialK1Owners = [eoaAddress];
17
+ // TODO: revisit this logic, maybe we should always add the passkey module data,
18
+ // otherwise if people active passkeys later, old accounts will not be able to use passkey module
19
+ if (enablePasskeys && passkeyAddress) {
20
+ const encodedPasskeyModuleData = yield getEncodedPasskeyModuleData({
21
+ passkeyAddress,
22
+ });
23
+ initialValidators.push(encodedPasskeyModuleData);
24
+ }
25
+ if (sessionKeyAddress) {
26
+ const encodedSessionKeyModuleData = encodeModuleData({
27
+ address: sessionKeyAddress,
28
+ parameters: '0x',
29
+ });
30
+ initialValidators.push(encodedSessionKeyModuleData);
31
+ }
32
+ return {
33
+ initialK1Owners,
34
+ initialValidators,
35
+ salt: getSalt(saltText),
36
+ };
37
+ });
38
+ /**
39
+ * Simulates the smart account deployment
40
+ * @param publicClient - The public client to use for simulation
41
+ * @param walletClient - The wallet client to use for account information
42
+ * @param params - The deployment parameters
43
+ * @returns The simulation result
44
+ * @throws {ZkSyncConnectorError} If simulation fails
45
+ * @testable This method can be mocked in tests
46
+ */
47
+ const simulateAccountDeployment = (publicClient_1, walletClient_1, params_1, factoryAddress_1, ...args_1) => __awaiter(void 0, [publicClient_1, walletClient_1, params_1, factoryAddress_1, ...args_1], void 0, function* (publicClient, walletClient, params, factoryAddress, checkBalance = true) {
48
+ var _b, _c, _d;
49
+ // this is to keep the same salt as calculated on zksync sso package
50
+ const salt = keccak256(toHex(params.salt));
51
+ const gasEstimate = yield publicClient.estimateContractGas({
52
+ abi: AAFactoryABI,
53
+ account: (_b = walletClient.account) === null || _b === void 0 ? void 0 : _b.address,
54
+ address: factoryAddress,
55
+ args: [salt, params.initialValidators, params.initialK1Owners],
56
+ functionName: 'deployProxySsoAccount',
57
+ });
58
+ if (checkBalance) {
59
+ const balance = yield publicClient.getBalance({
60
+ address: (_c = walletClient.account) === null || _c === void 0 ? void 0 : _c.address,
61
+ });
62
+ if (balance < gasEstimate) {
63
+ throw new Error('Insufficient balance to deploy account');
64
+ }
65
+ }
66
+ // Simulate the contract call
67
+ return publicClient.simulateContract({
68
+ abi: AAFactoryABI,
69
+ account: (_d = walletClient.account) === null || _d === void 0 ? void 0 : _d.address,
70
+ address: factoryAddress,
71
+ args: [salt, params.initialValidators, params.initialK1Owners],
72
+ functionName: 'deployProxySsoAccount',
73
+ });
74
+ });
75
+ /**
76
+ * Executes the smart account deployment transaction
77
+ * @param walletClient - The wallet client to use for transaction execution
78
+ * @param request - The deployment request object containing transaction parameters
79
+ * @returns Promise that resolves when the deployment transaction is confirmed
80
+ * @throws {DynamicError} If the smart account address is not initialized
81
+ * @throws {Error} If transaction execution or confirmation fails
82
+ */
83
+ const executeAccountDeployment = (walletClient, publicClient, request, smartAccountAddress) => __awaiter(void 0, void 0, void 0, function* () {
84
+ if (!smartAccountAddress) {
85
+ throw new Error('Smart account address is not initialized');
86
+ }
87
+ const tx = yield walletClient.writeContract(request);
88
+ yield publicClient.waitForTransactionReceipt({ hash: tx });
89
+ });
90
+
91
+ export { executeAccountDeployment, getDeploymentParameters, simulateAccountDeployment };
@@ -0,0 +1,10 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var viem = require('viem');
7
+
8
+ const getSalt = (saltText = 'DynamicLabs') => `0x${Buffer.from(viem.toBytes(saltText, { size: 32 })).toString('hex')}`;
9
+
10
+ exports.getSalt = getSalt;
@@ -0,0 +1 @@
1
+ export declare const getSalt: (saltText?: string) => `0x${string}`;
@@ -0,0 +1,6 @@
1
+ 'use client'
2
+ import { toBytes } from 'viem';
3
+
4
+ const getSalt = (saltText = 'DynamicLabs') => `0x${Buffer.from(toBytes(saltText, { size: 32 })).toString('hex')}`;
5
+
6
+ export { getSalt };
@@ -0,0 +1,4 @@
1
+ export * from './getSalt';
2
+ export * from './passkeys';
3
+ export * from './deployment';
4
+ export * from './network';
@@ -0,0 +1,17 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var _tslib = require('../../../_virtual/_tslib.cjs');
7
+
8
+ const ensureEoaConnectorNetwork = (eoaConnector, chainId) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
9
+ const eoaNetwork = yield eoaConnector.getNetwork();
10
+ if (Number(eoaNetwork) !== chainId) {
11
+ yield eoaConnector.switchNetwork({
12
+ networkChainId: chainId,
13
+ });
14
+ }
15
+ });
16
+
17
+ exports.ensureEoaConnectorNetwork = ensureEoaConnectorNetwork;
@@ -0,0 +1,2 @@
1
+ import { WalletConnector } from '@dynamic-labs/wallet-connector-core';
2
+ export declare const ensureEoaConnectorNetwork: (eoaConnector: WalletConnector, chainId: number) => Promise<void>;
@@ -0,0 +1,13 @@
1
+ 'use client'
2
+ import { __awaiter } from '../../../_virtual/_tslib.js';
3
+
4
+ const ensureEoaConnectorNetwork = (eoaConnector, chainId) => __awaiter(void 0, void 0, void 0, function* () {
5
+ const eoaNetwork = yield eoaConnector.getNetwork();
6
+ if (Number(eoaNetwork) !== chainId) {
7
+ yield eoaConnector.switchNetwork({
8
+ networkChainId: chainId,
9
+ });
10
+ }
11
+ });
12
+
13
+ export { ensureEoaConnectorNetwork };
@@ -0,0 +1,41 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var _tslib = require('../../../_virtual/_tslib.cjs');
7
+ var utils$1 = require('zksync-sso/utils');
8
+ var utils = require('@dynamic-labs/utils');
9
+
10
+ /**
11
+ * Retrieves and encodes passkey module data for smart account deployment
12
+ * @returns Encoded passkey module data as a hex string
13
+ * @throws {Error} If credential public key or ID is not found
14
+ * @private
15
+ */
16
+ const getEncodedPasskeyModuleData = (_a) => _tslib.__awaiter(void 0, [_a], void 0, function* ({ passkeyAddress, }) {
17
+ if (!passkeyAddress) {
18
+ throw new Error('Passkey address is not initialized');
19
+ }
20
+ const storedCredentialPublicKey = utils.StorageService.getItem('credentialPublicKey');
21
+ if (!storedCredentialPublicKey) {
22
+ throw new Error('Credential public key not found');
23
+ }
24
+ const credentialPublicKey = new Uint8Array(Object.values(storedCredentialPublicKey));
25
+ const credentialId = utils.StorageService.getItem('credentialId');
26
+ if (!credentialId) {
27
+ throw new Error('Credential ID not found');
28
+ }
29
+ const passkeyPublicKey = utils$1.getPublicKeyBytesFromPasskeySignature(credentialPublicKey);
30
+ const encodedPasskeyParameters = utils$1.encodePasskeyModuleParameters({
31
+ credentialId: credentialId,
32
+ expectedOrigin: window.location.origin,
33
+ passkeyPublicKey,
34
+ });
35
+ return utils$1.encodeModuleData({
36
+ address: passkeyAddress,
37
+ parameters: encodedPasskeyParameters,
38
+ });
39
+ });
40
+
41
+ exports.getEncodedPasskeyModuleData = getEncodedPasskeyModuleData;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Retrieves and encodes passkey module data for smart account deployment
3
+ * @returns Encoded passkey module data as a hex string
4
+ * @throws {Error} If credential public key or ID is not found
5
+ * @private
6
+ */
7
+ export declare const getEncodedPasskeyModuleData: ({ passkeyAddress, }: {
8
+ passkeyAddress?: `0x${string}`;
9
+ }) => Promise<`0x${string}`>;
@@ -0,0 +1,37 @@
1
+ 'use client'
2
+ import { __awaiter } from '../../../_virtual/_tslib.js';
3
+ import { getPublicKeyBytesFromPasskeySignature, encodePasskeyModuleParameters, encodeModuleData } from 'zksync-sso/utils';
4
+ import { StorageService } from '@dynamic-labs/utils';
5
+
6
+ /**
7
+ * Retrieves and encodes passkey module data for smart account deployment
8
+ * @returns Encoded passkey module data as a hex string
9
+ * @throws {Error} If credential public key or ID is not found
10
+ * @private
11
+ */
12
+ const getEncodedPasskeyModuleData = (_a) => __awaiter(void 0, [_a], void 0, function* ({ passkeyAddress, }) {
13
+ if (!passkeyAddress) {
14
+ throw new Error('Passkey address is not initialized');
15
+ }
16
+ const storedCredentialPublicKey = StorageService.getItem('credentialPublicKey');
17
+ if (!storedCredentialPublicKey) {
18
+ throw new Error('Credential public key not found');
19
+ }
20
+ const credentialPublicKey = new Uint8Array(Object.values(storedCredentialPublicKey));
21
+ const credentialId = StorageService.getItem('credentialId');
22
+ if (!credentialId) {
23
+ throw new Error('Credential ID not found');
24
+ }
25
+ const passkeyPublicKey = getPublicKeyBytesFromPasskeySignature(credentialPublicKey);
26
+ const encodedPasskeyParameters = encodePasskeyModuleParameters({
27
+ credentialId: credentialId,
28
+ expectedOrigin: window.location.origin,
29
+ passkeyPublicKey,
30
+ });
31
+ return encodeModuleData({
32
+ address: passkeyAddress,
33
+ parameters: encodedPasskeyParameters,
34
+ });
35
+ });
36
+
37
+ export { getEncodedPasskeyModuleData };