@meshconnect/web-link-sdk 2.1.0-rc.5 → 2.1.1

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/Link.js CHANGED
@@ -47,9 +47,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
47
47
  };
48
48
  import { addPopup, iframeId, removePopup } from './utils/popup';
49
49
  import { isLinkEventTypeKey } from './utils/event-types';
50
- import { isWalletBrowserEventTypeKey } from './utils/wallet-browser-event-types';
51
50
  import { sdkSpecs } from './utils/sdk-specs';
52
- import { connectToSpecificWallet, signedMessage, sendTransactionFromSDK, switchChainFromSDK, getWagmiCoreInjectedData, sendNonNativeTransactionFromSDK, disconnectAllAccounts } from './utils/wagmiCoreConnectorsUtils';
53
51
  var currentOptions;
54
52
  var possibleOrigins = new Set([
55
53
  'https://web.meshconnect.com',
@@ -70,337 +68,85 @@ function sendMessageToIframe(message) {
70
68
  }
71
69
  });
72
70
  }
73
- function handleLinkEvent(event) {
74
- return __awaiter(this, void 0, void 0, function () {
75
- var _a, payload, payload, payload, payload, w, h, left, top_1, injectedConnectors;
76
- var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
77
- return __generator(this, function (_o) {
78
- switch (_o.label) {
79
- case 0:
80
- _a = event.data.type;
81
- switch (_a) {
82
- case 'brokerageAccountAccessToken': return [3 /*break*/, 1];
83
- case 'delayedAuthentication': return [3 /*break*/, 2];
84
- case 'transferFinished': return [3 /*break*/, 3];
85
- case 'close': return [3 /*break*/, 4];
86
- case 'done': return [3 /*break*/, 4];
87
- case 'oauthLinkOpen': return [3 /*break*/, 5];
88
- case 'loaded': return [3 /*break*/, 6];
89
- }
90
- return [3 /*break*/, 8];
91
- case 1:
92
- {
93
- payload = {
94
- accessToken: event.data.payload
95
- };
96
- (_b = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onEvent) === null || _b === void 0 ? void 0 : _b.call(currentOptions, {
97
- type: 'integrationConnected',
98
- payload: payload
99
- });
100
- (_c = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onIntegrationConnected) === null || _c === void 0 ? void 0 : _c.call(currentOptions, payload);
101
- return [3 /*break*/, 9];
102
- }
103
- _o.label = 2;
104
- case 2:
105
- {
106
- payload = {
107
- delayedAuth: event.data.payload
108
- };
109
- (_d = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onEvent) === null || _d === void 0 ? void 0 : _d.call(currentOptions, {
110
- type: 'integrationConnected',
111
- payload: payload
112
- });
113
- (_e = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onIntegrationConnected) === null || _e === void 0 ? void 0 : _e.call(currentOptions, payload);
114
- return [3 /*break*/, 9];
115
- }
116
- _o.label = 3;
117
- case 3:
118
- {
119
- payload = event.data.payload;
120
- (_f = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onEvent) === null || _f === void 0 ? void 0 : _f.call(currentOptions, {
121
- type: 'transferCompleted',
122
- payload: payload
123
- });
124
- (_g = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onTransferFinished) === null || _g === void 0 ? void 0 : _g.call(currentOptions, payload);
125
- return [3 /*break*/, 9];
126
- }
127
- _o.label = 4;
128
- case 4:
129
- {
130
- payload = (_h = event.data) === null || _h === void 0 ? void 0 : _h.payload;
131
- (_j = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onExit) === null || _j === void 0 ? void 0 : _j.call(currentOptions, payload === null || payload === void 0 ? void 0 : payload.errorMessage, payload);
132
- removePopup();
133
- return [3 /*break*/, 9];
134
- }
135
- _o.label = 5;
136
- case 5:
137
- {
138
- if (event.data.link) {
139
- w = 700;
140
- h = 800;
141
- left = screen.width / 2 - w / 2;
142
- top_1 = screen.height / 2 - h / 2;
143
- (_k = window
144
- .open(event.data.link, '_blank', "popup,noopener,noreferrer,resizable,scrollbars,width=".concat(w, ",height=").concat(h, ",top=").concat(top_1, ",left=").concat(left))) === null || _k === void 0 ? void 0 : _k.focus();
145
- }
146
- return [3 /*break*/, 9];
147
- }
148
- _o.label = 6;
149
- case 6:
150
- sendMessageToIframe({
151
- type: 'meshSDKSpecs',
152
- payload: __assign({}, sdkSpecs)
153
- });
154
- if (currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.accessTokens) {
155
- sendMessageToIframe({
156
- type: 'frontAccessTokens',
157
- payload: currentOptions.accessTokens
158
- });
159
- }
160
- if (currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.transferDestinationTokens) {
161
- sendMessageToIframe({
162
- type: 'frontTransferDestinationTokens',
163
- payload: currentOptions.transferDestinationTokens
164
- });
165
- }
166
- return [4 /*yield*/, getWagmiCoreInjectedData()];
167
- case 7:
168
- injectedConnectors = _o.sent();
169
- if (injectedConnectors) {
170
- sendMessageToIframe({
171
- type: 'SDKinjectedWagmiConnectorsData',
172
- payload: injectedConnectors
173
- });
174
- }
175
- (_l = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onEvent) === null || _l === void 0 ? void 0 : _l.call(currentOptions, { type: 'pageLoaded' });
176
- return [3 /*break*/, 9];
177
- case 8:
178
- {
179
- if (isLinkEventTypeKey(event.data.type)) {
180
- (_m = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onEvent) === null || _m === void 0 ? void 0 : _m.call(currentOptions, event.data);
181
- }
182
- return [3 /*break*/, 9];
183
- }
184
- _o.label = 9;
185
- case 9: return [2 /*return*/];
71
+ function eventsListener(event) {
72
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
73
+ switch (event.data.type) {
74
+ case 'brokerageAccountAccessToken': {
75
+ var payload = {
76
+ accessToken: event.data.payload
77
+ };
78
+ (_a = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onEvent) === null || _a === void 0 ? void 0 : _a.call(currentOptions, {
79
+ type: 'integrationConnected',
80
+ payload: payload
81
+ });
82
+ (_b = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onIntegrationConnected) === null || _b === void 0 ? void 0 : _b.call(currentOptions, payload);
83
+ break;
84
+ }
85
+ case 'delayedAuthentication': {
86
+ var payload = {
87
+ delayedAuth: event.data.payload
88
+ };
89
+ (_c = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onEvent) === null || _c === void 0 ? void 0 : _c.call(currentOptions, {
90
+ type: 'integrationConnected',
91
+ payload: payload
92
+ });
93
+ (_d = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onIntegrationConnected) === null || _d === void 0 ? void 0 : _d.call(currentOptions, payload);
94
+ break;
95
+ }
96
+ case 'transferFinished': {
97
+ var payload = event.data.payload;
98
+ (_e = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onEvent) === null || _e === void 0 ? void 0 : _e.call(currentOptions, {
99
+ type: 'transferCompleted',
100
+ payload: payload
101
+ });
102
+ (_f = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onTransferFinished) === null || _f === void 0 ? void 0 : _f.call(currentOptions, payload);
103
+ break;
104
+ }
105
+ case 'close':
106
+ case 'done': {
107
+ var payload = (_g = event.data) === null || _g === void 0 ? void 0 : _g.payload;
108
+ (_h = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onExit) === null || _h === void 0 ? void 0 : _h.call(currentOptions, payload === null || payload === void 0 ? void 0 : payload.errorMessage, payload);
109
+ removePopup();
110
+ break;
111
+ }
112
+ case 'oauthLinkOpen': {
113
+ if (event.data.link) {
114
+ var w = 700;
115
+ var h = 800;
116
+ var left = screen.width / 2 - w / 2;
117
+ var top_1 = screen.height / 2 - h / 2;
118
+ (_j = window
119
+ .open(event.data.link, '_blank', "popup,noopener,noreferrer,resizable,scrollbars,width=".concat(w, ",height=").concat(h, ",top=").concat(top_1, ",left=").concat(left))) === null || _j === void 0 ? void 0 : _j.focus();
186
120
  }
187
- });
188
- });
189
- }
190
- function handleWalletBrowserEvent(event) {
191
- return __awaiter(this, void 0, void 0, function () {
192
- var _a, payload, result, error_1, payload, result, error_2, payload, result, error_3, payload, result, error_4, payload, result, error_5, payload, result, error_6, payload, result, error_7;
193
- return __generator(this, function (_b) {
194
- switch (_b.label) {
195
- case 0:
196
- _a = event.data.type;
197
- switch (_a) {
198
- case 'walletBrowserInjectedWalletSelected': return [3 /*break*/, 1];
199
- case 'walletBrowserSignRequest': return [3 /*break*/, 6];
200
- case 'walletBrowserChainSwitchRequest': return [3 /*break*/, 11];
201
- case 'walletBrowserNativeTransferRequest': return [3 /*break*/, 16];
202
- case 'walletBrowserNonNativeTransferRequest': return [3 /*break*/, 21];
203
- case 'walletBrowserNativeSmartDeposit': return [3 /*break*/, 26];
204
- case 'walletBrowserNonNativeSmartDeposit': return [3 /*break*/, 31];
205
- case 'walletBrowserDisconnect': return [3 /*break*/, 36];
206
- }
207
- return [3 /*break*/, 37];
208
- case 1:
209
- payload = event.data.payload;
210
- _b.label = 2;
211
- case 2:
212
- _b.trys.push([2, 4, , 5]);
213
- return [4 /*yield*/, connectToSpecificWallet(payload.integrationName)];
214
- case 3:
215
- result = _b.sent();
216
- if (result instanceof Error) {
217
- throw result;
218
- }
219
- sendMessageToIframe({
220
- type: 'SDKinjectedConnectionCompleted',
221
- payload: {
222
- accounts: result.accounts,
223
- chainId: result.chainId
224
- }
225
- });
226
- return [3 /*break*/, 5];
227
- case 4:
228
- error_1 = _b.sent();
229
- handleErrorAndSendMessage(error_1, 'SDKinjectedConnectionCompleted');
230
- return [3 /*break*/, 5];
231
- case 5: return [3 /*break*/, 37];
232
- case 6:
233
- payload = event.data.payload;
234
- _b.label = 7;
235
- case 7:
236
- _b.trys.push([7, 9, , 10]);
237
- return [4 /*yield*/, signedMessage(payload.address)];
238
- case 8:
239
- result = _b.sent();
240
- if (result instanceof Error) {
241
- throw result;
242
- }
243
- sendMessageToIframe({
244
- type: 'SDKsignRequestCompleted',
245
- payload: result
246
- });
247
- return [3 /*break*/, 10];
248
- case 9:
249
- error_2 = _b.sent();
250
- handleErrorAndSendMessage(error_2, 'SDKsignRequestCompleted');
251
- return [3 /*break*/, 10];
252
- case 10: return [3 /*break*/, 37];
253
- case 11:
254
- payload = event.data.payload;
255
- _b.label = 12;
256
- case 12:
257
- _b.trys.push([12, 14, , 15]);
258
- return [4 /*yield*/, switchChainFromSDK(payload.chainId)];
259
- case 13:
260
- result = _b.sent();
261
- if (result instanceof Error) {
262
- throw result;
263
- }
264
- sendMessageToIframe({
265
- type: 'SDKswitchChainCompleted',
266
- payload: result
267
- });
268
- return [3 /*break*/, 15];
269
- case 14:
270
- error_3 = _b.sent();
271
- handleErrorAndSendMessage(error_3, 'SDKswitchChainCompleted');
272
- return [3 /*break*/, 15];
273
- case 15: return [3 /*break*/, 37];
274
- case 16:
275
- payload = event.data.payload;
276
- _b.label = 17;
277
- case 17:
278
- _b.trys.push([17, 19, , 20]);
279
- return [4 /*yield*/, sendTransactionFromSDK(payload.toAddress, payload.amount, payload.decimalPlaces, payload.chainId, payload.account)];
280
- case 18:
281
- result = _b.sent();
282
- if (result instanceof Error) {
283
- throw result;
284
- }
285
- sendMessageToIframe({
286
- type: 'SDKnativeTransferCompleted',
287
- payload: result
288
- });
289
- return [3 /*break*/, 20];
290
- case 19:
291
- error_4 = _b.sent();
292
- handleErrorAndSendMessage(error_4, 'SDKnativeTransferCompleted');
293
- return [3 /*break*/, 20];
294
- case 20: return [3 /*break*/, 37];
295
- case 21:
296
- payload = event.data.payload;
297
- _b.label = 22;
298
- case 22:
299
- _b.trys.push([22, 24, , 25]);
300
- return [4 /*yield*/, sendNonNativeTransactionFromSDK(payload.address, JSON.parse(payload.abi), payload.functionName, payload.args)];
301
- case 23:
302
- result = _b.sent();
303
- if (result instanceof Error) {
304
- throw result;
305
- }
306
- sendMessageToIframe({
307
- type: 'SDKnonNativeTransferCompleted',
308
- payload: result
309
- });
310
- return [3 /*break*/, 25];
311
- case 24:
312
- error_5 = _b.sent();
313
- handleErrorAndSendMessage(error_5, 'SDKnonNativeTransferCompleted');
314
- return [3 /*break*/, 25];
315
- case 25: return [3 /*break*/, 37];
316
- case 26:
317
- payload = event.data.payload;
318
- _b.label = 27;
319
- case 27:
320
- _b.trys.push([27, 29, , 30]);
321
- return [4 /*yield*/, sendNonNativeTransactionFromSDK(payload.address, JSON.parse(payload.abi), payload.functionName, payload.args, payload.value)];
322
- case 28:
323
- result = _b.sent();
324
- if (result instanceof Error) {
325
- throw result;
326
- }
327
- sendMessageToIframe({
328
- type: 'SDKnativeSmartDepositCompleted',
329
- payload: {
330
- txHash: result
331
- }
332
- });
333
- return [3 /*break*/, 30];
334
- case 29:
335
- error_6 = _b.sent();
336
- handleErrorAndSendMessage(error_6, 'SDKnativeSmartDepositCompleted');
337
- return [3 /*break*/, 30];
338
- case 30: return [3 /*break*/, 37];
339
- case 31:
340
- payload = event.data.payload;
341
- _b.label = 32;
342
- case 32:
343
- _b.trys.push([32, 34, , 35]);
344
- return [4 /*yield*/, sendNonNativeTransactionFromSDK(payload.address, JSON.parse(payload.abi), payload.functionName, payload.args)];
345
- case 33:
346
- result = _b.sent();
347
- if (result) {
348
- sendMessageToIframe({
349
- type: 'SDKnonNativeSmartDepositCompleted',
350
- payload: {
351
- txHash: result
352
- }
353
- });
354
- }
355
- else {
356
- throw new Error('Transfer failed');
357
- }
358
- return [3 /*break*/, 35];
359
- case 34:
360
- error_7 = _b.sent();
361
- handleErrorAndSendMessage(error_7, 'SDKnonNativeSmartDepositCompleted');
362
- return [3 /*break*/, 35];
363
- case 35: return [3 /*break*/, 37];
364
- case 36:
365
- {
366
- disconnectAllAccounts();
367
- sendMessageToIframe({
368
- type: 'SDKdisconnectSuccess'
369
- });
370
- return [3 /*break*/, 37];
371
- }
372
- _b.label = 37;
373
- case 37: return [2 /*return*/];
121
+ break;
122
+ }
123
+ case 'loaded': {
124
+ sendMessageToIframe({
125
+ type: 'meshSDKSpecs',
126
+ payload: __assign({}, sdkSpecs)
127
+ });
128
+ if (currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.accessTokens) {
129
+ sendMessageToIframe({
130
+ type: 'frontAccessTokens',
131
+ payload: currentOptions.accessTokens
132
+ });
374
133
  }
375
- });
376
- });
377
- }
378
- function eventsListener(event) {
379
- return __awaiter(this, void 0, void 0, function () {
380
- return __generator(this, function (_a) {
381
- switch (_a.label) {
382
- case 0:
383
- if (!isWalletBrowserEventTypeKey(event.data.type)) return [3 /*break*/, 2];
384
- return [4 /*yield*/, handleWalletBrowserEvent(event)];
385
- case 1:
386
- _a.sent();
387
- return [3 /*break*/, 4];
388
- case 2: return [4 /*yield*/, handleLinkEvent(event)];
389
- case 3:
390
- _a.sent();
391
- _a.label = 4;
392
- case 4: return [2 /*return*/];
134
+ if (currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.transferDestinationTokens) {
135
+ sendMessageToIframe({
136
+ type: 'frontTransferDestinationTokens',
137
+ payload: currentOptions.transferDestinationTokens
138
+ });
393
139
  }
394
- });
395
- });
396
- }
397
- function handleErrorAndSendMessage(error, messageType) {
398
- sendMessageToIframe({
399
- type: messageType,
400
- payload: {
401
- error: error
140
+ (_k = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onEvent) === null || _k === void 0 ? void 0 : _k.call(currentOptions, { type: 'pageLoaded' });
141
+ break;
402
142
  }
403
- });
143
+ default: {
144
+ if (isLinkEventTypeKey(event.data.type)) {
145
+ (_l = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onEvent) === null || _l === void 0 ? void 0 : _l.call(currentOptions, event.data);
146
+ }
147
+ break;
148
+ }
149
+ }
404
150
  }
405
151
  export var createLink = function (options) {
406
152
  var openLink = function (linkToken) { return __awaiter(void 0, void 0, void 0, function () {
@@ -428,7 +174,6 @@ export var createLink = function (options) {
428
174
  removePopup();
429
175
  window.removeEventListener('message', eventsListener);
430
176
  (_a = options.onExit) === null || _a === void 0 ? void 0 : _a.call(options);
431
- disconnectAllAccounts();
432
177
  };
433
178
  return {
434
179
  openLink: openLink,