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