@dynamic-labs/waas-evm 4.9.2-preview.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,281 @@
1
+ 'use client'
2
+ import { __awaiter } from '../_virtual/_tslib.js';
3
+ import { createWalletClient, http, erc20Abi } from 'viem';
4
+ import { toAccount } from 'viem/accounts';
5
+ import { DynamicEvmWalletClient } from '@dynamic-labs-wallet/evm';
6
+ import { Logger } from '@dynamic-labs/logger';
7
+ import { isSameAddress } from '@dynamic-labs/wallet-connector-core';
8
+ import { EthereumWalletConnector, getOrMapViemChain, ViemUiTransaction } from '@dynamic-labs/ethereum-core';
9
+ import { DynamicError } from '@dynamic-labs/utils';
10
+
11
+ const logger = new Logger('DynamicWaasConnector');
12
+ class DynamicWaasEVMConnector extends EthereumWalletConnector {
13
+ constructor(props) {
14
+ super(props);
15
+ this.name = 'Dynamic Waas';
16
+ this.overrideKey = 'dynamicwaas';
17
+ this.isEmbeddedWallet = true;
18
+ }
19
+ setGetAuthTokenFunction(getAuthToken) {
20
+ this.getAuthToken = getAuthToken;
21
+ }
22
+ setEnvironmentId(environmentId) {
23
+ this.environmentId = environmentId;
24
+ }
25
+ setBaseApiUrl(baseApiUrl) {
26
+ this.baseApiUrl = baseApiUrl;
27
+ }
28
+ setlastUsedChainId(chainId) {
29
+ if (chainId === undefined) {
30
+ localStorage.removeItem(DynamicWaasEVMConnector.lastUsedChainIdStorageKey);
31
+ }
32
+ else {
33
+ localStorage.setItem(DynamicWaasEVMConnector.lastUsedChainIdStorageKey, chainId.toString());
34
+ }
35
+ }
36
+ getlastUsedChainId() {
37
+ const lastUsedChainIdLS = localStorage.getItem(DynamicWaasEVMConnector.lastUsedChainIdStorageKey);
38
+ if (!lastUsedChainIdLS)
39
+ return undefined;
40
+ try {
41
+ const chainId = parseInt(lastUsedChainIdLS);
42
+ if (isNaN(chainId)) {
43
+ return undefined;
44
+ }
45
+ const isChainCurrentlyEnabled = this.evmNetworks.some((network) => network.chainId === chainId);
46
+ if (!isChainCurrentlyEnabled) {
47
+ const lastUsedChainId = this.evmNetworks[0].chainId;
48
+ this.setlastUsedChainId(lastUsedChainId);
49
+ return lastUsedChainId;
50
+ }
51
+ return chainId;
52
+ }
53
+ catch (err) {
54
+ logger.error(err);
55
+ return undefined;
56
+ }
57
+ }
58
+ currentChainId() {
59
+ var _a, _b, _c;
60
+ return (_a = this.getlastUsedChainId()) !== null && _a !== void 0 ? _a : (_c = (_b = this.evmNetworks) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.chainId;
61
+ }
62
+ getNetwork() {
63
+ return __awaiter(this, void 0, void 0, function* () {
64
+ return this.currentChainId();
65
+ });
66
+ }
67
+ getEvmNetworkByChainId(chainId) {
68
+ return this.evmNetworks.find((network) => network.chainId === chainId);
69
+ }
70
+ currentEvmNetwork() {
71
+ const chainId = this.currentChainId();
72
+ if (!chainId) {
73
+ return undefined;
74
+ }
75
+ return this.getEvmNetworkByChainId(chainId);
76
+ }
77
+ switchNetwork(_a) {
78
+ return __awaiter(this, arguments, void 0, function* ({ networkChainId, }) {
79
+ if (!networkChainId) {
80
+ return;
81
+ }
82
+ let networkChainIdInt = networkChainId;
83
+ if (typeof networkChainId === 'string') {
84
+ networkChainIdInt = parseInt(networkChainId);
85
+ }
86
+ this.setlastUsedChainId(networkChainIdInt);
87
+ this.emit('chainChange', {
88
+ chain: networkChainIdInt.toString(),
89
+ });
90
+ });
91
+ }
92
+ getRpcUrl() {
93
+ var _a;
94
+ const evmNetwork = this.currentEvmNetwork();
95
+ if (!evmNetwork) {
96
+ throw new Error('EVM network not found');
97
+ }
98
+ return ((_a = evmNetwork === null || evmNetwork === void 0 ? void 0 : evmNetwork.privateCustomerRpcUrls) === null || _a === void 0 ? void 0 : _a[0]) || (evmNetwork === null || evmNetwork === void 0 ? void 0 : evmNetwork.rpcUrls[0]);
99
+ }
100
+ setActiveAccountAddress(accountAddress) {
101
+ this.activeAccountAddress = accountAddress;
102
+ }
103
+ createDynamicWaasClient() {
104
+ var _a;
105
+ const authToken = (_a = this.getAuthToken) === null || _a === void 0 ? void 0 : _a.call(this);
106
+ if (!authToken) {
107
+ throw new Error('Auth token is required');
108
+ }
109
+ if (!this.environmentId) {
110
+ throw new Error('Environment ID is required');
111
+ }
112
+ return new DynamicEvmWalletClient({
113
+ authToken,
114
+ baseApiUrl: this.baseApiUrl,
115
+ baseMPCRelayApiUrl: 'relay.dynamic-preprod.xyz',
116
+ environmentId: this.environmentId,
117
+ });
118
+ }
119
+ getWaasWalletClient() {
120
+ if (!this.dynamicWaasClient) {
121
+ this.dynamicWaasClient = this.createDynamicWaasClient();
122
+ }
123
+ return this.dynamicWaasClient;
124
+ }
125
+ validateActiveWallet(expectedAddress) {
126
+ return __awaiter(this, void 0, void 0, function* () {
127
+ const walletClient = this.getWaasWalletClient();
128
+ const targetWallet = yield walletClient.getWallet({
129
+ accountAddress: expectedAddress,
130
+ });
131
+ if (!targetWallet) {
132
+ throw new DynamicError('Account not found');
133
+ }
134
+ const isWalletActive = isSameAddress(targetWallet.accountAddress, this.activeAccountAddress || '', this.connectedChain);
135
+ if (!isWalletActive) {
136
+ this.activeAccountAddress = targetWallet.accountAddress;
137
+ }
138
+ });
139
+ }
140
+ getViemAccount({ accountAddress }) {
141
+ const client = this.getWaasWalletClient();
142
+ const customViemAccount = toAccount({
143
+ address: accountAddress,
144
+ signMessage: ({ message }) => client.signMessage({ accountAddress, message: message }),
145
+ signTransaction: (transaction) => client
146
+ .signTransaction({
147
+ senderAddress: accountAddress,
148
+ transaction,
149
+ })
150
+ .then((tx) => tx),
151
+ signTypedData: (typedData) => client.signMessage({
152
+ accountAddress,
153
+ message: typedData,
154
+ }),
155
+ });
156
+ return customViemAccount;
157
+ }
158
+ getWalletClient(chainId) {
159
+ const targetAccountAddress = this.activeAccountAddress;
160
+ if (!targetAccountAddress) {
161
+ return this.getPublicClient();
162
+ }
163
+ const rpcUrl = this.getRpcUrl();
164
+ const viemAccount = this.getViemAccount({
165
+ accountAddress: targetAccountAddress,
166
+ });
167
+ const evmNetwork = chainId
168
+ ? this.getEvmNetworkByChainId(parseInt(chainId))
169
+ : this.currentEvmNetwork();
170
+ if (!evmNetwork) {
171
+ throw new Error('EVM network not found');
172
+ }
173
+ const walletClient = createWalletClient({
174
+ account: viemAccount,
175
+ chain: getOrMapViemChain(evmNetwork),
176
+ transport: http(rpcUrl),
177
+ });
178
+ return walletClient;
179
+ }
180
+ createWalletAccount() {
181
+ return __awaiter(this, arguments, void 0, function* ({ thresholdSignatureScheme = 'TWO_OF_TWO', } = {}) {
182
+ const walletClient = this.getWaasWalletClient();
183
+ const createdWallet = yield walletClient.createWalletAccount({
184
+ thresholdSignatureScheme: thresholdSignatureScheme,
185
+ });
186
+ return createdWallet;
187
+ });
188
+ }
189
+ signMessage(message) {
190
+ var _a;
191
+ return (_a = this.getWalletClient()) === null || _a === void 0 ? void 0 : _a.signMessage({ message });
192
+ }
193
+ getSigner() {
194
+ return __awaiter(this, void 0, void 0, function* () {
195
+ return this.getWalletClient();
196
+ });
197
+ }
198
+ getWalletClientByAddress({ accountAddress, }) {
199
+ this.setActiveAccountAddress(accountAddress);
200
+ return this.getWalletClient();
201
+ }
202
+ exportPrivateKey() {
203
+ return __awaiter(this, arguments, void 0, function* ({ accountAddress, } = {}) {
204
+ const walletClient = this.getWaasWalletClient();
205
+ const targetAccountAddress = accountAddress || this.activeAccountAddress;
206
+ if (!targetAccountAddress) {
207
+ throw new Error('Account address is required');
208
+ }
209
+ const privateKey = yield walletClient.exportPrivateKey({
210
+ accountAddress: targetAccountAddress,
211
+ });
212
+ if (!(privateKey === null || privateKey === void 0 ? void 0 : privateKey.derivedPrivateKey)) {
213
+ throw new Error('Error exporting private key');
214
+ }
215
+ return privateKey.derivedPrivateKey;
216
+ });
217
+ }
218
+ importPrivateKey(_a) {
219
+ return __awaiter(this, arguments, void 0, function* ({ privateKey, chainName, thresholdSignatureScheme = 'TWO_OF_TWO', }) {
220
+ const walletClient = this.getWaasWalletClient();
221
+ yield walletClient.importPrivateKey({
222
+ chainName,
223
+ privateKey,
224
+ thresholdSignatureScheme: thresholdSignatureScheme,
225
+ });
226
+ });
227
+ }
228
+ exportClientKeyshares(_a) {
229
+ return __awaiter(this, arguments, void 0, function* ({ accountAddress, }) {
230
+ if (!accountAddress) {
231
+ throw new Error('Account address is required');
232
+ }
233
+ this.setActiveAccountAddress(accountAddress);
234
+ const walletClient = this.getWaasWalletClient();
235
+ yield walletClient.exportClientKeyshares({
236
+ accountAddress,
237
+ });
238
+ });
239
+ }
240
+ createUiTransaction(from) {
241
+ return __awaiter(this, void 0, void 0, function* () {
242
+ yield this.validateActiveWallet(from);
243
+ const walletClient = this.getWalletClient();
244
+ const publicClient = yield this.getPublicClient();
245
+ if (!publicClient || !walletClient) {
246
+ throw new DynamicError('No public client available');
247
+ }
248
+ return new ViemUiTransaction({
249
+ account: from,
250
+ onSubmit: (transaction) => __awaiter(this, void 0, void 0, function* () {
251
+ // Non native token
252
+ if (transaction.nonNativeAddress) {
253
+ return walletClient.writeContract({
254
+ abi: erc20Abi,
255
+ account: walletClient.account,
256
+ address: transaction.nonNativeAddress,
257
+ args: [transaction.to, transaction.nonNativeValue],
258
+ functionName: 'transfer',
259
+ maxFeePerGas: transaction.maxFeePerGas,
260
+ maxPriorityFeePerGas: transaction.maxPriorityFeePerGas,
261
+ });
262
+ }
263
+ // Native token
264
+ return walletClient.sendTransaction({
265
+ account: walletClient.account,
266
+ data: '0x',
267
+ maxFeePerGas: transaction.maxFeePerGas,
268
+ maxPriorityFeePerGas: transaction.maxPriorityFeePerGas,
269
+ to: transaction.to,
270
+ value: transaction.value,
271
+ });
272
+ }),
273
+ publicClient,
274
+ transaction: {},
275
+ });
276
+ });
277
+ }
278
+ }
279
+ DynamicWaasEVMConnector.lastUsedChainIdStorageKey = 'dynamic-waas-evm-last-used-chain-id';
280
+
281
+ export { DynamicWaasEVMConnector };
@@ -0,0 +1,10 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var DynamicWaasEVMConnector = require('./DynamicWaasEVMConnector.cjs');
7
+
8
+ const DynamicWaasEVMConnectors = () => [DynamicWaasEVMConnector.DynamicWaasEVMConnector];
9
+
10
+ exports.DynamicWaasEVMConnectors = DynamicWaasEVMConnectors;
@@ -0,0 +1,2 @@
1
+ import { DynamicWaasEVMConnector } from './DynamicWaasEVMConnector';
2
+ export declare const DynamicWaasEVMConnectors: () => (typeof DynamicWaasEVMConnector)[];
@@ -0,0 +1,6 @@
1
+ 'use client'
2
+ import { DynamicWaasEVMConnector } from './DynamicWaasEVMConnector.js';
3
+
4
+ const DynamicWaasEVMConnectors = () => [DynamicWaasEVMConnector];
5
+
6
+ export { DynamicWaasEVMConnectors };
package/src/index.cjs ADDED
@@ -0,0 +1,14 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var assertPackageVersion = require('@dynamic-labs/assert-package-version');
7
+ var _package = require('../package.cjs');
8
+ var DynamicWaasEVMConnector = require('./DynamicWaasEVMConnector.cjs');
9
+ var DynamicWaasEVMConnectors = require('./DynamicWaasEVMConnectors.cjs');
10
+
11
+ assertPackageVersion.assertPackageVersion('@dynamic-labs/waas-evm', _package.version);
12
+
13
+ exports.DynamicWaasEVMConnector = DynamicWaasEVMConnector.DynamicWaasEVMConnector;
14
+ exports.DynamicWaasEVMConnectors = DynamicWaasEVMConnectors.DynamicWaasEVMConnectors;
package/src/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { DynamicWaasEVMConnector } from './DynamicWaasEVMConnector';
2
+ export { DynamicWaasEVMConnectors } from './DynamicWaasEVMConnectors';
package/src/index.js ADDED
@@ -0,0 +1,7 @@
1
+ 'use client'
2
+ import { assertPackageVersion } from '@dynamic-labs/assert-package-version';
3
+ import { version } from '../package.js';
4
+ export { DynamicWaasEVMConnector } from './DynamicWaasEVMConnector.js';
5
+ export { DynamicWaasEVMConnectors } from './DynamicWaasEVMConnectors.js';
6
+
7
+ assertPackageVersion('@dynamic-labs/waas-evm', version);