@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,12 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* The buffer module from node.js, for the browser.
|
|
3
|
+
*
|
|
4
|
+
* @author Feross Aboukhadijeh <https://feross.org>
|
|
5
|
+
* @license MIT
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
|
|
9
|
+
|
|
10
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
11
|
+
|
|
12
|
+
/*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
class BaseError extends Error {
|
|
4
|
+
constructor(name, message, options = {}) {
|
|
5
|
+
const {
|
|
6
|
+
cause,
|
|
7
|
+
context
|
|
8
|
+
} = options;
|
|
9
|
+
super(message || name);
|
|
10
|
+
this.name = name;
|
|
11
|
+
this.stack = cause === null || cause === void 0 ? void 0 : cause.stack;
|
|
12
|
+
this.cause = cause;
|
|
13
|
+
this.context = context;
|
|
14
|
+
}
|
|
15
|
+
toJSON() {
|
|
16
|
+
return {
|
|
17
|
+
name: this.name,
|
|
18
|
+
message: this.message,
|
|
19
|
+
context: this.context,
|
|
20
|
+
cause: this.cause
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
exports.BaseError = BaseError;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const AirAuthMessageTypes = {
|
|
4
|
+
SETUP_COMPLETED: "air_auth_setup_completed",
|
|
5
|
+
LOGIN_REQUEST: "air_auth_login_request",
|
|
6
|
+
LOGIN_RESPONSE: "air_auth_login_response",
|
|
7
|
+
LOGIN_WALLET_RESPONSE: "air_auth_login_wallet_response",
|
|
8
|
+
USER_INFO_REQUEST: "air_auth_user_info_request",
|
|
9
|
+
USER_INFO_RESPONSE: "air_auth_user_info_response",
|
|
10
|
+
PARTNER_USER_INFO_REQUEST: "air_auth_partner_user_info_request",
|
|
11
|
+
PARTNER_USER_INFO_RESPONSE: "air_auth_partner_user_info_response",
|
|
12
|
+
REFRESH_TOKEN_REQUEST: "air_auth_refresh_token_request",
|
|
13
|
+
REFRESH_TOKEN_RESPONSE: "air_auth_refresh_token_response",
|
|
14
|
+
WALLET_TOKEN_REQUEST: "air_auth_wallet_token_request",
|
|
15
|
+
WALLET_TOKEN_RESPONSE: "air_auth_wallet_token_response",
|
|
16
|
+
INITIALIZATION_REQUEST: "air_auth_initialization_request",
|
|
17
|
+
INITIALIZATION_RESPONSE: "air_auth_initialization_response",
|
|
18
|
+
INIT_WALLET_COMMUNICATION: "air_auth_init_wallet_communication",
|
|
19
|
+
IFRAME_VISIBILITY_REQUEST: "air_auth_iframe_visibility_request",
|
|
20
|
+
SETUP_WALLET_REQUEST: "air_auth_setup_wallet_request",
|
|
21
|
+
SETUP_WALLET_RESPONSE: "air_auth_setup_wallet_response",
|
|
22
|
+
SIGN_SIWE_MESSAGE_REQUEST: "air_auth_sign_siwe_message_request",
|
|
23
|
+
SIGN_SIWE_MESSAGE_RESPONSE: "air_auth_sign_siwe_message_response",
|
|
24
|
+
LOGOUT_REQUEST: "air_auth_logout_request",
|
|
25
|
+
LOGOUT_RESPONSE: "air_auth_logout_response"
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
exports.AirAuthMessageTypes = AirAuthMessageTypes;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const AirMessageTypes = {
|
|
4
|
+
SERVICE_INITIALIZED: "air_service_initialized",
|
|
5
|
+
SERVICE_INITIALIZED_ERROR: "air_service_initialized_error",
|
|
6
|
+
LOGIN_REQUEST: "air_login_request",
|
|
7
|
+
CLAIM_REQUEST: "air_claim_request",
|
|
8
|
+
LOGIN_CLAIM_STATE: "air_login_claim_state",
|
|
9
|
+
CLOSE_MODAL: "air_close_modal",
|
|
10
|
+
INIT_AUTH_COMMUNICATION: "air_init_auth_communication",
|
|
11
|
+
DEPLOY_SMART_ACCOUNT_REQUEST: "air_deploy_smart_account_request",
|
|
12
|
+
DEPLOY_SMART_ACCOUNT_RESPONSE: "air_deploy_smart_account_response"
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
exports.AirMessageTypes = AirMessageTypes;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
|
|
4
|
+
var baseControllers = require('@toruslabs/base-controllers');
|
|
5
|
+
var auth = require('@web3auth/auth');
|
|
6
|
+
|
|
7
|
+
class PopupHandler extends auth.SafeEventEmitter {
|
|
8
|
+
constructor({
|
|
9
|
+
url,
|
|
10
|
+
target,
|
|
11
|
+
features,
|
|
12
|
+
timeout = 30000
|
|
13
|
+
}) {
|
|
14
|
+
super();
|
|
15
|
+
_defineProperty(this, "url", void 0);
|
|
16
|
+
_defineProperty(this, "target", void 0);
|
|
17
|
+
_defineProperty(this, "features", void 0);
|
|
18
|
+
_defineProperty(this, "window", void 0);
|
|
19
|
+
_defineProperty(this, "windowTimer", void 0);
|
|
20
|
+
_defineProperty(this, "iClosedWindow", void 0);
|
|
21
|
+
_defineProperty(this, "timeout", void 0);
|
|
22
|
+
this.url = url;
|
|
23
|
+
this.target = target || "_blank";
|
|
24
|
+
this.features = features || baseControllers.getPopupFeatures(baseControllers.FEATURES_DEFAULT_POPUP_WINDOW);
|
|
25
|
+
this.window = undefined;
|
|
26
|
+
this.windowTimer = undefined;
|
|
27
|
+
this.iClosedWindow = false;
|
|
28
|
+
this.timeout = timeout;
|
|
29
|
+
this._setupTimer();
|
|
30
|
+
}
|
|
31
|
+
_setupTimer() {
|
|
32
|
+
this.windowTimer = Number(setInterval(() => {
|
|
33
|
+
if (this.window && this.window.closed) {
|
|
34
|
+
clearInterval(this.windowTimer);
|
|
35
|
+
setTimeout(() => {
|
|
36
|
+
if (!this.iClosedWindow) {
|
|
37
|
+
this.emit("close");
|
|
38
|
+
}
|
|
39
|
+
this.iClosedWindow = false;
|
|
40
|
+
this.window = undefined;
|
|
41
|
+
}, this.timeout);
|
|
42
|
+
}
|
|
43
|
+
if (this.window === undefined) clearInterval(this.windowTimer);
|
|
44
|
+
}, 500));
|
|
45
|
+
}
|
|
46
|
+
open() {
|
|
47
|
+
var _this$window;
|
|
48
|
+
this.window = window.open(this.url.href, this.target, this.features);
|
|
49
|
+
if ((_this$window = this.window) !== null && _this$window !== void 0 && _this$window.focus) this.window.focus();
|
|
50
|
+
}
|
|
51
|
+
close() {
|
|
52
|
+
this.iClosedWindow = true;
|
|
53
|
+
if (this.window) this.window.close();
|
|
54
|
+
}
|
|
55
|
+
redirect(locationReplaceOnRedirect) {
|
|
56
|
+
if (locationReplaceOnRedirect) {
|
|
57
|
+
window.location.replace(this.url.href);
|
|
58
|
+
} else {
|
|
59
|
+
window.location.href = this.url.href;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
module.exports = PopupHandler;
|
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
|
|
4
|
+
var _classPrivateMethodInitSpec = require('@babel/runtime/helpers/classPrivateMethodInitSpec');
|
|
5
|
+
var _classPrivateFieldInitSpec = require('@babel/runtime/helpers/classPrivateFieldInitSpec');
|
|
6
|
+
var baseControllers = require('@toruslabs/base-controllers');
|
|
7
|
+
var auth = require('../../common/src/realm/messaging/auth.js');
|
|
8
|
+
var embed = require('./embed.js');
|
|
9
|
+
var error = require('./error.js');
|
|
10
|
+
var iframeController = require('./iframeController.js');
|
|
11
|
+
var interfaces = require('./interfaces.js');
|
|
12
|
+
var loglevel = require('./loglevel.js');
|
|
13
|
+
var messageService = require('./messageService.js');
|
|
14
|
+
var utils = require('./utils.js');
|
|
15
|
+
|
|
16
|
+
function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); }
|
|
17
|
+
function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; }
|
|
18
|
+
function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); }
|
|
19
|
+
var _authIframeController = /*#__PURE__*/new WeakMap();
|
|
20
|
+
var _loginResult = /*#__PURE__*/new WeakMap();
|
|
21
|
+
var _buildEnv = /*#__PURE__*/new WeakMap();
|
|
22
|
+
var _embedNonce = /*#__PURE__*/new WeakMap();
|
|
23
|
+
var _enableLogging = /*#__PURE__*/new WeakMap();
|
|
24
|
+
var _isInitialized = /*#__PURE__*/new WeakMap();
|
|
25
|
+
var _partnerId = /*#__PURE__*/new WeakMap();
|
|
26
|
+
var _realmEmbed = /*#__PURE__*/new WeakMap();
|
|
27
|
+
var _walletInitialization = /*#__PURE__*/new WeakMap();
|
|
28
|
+
var _airAuthListener = /*#__PURE__*/new WeakMap();
|
|
29
|
+
var _AirService_brand = /*#__PURE__*/new WeakSet();
|
|
30
|
+
class AirService {
|
|
31
|
+
constructor({
|
|
32
|
+
partnerId
|
|
33
|
+
}) {
|
|
34
|
+
_classPrivateMethodInitSpec(this, _AirService_brand);
|
|
35
|
+
_classPrivateFieldInitSpec(this, _authIframeController, void 0);
|
|
36
|
+
_classPrivateFieldInitSpec(this, _loginResult, void 0);
|
|
37
|
+
_classPrivateFieldInitSpec(this, _buildEnv, void 0);
|
|
38
|
+
_classPrivateFieldInitSpec(this, _embedNonce, baseControllers.randomId());
|
|
39
|
+
_classPrivateFieldInitSpec(this, _enableLogging, false);
|
|
40
|
+
_classPrivateFieldInitSpec(this, _isInitialized, false);
|
|
41
|
+
_classPrivateFieldInitSpec(this, _partnerId, void 0);
|
|
42
|
+
// Realm Embed related
|
|
43
|
+
_classPrivateFieldInitSpec(this, _realmEmbed, void 0);
|
|
44
|
+
_classPrivateFieldInitSpec(this, _walletInitialization, void 0);
|
|
45
|
+
_classPrivateFieldInitSpec(this, _airAuthListener, []);
|
|
46
|
+
_classPrivateFieldSet(_partnerId, this, partnerId);
|
|
47
|
+
_classPrivateFieldSet(_realmEmbed, this, new embed.default({
|
|
48
|
+
partnerId: _classPrivateFieldGet(_partnerId, this)
|
|
49
|
+
}));
|
|
50
|
+
}
|
|
51
|
+
get buildEnv() {
|
|
52
|
+
return _classPrivateFieldGet(_buildEnv, this);
|
|
53
|
+
}
|
|
54
|
+
get isAirAuth() {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
get isInitialized() {
|
|
58
|
+
return _classPrivateFieldGet(_isInitialized, this);
|
|
59
|
+
}
|
|
60
|
+
get isLoggedIn() {
|
|
61
|
+
return !!_classPrivateFieldGet(_loginResult, this);
|
|
62
|
+
}
|
|
63
|
+
get isWalletLoaded() {
|
|
64
|
+
var _classPrivateFieldGet2;
|
|
65
|
+
return (_classPrivateFieldGet2 = _classPrivateFieldGet(_realmEmbed, this)) === null || _classPrivateFieldGet2 === void 0 ? void 0 : _classPrivateFieldGet2.isInitialized;
|
|
66
|
+
}
|
|
67
|
+
async init({
|
|
68
|
+
buildEnv = interfaces.EMBED_BUILD_ENV.PRODUCTION,
|
|
69
|
+
enableLogging = false,
|
|
70
|
+
skipRehydration = false
|
|
71
|
+
}) {
|
|
72
|
+
if (!_classPrivateFieldGet(_partnerId, this)) throw new Error("Partner ID is required to initialize auth service");
|
|
73
|
+
if (_classPrivateFieldGet(_isInitialized, this)) return;
|
|
74
|
+
_classPrivateFieldSet(_buildEnv, this, buildEnv);
|
|
75
|
+
_classPrivateFieldSet(_enableLogging, this, enableLogging);
|
|
76
|
+
const {
|
|
77
|
+
authUrl,
|
|
78
|
+
logLevel
|
|
79
|
+
} = utils.AIR_URLS[buildEnv];
|
|
80
|
+
loglevel.setDefaultLevel(logLevel);
|
|
81
|
+
if (_classPrivateFieldGet(_enableLogging, this)) loglevel.enableAll();else loglevel.disableAll();
|
|
82
|
+
const authIframeOrigin = new URL(authUrl).origin;
|
|
83
|
+
await messageService.openAuthObservables({
|
|
84
|
+
authIframeOrigin
|
|
85
|
+
});
|
|
86
|
+
_classPrivateFieldSet(_authIframeController, this, new iframeController.IframeController(authUrl, `air-login-${_classPrivateFieldGet(_embedNonce, this)}`));
|
|
87
|
+
messageService.authMessage$.subscribe(async msg => {
|
|
88
|
+
switch (msg.type) {
|
|
89
|
+
case auth.AirAuthMessageTypes.IFRAME_VISIBILITY_REQUEST:
|
|
90
|
+
{
|
|
91
|
+
const authIframeController = _classPrivateFieldGet(_authIframeController, this);
|
|
92
|
+
authIframeController.setIframeVisibility(msg.payload.visible);
|
|
93
|
+
authIframeController.updateIframeState();
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
case auth.AirAuthMessageTypes.SETUP_WALLET_REQUEST:
|
|
97
|
+
{
|
|
98
|
+
await _assertClassBrand(_AirService_brand, this, _initializeWallet).call(this);
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
try {
|
|
104
|
+
_classPrivateFieldGet(_authIframeController, this).createIframe();
|
|
105
|
+
const initializationPromise = new Promise((resolve, reject) => {
|
|
106
|
+
const handleAuthMessage = ev => {
|
|
107
|
+
try {
|
|
108
|
+
if (ev.origin !== authIframeOrigin) return;
|
|
109
|
+
if (ev.data === auth.AirAuthMessageTypes.SETUP_COMPLETED) {
|
|
110
|
+
window.removeEventListener("message", handleAuthMessage);
|
|
111
|
+
_classPrivateFieldGet(_authIframeController, this).postMessage({
|
|
112
|
+
type: auth.AirAuthMessageTypes.INITIALIZATION_REQUEST,
|
|
113
|
+
payload: {
|
|
114
|
+
partnerId: _classPrivateFieldGet(_partnerId, this),
|
|
115
|
+
skipRehydration
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
resolve();
|
|
119
|
+
}
|
|
120
|
+
} catch (err) {
|
|
121
|
+
reject(err);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
window.addEventListener("message", handleAuthMessage);
|
|
125
|
+
});
|
|
126
|
+
const authServiceInitialization = messageService.waitForAuthInitialization();
|
|
127
|
+
const result = await Promise.all([initializationPromise, authServiceInitialization]);
|
|
128
|
+
_classPrivateFieldSet(_isInitialized, this, true);
|
|
129
|
+
_assertClassBrand(_AirService_brand, this, _triggerAirAuthInitialized).call(this);
|
|
130
|
+
_assertClassBrand(_AirService_brand, this, _prepareRealmEmbedListeners).call(this);
|
|
131
|
+
|
|
132
|
+
// rehydrated auth session
|
|
133
|
+
if (result[1].rehydrated) {
|
|
134
|
+
_classPrivateFieldSet(_loginResult, this, _assertClassBrand(_AirService_brand, this, _createLoginResult).call(this, result[1]));
|
|
135
|
+
_assertClassBrand(_AirService_brand, this, _triggerAirAuthLoggedIn).call(this);
|
|
136
|
+
return _classPrivateFieldGet(_loginResult, this);
|
|
137
|
+
}
|
|
138
|
+
} catch (error) {
|
|
139
|
+
loglevel.debug("Error initializing auth service", error);
|
|
140
|
+
await this.cleanUp();
|
|
141
|
+
throw error;
|
|
142
|
+
}
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
async cleanUp() {
|
|
146
|
+
var _classPrivateFieldGet3;
|
|
147
|
+
// Clear up wallet service iframe if exists
|
|
148
|
+
await _assertClassBrand(_AirService_brand, this, _cleanUpWallet).call(this);
|
|
149
|
+
|
|
150
|
+
// Logout auth session
|
|
151
|
+
await messageService.logoutAuth(_classPrivateFieldGet(_authIframeController, this).iframeElement);
|
|
152
|
+
|
|
153
|
+
// Destroy the auth iframe
|
|
154
|
+
const authIframeElement = (_classPrivateFieldGet3 = _classPrivateFieldGet(_authIframeController, this)) === null || _classPrivateFieldGet3 === void 0 ? void 0 : _classPrivateFieldGet3.iframeElement;
|
|
155
|
+
if (utils.isElement(authIframeElement) && window.document.body.contains(authIframeElement)) {
|
|
156
|
+
_classPrivateFieldGet(_authIframeController, this).destroy();
|
|
157
|
+
_classPrivateFieldSet(_authIframeController, this, undefined);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Close the message service
|
|
161
|
+
messageService.closeAuthObservables();
|
|
162
|
+
this.clearEventListeners();
|
|
163
|
+
_classPrivateFieldSet(_isInitialized, this, false);
|
|
164
|
+
}
|
|
165
|
+
async login(options) {
|
|
166
|
+
if (!_classPrivateFieldGet(_isInitialized, this)) throw new Error("Service is not initialized");
|
|
167
|
+
if (_classPrivateFieldGet(_loginResult, this)) return _classPrivateFieldGet(_loginResult, this);
|
|
168
|
+
const iframeController = _classPrivateFieldGet(_authIframeController, this);
|
|
169
|
+
const {
|
|
170
|
+
payload
|
|
171
|
+
} = await messageService.sendAuthLoginRequest(iframeController.iframeElement, {
|
|
172
|
+
partnerLoginToken: options === null || options === void 0 ? void 0 : options.authToken
|
|
173
|
+
});
|
|
174
|
+
if (payload.success === true) {
|
|
175
|
+
_classPrivateFieldSet(_loginResult, this, _assertClassBrand(_AirService_brand, this, _createLoginResult).call(this, payload));
|
|
176
|
+
_assertClassBrand(_AirService_brand, this, _triggerAirAuthLoggedIn).call(this);
|
|
177
|
+
return _classPrivateFieldGet(_loginResult, this);
|
|
178
|
+
}
|
|
179
|
+
throw new error.AirServiceError("UNKNOWN_ERROR", "Unknown error occurred");
|
|
180
|
+
}
|
|
181
|
+
async deploySmartAccount() {
|
|
182
|
+
if (!_classPrivateFieldGet(_isInitialized, this)) throw new Error("Service is not initialized");
|
|
183
|
+
await _assertClassBrand(_AirService_brand, this, _ensureWallet).call(this);
|
|
184
|
+
return _classPrivateFieldGet(_realmEmbed, this)._deploySmartAccount();
|
|
185
|
+
}
|
|
186
|
+
async logout() {
|
|
187
|
+
if (!_classPrivateFieldGet(_isInitialized, this)) throw new Error("Service is not initialized");
|
|
188
|
+
if (!this.isLoggedIn) throw new Error("No active session to logout");
|
|
189
|
+
|
|
190
|
+
// Clear up wallet
|
|
191
|
+
await _assertClassBrand(_AirService_brand, this, _cleanUpWallet).call(this);
|
|
192
|
+
await messageService.logoutAuth(_classPrivateFieldGet(_authIframeController, this).iframeElement);
|
|
193
|
+
_classPrivateFieldSet(_loginResult, this, undefined);
|
|
194
|
+
_assertClassBrand(_AirService_brand, this, _triggerAirAuthLoggedOut).call(this);
|
|
195
|
+
}
|
|
196
|
+
async getProvider() {
|
|
197
|
+
if (!this.isLoggedIn) return null;
|
|
198
|
+
await _assertClassBrand(_AirService_brand, this, _ensureWallet).call(this);
|
|
199
|
+
return _classPrivateFieldGet(_realmEmbed, this).provider;
|
|
200
|
+
}
|
|
201
|
+
async preloadWallet() {
|
|
202
|
+
return _assertClassBrand(_AirService_brand, this, _ensureWallet).call(this);
|
|
203
|
+
}
|
|
204
|
+
async claimAirId(options) {
|
|
205
|
+
await _assertClassBrand(_AirService_brand, this, _ensureWallet).call(this);
|
|
206
|
+
return _classPrivateFieldGet(_realmEmbed, this).claimRealmId(options);
|
|
207
|
+
}
|
|
208
|
+
async getUserInfo() {
|
|
209
|
+
if (!this.isLoggedIn) throw new Error("User not logged in");
|
|
210
|
+
const info = await messageService.sendUserInfoRequest(_classPrivateFieldGet(_authIframeController, this).iframeElement);
|
|
211
|
+
if (info.payload.success === false) {
|
|
212
|
+
throw new error.AirServiceError(info.payload.errorName, info.payload.errorMessage);
|
|
213
|
+
}
|
|
214
|
+
return {
|
|
215
|
+
id: info.payload.id,
|
|
216
|
+
abstractAccountAddress: info.payload.abstractAccountAddress,
|
|
217
|
+
email: info.payload.email,
|
|
218
|
+
airIds: info.payload.airIds
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
async getPartnerUserInfo() {
|
|
222
|
+
if (!this.isLoggedIn) throw new Error("User not logged in");
|
|
223
|
+
const info = await messageService.sendPartnerUserInfoRequest(_classPrivateFieldGet(_authIframeController, this).iframeElement);
|
|
224
|
+
if (info.payload.success === false) {
|
|
225
|
+
throw new error.AirServiceError(info.payload.errorName, info.payload.errorMessage);
|
|
226
|
+
}
|
|
227
|
+
return {
|
|
228
|
+
partnerId: info.payload.partnerId,
|
|
229
|
+
partnerUserId: info.payload.partnerUserId,
|
|
230
|
+
airId: info.payload.airId
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
on(listener) {
|
|
234
|
+
if (_classPrivateFieldGet(_airAuthListener, this).indexOf(listener) < 0) _classPrivateFieldGet(_airAuthListener, this).push(listener);
|
|
235
|
+
}
|
|
236
|
+
off(listener) {
|
|
237
|
+
if (_classPrivateFieldGet(_airAuthListener, this).indexOf(listener) >= 0) _classPrivateFieldGet(_airAuthListener, this).splice(_classPrivateFieldGet(_airAuthListener, this).indexOf(listener), 1);
|
|
238
|
+
}
|
|
239
|
+
clearEventListeners() {
|
|
240
|
+
_classPrivateFieldSet(_airAuthListener, this, []);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// This is for internal use only
|
|
244
|
+
// @ts-expect-error for unused function
|
|
245
|
+
async _showWalletWidget() {
|
|
246
|
+
await _assertClassBrand(_AirService_brand, this, _ensureWallet).call(this);
|
|
247
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
248
|
+
return _classPrivateFieldGet(_realmEmbed, this)._showWalletWidget();
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
async function _createWallet() {
|
|
252
|
+
return _classPrivateFieldGet(_realmEmbed, this).login({
|
|
253
|
+
skipClaiming: true,
|
|
254
|
+
legacy: false
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
async function _ensureWallet() {
|
|
258
|
+
if (!this.isLoggedIn) throw new Error("No active session to create wallet");
|
|
259
|
+
if (!_classPrivateFieldGet(_walletInitialization, this)) {
|
|
260
|
+
messageService.setupAuthIframe(_classPrivateFieldGet(_authIframeController, this).iframeElement);
|
|
261
|
+
_classPrivateFieldSet(_walletInitialization, this, (async () => {
|
|
262
|
+
try {
|
|
263
|
+
await _assertClassBrand(_AirService_brand, this, _initializeWallet).call(this);
|
|
264
|
+
return await _assertClassBrand(_AirService_brand, this, _createWallet).call(this);
|
|
265
|
+
} catch (error) {
|
|
266
|
+
loglevel.error("Error initializing wallet", error);
|
|
267
|
+
throw error;
|
|
268
|
+
}
|
|
269
|
+
})());
|
|
270
|
+
}
|
|
271
|
+
return _classPrivateFieldGet(_walletInitialization, this);
|
|
272
|
+
}
|
|
273
|
+
async function _cleanUpWallet() {
|
|
274
|
+
if (_classPrivateFieldGet(_walletInitialization, this) || _classPrivateFieldGet(_realmEmbed, this)) {
|
|
275
|
+
await _classPrivateFieldGet(_walletInitialization, this);
|
|
276
|
+
await _classPrivateFieldGet(_realmEmbed, this).cleanUp();
|
|
277
|
+
_classPrivateFieldSet(_walletInitialization, this, undefined);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
async function _initializeWallet() {
|
|
281
|
+
if (!_classPrivateFieldGet(_realmEmbed, this).isInitialized) {
|
|
282
|
+
try {
|
|
283
|
+
await _classPrivateFieldGet(_realmEmbed, this).init({
|
|
284
|
+
buildEnv: _classPrivateFieldGet(_buildEnv, this),
|
|
285
|
+
skipRehydration: true,
|
|
286
|
+
enableLogging: _classPrivateFieldGet(_enableLogging, this) || false
|
|
287
|
+
});
|
|
288
|
+
_classPrivateFieldGet(_authIframeController, this).postMessage({
|
|
289
|
+
type: auth.AirAuthMessageTypes.SETUP_WALLET_RESPONSE,
|
|
290
|
+
payload: {
|
|
291
|
+
success: true
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
} catch (error) {
|
|
295
|
+
loglevel.error("Error initializing wallet", error);
|
|
296
|
+
_classPrivateFieldGet(_authIframeController, this).postMessage({
|
|
297
|
+
type: auth.AirAuthMessageTypes.SETUP_WALLET_RESPONSE,
|
|
298
|
+
payload: {
|
|
299
|
+
success: false,
|
|
300
|
+
errorName: "UNKNOWN_ERROR",
|
|
301
|
+
errorMessage: "Unknown error occurred"
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
throw error;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
async function _triggerEventListeners(data) {
|
|
309
|
+
_classPrivateFieldGet(_airAuthListener, this).forEach(listener => {
|
|
310
|
+
listener(data);
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
async function _triggerAirAuthInitialized() {
|
|
314
|
+
await _assertClassBrand(_AirService_brand, this, _triggerEventListeners).call(this, {
|
|
315
|
+
event: "initialized"
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
async function _triggerAirAuthLoggedIn() {
|
|
319
|
+
await _assertClassBrand(_AirService_brand, this, _triggerEventListeners).call(this, {
|
|
320
|
+
event: "logged_in",
|
|
321
|
+
result: _classPrivateFieldGet(_loginResult, this)
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
async function _triggerAirAuthLoggedOut() {
|
|
325
|
+
await _assertClassBrand(_AirService_brand, this, _triggerEventListeners).call(this, {
|
|
326
|
+
event: "logged_out"
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
function _createLoginResult(payload) {
|
|
330
|
+
return {
|
|
331
|
+
isLoggedIn: true,
|
|
332
|
+
id: payload.id,
|
|
333
|
+
abstractAccountAddress: payload.abstractAccountAddress,
|
|
334
|
+
token: payload.partnerAccessToken
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
async function _prepareRealmEmbedListeners() {
|
|
338
|
+
_classPrivateFieldGet(_realmEmbed, this).on(data => {
|
|
339
|
+
if (data.event === "connected" || data.event === "logged_in") {
|
|
340
|
+
var _data$result$addresse;
|
|
341
|
+
const result = _objectSpread(_objectSpread({}, data.result), {}, {
|
|
342
|
+
addresses: {
|
|
343
|
+
eoa: data.result.addresses.eoa,
|
|
344
|
+
aa: (_data$result$addresse = data.result.addresses.aa) !== null && _data$result$addresse !== void 0 ? _data$result$addresse : ""
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
_assertClassBrand(_AirService_brand, this, _triggerEventListeners).call(this, {
|
|
348
|
+
event: "wallet_loaded",
|
|
349
|
+
result
|
|
350
|
+
});
|
|
351
|
+
} else if (data.event === "minting_started") {
|
|
352
|
+
_assertClassBrand(_AirService_brand, this, _triggerEventListeners).call(this, {
|
|
353
|
+
event: "minting_started"
|
|
354
|
+
});
|
|
355
|
+
} else if (data.event === "minting_failed") {
|
|
356
|
+
_assertClassBrand(_AirService_brand, this, _triggerEventListeners).call(this, {
|
|
357
|
+
event: "minting_failed",
|
|
358
|
+
errorMessage: data.errorMessage
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
module.exports = AirService;
|
|
@@ -0,0 +1,178 @@
|
|
|
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;
|