@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,22 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var messages = {
|
|
4
|
+
errors: {
|
|
5
|
+
disconnected: () => "Web3Auth: Lost connection to Web3Auth.",
|
|
6
|
+
permanentlyDisconnected: () => "Web3Auth: Disconnected from iframe. Page reload required.",
|
|
7
|
+
unsupportedSync: method => `Web3Auth: The Web3Auth Ethereum provider does not support synchronous methods like ${method} without a callback parameter.`,
|
|
8
|
+
invalidDuplexStream: () => "Must provide a Node.js-style duplex stream.",
|
|
9
|
+
invalidOptions: maxEventListeners => `Invalid options. Received: { maxEventListeners: ${maxEventListeners}}`,
|
|
10
|
+
invalidRequestArgs: () => `Expected a single, non-array, object argument.`,
|
|
11
|
+
invalidRequestMethod: () => `'args.method' must be a non-empty string.`,
|
|
12
|
+
invalidRequestParams: () => `'args.params' must be an object or array if provided.`,
|
|
13
|
+
invalidLoggerObject: () => `'args.logger' must be an object if provided.`,
|
|
14
|
+
invalidLoggerMethod: method => `'args.logger' must include required method '${method}'.`
|
|
15
|
+
},
|
|
16
|
+
info: {
|
|
17
|
+
connected: chainId => `Web3Auth: Connected to chain with ID "${chainId}".`
|
|
18
|
+
},
|
|
19
|
+
warnings: {}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
module.exports = messages;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns whether the given image URL exists
|
|
5
|
+
* @param url - the url of the image
|
|
6
|
+
* @returns whether the image exists
|
|
7
|
+
*/
|
|
8
|
+
function imgExists(url) {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
try {
|
|
11
|
+
const img = document.createElement("img");
|
|
12
|
+
img.onload = () => resolve(true);
|
|
13
|
+
img.onerror = () => resolve(false);
|
|
14
|
+
img.src = url;
|
|
15
|
+
} catch (e) {
|
|
16
|
+
reject(e);
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Extracts a name for the site from the DOM
|
|
23
|
+
*/
|
|
24
|
+
const getSiteName = window => {
|
|
25
|
+
const {
|
|
26
|
+
document
|
|
27
|
+
} = window;
|
|
28
|
+
const siteName = document.querySelector('head > meta[property="og:site_name"]');
|
|
29
|
+
if (siteName) {
|
|
30
|
+
return siteName.content;
|
|
31
|
+
}
|
|
32
|
+
const metaTitle = document.querySelector('head > meta[name="title"]');
|
|
33
|
+
if (metaTitle) {
|
|
34
|
+
return metaTitle.content;
|
|
35
|
+
}
|
|
36
|
+
if (document.title && document.title.length > 0) {
|
|
37
|
+
return document.title;
|
|
38
|
+
}
|
|
39
|
+
return window.location.hostname;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Extracts an icon for the site from the DOM
|
|
44
|
+
*/
|
|
45
|
+
async function getSiteIcon(window) {
|
|
46
|
+
try {
|
|
47
|
+
const {
|
|
48
|
+
document
|
|
49
|
+
} = window;
|
|
50
|
+
|
|
51
|
+
// Use the site's favicon if it exists
|
|
52
|
+
let icon = document.querySelector('head > link[rel="shortcut icon"]');
|
|
53
|
+
if (icon && (await imgExists(icon.href))) {
|
|
54
|
+
return icon.href;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Search through available icons in no particular order
|
|
58
|
+
icon = Array.from(document.querySelectorAll('head > link[rel="icon"]')).find(_icon => Boolean(_icon.href));
|
|
59
|
+
if (icon && (await imgExists(icon.href))) {
|
|
60
|
+
return icon.href;
|
|
61
|
+
}
|
|
62
|
+
return "";
|
|
63
|
+
} catch (error) {
|
|
64
|
+
return "";
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Gets site metadata and returns it
|
|
70
|
+
*
|
|
71
|
+
*/
|
|
72
|
+
const getSiteMetadata = async () => ({
|
|
73
|
+
name: getSiteName(window),
|
|
74
|
+
icon: await getSiteIcon(window)
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
module.exports = getSiteMetadata;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
|
|
4
|
+
var auth = require('@web3auth/auth');
|
|
5
|
+
var interfaces = require('./interfaces.js');
|
|
6
|
+
var loglevel = require('./loglevel.js');
|
|
7
|
+
|
|
8
|
+
const AIR_URLS = {
|
|
9
|
+
[interfaces.EMBED_BUILD_ENV.TESTING]: {
|
|
10
|
+
authUrl: "http://localhost:8100",
|
|
11
|
+
walletUrl: "https://lrc-mocaverse.web3auth.io",
|
|
12
|
+
logLevel: "debug"
|
|
13
|
+
},
|
|
14
|
+
[interfaces.EMBED_BUILD_ENV.DEVELOPMENT]: {
|
|
15
|
+
authUrl: "http://localhost:8100",
|
|
16
|
+
walletUrl: "https://localhost:4050",
|
|
17
|
+
logLevel: "debug"
|
|
18
|
+
},
|
|
19
|
+
[interfaces.EMBED_BUILD_ENV.STAGING]: {
|
|
20
|
+
authUrl: "https://login.staging.realmnetwork.io",
|
|
21
|
+
walletUrl: "https://account.staging.realmnetwork.io",
|
|
22
|
+
logLevel: "info"
|
|
23
|
+
},
|
|
24
|
+
[interfaces.EMBED_BUILD_ENV.PRODUCTION]: {
|
|
25
|
+
authUrl: "https://login.realmnetwork.io",
|
|
26
|
+
walletUrl: "https://account.realmnetwork.io",
|
|
27
|
+
logLevel: "error"
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const htmlToElement = html => {
|
|
31
|
+
const template = window.document.createElement("template");
|
|
32
|
+
const trimmedHtml = html.trim(); // Never return a text node of whitespace as the result
|
|
33
|
+
template.innerHTML = trimmedHtml;
|
|
34
|
+
return template.content.firstChild;
|
|
35
|
+
};
|
|
36
|
+
const isElement = element => element instanceof Element || element instanceof Document;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* json-rpc-engine middleware that logs RPC errors and and validates req.method.
|
|
40
|
+
*
|
|
41
|
+
* @param log - The logging API to use.
|
|
42
|
+
* @returns json-rpc-engine middleware function
|
|
43
|
+
*/
|
|
44
|
+
function createErrorMiddleware() {
|
|
45
|
+
return (req, res, next) => {
|
|
46
|
+
// json-rpc-engine will terminate the request when it notices this error
|
|
47
|
+
if (typeof req.method !== "string" || !req.method) {
|
|
48
|
+
res.error = auth.rpcErrors.invalidRequest({
|
|
49
|
+
message: `The request 'method' must be a non-empty string.`,
|
|
50
|
+
data: _objectSpread(_objectSpread({}, req || {}), {}, {
|
|
51
|
+
cause: `The request 'method' must be a non-empty string.`
|
|
52
|
+
})
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
next(done => {
|
|
56
|
+
const {
|
|
57
|
+
error
|
|
58
|
+
} = res;
|
|
59
|
+
if (!error) {
|
|
60
|
+
return done();
|
|
61
|
+
}
|
|
62
|
+
loglevel.error(`Ws-Embed - RPC Error: ${error.message}`, error);
|
|
63
|
+
return done();
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Logs a stream disconnection error. Emits an 'error' if given an
|
|
70
|
+
* EventEmitter that has listeners for the 'error' event.
|
|
71
|
+
*
|
|
72
|
+
* @param log - The logging API to use.
|
|
73
|
+
* @param remoteLabel - The label of the disconnected stream.
|
|
74
|
+
* @param error - The associated error to log.
|
|
75
|
+
* @param emitter - The logging API to use.
|
|
76
|
+
*/
|
|
77
|
+
function logStreamDisconnectWarning(remoteLabel, error, emitter) {
|
|
78
|
+
let warningMsg = `Web3Auth: Lost connection to "${remoteLabel}".`;
|
|
79
|
+
if (error !== null && error !== void 0 && error.stack) {
|
|
80
|
+
warningMsg += `\n${error.stack}`;
|
|
81
|
+
}
|
|
82
|
+
loglevel.warn(warningMsg);
|
|
83
|
+
if (emitter && emitter.listenerCount("error") > 0) {
|
|
84
|
+
emitter.emit("error", warningMsg);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const EMITTED_NOTIFICATIONS = ["eth_subscription" // per eth-json-rpc-filters/subscriptionManager
|
|
88
|
+
];
|
|
89
|
+
|
|
90
|
+
exports.AIR_URLS = AIR_URLS;
|
|
91
|
+
exports.EMITTED_NOTIFICATIONS = EMITTED_NOTIFICATIONS;
|
|
92
|
+
exports.createErrorMiddleware = createErrorMiddleware;
|
|
93
|
+
exports.htmlToElement = htmlToElement;
|
|
94
|
+
exports.isElement = isElement;
|
|
95
|
+
exports.logStreamDisconnectWarning = logStreamDisconnectWarning;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
class BaseError extends Error {
|
|
2
|
+
constructor(name, message, options = {}) {
|
|
3
|
+
const {
|
|
4
|
+
cause,
|
|
5
|
+
context
|
|
6
|
+
} = options;
|
|
7
|
+
super(message || name);
|
|
8
|
+
this.name = name;
|
|
9
|
+
this.stack = cause === null || cause === void 0 ? void 0 : cause.stack;
|
|
10
|
+
this.cause = cause;
|
|
11
|
+
this.context = context;
|
|
12
|
+
}
|
|
13
|
+
toJSON() {
|
|
14
|
+
return {
|
|
15
|
+
name: this.name,
|
|
16
|
+
message: this.message,
|
|
17
|
+
context: this.context,
|
|
18
|
+
cause: this.cause
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { BaseError };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const AirAuthMessageTypes = {
|
|
2
|
+
SETUP_COMPLETED: "air_auth_setup_completed",
|
|
3
|
+
LOGIN_REQUEST: "air_auth_login_request",
|
|
4
|
+
LOGIN_RESPONSE: "air_auth_login_response",
|
|
5
|
+
LOGIN_WALLET_RESPONSE: "air_auth_login_wallet_response",
|
|
6
|
+
USER_INFO_REQUEST: "air_auth_user_info_request",
|
|
7
|
+
USER_INFO_RESPONSE: "air_auth_user_info_response",
|
|
8
|
+
PARTNER_USER_INFO_REQUEST: "air_auth_partner_user_info_request",
|
|
9
|
+
PARTNER_USER_INFO_RESPONSE: "air_auth_partner_user_info_response",
|
|
10
|
+
REFRESH_TOKEN_REQUEST: "air_auth_refresh_token_request",
|
|
11
|
+
REFRESH_TOKEN_RESPONSE: "air_auth_refresh_token_response",
|
|
12
|
+
WALLET_TOKEN_REQUEST: "air_auth_wallet_token_request",
|
|
13
|
+
WALLET_TOKEN_RESPONSE: "air_auth_wallet_token_response",
|
|
14
|
+
INITIALIZATION_REQUEST: "air_auth_initialization_request",
|
|
15
|
+
INITIALIZATION_RESPONSE: "air_auth_initialization_response",
|
|
16
|
+
INIT_WALLET_COMMUNICATION: "air_auth_init_wallet_communication",
|
|
17
|
+
IFRAME_VISIBILITY_REQUEST: "air_auth_iframe_visibility_request",
|
|
18
|
+
SETUP_WALLET_REQUEST: "air_auth_setup_wallet_request",
|
|
19
|
+
SETUP_WALLET_RESPONSE: "air_auth_setup_wallet_response",
|
|
20
|
+
SIGN_SIWE_MESSAGE_REQUEST: "air_auth_sign_siwe_message_request",
|
|
21
|
+
SIGN_SIWE_MESSAGE_RESPONSE: "air_auth_sign_siwe_message_response",
|
|
22
|
+
LOGOUT_REQUEST: "air_auth_logout_request",
|
|
23
|
+
LOGOUT_RESPONSE: "air_auth_logout_response"
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export { AirAuthMessageTypes };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const AirMessageTypes = {
|
|
2
|
+
SERVICE_INITIALIZED: "air_service_initialized",
|
|
3
|
+
SERVICE_INITIALIZED_ERROR: "air_service_initialized_error",
|
|
4
|
+
LOGIN_REQUEST: "air_login_request",
|
|
5
|
+
CLAIM_REQUEST: "air_claim_request",
|
|
6
|
+
LOGIN_CLAIM_STATE: "air_login_claim_state",
|
|
7
|
+
CLOSE_MODAL: "air_close_modal",
|
|
8
|
+
INIT_AUTH_COMMUNICATION: "air_init_auth_communication",
|
|
9
|
+
DEPLOY_SMART_ACCOUNT_REQUEST: "air_deploy_smart_account_request",
|
|
10
|
+
DEPLOY_SMART_ACCOUNT_RESPONSE: "air_deploy_smart_account_response"
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { AirMessageTypes };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
2
|
+
import { getPopupFeatures, FEATURES_DEFAULT_POPUP_WINDOW } from '@toruslabs/base-controllers';
|
|
3
|
+
import { SafeEventEmitter } from '@web3auth/auth';
|
|
4
|
+
|
|
5
|
+
class PopupHandler extends SafeEventEmitter {
|
|
6
|
+
constructor({
|
|
7
|
+
url,
|
|
8
|
+
target,
|
|
9
|
+
features,
|
|
10
|
+
timeout = 30000
|
|
11
|
+
}) {
|
|
12
|
+
super();
|
|
13
|
+
_defineProperty(this, "url", void 0);
|
|
14
|
+
_defineProperty(this, "target", void 0);
|
|
15
|
+
_defineProperty(this, "features", void 0);
|
|
16
|
+
_defineProperty(this, "window", void 0);
|
|
17
|
+
_defineProperty(this, "windowTimer", void 0);
|
|
18
|
+
_defineProperty(this, "iClosedWindow", void 0);
|
|
19
|
+
_defineProperty(this, "timeout", void 0);
|
|
20
|
+
this.url = url;
|
|
21
|
+
this.target = target || "_blank";
|
|
22
|
+
this.features = features || getPopupFeatures(FEATURES_DEFAULT_POPUP_WINDOW);
|
|
23
|
+
this.window = undefined;
|
|
24
|
+
this.windowTimer = undefined;
|
|
25
|
+
this.iClosedWindow = false;
|
|
26
|
+
this.timeout = timeout;
|
|
27
|
+
this._setupTimer();
|
|
28
|
+
}
|
|
29
|
+
_setupTimer() {
|
|
30
|
+
this.windowTimer = Number(setInterval(() => {
|
|
31
|
+
if (this.window && this.window.closed) {
|
|
32
|
+
clearInterval(this.windowTimer);
|
|
33
|
+
setTimeout(() => {
|
|
34
|
+
if (!this.iClosedWindow) {
|
|
35
|
+
this.emit("close");
|
|
36
|
+
}
|
|
37
|
+
this.iClosedWindow = false;
|
|
38
|
+
this.window = undefined;
|
|
39
|
+
}, this.timeout);
|
|
40
|
+
}
|
|
41
|
+
if (this.window === undefined) clearInterval(this.windowTimer);
|
|
42
|
+
}, 500));
|
|
43
|
+
}
|
|
44
|
+
open() {
|
|
45
|
+
var _this$window;
|
|
46
|
+
this.window = window.open(this.url.href, this.target, this.features);
|
|
47
|
+
if ((_this$window = this.window) !== null && _this$window !== void 0 && _this$window.focus) this.window.focus();
|
|
48
|
+
}
|
|
49
|
+
close() {
|
|
50
|
+
this.iClosedWindow = true;
|
|
51
|
+
if (this.window) this.window.close();
|
|
52
|
+
}
|
|
53
|
+
redirect(locationReplaceOnRedirect) {
|
|
54
|
+
if (locationReplaceOnRedirect) {
|
|
55
|
+
window.location.replace(this.url.href);
|
|
56
|
+
} else {
|
|
57
|
+
window.location.href = this.url.href;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export { PopupHandler as default };
|
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import _objectSpread from '@babel/runtime/helpers/objectSpread2';
|
|
2
|
+
import _classPrivateMethodInitSpec from '@babel/runtime/helpers/classPrivateMethodInitSpec';
|
|
3
|
+
import _classPrivateFieldInitSpec from '@babel/runtime/helpers/classPrivateFieldInitSpec';
|
|
4
|
+
import { randomId } from '@toruslabs/base-controllers';
|
|
5
|
+
import { AirAuthMessageTypes } from '../../common/src/realm/messaging/auth.js';
|
|
6
|
+
import RealmEmbed from './embed.js';
|
|
7
|
+
import { AirServiceError } from './error.js';
|
|
8
|
+
import { IframeController } from './iframeController.js';
|
|
9
|
+
import { EMBED_BUILD_ENV } from './interfaces.js';
|
|
10
|
+
import log from './loglevel.js';
|
|
11
|
+
import AirMessageService from './messageService.js';
|
|
12
|
+
import { AIR_URLS, isElement } from './utils.js';
|
|
13
|
+
|
|
14
|
+
function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); }
|
|
15
|
+
function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; }
|
|
16
|
+
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"); }
|
|
17
|
+
var _authIframeController = /*#__PURE__*/new WeakMap();
|
|
18
|
+
var _loginResult = /*#__PURE__*/new WeakMap();
|
|
19
|
+
var _buildEnv = /*#__PURE__*/new WeakMap();
|
|
20
|
+
var _embedNonce = /*#__PURE__*/new WeakMap();
|
|
21
|
+
var _enableLogging = /*#__PURE__*/new WeakMap();
|
|
22
|
+
var _isInitialized = /*#__PURE__*/new WeakMap();
|
|
23
|
+
var _partnerId = /*#__PURE__*/new WeakMap();
|
|
24
|
+
var _realmEmbed = /*#__PURE__*/new WeakMap();
|
|
25
|
+
var _walletInitialization = /*#__PURE__*/new WeakMap();
|
|
26
|
+
var _airAuthListener = /*#__PURE__*/new WeakMap();
|
|
27
|
+
var _AirService_brand = /*#__PURE__*/new WeakSet();
|
|
28
|
+
class AirService {
|
|
29
|
+
constructor({
|
|
30
|
+
partnerId
|
|
31
|
+
}) {
|
|
32
|
+
_classPrivateMethodInitSpec(this, _AirService_brand);
|
|
33
|
+
_classPrivateFieldInitSpec(this, _authIframeController, void 0);
|
|
34
|
+
_classPrivateFieldInitSpec(this, _loginResult, void 0);
|
|
35
|
+
_classPrivateFieldInitSpec(this, _buildEnv, void 0);
|
|
36
|
+
_classPrivateFieldInitSpec(this, _embedNonce, randomId());
|
|
37
|
+
_classPrivateFieldInitSpec(this, _enableLogging, false);
|
|
38
|
+
_classPrivateFieldInitSpec(this, _isInitialized, false);
|
|
39
|
+
_classPrivateFieldInitSpec(this, _partnerId, void 0);
|
|
40
|
+
// Realm Embed related
|
|
41
|
+
_classPrivateFieldInitSpec(this, _realmEmbed, void 0);
|
|
42
|
+
_classPrivateFieldInitSpec(this, _walletInitialization, void 0);
|
|
43
|
+
_classPrivateFieldInitSpec(this, _airAuthListener, []);
|
|
44
|
+
_classPrivateFieldSet(_partnerId, this, partnerId);
|
|
45
|
+
_classPrivateFieldSet(_realmEmbed, this, new RealmEmbed({
|
|
46
|
+
partnerId: _classPrivateFieldGet(_partnerId, this)
|
|
47
|
+
}));
|
|
48
|
+
}
|
|
49
|
+
get buildEnv() {
|
|
50
|
+
return _classPrivateFieldGet(_buildEnv, this);
|
|
51
|
+
}
|
|
52
|
+
get isAirAuth() {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
get isInitialized() {
|
|
56
|
+
return _classPrivateFieldGet(_isInitialized, this);
|
|
57
|
+
}
|
|
58
|
+
get isLoggedIn() {
|
|
59
|
+
return !!_classPrivateFieldGet(_loginResult, this);
|
|
60
|
+
}
|
|
61
|
+
get isWalletLoaded() {
|
|
62
|
+
var _classPrivateFieldGet2;
|
|
63
|
+
return (_classPrivateFieldGet2 = _classPrivateFieldGet(_realmEmbed, this)) === null || _classPrivateFieldGet2 === void 0 ? void 0 : _classPrivateFieldGet2.isInitialized;
|
|
64
|
+
}
|
|
65
|
+
async init({
|
|
66
|
+
buildEnv = EMBED_BUILD_ENV.PRODUCTION,
|
|
67
|
+
enableLogging = false,
|
|
68
|
+
skipRehydration = false
|
|
69
|
+
}) {
|
|
70
|
+
if (!_classPrivateFieldGet(_partnerId, this)) throw new Error("Partner ID is required to initialize auth service");
|
|
71
|
+
if (_classPrivateFieldGet(_isInitialized, this)) return;
|
|
72
|
+
_classPrivateFieldSet(_buildEnv, this, buildEnv);
|
|
73
|
+
_classPrivateFieldSet(_enableLogging, this, enableLogging);
|
|
74
|
+
const {
|
|
75
|
+
authUrl,
|
|
76
|
+
logLevel
|
|
77
|
+
} = AIR_URLS[buildEnv];
|
|
78
|
+
log.setDefaultLevel(logLevel);
|
|
79
|
+
if (_classPrivateFieldGet(_enableLogging, this)) log.enableAll();else log.disableAll();
|
|
80
|
+
const authIframeOrigin = new URL(authUrl).origin;
|
|
81
|
+
await AirMessageService.openAuthObservables({
|
|
82
|
+
authIframeOrigin
|
|
83
|
+
});
|
|
84
|
+
_classPrivateFieldSet(_authIframeController, this, new IframeController(authUrl, `air-login-${_classPrivateFieldGet(_embedNonce, this)}`));
|
|
85
|
+
AirMessageService.authMessage$.subscribe(async msg => {
|
|
86
|
+
switch (msg.type) {
|
|
87
|
+
case AirAuthMessageTypes.IFRAME_VISIBILITY_REQUEST:
|
|
88
|
+
{
|
|
89
|
+
const authIframeController = _classPrivateFieldGet(_authIframeController, this);
|
|
90
|
+
authIframeController.setIframeVisibility(msg.payload.visible);
|
|
91
|
+
authIframeController.updateIframeState();
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
case AirAuthMessageTypes.SETUP_WALLET_REQUEST:
|
|
95
|
+
{
|
|
96
|
+
await _assertClassBrand(_AirService_brand, this, _initializeWallet).call(this);
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
try {
|
|
102
|
+
_classPrivateFieldGet(_authIframeController, this).createIframe();
|
|
103
|
+
const initializationPromise = new Promise((resolve, reject) => {
|
|
104
|
+
const handleAuthMessage = ev => {
|
|
105
|
+
try {
|
|
106
|
+
if (ev.origin !== authIframeOrigin) return;
|
|
107
|
+
if (ev.data === AirAuthMessageTypes.SETUP_COMPLETED) {
|
|
108
|
+
window.removeEventListener("message", handleAuthMessage);
|
|
109
|
+
_classPrivateFieldGet(_authIframeController, this).postMessage({
|
|
110
|
+
type: AirAuthMessageTypes.INITIALIZATION_REQUEST,
|
|
111
|
+
payload: {
|
|
112
|
+
partnerId: _classPrivateFieldGet(_partnerId, this),
|
|
113
|
+
skipRehydration
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
resolve();
|
|
117
|
+
}
|
|
118
|
+
} catch (err) {
|
|
119
|
+
reject(err);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
window.addEventListener("message", handleAuthMessage);
|
|
123
|
+
});
|
|
124
|
+
const authServiceInitialization = AirMessageService.waitForAuthInitialization();
|
|
125
|
+
const result = await Promise.all([initializationPromise, authServiceInitialization]);
|
|
126
|
+
_classPrivateFieldSet(_isInitialized, this, true);
|
|
127
|
+
_assertClassBrand(_AirService_brand, this, _triggerAirAuthInitialized).call(this);
|
|
128
|
+
_assertClassBrand(_AirService_brand, this, _prepareRealmEmbedListeners).call(this);
|
|
129
|
+
|
|
130
|
+
// rehydrated auth session
|
|
131
|
+
if (result[1].rehydrated) {
|
|
132
|
+
_classPrivateFieldSet(_loginResult, this, _assertClassBrand(_AirService_brand, this, _createLoginResult).call(this, result[1]));
|
|
133
|
+
_assertClassBrand(_AirService_brand, this, _triggerAirAuthLoggedIn).call(this);
|
|
134
|
+
return _classPrivateFieldGet(_loginResult, this);
|
|
135
|
+
}
|
|
136
|
+
} catch (error) {
|
|
137
|
+
log.debug("Error initializing auth service", error);
|
|
138
|
+
await this.cleanUp();
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
async cleanUp() {
|
|
144
|
+
var _classPrivateFieldGet3;
|
|
145
|
+
// Clear up wallet service iframe if exists
|
|
146
|
+
await _assertClassBrand(_AirService_brand, this, _cleanUpWallet).call(this);
|
|
147
|
+
|
|
148
|
+
// Logout auth session
|
|
149
|
+
await AirMessageService.logoutAuth(_classPrivateFieldGet(_authIframeController, this).iframeElement);
|
|
150
|
+
|
|
151
|
+
// Destroy the auth iframe
|
|
152
|
+
const authIframeElement = (_classPrivateFieldGet3 = _classPrivateFieldGet(_authIframeController, this)) === null || _classPrivateFieldGet3 === void 0 ? void 0 : _classPrivateFieldGet3.iframeElement;
|
|
153
|
+
if (isElement(authIframeElement) && window.document.body.contains(authIframeElement)) {
|
|
154
|
+
_classPrivateFieldGet(_authIframeController, this).destroy();
|
|
155
|
+
_classPrivateFieldSet(_authIframeController, this, undefined);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Close the message service
|
|
159
|
+
AirMessageService.closeAuthObservables();
|
|
160
|
+
this.clearEventListeners();
|
|
161
|
+
_classPrivateFieldSet(_isInitialized, this, false);
|
|
162
|
+
}
|
|
163
|
+
async login(options) {
|
|
164
|
+
if (!_classPrivateFieldGet(_isInitialized, this)) throw new Error("Service is not initialized");
|
|
165
|
+
if (_classPrivateFieldGet(_loginResult, this)) return _classPrivateFieldGet(_loginResult, this);
|
|
166
|
+
const iframeController = _classPrivateFieldGet(_authIframeController, this);
|
|
167
|
+
const {
|
|
168
|
+
payload
|
|
169
|
+
} = await AirMessageService.sendAuthLoginRequest(iframeController.iframeElement, {
|
|
170
|
+
partnerLoginToken: options === null || options === void 0 ? void 0 : options.authToken
|
|
171
|
+
});
|
|
172
|
+
if (payload.success === true) {
|
|
173
|
+
_classPrivateFieldSet(_loginResult, this, _assertClassBrand(_AirService_brand, this, _createLoginResult).call(this, payload));
|
|
174
|
+
_assertClassBrand(_AirService_brand, this, _triggerAirAuthLoggedIn).call(this);
|
|
175
|
+
return _classPrivateFieldGet(_loginResult, this);
|
|
176
|
+
}
|
|
177
|
+
throw new AirServiceError("UNKNOWN_ERROR", "Unknown error occurred");
|
|
178
|
+
}
|
|
179
|
+
async deploySmartAccount() {
|
|
180
|
+
if (!_classPrivateFieldGet(_isInitialized, this)) throw new Error("Service is not initialized");
|
|
181
|
+
await _assertClassBrand(_AirService_brand, this, _ensureWallet).call(this);
|
|
182
|
+
return _classPrivateFieldGet(_realmEmbed, this)._deploySmartAccount();
|
|
183
|
+
}
|
|
184
|
+
async logout() {
|
|
185
|
+
if (!_classPrivateFieldGet(_isInitialized, this)) throw new Error("Service is not initialized");
|
|
186
|
+
if (!this.isLoggedIn) throw new Error("No active session to logout");
|
|
187
|
+
|
|
188
|
+
// Clear up wallet
|
|
189
|
+
await _assertClassBrand(_AirService_brand, this, _cleanUpWallet).call(this);
|
|
190
|
+
await AirMessageService.logoutAuth(_classPrivateFieldGet(_authIframeController, this).iframeElement);
|
|
191
|
+
_classPrivateFieldSet(_loginResult, this, undefined);
|
|
192
|
+
_assertClassBrand(_AirService_brand, this, _triggerAirAuthLoggedOut).call(this);
|
|
193
|
+
}
|
|
194
|
+
async getProvider() {
|
|
195
|
+
if (!this.isLoggedIn) return null;
|
|
196
|
+
await _assertClassBrand(_AirService_brand, this, _ensureWallet).call(this);
|
|
197
|
+
return _classPrivateFieldGet(_realmEmbed, this).provider;
|
|
198
|
+
}
|
|
199
|
+
async preloadWallet() {
|
|
200
|
+
return _assertClassBrand(_AirService_brand, this, _ensureWallet).call(this);
|
|
201
|
+
}
|
|
202
|
+
async claimAirId(options) {
|
|
203
|
+
await _assertClassBrand(_AirService_brand, this, _ensureWallet).call(this);
|
|
204
|
+
return _classPrivateFieldGet(_realmEmbed, this).claimRealmId(options);
|
|
205
|
+
}
|
|
206
|
+
async getUserInfo() {
|
|
207
|
+
if (!this.isLoggedIn) throw new Error("User not logged in");
|
|
208
|
+
const info = await AirMessageService.sendUserInfoRequest(_classPrivateFieldGet(_authIframeController, this).iframeElement);
|
|
209
|
+
if (info.payload.success === false) {
|
|
210
|
+
throw new AirServiceError(info.payload.errorName, info.payload.errorMessage);
|
|
211
|
+
}
|
|
212
|
+
return {
|
|
213
|
+
id: info.payload.id,
|
|
214
|
+
abstractAccountAddress: info.payload.abstractAccountAddress,
|
|
215
|
+
email: info.payload.email,
|
|
216
|
+
airIds: info.payload.airIds
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
async getPartnerUserInfo() {
|
|
220
|
+
if (!this.isLoggedIn) throw new Error("User not logged in");
|
|
221
|
+
const info = await AirMessageService.sendPartnerUserInfoRequest(_classPrivateFieldGet(_authIframeController, this).iframeElement);
|
|
222
|
+
if (info.payload.success === false) {
|
|
223
|
+
throw new AirServiceError(info.payload.errorName, info.payload.errorMessage);
|
|
224
|
+
}
|
|
225
|
+
return {
|
|
226
|
+
partnerId: info.payload.partnerId,
|
|
227
|
+
partnerUserId: info.payload.partnerUserId,
|
|
228
|
+
airId: info.payload.airId
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
on(listener) {
|
|
232
|
+
if (_classPrivateFieldGet(_airAuthListener, this).indexOf(listener) < 0) _classPrivateFieldGet(_airAuthListener, this).push(listener);
|
|
233
|
+
}
|
|
234
|
+
off(listener) {
|
|
235
|
+
if (_classPrivateFieldGet(_airAuthListener, this).indexOf(listener) >= 0) _classPrivateFieldGet(_airAuthListener, this).splice(_classPrivateFieldGet(_airAuthListener, this).indexOf(listener), 1);
|
|
236
|
+
}
|
|
237
|
+
clearEventListeners() {
|
|
238
|
+
_classPrivateFieldSet(_airAuthListener, this, []);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// This is for internal use only
|
|
242
|
+
// @ts-expect-error for unused function
|
|
243
|
+
async _showWalletWidget() {
|
|
244
|
+
await _assertClassBrand(_AirService_brand, this, _ensureWallet).call(this);
|
|
245
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
246
|
+
return _classPrivateFieldGet(_realmEmbed, this)._showWalletWidget();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
async function _createWallet() {
|
|
250
|
+
return _classPrivateFieldGet(_realmEmbed, this).login({
|
|
251
|
+
skipClaiming: true,
|
|
252
|
+
legacy: false
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
async function _ensureWallet() {
|
|
256
|
+
if (!this.isLoggedIn) throw new Error("No active session to create wallet");
|
|
257
|
+
if (!_classPrivateFieldGet(_walletInitialization, this)) {
|
|
258
|
+
AirMessageService.setupAuthIframe(_classPrivateFieldGet(_authIframeController, this).iframeElement);
|
|
259
|
+
_classPrivateFieldSet(_walletInitialization, this, (async () => {
|
|
260
|
+
try {
|
|
261
|
+
await _assertClassBrand(_AirService_brand, this, _initializeWallet).call(this);
|
|
262
|
+
return await _assertClassBrand(_AirService_brand, this, _createWallet).call(this);
|
|
263
|
+
} catch (error) {
|
|
264
|
+
log.error("Error initializing wallet", error);
|
|
265
|
+
throw error;
|
|
266
|
+
}
|
|
267
|
+
})());
|
|
268
|
+
}
|
|
269
|
+
return _classPrivateFieldGet(_walletInitialization, this);
|
|
270
|
+
}
|
|
271
|
+
async function _cleanUpWallet() {
|
|
272
|
+
if (_classPrivateFieldGet(_walletInitialization, this) || _classPrivateFieldGet(_realmEmbed, this)) {
|
|
273
|
+
await _classPrivateFieldGet(_walletInitialization, this);
|
|
274
|
+
await _classPrivateFieldGet(_realmEmbed, this).cleanUp();
|
|
275
|
+
_classPrivateFieldSet(_walletInitialization, this, undefined);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
async function _initializeWallet() {
|
|
279
|
+
if (!_classPrivateFieldGet(_realmEmbed, this).isInitialized) {
|
|
280
|
+
try {
|
|
281
|
+
await _classPrivateFieldGet(_realmEmbed, this).init({
|
|
282
|
+
buildEnv: _classPrivateFieldGet(_buildEnv, this),
|
|
283
|
+
skipRehydration: true,
|
|
284
|
+
enableLogging: _classPrivateFieldGet(_enableLogging, this) || false
|
|
285
|
+
});
|
|
286
|
+
_classPrivateFieldGet(_authIframeController, this).postMessage({
|
|
287
|
+
type: AirAuthMessageTypes.SETUP_WALLET_RESPONSE,
|
|
288
|
+
payload: {
|
|
289
|
+
success: true
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
} catch (error) {
|
|
293
|
+
log.error("Error initializing wallet", error);
|
|
294
|
+
_classPrivateFieldGet(_authIframeController, this).postMessage({
|
|
295
|
+
type: AirAuthMessageTypes.SETUP_WALLET_RESPONSE,
|
|
296
|
+
payload: {
|
|
297
|
+
success: false,
|
|
298
|
+
errorName: "UNKNOWN_ERROR",
|
|
299
|
+
errorMessage: "Unknown error occurred"
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
throw error;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
async function _triggerEventListeners(data) {
|
|
307
|
+
_classPrivateFieldGet(_airAuthListener, this).forEach(listener => {
|
|
308
|
+
listener(data);
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
async function _triggerAirAuthInitialized() {
|
|
312
|
+
await _assertClassBrand(_AirService_brand, this, _triggerEventListeners).call(this, {
|
|
313
|
+
event: "initialized"
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
async function _triggerAirAuthLoggedIn() {
|
|
317
|
+
await _assertClassBrand(_AirService_brand, this, _triggerEventListeners).call(this, {
|
|
318
|
+
event: "logged_in",
|
|
319
|
+
result: _classPrivateFieldGet(_loginResult, this)
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
async function _triggerAirAuthLoggedOut() {
|
|
323
|
+
await _assertClassBrand(_AirService_brand, this, _triggerEventListeners).call(this, {
|
|
324
|
+
event: "logged_out"
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
function _createLoginResult(payload) {
|
|
328
|
+
return {
|
|
329
|
+
isLoggedIn: true,
|
|
330
|
+
id: payload.id,
|
|
331
|
+
abstractAccountAddress: payload.abstractAccountAddress,
|
|
332
|
+
token: payload.partnerAccessToken
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
async function _prepareRealmEmbedListeners() {
|
|
336
|
+
_classPrivateFieldGet(_realmEmbed, this).on(data => {
|
|
337
|
+
if (data.event === "connected" || data.event === "logged_in") {
|
|
338
|
+
var _data$result$addresse;
|
|
339
|
+
const result = _objectSpread(_objectSpread({}, data.result), {}, {
|
|
340
|
+
addresses: {
|
|
341
|
+
eoa: data.result.addresses.eoa,
|
|
342
|
+
aa: (_data$result$addresse = data.result.addresses.aa) !== null && _data$result$addresse !== void 0 ? _data$result$addresse : ""
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
_assertClassBrand(_AirService_brand, this, _triggerEventListeners).call(this, {
|
|
346
|
+
event: "wallet_loaded",
|
|
347
|
+
result
|
|
348
|
+
});
|
|
349
|
+
} else if (data.event === "minting_started") {
|
|
350
|
+
_assertClassBrand(_AirService_brand, this, _triggerEventListeners).call(this, {
|
|
351
|
+
event: "minting_started"
|
|
352
|
+
});
|
|
353
|
+
} else if (data.event === "minting_failed") {
|
|
354
|
+
_assertClassBrand(_AirService_brand, this, _triggerEventListeners).call(this, {
|
|
355
|
+
event: "minting_failed",
|
|
356
|
+
errorMessage: data.errorMessage
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
export { AirService as default };
|