@mocanetwork/airkit 0.5.0-beta.2

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.
Files changed (80) hide show
  1. package/README.md +177 -0
  2. package/dist/airkit.cjs.js +2662 -0
  3. package/dist/airkit.esm.js +2468 -0
  4. package/dist/airkit.umd.min.js +2 -0
  5. package/dist/airkit.umd.min.js.LICENSE.txt +12 -0
  6. package/dist/lib.cjs/common/src/error.js +25 -0
  7. package/dist/lib.cjs/common/src/realm/messaging/auth.js +28 -0
  8. package/dist/lib.cjs/common/src/realm/messaging/types.js +15 -0
  9. package/dist/lib.cjs/ws-embed/src/PopupHandler.js +64 -0
  10. package/dist/lib.cjs/ws-embed/src/airService.js +364 -0
  11. package/dist/lib.cjs/ws-embed/src/baseProvider.js +178 -0
  12. package/dist/lib.cjs/ws-embed/src/communicationProvider.js +346 -0
  13. package/dist/lib.cjs/ws-embed/src/embed.js +735 -0
  14. package/dist/lib.cjs/ws-embed/src/error.js +21 -0
  15. package/dist/lib.cjs/ws-embed/src/iframeController.js +68 -0
  16. package/dist/lib.cjs/ws-embed/src/inPageProvider.js +307 -0
  17. package/dist/lib.cjs/ws-embed/src/index.js +20 -0
  18. package/dist/lib.cjs/ws-embed/src/interfaces.js +29 -0
  19. package/dist/lib.cjs/ws-embed/src/isStream.js +20 -0
  20. package/dist/lib.cjs/ws-embed/src/loglevel.js +7 -0
  21. package/dist/lib.cjs/ws-embed/src/messageService.js +212 -0
  22. package/dist/lib.cjs/ws-embed/src/messages.js +22 -0
  23. package/dist/lib.cjs/ws-embed/src/siteMetadata.js +77 -0
  24. package/dist/lib.cjs/ws-embed/src/utils.js +95 -0
  25. package/dist/lib.esm/common/src/error.js +23 -0
  26. package/dist/lib.esm/common/src/realm/messaging/auth.js +26 -0
  27. package/dist/lib.esm/common/src/realm/messaging/types.js +13 -0
  28. package/dist/lib.esm/ws-embed/src/PopupHandler.js +62 -0
  29. package/dist/lib.esm/ws-embed/src/airService.js +362 -0
  30. package/dist/lib.esm/ws-embed/src/baseProvider.js +176 -0
  31. package/dist/lib.esm/ws-embed/src/communicationProvider.js +344 -0
  32. package/dist/lib.esm/ws-embed/src/embed.js +730 -0
  33. package/dist/lib.esm/ws-embed/src/error.js +18 -0
  34. package/dist/lib.esm/ws-embed/src/iframeController.js +66 -0
  35. package/dist/lib.esm/ws-embed/src/inPageProvider.js +302 -0
  36. package/dist/lib.esm/ws-embed/src/index.js +5 -0
  37. package/dist/lib.esm/ws-embed/src/interfaces.js +21 -0
  38. package/dist/lib.esm/ws-embed/src/isStream.js +15 -0
  39. package/dist/lib.esm/ws-embed/src/loglevel.js +5 -0
  40. package/dist/lib.esm/ws-embed/src/messageService.js +210 -0
  41. package/dist/lib.esm/ws-embed/src/messages.js +20 -0
  42. package/dist/lib.esm/ws-embed/src/siteMetadata.js +75 -0
  43. package/dist/lib.esm/ws-embed/src/utils.js +88 -0
  44. package/dist/types/PopupHandler.d.ts +24 -0
  45. package/dist/types/airService.d.ts +34 -0
  46. package/dist/types/baseProvider.d.ts +66 -0
  47. package/dist/types/common/custom.d.ts +4 -0
  48. package/dist/types/common/error.d.ts +17 -0
  49. package/dist/types/common/realm/error/auth.d.ts +1 -0
  50. package/dist/types/common/realm/error/types.d.ts +1 -0
  51. package/dist/types/common/realm/messaging/auth.d.ts +138 -0
  52. package/dist/types/common/realm/messaging/types.d.ts +95 -0
  53. package/dist/types/common/realm/partner/config.d.ts +68 -0
  54. package/dist/types/common/realm/user/types.d.ts +78 -0
  55. package/dist/types/common/realm/wallet/bybit/bybit.d.ts +2 -0
  56. package/dist/types/common/realm/wallet/coinbase/coinbase.d.ts +2 -0
  57. package/dist/types/common/realm/wallet/cryptoCom/cryptoCom.d.ts +2 -0
  58. package/dist/types/common/realm/wallet/index.d.ts +22 -0
  59. package/dist/types/common/realm/wallet/metamask/metamask.d.ts +2 -0
  60. package/dist/types/common/realm/wallet/okx/okx.d.ts +2 -0
  61. package/dist/types/common/realm/wallet/phantom/phantom.d.ts +2 -0
  62. package/dist/types/common/realm/wallet/rabby/rabby.d.ts +2 -0
  63. package/dist/types/common/realm/wallet/rainbow/rainbow.d.ts +2 -0
  64. package/dist/types/common/realm/wallet/trust/trust.d.ts +2 -0
  65. package/dist/types/common/types.d.ts +8 -0
  66. package/dist/types/common/utils.d.ts +2 -0
  67. package/dist/types/communicationProvider.d.ts +82 -0
  68. package/dist/types/embed.d.ts +42 -0
  69. package/dist/types/error.d.ts +9 -0
  70. package/dist/types/iframeController.d.ts +18 -0
  71. package/dist/types/inPageProvider.d.ts +106 -0
  72. package/dist/types/index.d.ts +4 -0
  73. package/dist/types/interfaces.d.ts +261 -0
  74. package/dist/types/isStream.d.ts +4 -0
  75. package/dist/types/loglevel.d.ts +3 -0
  76. package/dist/types/messageService.d.ts +51 -0
  77. package/dist/types/messages.d.ts +19 -0
  78. package/dist/types/siteMetadata.d.ts +9 -0
  79. package/dist/types/utils.d.ts +24 -0
  80. package/package.json +76 -0
@@ -0,0 +1,346 @@
1
+ 'use strict';
2
+
3
+ var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
4
+ var _defineProperty = require('@babel/runtime/helpers/defineProperty');
5
+ var baseControllers = require('@toruslabs/base-controllers');
6
+ var auth = require('@web3auth/auth');
7
+ var baseProvider = require('./baseProvider.js');
8
+ var interfaces = require('./interfaces.js');
9
+ var loglevel = require('./loglevel.js');
10
+ var messages = require('./messages.js');
11
+ var PopupHandler = require('./PopupHandler.js');
12
+
13
+ /**
14
+ * @param connectionStream - A Node.js duplex stream
15
+ * @param opts - An options bag
16
+ */
17
+ class CommunicationProvider extends baseProvider {
18
+ constructor(connectionStream, {
19
+ maxEventListeners = 100,
20
+ jsonRpcStreamName = "provider"
21
+ }, state) {
22
+ super(connectionStream, {
23
+ maxEventListeners,
24
+ jsonRpcStreamName
25
+ });
26
+
27
+ // private state
28
+ _defineProperty(this, "tryWindowHandle", void 0);
29
+ _defineProperty(this, "onLoginStateHandle", void 0);
30
+ _defineProperty(this, "windowRefs", void 0);
31
+ _defineProperty(this, "iframeUrl", void 0);
32
+ _defineProperty(this, "iframeId", void 0);
33
+ this.state = _objectSpread(_objectSpread({}, CommunicationProvider.defaultState), state);
34
+
35
+ // public state
36
+ this.iframeUrl = "";
37
+ this.iframeId = "";
38
+ this.windowRefs = new Map();
39
+
40
+ // setup own event listeners
41
+
42
+ // EIP-1193 connect
43
+ this.on("connect", () => {
44
+ this.state.isConnected = true;
45
+ });
46
+ const notificationHandler = payload => {
47
+ const {
48
+ method,
49
+ params
50
+ } = payload;
51
+ // create_window should never come here..
52
+ // we either pre-open from embed.
53
+ // if it's blocked, we communicate down that it's blocked and we show full screen iframe and open from iframe
54
+ if (method === baseControllers.COMMUNICATION_NOTIFICATIONS.IFRAME_STATUS) {
55
+ const {
56
+ isFullScreen,
57
+ rid
58
+ } = params;
59
+ this.displayIframe({
60
+ isFull: isFullScreen,
61
+ rid: rid
62
+ }, true);
63
+ } else if (method === baseControllers.COMMUNICATION_NOTIFICATIONS.CLOSE_WINDOW) {
64
+ this.handleCloseWindow(params);
65
+ } else if (method === baseControllers.COMMUNICATION_NOTIFICATIONS.USER_LOGGED_IN) {
66
+ loglevel.info("[TEST METAMASK] User received notification ", baseControllers.COMMUNICATION_NOTIFICATIONS.USER_LOGGED_IN);
67
+ const {
68
+ currentLoginProvider
69
+ } = params;
70
+ this.state.isLoggedIn = true;
71
+ this.state.currentLoginProvider = currentLoginProvider;
72
+ if (this.onLoginStateHandle) this.onLoginStateHandle(true);
73
+ } else if (method === baseControllers.COMMUNICATION_NOTIFICATIONS.USER_LOGGED_OUT) {
74
+ this.state.isLoggedIn = false;
75
+ this.state.currentLoginProvider = null;
76
+ this.displayIframe();
77
+ if (this.onLoginStateHandle) this.onLoginStateHandle(false);
78
+ }
79
+ };
80
+ this.jsonRpcConnectionEvents.on("notification", notificationHandler);
81
+ }
82
+ get isLoggedIn() {
83
+ return this.state.isLoggedIn;
84
+ }
85
+
86
+ // e.g. Metamask, WalletConnect, etc.
87
+ get isExternalWallet() {
88
+ return !this.state.currentLoginProvider || this.state.currentLoginProvider === interfaces.EXTERNAL_LOGIN_PROVIDER.METAMASK || this.state.currentLoginProvider === interfaces.EXTERNAL_LOGIN_PROVIDER.WALLET_CONNECT || this.state.currentLoginProvider === interfaces.EXTERNAL_LOGIN_PROVIDER.WAGMI;
89
+ }
90
+ get isIFrameFullScreen() {
91
+ return this.state.isIFrameFullScreen;
92
+ }
93
+
94
+ /**
95
+ * Returns whether the inPage provider is connected to Torus.
96
+ */
97
+ isConnected() {
98
+ return this.state.isConnected;
99
+ }
100
+ async initializeState(params) {
101
+ try {
102
+ const {
103
+ torusUrl,
104
+ torusIframeId
105
+ } = params;
106
+ this.iframeUrl = torusUrl;
107
+ this.iframeId = torusIframeId;
108
+ const {
109
+ currentLoginProvider,
110
+ isLoggedIn
111
+ } = await this.request({
112
+ method: baseControllers.COMMUNICATION_JRPC_METHODS.GET_PROVIDER_STATE,
113
+ params: []
114
+ });
115
+
116
+ // indicate that we've connected, for EIP-1193 compliance
117
+ if (isLoggedIn) this.handleConnect(currentLoginProvider, isLoggedIn);
118
+ } catch (error) {
119
+ loglevel.error("Web3Auth: Failed to get initial state. Please report this bug.", error);
120
+ } finally {
121
+ loglevel.info("initialized communication state");
122
+ this.state.initialized = true;
123
+ }
124
+ }
125
+ hideTorusButton() {
126
+ this.state.widgetVisibility = false;
127
+ this.displayIframe();
128
+ }
129
+ showTorusButton() {
130
+ this.state.widgetVisibility = true;
131
+ this.displayIframe();
132
+ }
133
+ moveToBackground() {
134
+ this.state.isBackground = true;
135
+ this.displayIframe({
136
+ isFull: this.state.isIFrameFullScreen
137
+ });
138
+ }
139
+ moveToForeground() {
140
+ this.state.isBackground = false;
141
+ this.displayIframe({
142
+ isFull: this.state.isIFrameFullScreen
143
+ });
144
+ }
145
+ setIframeVisibility({
146
+ isVisible
147
+ }) {
148
+ this.state.isBackground = !isVisible;
149
+ }
150
+ displayIframe({
151
+ isFull = false,
152
+ rid = ""
153
+ } = {}, walletRequest = false) {
154
+ const style = {};
155
+ // set phase
156
+ if (!isFull) {
157
+ style.display = this.state.widgetVisibility && !this.state.isBackground ? "block" : "none";
158
+ style.height = "70px";
159
+ style.width = "70px";
160
+ switch (this.state.buttonPosition) {
161
+ case interfaces.BUTTON_POSITION.TOP_LEFT:
162
+ style.top = "0px";
163
+ style.left = "0px";
164
+ style.right = "auto";
165
+ style.bottom = "auto";
166
+ break;
167
+ case interfaces.BUTTON_POSITION.TOP_RIGHT:
168
+ style.top = "0px";
169
+ style.right = "0px";
170
+ style.left = "auto";
171
+ style.bottom = "auto";
172
+ break;
173
+ case interfaces.BUTTON_POSITION.BOTTOM_RIGHT:
174
+ style.bottom = "0px";
175
+ style.right = "0px";
176
+ style.top = "auto";
177
+ style.left = "auto";
178
+ break;
179
+ case interfaces.BUTTON_POSITION.BOTTOM_LEFT:
180
+ default:
181
+ style.bottom = "0px";
182
+ style.left = "0px";
183
+ style.top = "auto";
184
+ style.right = "auto";
185
+ break;
186
+ }
187
+ } else {
188
+ style.display = this.state.isBackground ? "none" : "block";
189
+ style.width = "100%";
190
+ style.height = "100%";
191
+ style.top = "0px";
192
+ style.right = "0px";
193
+ style.left = "0px";
194
+ style.bottom = "0px";
195
+ }
196
+ const iframe = document.getElementById(this.iframeId);
197
+ Object.assign(iframe.style, style);
198
+ this.state.isIFrameFullScreen = isFull;
199
+ if (!walletRequest) {
200
+ this.request({
201
+ method: baseControllers.COMMUNICATION_JRPC_METHODS.IFRAME_STATUS,
202
+ params: {
203
+ isIFrameFullScreen: isFull,
204
+ rid
205
+ }
206
+ });
207
+ }
208
+ }
209
+
210
+ /**
211
+ * Scenarios:
212
+ * - Login request or pre-open confirmation windows
213
+ * We try to open here or send a rpc request to iframe that window is blocked.
214
+ */
215
+ async handleWindow(windowId, {
216
+ url,
217
+ target,
218
+ features,
219
+ timeout
220
+ } = {}) {
221
+ const finalUrl = new URL(url || `${this.iframeUrl}/redirect?windowId=${windowId}&sessionNamespace=${window.location.hostname}`);
222
+ const handledWindow = new PopupHandler({
223
+ url: finalUrl,
224
+ target,
225
+ features,
226
+ timeout
227
+ });
228
+ handledWindow.open();
229
+ if (!handledWindow.window) {
230
+ this.displayIframe({
231
+ isFull: true
232
+ });
233
+ this.request({
234
+ method: baseControllers.COMMUNICATION_JRPC_METHODS.WINDOW_BLOCKED,
235
+ params: {
236
+ windowId,
237
+ finalUrl: finalUrl.href
238
+ }
239
+ });
240
+ return;
241
+ }
242
+ // Add to collection only if window is opened
243
+ this.windowRefs.set(windowId, handledWindow);
244
+ handledWindow.once("close", () => {
245
+ // user closed the window
246
+ this.windowRefs.delete(windowId);
247
+ this.request({
248
+ method: baseControllers.COMMUNICATION_JRPC_METHODS.CLOSED_WINDOW,
249
+ params: {
250
+ windowId
251
+ }
252
+ });
253
+ });
254
+ }
255
+
256
+ /**
257
+ * Internal RPC method. Forwards requests to background via the RPC engine.
258
+ * Also remap ids inbound and outbound
259
+ */
260
+ rpcRequest(payload, callback) {
261
+ const cb = callback;
262
+ const _payload = payload;
263
+ if (!Array.isArray(_payload)) {
264
+ if (!_payload.jsonrpc) {
265
+ _payload.jsonrpc = "2.0";
266
+ }
267
+ }
268
+ this.rpcEngine.handle(_payload, cb);
269
+ }
270
+
271
+ /**
272
+ * When the provider becomes connected, updates internal state and emits
273
+ * required events. Idempotent.
274
+ *
275
+ * @param currentLoginProvider - The login Provider
276
+ * emits TorusInpageProvider#connect
277
+ */
278
+ handleConnect(currentLoginProvider, isLoggedIn) {
279
+ if (!this.state.isConnected) {
280
+ this.state.isConnected = true;
281
+ this.emit("connect", {
282
+ currentLoginProvider,
283
+ isLoggedIn
284
+ });
285
+ loglevel.debug(messages.info.connected(currentLoginProvider));
286
+ }
287
+ }
288
+
289
+ /**
290
+ * When the provider becomes disconnected, updates internal state and emits
291
+ * required events. Idempotent with respect to the isRecoverable parameter.
292
+ *
293
+ * Error codes per the CloseEvent status codes as required by EIP-1193:
294
+ * https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes
295
+ *
296
+ * @param isRecoverable - Whether the disconnection is recoverable.
297
+ * @param errorMessage - A custom error message.
298
+ * emits TorusInpageProvider#disconnect
299
+ */
300
+ handleDisconnect(isRecoverable, errorMessage) {
301
+ if (this.state.isConnected || !this.state.isPermanentlyDisconnected && !isRecoverable) {
302
+ this.state.isConnected = false;
303
+ let error;
304
+ if (isRecoverable) {
305
+ error = new auth.EthereumProviderError(1013,
306
+ // Try again later
307
+ errorMessage || messages.errors.disconnected());
308
+ loglevel.debug(error);
309
+ } else {
310
+ error = new auth.EthereumProviderError(1011,
311
+ // Internal error
312
+ errorMessage || messages.errors.permanentlyDisconnected());
313
+ loglevel.error(error);
314
+ this.state.currentLoginProvider = null;
315
+ this.state.isLoggedIn = false;
316
+ this.state.widgetVisibility = false;
317
+ this.state.isIFrameFullScreen = false;
318
+ this.state.isPermanentlyDisconnected = true;
319
+ }
320
+ this.emit("disconnect", error);
321
+ }
322
+ }
323
+ handleCloseWindow(params) {
324
+ const {
325
+ windowId
326
+ } = params;
327
+ if (this.windowRefs.has(windowId)) {
328
+ this.windowRefs.get(windowId).close();
329
+ this.windowRefs.delete(windowId);
330
+ }
331
+ }
332
+ }
333
+ _defineProperty(CommunicationProvider, "defaultState", {
334
+ buttonPosition: "bottom-left",
335
+ currentLoginProvider: null,
336
+ isIFrameFullScreen: false,
337
+ isBackground: false,
338
+ widgetVisibility: false,
339
+ initialized: false,
340
+ isLoggedIn: false,
341
+ isPermanentlyDisconnected: false,
342
+ isConnected: false,
343
+ hasEmittedConnection: false
344
+ });
345
+
346
+ module.exports = CommunicationProvider;