@mocanetwork/airkit 0.5.0 → 0.6.0-beta.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.
- package/dist/airkit.cjs.js +2637 -2565
- package/dist/airkit.esm.js +2645 -2383
- package/dist/airkit.umd.js +2852 -0
- package/dist/types/airService.d.ts +12 -11
- package/dist/types/airWalletProvider.d.ts +13 -0
- package/dist/types/common/air/error.d.ts +8 -0
- package/dist/types/common/custom.d.ts +6 -0
- package/dist/types/common/provider/error.d.ts +16 -0
- package/dist/types/common/provider/types.d.ts +24 -0
- package/dist/types/common/realm/error.d.ts +0 -0
- package/dist/types/common/realm/messaging/auth.d.ts +14 -15
- package/dist/types/common/realm/messaging/provider.d.ts +32 -0
- package/dist/types/common/realm/messaging/types.d.ts +29 -10
- package/dist/types/common/realm/partner/config.d.ts +0 -32
- package/dist/types/common/realm/smart-session/types.d.ts +1 -1
- package/dist/types/common/realm/user/types.d.ts +0 -28
- package/dist/types/common/utils.d.ts +16 -0
- package/dist/types/error.d.ts +1 -1
- package/dist/types/index.d.ts +1 -2
- package/dist/types/interfaces.d.ts +18 -181
- package/dist/types/messageService.d.ts +18 -20
- package/dist/types/utils.d.ts +3 -21
- package/package.json +7 -15
- package/dist/airkit.umd.min.js +0 -2
- package/dist/airkit.umd.min.js.LICENSE.txt +0 -12
- package/dist/lib.cjs/common/src/error.js +0 -25
- package/dist/lib.cjs/common/src/realm/messaging/auth.js +0 -28
- package/dist/lib.cjs/common/src/realm/messaging/types.js +0 -21
- package/dist/lib.cjs/ws-embed/src/PopupHandler.js +0 -64
- package/dist/lib.cjs/ws-embed/src/airService.js +0 -398
- package/dist/lib.cjs/ws-embed/src/baseProvider.js +0 -178
- package/dist/lib.cjs/ws-embed/src/communicationProvider.js +0 -346
- package/dist/lib.cjs/ws-embed/src/embed.js +0 -750
- package/dist/lib.cjs/ws-embed/src/error.js +0 -21
- package/dist/lib.cjs/ws-embed/src/iframeController.js +0 -68
- package/dist/lib.cjs/ws-embed/src/inPageProvider.js +0 -307
- package/dist/lib.cjs/ws-embed/src/index.js +0 -20
- package/dist/lib.cjs/ws-embed/src/interfaces.js +0 -29
- package/dist/lib.cjs/ws-embed/src/isStream.js +0 -20
- package/dist/lib.cjs/ws-embed/src/loglevel.js +0 -7
- package/dist/lib.cjs/ws-embed/src/messageService.js +0 -269
- package/dist/lib.cjs/ws-embed/src/messages.js +0 -22
- package/dist/lib.cjs/ws-embed/src/siteMetadata.js +0 -77
- package/dist/lib.cjs/ws-embed/src/utils.js +0 -95
- package/dist/lib.esm/common/src/error.js +0 -23
- package/dist/lib.esm/common/src/realm/messaging/auth.js +0 -26
- package/dist/lib.esm/common/src/realm/messaging/types.js +0 -19
- package/dist/lib.esm/ws-embed/src/PopupHandler.js +0 -62
- package/dist/lib.esm/ws-embed/src/airService.js +0 -396
- package/dist/lib.esm/ws-embed/src/baseProvider.js +0 -176
- package/dist/lib.esm/ws-embed/src/communicationProvider.js +0 -344
- package/dist/lib.esm/ws-embed/src/embed.js +0 -745
- package/dist/lib.esm/ws-embed/src/error.js +0 -18
- package/dist/lib.esm/ws-embed/src/iframeController.js +0 -66
- package/dist/lib.esm/ws-embed/src/inPageProvider.js +0 -302
- package/dist/lib.esm/ws-embed/src/index.js +0 -5
- package/dist/lib.esm/ws-embed/src/interfaces.js +0 -21
- package/dist/lib.esm/ws-embed/src/isStream.js +0 -15
- package/dist/lib.esm/ws-embed/src/loglevel.js +0 -5
- package/dist/lib.esm/ws-embed/src/messageService.js +0 -267
- package/dist/lib.esm/ws-embed/src/messages.js +0 -20
- package/dist/lib.esm/ws-embed/src/siteMetadata.js +0 -75
- package/dist/lib.esm/ws-embed/src/utils.js +0 -88
- package/dist/types/PopupHandler.d.ts +0 -24
- package/dist/types/baseProvider.d.ts +0 -66
- package/dist/types/communicationProvider.d.ts +0 -82
- package/dist/types/embed.d.ts +0 -55
- package/dist/types/inPageProvider.d.ts +0 -106
- package/dist/types/isStream.d.ts +0 -4
- package/dist/types/messages.d.ts +0 -19
|
@@ -1,178 +0,0 @@
|
|
|
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 pump = require('pump');
|
|
8
|
-
var isStream = require('./isStream.js');
|
|
9
|
-
var messages = require('./messages.js');
|
|
10
|
-
var utils = require('./utils.js');
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @param connectionStream - A Node.js duplex stream
|
|
14
|
-
* @param opts - An options bag
|
|
15
|
-
*/
|
|
16
|
-
class BaseProvider extends auth.SafeEventEmitter {
|
|
17
|
-
constructor(connectionStream, {
|
|
18
|
-
maxEventListeners = 100,
|
|
19
|
-
jsonRpcStreamName = "provider"
|
|
20
|
-
}) {
|
|
21
|
-
super();
|
|
22
|
-
_defineProperty(this, "rpcEngine", void 0);
|
|
23
|
-
_defineProperty(this, "jsonRpcConnectionEvents", void 0);
|
|
24
|
-
/**
|
|
25
|
-
* Indicating that this provider is a Web3Auth provider.
|
|
26
|
-
*/
|
|
27
|
-
_defineProperty(this, "isWeb3Auth", void 0);
|
|
28
|
-
_defineProperty(this, "state", void 0);
|
|
29
|
-
if (!isStream.isDuplexStream(connectionStream)) {
|
|
30
|
-
throw new Error(messages.errors.invalidDuplexStream());
|
|
31
|
-
}
|
|
32
|
-
this.isWeb3Auth = true;
|
|
33
|
-
this.setMaxListeners(maxEventListeners);
|
|
34
|
-
this.handleConnect = this.handleConnect.bind(this);
|
|
35
|
-
this.handleDisconnect = this.handleDisconnect.bind(this);
|
|
36
|
-
this.handleStreamDisconnect = this.handleStreamDisconnect.bind(this);
|
|
37
|
-
this.rpcRequest = this.rpcRequest.bind(this);
|
|
38
|
-
this.initializeState = this.initializeState.bind(this);
|
|
39
|
-
this.request = this.request.bind(this);
|
|
40
|
-
this.sendAsync = this.sendAsync.bind(this);
|
|
41
|
-
this.send = this.send.bind(this);
|
|
42
|
-
// this.enable = this.enable.bind(this);
|
|
43
|
-
|
|
44
|
-
// setup connectionStream multiplexing
|
|
45
|
-
const mux = new auth.ObjectMultiplex();
|
|
46
|
-
pump(connectionStream, mux, connectionStream, this.handleStreamDisconnect.bind(this, "Web3Auth"));
|
|
47
|
-
|
|
48
|
-
// ignore phishing warning message (handled elsewhere)
|
|
49
|
-
mux.ignoreStream("phishing");
|
|
50
|
-
|
|
51
|
-
// setup own event listeners
|
|
52
|
-
// connect to async provider
|
|
53
|
-
|
|
54
|
-
const jsonRpcConnection = auth.createStreamMiddleware();
|
|
55
|
-
pump(jsonRpcConnection.stream, mux.createStream(jsonRpcStreamName), jsonRpcConnection.stream, this.handleStreamDisconnect.bind(this, "Web3Auth RpcProvider"));
|
|
56
|
-
|
|
57
|
-
// handle RPC requests via dapp-side rpc engine
|
|
58
|
-
const rpcEngine = new auth.JRPCEngine();
|
|
59
|
-
rpcEngine.push(auth.createIdRemapMiddleware());
|
|
60
|
-
rpcEngine.push(utils.createErrorMiddleware());
|
|
61
|
-
rpcEngine.push(baseControllers.createLoggerMiddleware({
|
|
62
|
-
origin: location.origin
|
|
63
|
-
}));
|
|
64
|
-
rpcEngine.push(jsonRpcConnection.middleware);
|
|
65
|
-
this.rpcEngine = rpcEngine;
|
|
66
|
-
this.jsonRpcConnectionEvents = jsonRpcConnection.events;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Submits an RPC request for the given method, with the given params.
|
|
71
|
-
* Resolves with the result of the method call, or rejects on error.
|
|
72
|
-
*
|
|
73
|
-
* @param args - The RPC request arguments.
|
|
74
|
-
* @returns A Promise that resolves with the result of the RPC method,
|
|
75
|
-
* or rejects if an error is encountered.
|
|
76
|
-
*/
|
|
77
|
-
async request(args) {
|
|
78
|
-
if (!args || typeof args !== "object" || Array.isArray(args)) {
|
|
79
|
-
throw auth.rpcErrors.invalidRequest({
|
|
80
|
-
message: messages.errors.invalidRequestArgs(),
|
|
81
|
-
data: _objectSpread(_objectSpread({}, args || {}), {}, {
|
|
82
|
-
cause: messages.errors.invalidRequestArgs()
|
|
83
|
-
})
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
const {
|
|
87
|
-
method,
|
|
88
|
-
params
|
|
89
|
-
} = args;
|
|
90
|
-
if (typeof method !== "string" || method.length === 0) {
|
|
91
|
-
throw auth.rpcErrors.invalidRequest({
|
|
92
|
-
message: messages.errors.invalidRequestMethod(),
|
|
93
|
-
data: _objectSpread(_objectSpread({}, args || {}), {}, {
|
|
94
|
-
cause: messages.errors.invalidRequestArgs()
|
|
95
|
-
})
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
if (params !== undefined && !Array.isArray(params) && (typeof params !== "object" || params === null)) {
|
|
99
|
-
throw auth.rpcErrors.invalidRequest({
|
|
100
|
-
message: messages.errors.invalidRequestParams(),
|
|
101
|
-
data: _objectSpread(_objectSpread({}, args || {}), {}, {
|
|
102
|
-
cause: messages.errors.invalidRequestArgs()
|
|
103
|
-
})
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
return new Promise((resolve, reject) => {
|
|
107
|
-
this.rpcRequest({
|
|
108
|
-
method,
|
|
109
|
-
params
|
|
110
|
-
}, auth.getRpcPromiseCallback(resolve, reject));
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Submits an RPC request per the given JSON-RPC request object.
|
|
115
|
-
*
|
|
116
|
-
* @param payload - The RPC request object.
|
|
117
|
-
* @param cb - The callback function.
|
|
118
|
-
*/
|
|
119
|
-
send(payload, callback) {
|
|
120
|
-
this.rpcRequest(payload, callback);
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Submits an RPC request per the given JSON-RPC request object.
|
|
124
|
-
*
|
|
125
|
-
* @param payload - The RPC request object.
|
|
126
|
-
* @param cb - The callback function.
|
|
127
|
-
*/
|
|
128
|
-
sendAsync(payload) {
|
|
129
|
-
return new Promise((resolve, reject) => {
|
|
130
|
-
this.rpcRequest(payload, auth.getRpcPromiseCallback(resolve, reject));
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Called when connection is lost to critical streams.
|
|
136
|
-
*
|
|
137
|
-
* emits InpageProvider#disconnect
|
|
138
|
-
*/
|
|
139
|
-
handleStreamDisconnect(streamName, error) {
|
|
140
|
-
utils.logStreamDisconnectWarning(streamName, error, this);
|
|
141
|
-
this.handleDisconnect(false, error ? error.message : undefined);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// Private Methods
|
|
145
|
-
//= ===================
|
|
146
|
-
/**
|
|
147
|
-
* Constructor helper.
|
|
148
|
-
* Populates initial state by calling 'wallet_getProviderState' and emits
|
|
149
|
-
* necessary events.
|
|
150
|
-
*/
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Internal RPC method. Forwards requests to background via the RPC engine.
|
|
154
|
-
* Also remap ids inbound and outbound
|
|
155
|
-
*/
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* When the provider becomes connected, updates internal state and emits
|
|
159
|
-
* required events. Idempotent.
|
|
160
|
-
*
|
|
161
|
-
* @param chainId - The ID of the newly connected chain.
|
|
162
|
-
* emits InPageProvider#connect
|
|
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 InpageProvider#disconnect
|
|
175
|
-
*/
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
module.exports = BaseProvider;
|
|
@@ -1,346 +0,0 @@
|
|
|
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;
|