@mocanetwork/airkit 0.5.0 → 1.0.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.
Files changed (71) hide show
  1. package/dist/airkit.cjs.js +1921 -2597
  2. package/dist/airkit.esm.js +1925 -2413
  3. package/dist/airkit.umd.js +2104 -0
  4. package/dist/types/airService.d.ts +17 -12
  5. package/dist/types/airWalletProvider.d.ts +13 -0
  6. package/dist/types/common/air/error.d.ts +8 -0
  7. package/dist/types/common/custom.d.ts +6 -0
  8. package/dist/types/common/provider/error.d.ts +16 -0
  9. package/dist/types/common/provider/types.d.ts +24 -0
  10. package/dist/types/common/realm/error.d.ts +0 -0
  11. package/dist/types/common/realm/messaging/auth.d.ts +18 -21
  12. package/dist/types/common/realm/messaging/provider.d.ts +32 -0
  13. package/dist/types/common/realm/messaging/types.d.ts +52 -63
  14. package/dist/types/common/realm/partner/config.d.ts +1 -32
  15. package/dist/types/common/realm/smart-session/types.d.ts +1 -1
  16. package/dist/types/common/realm/user/types.d.ts +2 -31
  17. package/dist/types/common/types.d.ts +3 -0
  18. package/dist/types/common/utils.d.ts +17 -0
  19. package/dist/types/error.d.ts +1 -1
  20. package/dist/types/index.d.ts +2 -2
  21. package/dist/types/interfaces.d.ts +26 -187
  22. package/dist/types/messageService.d.ts +23 -31
  23. package/dist/types/utils.d.ts +3 -21
  24. package/package.json +7 -15
  25. package/dist/airkit.umd.min.js +0 -2
  26. package/dist/airkit.umd.min.js.LICENSE.txt +0 -12
  27. package/dist/lib.cjs/common/src/error.js +0 -25
  28. package/dist/lib.cjs/common/src/realm/messaging/auth.js +0 -28
  29. package/dist/lib.cjs/common/src/realm/messaging/types.js +0 -21
  30. package/dist/lib.cjs/ws-embed/src/PopupHandler.js +0 -64
  31. package/dist/lib.cjs/ws-embed/src/airService.js +0 -398
  32. package/dist/lib.cjs/ws-embed/src/baseProvider.js +0 -178
  33. package/dist/lib.cjs/ws-embed/src/communicationProvider.js +0 -346
  34. package/dist/lib.cjs/ws-embed/src/embed.js +0 -750
  35. package/dist/lib.cjs/ws-embed/src/error.js +0 -21
  36. package/dist/lib.cjs/ws-embed/src/iframeController.js +0 -68
  37. package/dist/lib.cjs/ws-embed/src/inPageProvider.js +0 -307
  38. package/dist/lib.cjs/ws-embed/src/index.js +0 -20
  39. package/dist/lib.cjs/ws-embed/src/interfaces.js +0 -29
  40. package/dist/lib.cjs/ws-embed/src/isStream.js +0 -20
  41. package/dist/lib.cjs/ws-embed/src/loglevel.js +0 -7
  42. package/dist/lib.cjs/ws-embed/src/messageService.js +0 -269
  43. package/dist/lib.cjs/ws-embed/src/messages.js +0 -22
  44. package/dist/lib.cjs/ws-embed/src/siteMetadata.js +0 -77
  45. package/dist/lib.cjs/ws-embed/src/utils.js +0 -95
  46. package/dist/lib.esm/common/src/error.js +0 -23
  47. package/dist/lib.esm/common/src/realm/messaging/auth.js +0 -26
  48. package/dist/lib.esm/common/src/realm/messaging/types.js +0 -19
  49. package/dist/lib.esm/ws-embed/src/PopupHandler.js +0 -62
  50. package/dist/lib.esm/ws-embed/src/airService.js +0 -396
  51. package/dist/lib.esm/ws-embed/src/baseProvider.js +0 -176
  52. package/dist/lib.esm/ws-embed/src/communicationProvider.js +0 -344
  53. package/dist/lib.esm/ws-embed/src/embed.js +0 -745
  54. package/dist/lib.esm/ws-embed/src/error.js +0 -18
  55. package/dist/lib.esm/ws-embed/src/iframeController.js +0 -66
  56. package/dist/lib.esm/ws-embed/src/inPageProvider.js +0 -302
  57. package/dist/lib.esm/ws-embed/src/index.js +0 -5
  58. package/dist/lib.esm/ws-embed/src/interfaces.js +0 -21
  59. package/dist/lib.esm/ws-embed/src/isStream.js +0 -15
  60. package/dist/lib.esm/ws-embed/src/loglevel.js +0 -5
  61. package/dist/lib.esm/ws-embed/src/messageService.js +0 -267
  62. package/dist/lib.esm/ws-embed/src/messages.js +0 -20
  63. package/dist/lib.esm/ws-embed/src/siteMetadata.js +0 -75
  64. package/dist/lib.esm/ws-embed/src/utils.js +0 -88
  65. package/dist/types/PopupHandler.d.ts +0 -24
  66. package/dist/types/baseProvider.d.ts +0 -66
  67. package/dist/types/communicationProvider.d.ts +0 -82
  68. package/dist/types/embed.d.ts +0 -55
  69. package/dist/types/inPageProvider.d.ts +0 -106
  70. package/dist/types/isStream.d.ts +0 -4
  71. package/dist/types/messages.d.ts +0 -19
@@ -1,18 +0,0 @@
1
- import { BaseError } from '../../common/src/error.js';
2
-
3
- class AirServiceError extends BaseError {
4
- static from(error) {
5
- if (error instanceof AirServiceError) {
6
- return error;
7
- } else if (error instanceof Object && "message" in error) {
8
- if (error.message === "User cancelled login") {
9
- return new AirServiceError("USER_CANCELLED", error.message);
10
- }
11
- return new AirServiceError("UNKNOWN_ERROR", error.message.toString());
12
- }
13
- return new AirServiceError("UNKNOWN_ERROR");
14
- }
15
- }
16
- class RealmEmbedError extends AirServiceError {}
17
-
18
- export { AirServiceError, RealmEmbedError };
@@ -1,66 +0,0 @@
1
- import _objectSpread from '@babel/runtime/helpers/objectSpread2';
2
- import _defineProperty from '@babel/runtime/helpers/defineProperty';
3
-
4
- class IframeController {
5
- constructor(iframeUrl, iframeId, state) {
6
- _defineProperty(this, "state", void 0);
7
- _defineProperty(this, "_iframeElement", null);
8
- _defineProperty(this, "iframeUrl", void 0);
9
- _defineProperty(this, "iframeId", void 0);
10
- this.state = _objectSpread(_objectSpread({}, IframeController.defaultState), state);
11
- this.iframeUrl = iframeUrl;
12
- this.iframeId = iframeId;
13
- }
14
- get iframeElement() {
15
- return this._iframeElement;
16
- }
17
- createIframe() {
18
- if (this._iframeElement) return this._iframeElement;
19
- const iframe = document.createElement("iframe");
20
- iframe.id = this.iframeId;
21
- iframe.allow = "publickey-credentials-get *; publickey-credentials-create *";
22
- iframe.src = this.iframeUrl;
23
- iframe.style.position = "fixed";
24
- iframe.style.zIndex = "999999";
25
- iframe.style.border = "none";
26
- iframe.style.margin = "0";
27
- iframe.style.padding = "0";
28
- iframe.style.display = "none";
29
- document.body.appendChild(iframe);
30
- this._iframeElement = iframe;
31
- return iframe;
32
- }
33
- setIframeVisibility(isVisible) {
34
- this.state.isVisible = isVisible;
35
- }
36
- updateIframeState() {
37
- if (!this.iframeElement) return;
38
- const style = {};
39
- style.display = this.state.isVisible ? "block" : "none";
40
- style.width = "100%";
41
- style.height = "100%";
42
- style.top = "0px";
43
- style.right = "0px";
44
- style.left = "0px";
45
- style.bottom = "0px";
46
- Object.assign(this.iframeElement.style, style);
47
- }
48
- destroy() {
49
- if (this.iframeElement) {
50
- this.iframeElement.remove();
51
- this._iframeElement = null;
52
- }
53
- }
54
- postMessage(message) {
55
- if (!this.iframeElement) return;
56
- const {
57
- origin
58
- } = new URL(this.iframeElement.src);
59
- this.iframeElement.contentWindow.postMessage(message, origin);
60
- }
61
- }
62
- _defineProperty(IframeController, "defaultState", {
63
- isVisible: false
64
- });
65
-
66
- export { IframeController };
@@ -1,302 +0,0 @@
1
- import _objectSpread from '@babel/runtime/helpers/objectSpread2';
2
- import _defineProperty from '@babel/runtime/helpers/defineProperty';
3
- import { PROVIDER_JRPC_METHODS, PROVIDER_NOTIFICATIONS } from '@toruslabs/base-controllers';
4
- import { EthereumProviderError } from '@web3auth/auth';
5
- import dequal from 'fast-deep-equal';
6
- import BaseProvider from './baseProvider.js';
7
- import log from './loglevel.js';
8
- import messages from './messages.js';
9
- import { EMITTED_NOTIFICATIONS } from './utils.js';
10
-
11
- /**
12
- * @param connectionStream - A Node.js duplex stream
13
- * @param opts - An options bag
14
- */
15
- class AirInPageProvider extends BaseProvider {
16
- constructor(connectionStream, {
17
- maxEventListeners = 100,
18
- jsonRpcStreamName = "provider"
19
- }) {
20
- super(connectionStream, {
21
- maxEventListeners,
22
- jsonRpcStreamName
23
- });
24
-
25
- // private state
26
- /**
27
- * The chain ID of the currently connected EVM chain.
28
- * See [chainId.network]{@link https://chainid.network} for more information.
29
- */
30
- _defineProperty(this, "chainId", void 0);
31
- /**
32
- * The user's currently selected EVM address.
33
- * If null, Torus is either locked or the user has not permitted any
34
- * addresses to be viewed.
35
- */
36
- _defineProperty(this, "selectedAddress", void 0);
37
- _defineProperty(this, "tryWindowHandle", void 0);
38
- this.state = _objectSpread({}, AirInPageProvider.defaultState);
39
-
40
- // public state
41
- this.selectedAddress = null;
42
- this.chainId = null;
43
- this.handleAccountsChanged = this.handleAccountsChanged.bind(this);
44
- this.handleChainChanged = this.handleChainChanged.bind(this);
45
- this.handleUnlockStateChanged = this.handleUnlockStateChanged.bind(this);
46
-
47
- // setup own event listeners
48
-
49
- // EIP-1193 connect
50
- this.on("connect", () => {
51
- this.state.isConnected = true;
52
- });
53
- const jsonRpcNotificationHandler = payload => {
54
- const {
55
- method,
56
- params
57
- } = payload;
58
- if (method === PROVIDER_NOTIFICATIONS.ACCOUNTS_CHANGED) {
59
- this.handleAccountsChanged(params);
60
- } else if (method === PROVIDER_NOTIFICATIONS.UNLOCK_STATE_CHANGED) {
61
- this.handleUnlockStateChanged(params);
62
- } else if (method === PROVIDER_NOTIFICATIONS.CHAIN_CHANGED) {
63
- this.handleChainChanged(params);
64
- } else if (EMITTED_NOTIFICATIONS.includes(method)) {
65
- this.emit("data", payload);
66
- this.emit("notification", params.result);
67
- this.emit("message", {
68
- type: method,
69
- data: params
70
- });
71
- }
72
- };
73
-
74
- // json rpc notification listener
75
- this.jsonRpcConnectionEvents.on("notification", jsonRpcNotificationHandler);
76
- }
77
-
78
- /**
79
- * Returns whether the inpage provider is connected to Torus.
80
- */
81
- isConnected() {
82
- return this.state.isConnected;
83
- }
84
-
85
- // Private Methods
86
- //= ===================
87
- /**
88
- * Constructor helper.
89
- * Populates initial state by calling 'wallet_getProviderState' and emits
90
- * necessary events.
91
- */
92
- async initializeState() {
93
- try {
94
- const {
95
- accounts,
96
- chainId,
97
- isUnlocked
98
- } = await this.request({
99
- method: PROVIDER_JRPC_METHODS.GET_PROVIDER_STATE,
100
- params: []
101
- });
102
-
103
- // indicate that we've connected, for EIP-1193 compliance
104
- this.emit("connect", {
105
- chainId
106
- });
107
- this.handleChainChanged({
108
- chainId
109
- });
110
- this.handleUnlockStateChanged({
111
- accounts,
112
- isUnlocked
113
- });
114
- this.handleAccountsChanged(accounts);
115
- } catch (error) {
116
- log.error("WsEmbed: Failed to get initial state. Please report this bug.", error);
117
- } finally {
118
- log.info("initialized provider state");
119
- this.state.initialized = true;
120
- }
121
- }
122
-
123
- /**
124
- * Internal RPC method. Forwards requests to background via the RPC engine.
125
- * Also remap ids inbound and outbound
126
- */
127
- rpcRequest(payload, callback, isInternal = false) {
128
- let cb = callback;
129
- const _payload = payload;
130
- if (!Array.isArray(_payload)) {
131
- if (!_payload.jsonrpc) {
132
- _payload.jsonrpc = "2.0";
133
- }
134
- if (_payload.method === "eth_accounts" || _payload.method === "eth_requestAccounts") {
135
- // handle accounts changing
136
- cb = (err, res) => {
137
- this.handleAccountsChanged(res.result || [], _payload.method === "eth_accounts", isInternal);
138
- callback(err, res);
139
- };
140
- } else if (_payload.method === "wallet_getProviderState") {
141
- this.rpcEngine.handle(payload, cb);
142
- return;
143
- }
144
- }
145
- this.tryWindowHandle(_payload, cb);
146
- }
147
-
148
- /**
149
- * When the provider becomes connected, updates internal state and emits
150
- * required events. Idempotent.
151
- *
152
- * @param chainId - The ID of the newly connected chain.
153
- * emits TorusInpageProvider#connect
154
- */
155
- handleConnect(chainId) {
156
- if (!this.state.isConnected) {
157
- this.state.isConnected = true;
158
- this.emit("connect", {
159
- chainId
160
- });
161
- log.debug(messages.info.connected(chainId));
162
- }
163
- }
164
-
165
- /**
166
- * When the provider becomes disconnected, updates internal state and emits
167
- * required events. Idempotent with respect to the isRecoverable parameter.
168
- *
169
- * Error codes per the CloseEvent status codes as required by EIP-1193:
170
- * https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes
171
- *
172
- * @param isRecoverable - Whether the disconnection is recoverable.
173
- * @param errorMessage - A custom error message.
174
- * emits TorusInpageProvider#disconnect
175
- */
176
- handleDisconnect(isRecoverable, errorMessage) {
177
- if (this.state.isConnected || !this.state.isPermanentlyDisconnected && !isRecoverable) {
178
- this.state.isConnected = false;
179
- let error;
180
- if (isRecoverable) {
181
- error = new EthereumProviderError(1013,
182
- // Try again later
183
- errorMessage || messages.errors.disconnected());
184
- log.debug(error);
185
- } else {
186
- error = new EthereumProviderError(1011,
187
- // Internal error
188
- errorMessage || messages.errors.permanentlyDisconnected());
189
- log.error(error);
190
- this.chainId = null;
191
- this.state.accounts = null;
192
- this.selectedAddress = null;
193
- this.state.isUnlocked = false;
194
- this.state.isPermanentlyDisconnected = true;
195
- }
196
- this.emit("disconnect", error);
197
- }
198
- }
199
-
200
- /**
201
- * Called when accounts may have changed.
202
- */
203
- handleAccountsChanged(accounts, isEthAccounts = false, isInternal = false) {
204
- // defensive programming
205
- let finalAccounts = accounts;
206
- if (!Array.isArray(finalAccounts)) {
207
- log.error("WsEmbed: Received non-array accounts parameter. Please report this bug.", finalAccounts);
208
- finalAccounts = [];
209
- }
210
- for (const account of accounts) {
211
- if (typeof account !== "string") {
212
- log.error("WsEmbed: Received non-string account. Please report this bug.", accounts);
213
- finalAccounts = [];
214
- break;
215
- }
216
- }
217
-
218
- // emit accountsChanged if anything about the accounts array has changed
219
- if (!dequal(this.state.accounts, finalAccounts)) {
220
- // we should always have the correct accounts even before eth_accounts
221
- // returns, except in cases where isInternal is true
222
- if (isEthAccounts && Array.isArray(this.state.accounts) && this.state.accounts.length > 0 && !isInternal) {
223
- log.error('WsEmbed: "eth_accounts" unexpectedly updated accounts. Please report this bug.', finalAccounts);
224
- }
225
- this.state.accounts = finalAccounts;
226
- this.emit("accountsChanged", finalAccounts);
227
- }
228
-
229
- // handle selectedAddress
230
- if (this.selectedAddress !== finalAccounts[0]) {
231
- this.selectedAddress = finalAccounts[0] || null;
232
- }
233
- }
234
-
235
- /**
236
- * Upon receipt of a new chainId and networkVersion, emits corresponding
237
- * events and sets relevant public state.
238
- * Does nothing if neither the chainId nor the networkVersion are different
239
- * from existing values.
240
- *
241
- * emits TorusInpageProvider#chainChanged
242
- * @param networkInfo - An object with network info.
243
- */
244
- handleChainChanged({
245
- chainId
246
- } = {}) {
247
- if (!chainId) {
248
- log.error("WsEmbed: Received invalid network parameters. Please report this bug.", {
249
- chainId
250
- });
251
- return;
252
- }
253
- if (chainId === "loading") {
254
- this.handleDisconnect(true);
255
- } else {
256
- this.handleConnect(chainId);
257
- if (chainId !== this.chainId) {
258
- this.chainId = chainId;
259
- if (this.state.initialized) {
260
- this.emit("chainChanged", this.chainId);
261
- }
262
- }
263
- }
264
- }
265
-
266
- /**
267
- * Upon receipt of a new isUnlocked state, sets relevant public state.
268
- * Calls the accounts changed handler with the received accounts, or an empty
269
- * array.
270
- *
271
- * Does nothing if the received value is equal to the existing value.
272
- * There are no lock/unlock events.
273
- *
274
- * @param opts - Options bag.
275
- */
276
- handleUnlockStateChanged({
277
- accounts,
278
- isUnlocked
279
- } = {}) {
280
- if (typeof isUnlocked !== "boolean") {
281
- log.error("WsEmbed: Received invalid isUnlocked parameter. Please report this bug.", {
282
- isUnlocked
283
- });
284
- return;
285
- }
286
- if (isUnlocked !== this.state.isUnlocked) {
287
- this.state.isUnlocked = isUnlocked;
288
- this.handleAccountsChanged(accounts || []);
289
- }
290
- }
291
- }
292
- _defineProperty(AirInPageProvider, "defaultState", {
293
- accounts: null,
294
- isConnected: false,
295
- isUnlocked: false,
296
- initialized: false,
297
- isPermanentlyDisconnected: false,
298
- hasEmittedConnection: false
299
- });
300
- class TorusInPageProvider extends AirInPageProvider {}
301
-
302
- export { TorusInPageProvider, AirInPageProvider as default };
@@ -1,5 +0,0 @@
1
- export { default as AirService } from './airService.js';
2
- export { default as RealmEmbed } from './embed.js';
3
- export { default as AirInPageProvider } from './inPageProvider.js';
4
- export { BUTTON_POSITION, EMBED_BUILD_ENV, EXTERNAL_LOGIN_PROVIDER } from './interfaces.js';
5
- export { CONFIRMATION_STRATEGY } from '@toruslabs/base-controllers';
@@ -1,21 +0,0 @@
1
- export { CONFIRMATION_STRATEGY } from '@toruslabs/base-controllers';
2
-
3
- const EMBED_BUILD_ENV = {
4
- PRODUCTION: "production",
5
- STAGING: "staging",
6
- DEVELOPMENT: "development",
7
- TESTING: "testing"
8
- };
9
- const BUTTON_POSITION = {
10
- BOTTOM_LEFT: "bottom-left",
11
- TOP_LEFT: "top-left",
12
- BOTTOM_RIGHT: "bottom-right",
13
- TOP_RIGHT: "top-right"
14
- };
15
- const EXTERNAL_LOGIN_PROVIDER = {
16
- METAMASK: "metamask",
17
- WALLET_CONNECT: "walletconnect",
18
- WAGMI: "wagmi"
19
- };
20
-
21
- export { BUTTON_POSITION, EMBED_BUILD_ENV, EXTERNAL_LOGIN_PROVIDER };
@@ -1,15 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- function isStream(stream) {
3
- return stream !== null && typeof stream === "object" && typeof stream.pipe === "function";
4
- }
5
- function isWritableStream(stream) {
6
- return isStream(stream) && stream.writable !== false && typeof stream._write === "function" && typeof stream._writableState === "object";
7
- }
8
- function isReadableStream(stream) {
9
- return isStream(stream) && stream.readable !== false && typeof stream._read === "function" && typeof stream._readableState === "object";
10
- }
11
- function isDuplexStream(stream) {
12
- return isWritableStream(stream) && isReadableStream(stream);
13
- }
14
-
15
- export { isDuplexStream, isReadableStream, isStream, isWritableStream };
@@ -1,5 +0,0 @@
1
- import loglevel from 'loglevel';
2
-
3
- var log = loglevel.getLogger("ws-embed");
4
-
5
- export { log as default };