@dynamic-labs-wallet/browser-wallet-client 0.0.0-beta-192.1 → 0.0.0-beta-272

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/index.cjs.js CHANGED
@@ -1,60 +1,27 @@
1
1
  'use strict';
2
2
 
3
+ var core = require('@dynamic-labs-wallet/core');
4
+ var uuid = require('uuid');
3
5
  var messageTransport = require('@dynamic-labs/message-transport');
4
6
  var logger$1 = require('@dynamic-labs/logger');
5
- var core = require('@dynamic-labs-wallet/core');
6
7
 
7
- const setupMessageTransportBridge = (messageTransport$1, iframe, iframeOrigin)=>{
8
- if (!(iframe == null ? void 0 : iframe.contentWindow)) {
9
- throw new Error('Iframe or contentWindow not available');
10
- }
11
- const logger = new logger$1.Logger('debug');
12
- messageTransport$1.on((message)=>{
13
- // Forward the message to webview via postMessage
14
- if (message.origin === 'host') {
15
- var _iframe_contentWindow;
16
- iframe == null ? void 0 : (_iframe_contentWindow = iframe.contentWindow) == null ? void 0 : _iframe_contentWindow.postMessage(message, iframeOrigin);
17
- }
18
- });
19
- const handleIncomingMessage = (message)=>{
20
- const { data } = message;
21
- if (!data) return;
22
- if ((data == null ? void 0 : data.origin) !== 'webview') {
23
- return;
24
- }
25
- if (typeof data !== 'object') {
26
- return;
27
- }
28
- try {
29
- const message = messageTransport.parseMessageTransportData(data);
30
- messageTransport$1.emit(message);
31
- } catch (error) {
32
- if (!(error instanceof SyntaxError)) {
33
- logger.error('Error handling incoming message:', error);
34
- }
8
+ function _extends() {
9
+ _extends = Object.assign || function assign(target) {
10
+ for(var i = 1; i < arguments.length; i++){
11
+ var source = arguments[i];
12
+ for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
35
13
  }
14
+ return target;
36
15
  };
37
- /**
38
- * Handle incoming message from android client
39
- */ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
40
- // @ts-ignore
41
- document.addEventListener('message', handleIncomingMessage);
42
- /**
43
- * Handle incoming message from iOS client
44
- */ window.addEventListener('message', handleIncomingMessage);
45
- };
16
+ return _extends.apply(this, arguments);
17
+ }
46
18
 
47
19
  class iframeMessageHandler {
48
20
  async getWallets(request) {
49
21
  return this.requestChannel.request('getWallets', request);
50
22
  }
51
- async getWallet({ chainName, accountAddress, walletOperation, signedSessionId }) {
52
- return this.requestChannel.request('getWallet', {
53
- chainName,
54
- accountAddress,
55
- walletOperation,
56
- signedSessionId
57
- });
23
+ async getWallet(request) {
24
+ return this.requestChannel.request('getWallet', request);
58
25
  }
59
26
  async createWalletAccount(request) {
60
27
  return this.requestChannel.request('createWalletAccount', request);
@@ -107,6 +74,9 @@ class iframeMessageHandler {
107
74
  async offlineExportPrivateKey(request) {
108
75
  return this.requestChannel.request('offlineExportPrivateKey', request);
109
76
  }
77
+ async signTypedData(request) {
78
+ return this.requestChannel.request('signTypedData', request);
79
+ }
110
80
  async cleanup() {
111
81
  return this.requestChannel.request('cleanup');
112
82
  }
@@ -117,7 +87,47 @@ class iframeMessageHandler {
117
87
 
118
88
  const logger = new logger$1.Logger('DynamicWaasWalletClient');
119
89
 
120
- class DynamicWalletClient {
90
+ const setupMessageTransportBridge = (messageTransport$1, iframe, iframeOrigin)=>{
91
+ if (!(iframe == null ? void 0 : iframe.contentWindow)) {
92
+ throw new Error('Iframe or contentWindow not available');
93
+ }
94
+ const logger = new logger$1.Logger('debug');
95
+ messageTransport$1.on((message)=>{
96
+ // Forward the message to webview via postMessage
97
+ if (message.origin === 'host') {
98
+ var _iframe_contentWindow;
99
+ iframe == null ? void 0 : (_iframe_contentWindow = iframe.contentWindow) == null ? void 0 : _iframe_contentWindow.postMessage(message, iframeOrigin);
100
+ }
101
+ });
102
+ const handleIncomingMessage = (message)=>{
103
+ const { data } = message;
104
+ if (!data) return;
105
+ if ((data == null ? void 0 : data.origin) !== 'webview') {
106
+ return;
107
+ }
108
+ if (typeof data !== 'object') {
109
+ return;
110
+ }
111
+ try {
112
+ const message = messageTransport.parseMessageTransportData(data);
113
+ messageTransport$1.emit(message);
114
+ } catch (error) {
115
+ if (!(error instanceof SyntaxError)) {
116
+ logger.error('Error handling incoming message:', error);
117
+ }
118
+ }
119
+ };
120
+ /**
121
+ * Handle incoming message from android client
122
+ */ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
123
+ // @ts-ignore
124
+ document.addEventListener('message', handleIncomingMessage);
125
+ /**
126
+ * Handle incoming message from iOS client
127
+ */ window.addEventListener('message', handleIncomingMessage);
128
+ };
129
+
130
+ class IframeManager {
121
131
  // Simply load the iframe from localhost
122
132
  async initialize() {
123
133
  await this.doInitializeIframeCommunication();
@@ -126,10 +136,10 @@ class DynamicWalletClient {
126
136
  * this is called on class construction time
127
137
  * @returns {Promise<void>} that resolves when the iframe is loaded and the message transport and iframe storage are initialized
128
138
  */ initializeIframeCommunication() {
129
- if (!DynamicWalletClient.iframeLoadPromise) {
130
- DynamicWalletClient.iframeLoadPromise = this.doInitializeIframeCommunication();
131
- }
132
- return DynamicWalletClient.iframeLoadPromise;
139
+ var _IframeManager;
140
+ var _iframeLoadPromise;
141
+ (_iframeLoadPromise = (_IframeManager = IframeManager).iframeLoadPromise) != null ? _iframeLoadPromise : _IframeManager.iframeLoadPromise = this.doInitializeIframeCommunication();
142
+ return IframeManager.iframeLoadPromise;
133
143
  }
134
144
  /**
135
145
  * initialize the iframe communication by awaiting the iframe load promise
@@ -145,6 +155,10 @@ class DynamicWalletClient {
145
155
  /**
146
156
  * initialize the message transport after iframe is successfully loaded
147
157
  */ async initializeMessageTransport() {
158
+ if (this.messageTransport && this.iframeMessageHandler) {
159
+ this.logger.debug('Skipping initializeMessageTransport: transport and message handler already initialized');
160
+ return;
161
+ }
148
162
  await this.initializeIframeCommunication();
149
163
  const transport = messageTransport.applyDefaultMessageOrigin({
150
164
  defaultOrigin: 'host',
@@ -171,61 +185,145 @@ class DynamicWalletClient {
171
185
  throw new Error('Failed to establish secure token exchange: ' + error);
172
186
  }
173
187
  }
188
+ /**
189
+ * Reset the shared iframe and iframe load promise, and iframe instance count
190
+ */ async resetSharedIframe() {
191
+ IframeManager.sharedIframe = null;
192
+ IframeManager.iframeInstanceCount = 0;
193
+ IframeManager.iframeLoadPromise = null;
194
+ this.iframe = null;
195
+ this.iframeMessageHandler = null;
196
+ this.messageTransport = null;
197
+ // Double the timeout and cap at 60 seconds to give more time for slow networks
198
+ IframeManager.iframeLoadTimeout = Math.min(IframeManager.iframeLoadTimeout * 2, 60000);
199
+ }
174
200
  async loadIframe() {
175
201
  // If the iframe is already loaded, just assign and resolve
176
- if (DynamicWalletClient.sharedIframe) {
177
- this.iframe = DynamicWalletClient.sharedIframe;
178
- DynamicWalletClient.iframeInstanceCount++;
202
+ if (IframeManager.sharedIframe) {
203
+ this.assignExistingIframe();
179
204
  return Promise.resolve();
180
205
  }
181
206
  // If a load is in progress, wait for it, then assign
182
- if (DynamicWalletClient.iframeLoadPromise) {
183
- return DynamicWalletClient.iframeLoadPromise.then(()=>{
184
- this.iframe = DynamicWalletClient.sharedIframe;
185
- DynamicWalletClient.iframeInstanceCount++;
207
+ if (IframeManager.iframeLoadPromise) {
208
+ return IframeManager.iframeLoadPromise.then(()=>{
209
+ this.assignExistingIframe();
186
210
  });
187
211
  }
188
- DynamicWalletClient.iframeLoadPromise = new Promise((resolve, reject)=>{
189
- const iframe = document.createElement('iframe');
190
- const iframeTimeoutId = setTimeout(()=>{
191
- reject(new Error('Iframe load timeout'));
192
- }, 10000);
193
- iframe.style.display = 'none';
194
- iframe.setAttribute('title', 'Dynamic Wallet Iframe');
195
- iframe.setAttribute('sandbox', 'allow-scripts allow-same-origin allow-downloads');
196
- iframe.setAttribute('referrerpolicy', 'origin');
197
- iframe.style.position = 'fixed';
198
- iframe.style.top = '0';
199
- iframe.style.left = '0';
200
- iframe.style.width = '0';
201
- iframe.style.height = '0';
202
- iframe.style.border = 'none';
203
- iframe.style.pointerEvents = 'none';
204
- var _this_instanceId;
205
- const params = new URLSearchParams({
206
- instanceId: (_this_instanceId = this.instanceId) != null ? _this_instanceId : '',
207
- hostOrigin: window.location.origin,
208
- environmentId: this.environmentId,
209
- baseApiUrl: this.baseApiUrl,
210
- baseMPCRelayApiUrl: this.baseMPCRelayApiUrl
211
- });
212
- iframe.src = `${this.iframeDomain}/waas-v1/${this.environmentId}?${params.toString()}`;
213
- this.logger.debug('Creating iframe with src:', iframe.src);
214
- document.body.appendChild(iframe);
215
- iframe.onload = ()=>{
212
+ IframeManager.iframeLoadPromise = this.createIframeLoadPromise();
213
+ return IframeManager.iframeLoadPromise;
214
+ }
215
+ assignExistingIframe() {
216
+ this.iframe = IframeManager.sharedIframe;
217
+ IframeManager.iframeInstanceCount++;
218
+ }
219
+ createIframeLoadPromise() {
220
+ return new Promise((resolve, reject)=>{
221
+ const attemptLoad = ()=>{
222
+ IframeManager.iframeLoadAttempts++;
223
+ this.logger.info(`Loading iframe for waas wallet client... (attempt ${IframeManager.iframeLoadAttempts}/${IframeManager.maxRetryAttempts + 1})`, this.getIframeContext());
224
+ const iframe = document.createElement('iframe');
225
+ let messageListener = null;
226
+ const context = _extends({}, this.getIframeContext(), {
227
+ attempt: IframeManager.iframeLoadAttempts
228
+ });
229
+ // Set up timeout that will trigger iframe error, a retry will be triggered on this iframe error
230
+ const iframeTimeoutId = setTimeout(()=>{
231
+ if (iframe.onerror) {
232
+ iframe.onerror('Iframe load timeout');
233
+ }
234
+ }, IframeManager.iframeLoadTimeout);
235
+ messageListener = this.createMessageListener(iframe, iframeTimeoutId, resolve);
236
+ window.addEventListener('message', messageListener);
237
+ this.configureIframe(iframe);
238
+ this.setIframeSource(iframe);
239
+ this.logger.debug('Creating iframe with src:', iframe.src);
240
+ document.body.appendChild(iframe);
241
+ this.setupIframeEventHandlersWithRetry(iframe, messageListener, iframeTimeoutId, reject, attemptLoad, context);
242
+ };
243
+ // Start the first attempt
244
+ attemptLoad();
245
+ });
246
+ }
247
+ setupIframeEventHandlersWithRetry(iframe, messageListener, iframeTimeoutId, reject, attemptLoad, context) {
248
+ iframe.onload = ()=>{
249
+ this.logger.debug('Iframe onload fired, waiting for ready message...');
250
+ };
251
+ iframe.onerror = (error)=>{
252
+ if (messageListener) {
253
+ window.removeEventListener('message', messageListener);
254
+ }
255
+ clearTimeout(iframeTimeoutId);
256
+ // Check if we should retry
257
+ if (IframeManager.iframeLoadAttempts <= IframeManager.maxRetryAttempts) {
258
+ const errorMsg = error instanceof Error ? error.message : 'Unknown error occurred.';
259
+ this.logger.warn(`(loadIframe) Iframe failed to load on attempt ${IframeManager.iframeLoadAttempts}, retrying... context: ${JSON.stringify(context)}, error: ${errorMsg}`);
260
+ // Clean up current attempt
261
+ if (iframe.parentNode) {
262
+ iframe.parentNode.removeChild(iframe);
263
+ }
264
+ // Retry after a short delay
265
+ setTimeout(()=>{
266
+ attemptLoad();
267
+ }, 1000); // 1 second delay between retries
268
+ } else {
269
+ // Max retries reached, give up
270
+ this.logger.error('Iframe failed to load after all retry attempts: ', error);
271
+ this.resetSharedIframe();
272
+ IframeManager.iframeLoadAttempts = 0;
273
+ reject(new Error(`Failed to load iframe after all retry attempts... context: ${JSON.stringify(context)}`));
274
+ }
275
+ };
276
+ }
277
+ getIframeContext() {
278
+ var _this_sdkVersion;
279
+ return {
280
+ iframeDomain: this.iframeDomain,
281
+ environmentId: this.environmentId,
282
+ sdkVersion: (_this_sdkVersion = this.sdkVersion) != null ? _this_sdkVersion : '',
283
+ instanceId: this.instanceId,
284
+ chainName: this.chainName,
285
+ iframeLoadTimeout: IframeManager.iframeLoadTimeout
286
+ };
287
+ }
288
+ createMessageListener(iframe, iframeTimeoutId, resolve) {
289
+ const messageListener = (event)=>{
290
+ if (event.source === iframe.contentWindow && event.data === `iframe-ready-${this.instanceId}`) {
291
+ window.removeEventListener('message', messageListener);
216
292
  clearTimeout(iframeTimeoutId);
217
- DynamicWalletClient.sharedIframe = iframe;
293
+ IframeManager.sharedIframe = iframe;
218
294
  this.iframe = iframe;
219
- DynamicWalletClient.iframeInstanceCount++;
295
+ IframeManager.iframeInstanceCount++;
296
+ IframeManager.iframeLoadAttempts = 0; // Reset retry counter on success
220
297
  resolve();
221
- };
222
- iframe.onerror = (error)=>{
223
- clearTimeout(iframeTimeoutId);
224
- this.logger.error('Iframe failed to load:', error);
225
- reject(new Error('Failed to load iframe'));
226
- };
298
+ this.logger.info('Iframe loaded successfully...', this.getIframeContext());
299
+ }
300
+ };
301
+ return messageListener;
302
+ }
303
+ configureIframe(iframe) {
304
+ iframe.style.display = 'none';
305
+ iframe.setAttribute('title', 'Dynamic Wallet Iframe');
306
+ iframe.setAttribute('sandbox', 'allow-scripts allow-same-origin allow-downloads');
307
+ iframe.setAttribute('referrerpolicy', 'origin');
308
+ iframe.style.position = 'fixed';
309
+ iframe.style.top = '0';
310
+ iframe.style.left = '0';
311
+ iframe.style.width = '0';
312
+ iframe.style.height = '0';
313
+ iframe.style.border = 'none';
314
+ iframe.style.pointerEvents = 'none';
315
+ }
316
+ setIframeSource(iframe) {
317
+ var _this_instanceId, _this_sdkVersion;
318
+ const params = new URLSearchParams({
319
+ instanceId: (_this_instanceId = this.instanceId) != null ? _this_instanceId : '',
320
+ hostOrigin: window.location.origin,
321
+ environmentId: this.environmentId,
322
+ baseApiUrl: this.baseApiUrl,
323
+ baseMPCRelayApiUrl: this.baseMPCRelayApiUrl,
324
+ sdkVersion: (_this_sdkVersion = this.sdkVersion) != null ? _this_sdkVersion : ''
227
325
  });
228
- return DynamicWalletClient.iframeLoadPromise;
326
+ iframe.src = `${this.iframeDomain}/waas-v1/${this.environmentId}?${params.toString()}`;
229
327
  }
230
328
  /**
231
329
  * Load an iframe for a specific container
@@ -233,39 +331,78 @@ class DynamicWalletClient {
233
331
  * @returns {Promise<HTMLIFrameElement>} that resolves when the iframe is loaded
234
332
  */ loadIframeForContainer(container) {
235
333
  return new Promise((resolve, reject)=>{
334
+ var _this_sdkVersion;
335
+ const context = {
336
+ iframeDomain: this.iframeDomain,
337
+ environmentId: this.environmentId,
338
+ sdkVersion: (_this_sdkVersion = this.sdkVersion) != null ? _this_sdkVersion : '',
339
+ instanceId: this.instanceId,
340
+ chainName: this.chainName,
341
+ iframeLoadTimeout: IframeManager.iframeLoadTimeout
342
+ };
343
+ this.logger.info(`Loading iframe for container...`, context);
236
344
  const iframe = document.createElement('iframe');
345
+ let messageListener = null;
237
346
  const iframeTimeoutId = setTimeout(()=>{
238
- reject(new Error('Iframe load timeout'));
239
- }, 10000);
347
+ if (messageListener) {
348
+ window.removeEventListener('message', messageListener);
349
+ }
350
+ this.logger.error(`(loadIframeForContainer) Iframe load timeout due to no handshake message from iframe, this could be network issues, incorrect iframe domain, or CORS errors that prevents iframe from being loaded or sending handshake message, context: ${JSON.stringify(context)}`);
351
+ reject(new Error(`(loadIframeForContainer) Iframe load timeout due to no handshake message from iframe, this could be network issues, incorrect iframe domain, or CORS errors that prevents iframe from being loaded or sending handshake message, context: ${JSON.stringify(context)}`));
352
+ }, IframeManager.iframeLoadTimeout);
240
353
  iframe.style.display = 'block';
241
354
  iframe.style.width = '100%';
242
355
  iframe.style.height = '100%';
243
356
  iframe.setAttribute('title', 'Dynamic Wallet Storage');
244
357
  iframe.setAttribute('sandbox', 'allow-scripts allow-same-origin');
245
358
  iframe.setAttribute('referrerpolicy', 'origin');
246
- var _this_instanceId;
359
+ var _this_instanceId, _this_sdkVersion1;
247
360
  const params = new URLSearchParams({
248
361
  instanceId: (_this_instanceId = this.instanceId) != null ? _this_instanceId : '',
249
362
  hostOrigin: window.location.origin,
250
363
  environmentId: this.environmentId,
251
364
  baseApiUrl: this.baseApiUrl,
252
365
  baseMPCRelayApiUrl: this.baseMPCRelayApiUrl,
253
- chain: this.chainName
366
+ sdkVersion: (_this_sdkVersion1 = this.sdkVersion) != null ? _this_sdkVersion1 : ''
254
367
  });
255
368
  iframe.src = `${this.iframeDomain}/waas-v1/${this.environmentId}?${params.toString()}`;
256
369
  this.logger.debug('Creating iframe with src:', iframe.src);
257
370
  // Add iframe to the provided container
258
371
  container.appendChild(iframe);
259
372
  iframe.onload = ()=>{
260
- clearTimeout(iframeTimeoutId);
261
- this.logger.debug('Iframe loaded successfully');
262
- resolve(iframe);
373
+ // The message listener is already set up, so iframe can send ready message
374
+ this.logger.debug('Iframe onload fired, waiting for ready message...');
263
375
  };
264
376
  iframe.onerror = (error)=>{
377
+ if (messageListener) {
378
+ window.removeEventListener('message', messageListener);
379
+ }
265
380
  clearTimeout(iframeTimeoutId);
266
- this.logger.error('Iframe failed to load:', error);
267
- reject(new Error('Failed to load iframe'));
381
+ this.logger.error('Iframe failed to load due to errors: ', error);
382
+ reject(new Error('Failed to load iframe due to unknown load errors, this is likely a browser or network issue.'));
268
383
  };
384
+ // Set up message listener BEFORE creating iframe
385
+ messageListener = (event)=>{
386
+ if (event.source === iframe.contentWindow && event.data === `iframe-ready-${this.instanceId}`) {
387
+ if (messageListener) {
388
+ window.removeEventListener('message', messageListener);
389
+ }
390
+ clearTimeout(iframeTimeoutId);
391
+ IframeManager.sharedIframe = iframe;
392
+ this.iframe = iframe;
393
+ IframeManager.iframeInstanceCount++;
394
+ resolve(iframe);
395
+ var _this_sdkVersion;
396
+ this.logger.info('Iframe loaded successfully...', {
397
+ iframeDomain: this.iframeDomain,
398
+ environmentId: this.environmentId,
399
+ sdkVersion: (_this_sdkVersion = this.sdkVersion) != null ? _this_sdkVersion : '',
400
+ instanceId: this.instanceId,
401
+ chainName: this.chainName
402
+ });
403
+ }
404
+ };
405
+ window.addEventListener('message', messageListener);
269
406
  });
270
407
  }
271
408
  /**
@@ -299,6 +436,51 @@ class DynamicWalletClient {
299
436
  throw error;
300
437
  }
301
438
  }
439
+ async cleanup() {
440
+ await this.initializeMessageTransport();
441
+ if (!this.iframeMessageHandler) {
442
+ throw new Error('Iframe message handler not initialized');
443
+ }
444
+ await this.iframeMessageHandler.cleanup();
445
+ if (this.iframe) {
446
+ IframeManager.iframeInstanceCount--;
447
+ if (IframeManager.sharedIframe && IframeManager.iframeInstanceCount === 0) {
448
+ document.body.removeChild(IframeManager.sharedIframe);
449
+ IframeManager.sharedIframe = null;
450
+ IframeManager.iframeLoadPromise = null;
451
+ }
452
+ this.iframe = null;
453
+ }
454
+ }
455
+ constructor({ environmentId, authToken, baseApiUrl, baseMPCRelayApiUrl, chainName, sdkVersion, debug }){
456
+ this.logger = logger;
457
+ this.instanceId = null;
458
+ this.iframeDomain = null;
459
+ this.messageTransport = null;
460
+ this.iframeMessageHandler = null;
461
+ this.iframe = null;
462
+ this.environmentId = environmentId;
463
+ this.authToken = authToken;
464
+ this.baseApiUrl = baseApiUrl;
465
+ this.baseMPCRelayApiUrl = baseMPCRelayApiUrl;
466
+ this.chainName = chainName;
467
+ this.sdkVersion = sdkVersion;
468
+ const environment = core.getEnvironmentFromUrl(baseApiUrl);
469
+ this.iframeDomain = core.IFRAME_DOMAIN_MAP[environment];
470
+ // Generate unique instanceId when client is created
471
+ this.instanceId = uuid.v4();
472
+ this.debug = Boolean(debug);
473
+ this.logger.setLogLevel(this.debug ? 'DEBUG' : 'INFO');
474
+ }
475
+ }
476
+ IframeManager.iframeLoadPromise = null;
477
+ IframeManager.iframeLoadTimeout = 10000;
478
+ IframeManager.iframeLoadAttempts = 0;
479
+ IframeManager.maxRetryAttempts = 1;
480
+ IframeManager.sharedIframe = null;
481
+ IframeManager.iframeInstanceCount = 0;
482
+
483
+ class DynamicWalletClient extends IframeManager {
302
484
  async getWallets() {
303
485
  await this.initializeMessageTransport();
304
486
  if (!this.iframeMessageHandler) {
@@ -308,7 +490,7 @@ class DynamicWalletClient {
308
490
  chainName: this.chainName
309
491
  });
310
492
  }
311
- async getWallet({ accountAddress, walletOperation = core.WalletOperation.NO_OPERATION, signedSessionId }) {
493
+ async getWallet({ accountAddress, walletOperation = core.WalletOperation.NO_OPERATION, signedSessionId, authToken }) {
312
494
  await this.initializeMessageTransport();
313
495
  if (!this.iframeMessageHandler) {
314
496
  throw new Error('Iframe message handler not initialized');
@@ -317,10 +499,11 @@ class DynamicWalletClient {
317
499
  chainName: this.chainName,
318
500
  accountAddress,
319
501
  walletOperation,
320
- signedSessionId
502
+ signedSessionId,
503
+ authToken
321
504
  });
322
505
  }
323
- async createWalletAccount({ thresholdSignatureScheme, password = undefined, signedSessionId }) {
506
+ async createWalletAccount({ thresholdSignatureScheme, password = undefined, signedSessionId, authToken }) {
324
507
  await this.initializeMessageTransport();
325
508
  if (!this.iframeMessageHandler) {
326
509
  throw new Error('Iframe message handler not initialized');
@@ -329,10 +512,11 @@ class DynamicWalletClient {
329
512
  chainName: this.chainName,
330
513
  thresholdSignatureScheme,
331
514
  password,
332
- signedSessionId
515
+ signedSessionId,
516
+ authToken
333
517
  });
334
518
  }
335
- async requiresPasswordForOperation({ accountAddress, walletOperation = core.WalletOperation.REACH_THRESHOLD }) {
519
+ async requiresPasswordForOperation({ accountAddress, walletOperation = core.WalletOperation.REACH_THRESHOLD, authToken }) {
336
520
  await this.initializeMessageTransport();
337
521
  if (!this.iframeMessageHandler) {
338
522
  throw new Error('Iframe message handler not initialized');
@@ -340,20 +524,22 @@ class DynamicWalletClient {
340
524
  return this.iframeMessageHandler.requiresPasswordForOperation({
341
525
  chainName: this.chainName,
342
526
  accountAddress,
343
- walletOperation
527
+ walletOperation,
528
+ authToken
344
529
  });
345
530
  }
346
- async isPasswordEncrypted({ accountAddress }) {
531
+ async isPasswordEncrypted({ accountAddress, authToken }) {
347
532
  await this.initializeMessageTransport();
348
533
  if (!this.iframeMessageHandler) {
349
534
  throw new Error('Iframe message handler not initialized');
350
535
  }
351
536
  return this.iframeMessageHandler.isPasswordEncrypted({
352
537
  chainName: this.chainName,
353
- accountAddress
538
+ accountAddress,
539
+ authToken
354
540
  });
355
541
  }
356
- async signMessage({ message, accountAddress, password = undefined, signedSessionId }) {
542
+ async signMessage({ message, accountAddress, password = undefined, signedSessionId, authToken }) {
357
543
  await this.initializeMessageTransport();
358
544
  if (!this.iframeMessageHandler) {
359
545
  throw new Error('Iframe message handler not initialized');
@@ -363,10 +549,11 @@ class DynamicWalletClient {
363
549
  message,
364
550
  accountAddress,
365
551
  password,
366
- signedSessionId
552
+ signedSessionId,
553
+ authToken
367
554
  });
368
555
  }
369
- async signRawMessage({ message, accountAddress, password = undefined, signedSessionId }) {
556
+ async signRawMessage({ message, accountAddress, password = undefined, signedSessionId, authToken }) {
370
557
  await this.initializeMessageTransport();
371
558
  if (!this.iframeMessageHandler) {
372
559
  throw new Error('Iframe message handler not initialized');
@@ -376,7 +563,8 @@ class DynamicWalletClient {
376
563
  message,
377
564
  accountAddress,
378
565
  password,
379
- signedSessionId
566
+ signedSessionId,
567
+ authToken
380
568
  });
381
569
  }
382
570
  /**
@@ -389,7 +577,7 @@ class DynamicWalletClient {
389
577
  * SUI:
390
578
  * const txBytes = await txb.build({ client });
391
579
  * const txString = Buffer.from(txBytes).toString("hex");
392
- */ async signTransaction({ senderAddress, transaction, password = undefined, signedSessionId }) {
580
+ */ async signTransaction({ senderAddress, transaction, password = undefined, signedSessionId, authToken }) {
393
581
  await this.initializeMessageTransport();
394
582
  if (!this.iframeMessageHandler) {
395
583
  throw new Error('Iframe message handler not initialized');
@@ -399,10 +587,25 @@ class DynamicWalletClient {
399
587
  senderAddress,
400
588
  transaction,
401
589
  password,
402
- signedSessionId
590
+ signedSessionId,
591
+ authToken
592
+ });
593
+ }
594
+ async signTypedData({ accountAddress, typedData, password = undefined, signedSessionId, authToken }) {
595
+ await this.initializeMessageTransport();
596
+ if (!this.iframeMessageHandler) {
597
+ throw new Error('Iframe message handler not initialized');
598
+ }
599
+ return this.iframeMessageHandler.signTypedData({
600
+ chainName: this.chainName,
601
+ accountAddress,
602
+ typedData: JSON.stringify(typedData),
603
+ password,
604
+ signedSessionId,
605
+ authToken
403
606
  });
404
607
  }
405
- async backupKeySharesToGoogleDrive({ accountAddress, password = undefined, signedSessionId }) {
608
+ async backupKeySharesToGoogleDrive({ accountAddress, password = undefined, signedSessionId, authToken }) {
406
609
  await this.initializeMessageTransport();
407
610
  if (!this.iframeMessageHandler) {
408
611
  throw new Error('Iframe message handler not initialized');
@@ -411,10 +614,11 @@ class DynamicWalletClient {
411
614
  chainName: this.chainName,
412
615
  accountAddress,
413
616
  password,
414
- signedSessionId
617
+ signedSessionId,
618
+ authToken
415
619
  });
416
620
  }
417
- async restoreBackupFromGoogleDrive({ accountAddress, displayContainer, password, signedSessionId }) {
621
+ async restoreBackupFromGoogleDrive({ accountAddress, displayContainer, password, signedSessionId, authToken }) {
418
622
  const { iframeDisplay } = await this.initializeIframeDisplayForContainer({
419
623
  container: displayContainer
420
624
  });
@@ -425,10 +629,11 @@ class DynamicWalletClient {
425
629
  chainName: this.chainName,
426
630
  accountAddress,
427
631
  password,
428
- signedSessionId
632
+ signedSessionId,
633
+ authToken
429
634
  });
430
635
  }
431
- async refreshWalletAccountShares({ accountAddress, password, signedSessionId }) {
636
+ async refreshWalletAccountShares({ accountAddress, password, signedSessionId, authToken }) {
432
637
  await this.initializeMessageTransport();
433
638
  if (!this.iframeMessageHandler) {
434
639
  throw new Error('Iframe message handler not initialized');
@@ -437,10 +642,11 @@ class DynamicWalletClient {
437
642
  chainName: this.chainName,
438
643
  accountAddress: accountAddress,
439
644
  password: password,
440
- signedSessionId
645
+ signedSessionId,
646
+ authToken
441
647
  });
442
648
  }
443
- async reshare({ accountAddress, oldThresholdSignatureScheme, newThresholdSignatureScheme, password, signedSessionId }) {
649
+ async reshare({ accountAddress, oldThresholdSignatureScheme, newThresholdSignatureScheme, password, signedSessionId, authToken }) {
444
650
  await this.initializeMessageTransport();
445
651
  if (!this.iframeMessageHandler) {
446
652
  throw new Error('Iframe message handler not initialized');
@@ -451,10 +657,11 @@ class DynamicWalletClient {
451
657
  oldThresholdSignatureScheme,
452
658
  newThresholdSignatureScheme,
453
659
  password,
454
- signedSessionId
660
+ signedSessionId,
661
+ authToken
455
662
  });
456
663
  }
457
- async exportPrivateKey({ accountAddress, displayContainer, password, signedSessionId }) {
664
+ async exportPrivateKey({ accountAddress, displayContainer, password, signedSessionId, authToken }) {
458
665
  const { iframeDisplay } = await this.initializeIframeDisplayForContainer({
459
666
  container: displayContainer
460
667
  });
@@ -465,10 +672,11 @@ class DynamicWalletClient {
465
672
  chainName: this.chainName,
466
673
  accountAddress,
467
674
  password,
468
- signedSessionId
675
+ signedSessionId,
676
+ authToken
469
677
  });
470
678
  }
471
- async verifyPassword({ accountAddress, password, walletOperation = core.WalletOperation.NO_OPERATION, signedSessionId }) {
679
+ async verifyPassword({ accountAddress, password, walletOperation = core.WalletOperation.NO_OPERATION, signedSessionId, authToken }) {
472
680
  await this.initializeMessageTransport();
473
681
  if (!this.iframeMessageHandler) {
474
682
  throw new Error('Iframe message handler not initialized');
@@ -478,10 +686,11 @@ class DynamicWalletClient {
478
686
  accountAddress,
479
687
  password,
480
688
  walletOperation,
481
- signedSessionId
689
+ signedSessionId,
690
+ authToken
482
691
  });
483
692
  }
484
- async updatePassword({ accountAddress, existingPassword, newPassword, signedSessionId }) {
693
+ async updatePassword({ accountAddress, existingPassword, newPassword, signedSessionId, authToken }) {
485
694
  await this.initializeMessageTransport();
486
695
  if (!this.iframeMessageHandler) {
487
696
  throw new Error('Iframe message handler not initialized');
@@ -491,10 +700,11 @@ class DynamicWalletClient {
491
700
  accountAddress,
492
701
  existingPassword,
493
702
  newPassword,
494
- signedSessionId
703
+ signedSessionId,
704
+ authToken
495
705
  });
496
706
  }
497
- async importPrivateKey({ privateKey, thresholdSignatureScheme }) {
707
+ async importPrivateKey({ privateKey, thresholdSignatureScheme, signedSessionId, authToken }) {
498
708
  await this.initializeMessageTransport();
499
709
  if (!this.iframeMessageHandler) {
500
710
  throw new Error('Iframe message handler not initialized');
@@ -502,10 +712,12 @@ class DynamicWalletClient {
502
712
  return this.iframeMessageHandler.importPrivateKey({
503
713
  chainName: this.chainName,
504
714
  privateKey,
505
- thresholdSignatureScheme
715
+ thresholdSignatureScheme,
716
+ signedSessionId,
717
+ authToken
506
718
  });
507
719
  }
508
- async exportClientKeyshares({ accountAddress, password, signedSessionId }) {
720
+ async exportClientKeyshares({ accountAddress, password, signedSessionId, authToken }) {
509
721
  await this.initializeMessageTransport();
510
722
  if (!this.iframeMessageHandler) {
511
723
  throw new Error('Iframe message handler not initialized');
@@ -514,7 +726,8 @@ class DynamicWalletClient {
514
726
  chainName: this.chainName,
515
727
  accountAddress,
516
728
  password,
517
- signedSessionId
729
+ signedSessionId,
730
+ authToken
518
731
  });
519
732
  }
520
733
  /**
@@ -537,62 +750,33 @@ class DynamicWalletClient {
537
750
  base64Args
538
751
  });
539
752
  }
540
- async cleanup() {
541
- await this.initializeMessageTransport();
542
- if (!this.iframeMessageHandler) {
543
- throw new Error('Iframe message handler not initialized');
544
- }
545
- await this.iframeMessageHandler.cleanup();
546
- if (this.iframe) {
547
- DynamicWalletClient.iframeInstanceCount--;
548
- if (DynamicWalletClient.sharedIframe && DynamicWalletClient.iframeInstanceCount === 0) {
549
- document.body.removeChild(DynamicWalletClient.sharedIframe);
550
- DynamicWalletClient.sharedIframe = null;
551
- DynamicWalletClient.iframeLoadPromise = null;
552
- }
553
- this.iframe = null;
554
- }
555
- }
556
- constructor({ environmentId, authToken, baseApiUrl, baseMPCRelayApiUrl, chainName, debug }){
557
- this.logger = logger;
558
- this.instanceId = null;
559
- this.iframeDomain = null;
560
- this.messageTransport = null;
561
- this.iframeMessageHandler = null;
562
- this.iframe = null;
563
- this.environmentId = environmentId;
564
- this.authToken = authToken;
565
- this.baseApiUrl = baseApiUrl;
566
- this.baseMPCRelayApiUrl = baseMPCRelayApiUrl;
567
- this.chainName = chainName;
568
- const environment = core.getEnvironmentFromUrl(baseApiUrl);
569
- this.iframeDomain = core.IFRAME_DOMAIN_MAP[environment];
570
- // Generate unique instanceId when client is created
571
- this.instanceId = crypto.randomUUID();
572
- this.debug = Boolean(debug);
573
- this.logger.setLogLevel(this.debug ? 'DEBUG' : 'INFO');
574
- // initialize the client
575
- this.initialize();
753
+ constructor({ environmentId, authToken, baseApiUrl, baseMPCRelayApiUrl, chainName, sdkVersion, debug }){
754
+ super({
755
+ environmentId,
756
+ authToken,
757
+ baseApiUrl,
758
+ baseMPCRelayApiUrl,
759
+ chainName,
760
+ sdkVersion,
761
+ debug
762
+ });
576
763
  }
577
764
  }
578
- DynamicWalletClient.iframeLoadPromise = null;
579
- DynamicWalletClient.sharedIframe = null;
580
- DynamicWalletClient.iframeInstanceCount = 0;
581
765
 
582
766
  Object.defineProperty(exports, "MPC_RELAY_PREPROD_API_URL", {
583
- enumerable: true,
584
- get: function () { return core.MPC_RELAY_PREPROD_API_URL; }
767
+ enumerable: true,
768
+ get: function () { return core.MPC_RELAY_PREPROD_API_URL; }
585
769
  });
586
770
  Object.defineProperty(exports, "MPC_RELAY_PROD_API_URL", {
587
- enumerable: true,
588
- get: function () { return core.MPC_RELAY_PROD_API_URL; }
771
+ enumerable: true,
772
+ get: function () { return core.MPC_RELAY_PROD_API_URL; }
589
773
  });
590
774
  Object.defineProperty(exports, "ThresholdSignatureScheme", {
591
- enumerable: true,
592
- get: function () { return core.ThresholdSignatureScheme; }
775
+ enumerable: true,
776
+ get: function () { return core.ThresholdSignatureScheme; }
593
777
  });
594
778
  Object.defineProperty(exports, "WalletOperation", {
595
- enumerable: true,
596
- get: function () { return core.WalletOperation; }
779
+ enumerable: true,
780
+ get: function () { return core.WalletOperation; }
597
781
  });
598
782
  exports.DynamicWalletClient = DynamicWalletClient;