@moon-x/core 0.2.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/README.md +39 -0
- package/dist/chain-C9dvKXUY.d.mts +48 -0
- package/dist/chain-C9dvKXUY.d.ts +48 -0
- package/dist/chunk-264CEGDS.mjs +46 -0
- package/dist/chunk-CDT4MC7S.mjs +1532 -0
- package/dist/chunk-CMYR6AOY.mjs +0 -0
- package/dist/chunk-GQKIA37O.mjs +20 -0
- package/dist/chunk-IMLBIIJ4.mjs +36 -0
- package/dist/chunk-MOREUKOG.mjs +18 -0
- package/dist/chunk-SOKLPX7V.mjs +270 -0
- package/dist/index.d.mts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +402 -0
- package/dist/index.mjs +33 -0
- package/dist/interfaces-9k0eKCc4.d.ts +120 -0
- package/dist/interfaces-fNhwnCeY.d.mts +120 -0
- package/dist/lib/index.d.mts +410 -0
- package/dist/lib/index.d.ts +410 -0
- package/dist/lib/index.js +1622 -0
- package/dist/lib/index.mjs +89 -0
- package/dist/passkey-cache-B9PT3AWX.d.mts +47 -0
- package/dist/passkey-cache-B9PT3AWX.d.ts +47 -0
- package/dist/passkey-metadata-QqFF2SWQ.d.mts +25 -0
- package/dist/passkey-metadata-QqFF2SWQ.d.ts +25 -0
- package/dist/post-message-C_99BCBh.d.mts +70 -0
- package/dist/post-message-C_99BCBh.d.ts +70 -0
- package/dist/react/ethereum.d.mts +26 -0
- package/dist/react/ethereum.d.ts +26 -0
- package/dist/react/ethereum.js +99 -0
- package/dist/react/ethereum.mjs +56 -0
- package/dist/react/index.d.mts +182 -0
- package/dist/react/index.d.ts +182 -0
- package/dist/react/index.js +586 -0
- package/dist/react/index.mjs +328 -0
- package/dist/react/solana.d.mts +17 -0
- package/dist/react/solana.d.ts +17 -0
- package/dist/react/solana.js +75 -0
- package/dist/react/solana.mjs +35 -0
- package/dist/sdk/index.d.mts +126 -0
- package/dist/sdk/index.d.ts +126 -0
- package/dist/sdk/index.js +864 -0
- package/dist/sdk/index.mjs +579 -0
- package/dist/types/index.d.mts +1190 -0
- package/dist/types/index.d.ts +1190 -0
- package/dist/types/index.js +64 -0
- package/dist/types/index.mjs +10 -0
- package/dist/utils/index.d.mts +11 -0
- package/dist/utils/index.d.ts +11 -0
- package/dist/utils/index.js +365 -0
- package/dist/utils/index.mjs +25 -0
- package/dist/web/index.d.mts +20 -0
- package/dist/web/index.d.ts +20 -0
- package/dist/web/index.js +470 -0
- package/dist/web/index.mjs +155 -0
- package/package.json +111 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/types/index.ts
|
|
21
|
+
var types_exports = {};
|
|
22
|
+
__export(types_exports, {
|
|
23
|
+
Network: () => Network,
|
|
24
|
+
WALLET_ERROR_CODES: () => WALLET_ERROR_CODES,
|
|
25
|
+
WalletCreationError: () => WalletCreationError
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(types_exports);
|
|
28
|
+
|
|
29
|
+
// src/types/wallet.ts
|
|
30
|
+
var WALLET_ERROR_CODES = {
|
|
31
|
+
WALLET_NOT_CONNECTED: "WALLET_NOT_CONNECTED",
|
|
32
|
+
USER_REJECTED: "USER_REJECTED",
|
|
33
|
+
INVALID_MESSAGE: "INVALID_MESSAGE",
|
|
34
|
+
INVALID_TRANSACTION: "INVALID_TRANSACTION",
|
|
35
|
+
NETWORK_ERROR: "NETWORK_ERROR",
|
|
36
|
+
UNKNOWN_ERROR: "UNKNOWN_ERROR"
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// src/types/index.ts
|
|
40
|
+
var WalletCreationError = /* @__PURE__ */ ((WalletCreationError2) => {
|
|
41
|
+
WalletCreationError2["SESSION_INVALID"] = "Session is invalid - cannot create wallet";
|
|
42
|
+
WalletCreationError2["FAILED_TO_CREATE_WALLET"] = "Failed to create wallet";
|
|
43
|
+
return WalletCreationError2;
|
|
44
|
+
})(WalletCreationError || {});
|
|
45
|
+
var Network = /* @__PURE__ */ ((Network2) => {
|
|
46
|
+
Network2["Bitcoin"] = "bitcoin";
|
|
47
|
+
Network2["Solana"] = "solana";
|
|
48
|
+
Network2["Ripple"] = "ripple";
|
|
49
|
+
Network2["Ethereum"] = "ethereum";
|
|
50
|
+
Network2["Polygon"] = "polygon";
|
|
51
|
+
Network2["Arbitrum"] = "arbitrum";
|
|
52
|
+
Network2["Optimism"] = "optimism";
|
|
53
|
+
Network2["Base"] = "base";
|
|
54
|
+
Network2["ZkSync"] = "zksync";
|
|
55
|
+
Network2["BinanceSmartChain"] = "bsc";
|
|
56
|
+
Network2["Tron"] = "tron";
|
|
57
|
+
return Network2;
|
|
58
|
+
})(Network || {});
|
|
59
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
60
|
+
0 && (module.exports = {
|
|
61
|
+
Network,
|
|
62
|
+
WALLET_ERROR_CODES,
|
|
63
|
+
WalletCreationError
|
|
64
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { F as FORWARDED_ERROR_FIELDS, P as PostMessageClient, a as PostMessageMethod, b as PostMessageServer, c as PostMessageType } from '../post-message-C_99BCBh.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Get the appropriate MoonKey API URL based on environment configuration
|
|
5
|
+
*/
|
|
6
|
+
declare const getMoonKeyApiUrl: () => string;
|
|
7
|
+
|
|
8
|
+
declare const getIframeUrl: (path?: string) => string;
|
|
9
|
+
declare const getIframeOrigin: (url: string) => string;
|
|
10
|
+
|
|
11
|
+
export { getIframeOrigin, getIframeUrl, getMoonKeyApiUrl };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { F as FORWARDED_ERROR_FIELDS, P as PostMessageClient, a as PostMessageMethod, b as PostMessageServer, c as PostMessageType } from '../post-message-C_99BCBh.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Get the appropriate MoonKey API URL based on environment configuration
|
|
5
|
+
*/
|
|
6
|
+
declare const getMoonKeyApiUrl: () => string;
|
|
7
|
+
|
|
8
|
+
declare const getIframeUrl: (path?: string) => string;
|
|
9
|
+
declare const getIframeOrigin: (url: string) => string;
|
|
10
|
+
|
|
11
|
+
export { getIframeOrigin, getIframeUrl, getMoonKeyApiUrl };
|
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/utils/index.ts
|
|
21
|
+
var utils_exports = {};
|
|
22
|
+
__export(utils_exports, {
|
|
23
|
+
FORWARDED_ERROR_FIELDS: () => FORWARDED_ERROR_FIELDS,
|
|
24
|
+
PostMessageClient: () => PostMessageClient,
|
|
25
|
+
PostMessageMethod: () => PostMessageMethod,
|
|
26
|
+
PostMessageServer: () => PostMessageServer,
|
|
27
|
+
PostMessageType: () => PostMessageType,
|
|
28
|
+
getIframeOrigin: () => getIframeOrigin,
|
|
29
|
+
getIframeUrl: () => getIframeUrl,
|
|
30
|
+
getMoonKeyApiUrl: () => getMoonKeyApiUrl
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(utils_exports);
|
|
33
|
+
|
|
34
|
+
// src/utils/get-api-url.ts
|
|
35
|
+
var getMoonKeyApiUrl = () => {
|
|
36
|
+
if (process.env?.NEXT_PUBLIC_MOONKEY_API_URL) {
|
|
37
|
+
return process.env.NEXT_PUBLIC_MOONKEY_API_URL;
|
|
38
|
+
}
|
|
39
|
+
if (process.env?.NEXT_PUBLIC_MOONKEY_IFRAME_URL) {
|
|
40
|
+
const iframeUrl = process.env.NEXT_PUBLIC_MOONKEY_IFRAME_URL;
|
|
41
|
+
if (iframeUrl.includes("staging") || iframeUrl.includes("auth-staging")) {
|
|
42
|
+
return "https://api.moonx-dev.com";
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (process.env?.NEXT_PUBLIC_VERCEL_ENV === "preview") {
|
|
46
|
+
return "https://api.moonx-dev.com";
|
|
47
|
+
}
|
|
48
|
+
return "https://api.moonx-dev.com";
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// src/utils/get-iframe-url.ts
|
|
52
|
+
var DEFAULT_IFRAME_PROJECT_NAME = "moonx-wallet-iframe";
|
|
53
|
+
var joinUrl = (baseUrl, path) => {
|
|
54
|
+
if (!path) return baseUrl;
|
|
55
|
+
const normalizedBase = baseUrl.replace(/\/$/, "");
|
|
56
|
+
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
|
|
57
|
+
return `${normalizedBase}${normalizedPath}`;
|
|
58
|
+
};
|
|
59
|
+
var withBypass = (url) => {
|
|
60
|
+
const bypass = process.env.NEXT_PUBLIC_VERCEL_PROTECTION_BYPASS;
|
|
61
|
+
if (!bypass) return url;
|
|
62
|
+
const separator = url.includes("?") ? "&" : "?";
|
|
63
|
+
return `${url}${separator}x-vercel-protection-bypass=${bypass}&x-vercel-set-bypass-cookie=samesitenone`;
|
|
64
|
+
};
|
|
65
|
+
var getIframeUrl = (path = "") => {
|
|
66
|
+
if (process.env.NEXT_PUBLIC_VERCEL_ENV === "preview" && process.env.NEXT_PUBLIC_VERCEL_BRANCH_URL) {
|
|
67
|
+
const vercelUrl = process.env.NEXT_PUBLIC_VERCEL_BRANCH_URL.replace(
|
|
68
|
+
/^https?:\/\//,
|
|
69
|
+
""
|
|
70
|
+
).replace(/\/$/, "");
|
|
71
|
+
const iframeProjectName = process.env.NEXT_PUBLIC_MOONKEY_IFRAME_PROJECT_NAME || DEFAULT_IFRAME_PROJECT_NAME;
|
|
72
|
+
const iframeUrl = /-git-/.test(vercelUrl) ? vercelUrl.replace(/^[^.]+?-git-/, `${iframeProjectName}-git-`) : (
|
|
73
|
+
// Fallback: legacy `{prefix}-{hash}.vercel.app` style
|
|
74
|
+
vercelUrl.replace(/^[^-]+-/, `${iframeProjectName}-`)
|
|
75
|
+
);
|
|
76
|
+
return withBypass(joinUrl(`https://${iframeUrl}`, path));
|
|
77
|
+
}
|
|
78
|
+
if (process.env.NEXT_PUBLIC_MOONKEY_IFRAME_URL) {
|
|
79
|
+
return joinUrl(process.env.NEXT_PUBLIC_MOONKEY_IFRAME_URL, path);
|
|
80
|
+
}
|
|
81
|
+
return joinUrl("https://iframe.moonx-dev.com", path);
|
|
82
|
+
};
|
|
83
|
+
var getIframeOrigin = (url) => {
|
|
84
|
+
try {
|
|
85
|
+
const urlObj = new URL(url);
|
|
86
|
+
return urlObj.origin;
|
|
87
|
+
} catch (error) {
|
|
88
|
+
console.error("Invalid URL:", url);
|
|
89
|
+
return "";
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
// src/utils/post-message.ts
|
|
94
|
+
var FORWARDED_ERROR_FIELDS = [
|
|
95
|
+
"error_type",
|
|
96
|
+
"error_code",
|
|
97
|
+
"status_code"
|
|
98
|
+
];
|
|
99
|
+
var generateUUID = () => {
|
|
100
|
+
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
101
|
+
return crypto.randomUUID();
|
|
102
|
+
}
|
|
103
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
104
|
+
const r = Math.random() * 16 | 0;
|
|
105
|
+
const v = c === "x" ? r : r & 3 | 8;
|
|
106
|
+
return v.toString(16);
|
|
107
|
+
});
|
|
108
|
+
};
|
|
109
|
+
var PostMessageType = {
|
|
110
|
+
AUTH: "auth",
|
|
111
|
+
INIT: "init",
|
|
112
|
+
WALLET: "wallet",
|
|
113
|
+
EXTERNAL_WALLET: "external_wallet"
|
|
114
|
+
};
|
|
115
|
+
var PostMessageMethod = {
|
|
116
|
+
CLOSE: "CLOSE",
|
|
117
|
+
CREATE_WALLET: "CREATE_WALLET",
|
|
118
|
+
IMPORT_KEY: "IMPORT_KEY",
|
|
119
|
+
FUND_WALLET: "FUND_WALLET",
|
|
120
|
+
GET_APP_CONFIG: "GET_APP_CONFIG",
|
|
121
|
+
GET_BALANCE: "GET_BALANCE",
|
|
122
|
+
GET_CONFIRM_TRANSACTION: "GET_CONFIRM_TRANSACTION",
|
|
123
|
+
GET_CONFIRM_ETHEREUM_TRANSACTION: "GET_CONFIRM_ETHEREUM_TRANSACTION",
|
|
124
|
+
GET_CURRENT_SESSION: "GET_CURRENT_SESSION",
|
|
125
|
+
GET_TOKEN_ACCOUNTS_BY_OWNER: "GET_TOKEN_ACCOUNTS_BY_OWNER",
|
|
126
|
+
GET_USER: "GET_USER",
|
|
127
|
+
GET_WALLET: "GET_WALLET",
|
|
128
|
+
GET_WALLETS: "GET_WALLETS",
|
|
129
|
+
IS_AUTHENTICATED: "IS_AUTHENTICATED",
|
|
130
|
+
LOGIN_CREATE: "LOGIN_CREATE",
|
|
131
|
+
LOGOUT: "LOGOUT",
|
|
132
|
+
PREFETCH_CAPTCHA: "PREFETCH_CAPTCHA",
|
|
133
|
+
READY: "READY",
|
|
134
|
+
SEND_TRANSACTION: "SEND_TRANSACTION",
|
|
135
|
+
SIGN_MESSAGE: "SIGN_MESSAGE",
|
|
136
|
+
SIGN_TRANSACTION: "SIGN_TRANSACTION",
|
|
137
|
+
SIGN_TYPED_DATA: "SIGN_TYPED_DATA",
|
|
138
|
+
// Sign a pre-computed 32-byte digest as ECDSA. Caller-supplied hash
|
|
139
|
+
// — no EIP-191 prefix, no tx parsing, no domain separator. Useful
|
|
140
|
+
// for ZK circuits / custom auth flows / anywhere the digest is
|
|
141
|
+
// computed externally. Mirrors Privy's `secp256k1_sign` provider
|
|
142
|
+
// method but exposed as its own postMessage to keep the iframe
|
|
143
|
+
// dispatcher boring.
|
|
144
|
+
SIGN_HASH: "SIGN_HASH",
|
|
145
|
+
SIGN_7702_AUTHORIZATION: "SIGN_7702_AUTHORIZATION",
|
|
146
|
+
VERIFY_EMAIL_OTP: "VERIFY_EMAIL_OTP",
|
|
147
|
+
VERIFY_OAUTH: "VERIFY_OAUTH",
|
|
148
|
+
START_OAUTH: "START_OAUTH",
|
|
149
|
+
WALLET_REGISTRATION_NONCE: "WALLET_REGISTRATION_NONCE",
|
|
150
|
+
VERIFY_SIWE_WALLET_REGISTRATION: "VERIFY_SIWE_WALLET_REGISTRATION",
|
|
151
|
+
ATTACH_OAUTH: "ATTACH_OAUTH",
|
|
152
|
+
DETACH_OAUTH: "DETACH_OAUTH",
|
|
153
|
+
// Passkey as a post-identity enrollment / unlock ceremony. The two
|
|
154
|
+
// methods below split the ceremony so the parent owns the WebAuthn
|
|
155
|
+
// API call (where user activation lives) and the iframe owns the
|
|
156
|
+
// begin/verify HTTP calls (where the session JWT lives).
|
|
157
|
+
PASSKEY_REGISTER_BEGIN: "PASSKEY_REGISTER_BEGIN",
|
|
158
|
+
PASSKEY_REGISTER_VERIFY: "PASSKEY_REGISTER_VERIFY",
|
|
159
|
+
GET_PASSKEY_STATUS: "GET_PASSKEY_STATUS",
|
|
160
|
+
// Parent-side passkey assertion plumbing. Parent calls GET_DEK_INFO
|
|
161
|
+
// to learn which credential IDs to put in allowCredentials and to
|
|
162
|
+
// know whether bootstrap-vs-unwrap path applies, then runs
|
|
163
|
+
// navigator.credentials.get itself (where iframe-cross-origin
|
|
164
|
+
// activation rules don't bite). Wallet-op postMessages then carry
|
|
165
|
+
// the resulting user_handle_b64url + credential_id_b64url back into
|
|
166
|
+
// the iframe so encrypted-keyshare helpers can unwrap the DEK.
|
|
167
|
+
GET_DEK_INFO: "GET_DEK_INFO",
|
|
168
|
+
// Multi-passkey management on an existing user. ADD_PASSKEY_WRAP is
|
|
169
|
+
// the iframe half of the parent-driven add-passkey flow: parent runs
|
|
170
|
+
// both WebAuthn ceremonies (assert old credential, register new
|
|
171
|
+
// credential), then posts the two resulting (userHandle, credId)
|
|
172
|
+
// pairs here so the iframe can unlock the DEK with the old
|
|
173
|
+
// credential and seal a new wrap with the new credential.
|
|
174
|
+
ADD_PASSKEY_WRAP: "ADD_PASSKEY_WRAP",
|
|
175
|
+
REMOVE_PASSKEY: "REMOVE_PASSKEY"
|
|
176
|
+
};
|
|
177
|
+
var PostMessageClient = class {
|
|
178
|
+
constructor(target, origin = "*") {
|
|
179
|
+
this.target = target;
|
|
180
|
+
this.origin = origin;
|
|
181
|
+
}
|
|
182
|
+
async send(type, method, payload) {
|
|
183
|
+
const id = generateUUID();
|
|
184
|
+
return new Promise((resolve, reject) => {
|
|
185
|
+
const timeout = setTimeout(() => {
|
|
186
|
+
window.removeEventListener("message", handler);
|
|
187
|
+
reject(new Error("Timeout: No response from iframe"));
|
|
188
|
+
}, 3e4);
|
|
189
|
+
const handler = (event) => {
|
|
190
|
+
if (this.origin !== "*" && event.origin !== this.origin) {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
const response = event.data;
|
|
194
|
+
if (response.id === id) {
|
|
195
|
+
clearTimeout(timeout);
|
|
196
|
+
window.removeEventListener("message", handler);
|
|
197
|
+
if (response.success) {
|
|
198
|
+
resolve(response.data);
|
|
199
|
+
} else {
|
|
200
|
+
const err = new Error(response.error);
|
|
201
|
+
for (const key of FORWARDED_ERROR_FIELDS) {
|
|
202
|
+
const value = response[key];
|
|
203
|
+
if (value !== void 0) {
|
|
204
|
+
err[key] = value;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
reject(err);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
window.addEventListener("message", handler);
|
|
212
|
+
this.target.postMessage(
|
|
213
|
+
{
|
|
214
|
+
id,
|
|
215
|
+
type,
|
|
216
|
+
method,
|
|
217
|
+
payload
|
|
218
|
+
},
|
|
219
|
+
this.origin
|
|
220
|
+
);
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
async sendOneWay(type, method, payload) {
|
|
224
|
+
const id = generateUUID();
|
|
225
|
+
const envelope = {
|
|
226
|
+
id,
|
|
227
|
+
type,
|
|
228
|
+
method,
|
|
229
|
+
payload
|
|
230
|
+
};
|
|
231
|
+
if (typeof window !== "undefined" && typeof window.ReactNativeWebView !== "undefined") {
|
|
232
|
+
try {
|
|
233
|
+
window.ReactNativeWebView.postMessage(JSON.stringify(envelope));
|
|
234
|
+
} catch {
|
|
235
|
+
}
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
this.target.postMessage(envelope, this.origin);
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
var isReactNativeWebView = () => typeof window !== "undefined" && typeof window.ReactNativeWebView !== "undefined";
|
|
242
|
+
var sendReactNativeReply = (response) => {
|
|
243
|
+
try {
|
|
244
|
+
window.ReactNativeWebView.postMessage(JSON.stringify(response));
|
|
245
|
+
} catch {
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
var PostMessageServer = class {
|
|
249
|
+
constructor(originValidator) {
|
|
250
|
+
this.trustedSources = /* @__PURE__ */ new Set();
|
|
251
|
+
this.handlers = /* @__PURE__ */ new Map();
|
|
252
|
+
this.originValidator = originValidator;
|
|
253
|
+
window.addEventListener("message", this.handleMessage.bind(this));
|
|
254
|
+
}
|
|
255
|
+
addTrustedSource(source) {
|
|
256
|
+
this.trustedSources.add(source);
|
|
257
|
+
}
|
|
258
|
+
removeTrustedSource(source) {
|
|
259
|
+
this.trustedSources.delete(source);
|
|
260
|
+
}
|
|
261
|
+
register(type, method, handler) {
|
|
262
|
+
const key = `${type}:${method}`;
|
|
263
|
+
this.handlers.set(key, handler);
|
|
264
|
+
}
|
|
265
|
+
cleanup() {
|
|
266
|
+
window.removeEventListener("message", this.handleMessage.bind(this));
|
|
267
|
+
this.handlers.clear();
|
|
268
|
+
}
|
|
269
|
+
static isLocalhostOrigin(origin) {
|
|
270
|
+
try {
|
|
271
|
+
const url = new URL(origin);
|
|
272
|
+
return url.hostname === "localhost" || url.hostname === "127.0.0.1" || url.hostname === "[::1]";
|
|
273
|
+
} catch {
|
|
274
|
+
return false;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
isValidPostMessageRequest(data) {
|
|
278
|
+
return data && typeof data === "object" && typeof data.id === "string" && typeof data.type === "string" && typeof data.method === "string";
|
|
279
|
+
}
|
|
280
|
+
async handleMessage(event) {
|
|
281
|
+
if (!this.isValidPostMessageRequest(event.data)) {
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (!event.origin || event.origin === "null") {
|
|
285
|
+
console.error("[PostMessageServer] Invalid or null origin received");
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
const request = event.data;
|
|
289
|
+
const rnMode = isReactNativeWebView();
|
|
290
|
+
const reply = (response) => {
|
|
291
|
+
if (rnMode) {
|
|
292
|
+
sendReactNativeReply(response);
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
if (event.source && event.origin) {
|
|
296
|
+
event.source.postMessage(response, { targetOrigin: event.origin });
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
if (this.originValidator && !rnMode) {
|
|
300
|
+
const publishableKey = request.payload?.publishableKey;
|
|
301
|
+
try {
|
|
302
|
+
const isAllowed = await this.originValidator(
|
|
303
|
+
event.origin,
|
|
304
|
+
publishableKey
|
|
305
|
+
);
|
|
306
|
+
if (!isAllowed) {
|
|
307
|
+
reply({
|
|
308
|
+
id: request.id,
|
|
309
|
+
type: request.type,
|
|
310
|
+
success: false,
|
|
311
|
+
error: `Origin ${event.origin} is not whitelisted. Please add it to your MoonKey dashboard.`
|
|
312
|
+
});
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
} catch (error) {
|
|
316
|
+
reply({
|
|
317
|
+
id: request.id,
|
|
318
|
+
type: request.type,
|
|
319
|
+
success: false,
|
|
320
|
+
error: "Origin validation failed"
|
|
321
|
+
});
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
const key = `${request.type}:${request.method}`;
|
|
326
|
+
const handler = this.handlers.get(key);
|
|
327
|
+
if (handler) {
|
|
328
|
+
try {
|
|
329
|
+
const data = await handler(request.payload);
|
|
330
|
+
reply({
|
|
331
|
+
id: request.id,
|
|
332
|
+
type: request.type,
|
|
333
|
+
success: true,
|
|
334
|
+
data
|
|
335
|
+
});
|
|
336
|
+
} catch (error) {
|
|
337
|
+
const extras = {};
|
|
338
|
+
if (error && typeof error === "object") {
|
|
339
|
+
for (const key2 of FORWARDED_ERROR_FIELDS) {
|
|
340
|
+
const value = error[key2];
|
|
341
|
+
if (value !== void 0) extras[key2] = value;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
reply({
|
|
345
|
+
...extras,
|
|
346
|
+
id: request.id,
|
|
347
|
+
type: request.type,
|
|
348
|
+
success: false,
|
|
349
|
+
error: error instanceof Error ? error.message : "Unknown error"
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
356
|
+
0 && (module.exports = {
|
|
357
|
+
FORWARDED_ERROR_FIELDS,
|
|
358
|
+
PostMessageClient,
|
|
359
|
+
PostMessageMethod,
|
|
360
|
+
PostMessageServer,
|
|
361
|
+
PostMessageType,
|
|
362
|
+
getIframeOrigin,
|
|
363
|
+
getIframeUrl,
|
|
364
|
+
getMoonKeyApiUrl
|
|
365
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import "../chunk-CMYR6AOY.mjs";
|
|
2
|
+
import {
|
|
3
|
+
getIframeOrigin,
|
|
4
|
+
getIframeUrl
|
|
5
|
+
} from "../chunk-264CEGDS.mjs";
|
|
6
|
+
import {
|
|
7
|
+
FORWARDED_ERROR_FIELDS,
|
|
8
|
+
PostMessageClient,
|
|
9
|
+
PostMessageMethod,
|
|
10
|
+
PostMessageServer,
|
|
11
|
+
PostMessageType
|
|
12
|
+
} from "../chunk-SOKLPX7V.mjs";
|
|
13
|
+
import {
|
|
14
|
+
getMoonKeyApiUrl
|
|
15
|
+
} from "../chunk-GQKIA37O.mjs";
|
|
16
|
+
export {
|
|
17
|
+
FORWARDED_ERROR_FIELDS,
|
|
18
|
+
PostMessageClient,
|
|
19
|
+
PostMessageMethod,
|
|
20
|
+
PostMessageServer,
|
|
21
|
+
PostMessageType,
|
|
22
|
+
getIframeOrigin,
|
|
23
|
+
getIframeUrl,
|
|
24
|
+
getMoonKeyApiUrl
|
|
25
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { P as PostMessageClient } from '../post-message-C_99BCBh.mjs';
|
|
2
|
+
import { T as Transport, P as PasskeyAssertor, K as KvStorage } from '../interfaces-fNhwnCeY.mjs';
|
|
3
|
+
|
|
4
|
+
declare const getCachedPostMessageClient: (contentWindow: Window) => PostMessageClient;
|
|
5
|
+
/**
|
|
6
|
+
* Create a web Transport bound to an iframe's contentWindow. Pass a
|
|
7
|
+
* getter for the iframe ref so the Transport survives mounts/remounts
|
|
8
|
+
* (the iframe's contentWindow can change). `isReady` short-circuits
|
|
9
|
+
* the SDK methods before they try to send to a not-yet-mounted iframe.
|
|
10
|
+
*/
|
|
11
|
+
declare const createIframeTransport: ({ getContentWindow, isReady, }: {
|
|
12
|
+
getContentWindow: () => Window | null | undefined;
|
|
13
|
+
isReady: () => boolean;
|
|
14
|
+
}) => Transport;
|
|
15
|
+
|
|
16
|
+
declare const createWebAuthnPasskey: () => PasskeyAssertor;
|
|
17
|
+
|
|
18
|
+
declare const createLocalStorageKv: (prefix?: string) => KvStorage;
|
|
19
|
+
|
|
20
|
+
export { createIframeTransport, createLocalStorageKv, createWebAuthnPasskey, getCachedPostMessageClient };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { P as PostMessageClient } from '../post-message-C_99BCBh.js';
|
|
2
|
+
import { T as Transport, P as PasskeyAssertor, K as KvStorage } from '../interfaces-9k0eKCc4.js';
|
|
3
|
+
|
|
4
|
+
declare const getCachedPostMessageClient: (contentWindow: Window) => PostMessageClient;
|
|
5
|
+
/**
|
|
6
|
+
* Create a web Transport bound to an iframe's contentWindow. Pass a
|
|
7
|
+
* getter for the iframe ref so the Transport survives mounts/remounts
|
|
8
|
+
* (the iframe's contentWindow can change). `isReady` short-circuits
|
|
9
|
+
* the SDK methods before they try to send to a not-yet-mounted iframe.
|
|
10
|
+
*/
|
|
11
|
+
declare const createIframeTransport: ({ getContentWindow, isReady, }: {
|
|
12
|
+
getContentWindow: () => Window | null | undefined;
|
|
13
|
+
isReady: () => boolean;
|
|
14
|
+
}) => Transport;
|
|
15
|
+
|
|
16
|
+
declare const createWebAuthnPasskey: () => PasskeyAssertor;
|
|
17
|
+
|
|
18
|
+
declare const createLocalStorageKv: (prefix?: string) => KvStorage;
|
|
19
|
+
|
|
20
|
+
export { createIframeTransport, createLocalStorageKv, createWebAuthnPasskey, getCachedPostMessageClient };
|