@moon-x/react-native-sdk 0.0.1
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 +462 -0
- package/app.plugin.js +4 -0
- package/dist/ethereum.d.mts +2 -0
- package/dist/ethereum.d.ts +2 -0
- package/dist/ethereum.js +42 -0
- package/dist/ethereum.mjs +19 -0
- package/dist/index.d.mts +273 -0
- package/dist/index.d.ts +273 -0
- package/dist/index.js +1008 -0
- package/dist/index.mjs +982 -0
- package/dist/solana.d.mts +1 -0
- package/dist/solana.d.ts +1 -0
- package/dist/solana.js +36 -0
- package/dist/solana.mjs +13 -0
- package/package.json +85 -0
- package/plugin/index.js +172 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,982 @@
|
|
|
1
|
+
// src/provider.tsx
|
|
2
|
+
import {
|
|
3
|
+
createContext,
|
|
4
|
+
useCallback,
|
|
5
|
+
useContext,
|
|
6
|
+
useEffect,
|
|
7
|
+
useMemo,
|
|
8
|
+
useRef,
|
|
9
|
+
useState
|
|
10
|
+
} from "react";
|
|
11
|
+
import {
|
|
12
|
+
Animated,
|
|
13
|
+
Platform,
|
|
14
|
+
Pressable,
|
|
15
|
+
StyleSheet,
|
|
16
|
+
Text,
|
|
17
|
+
useWindowDimensions,
|
|
18
|
+
View
|
|
19
|
+
} from "react-native";
|
|
20
|
+
import { WebView } from "react-native-webview";
|
|
21
|
+
|
|
22
|
+
// src/sdk/sdk-authentication-methods.ts
|
|
23
|
+
import { getHeadlessAuthenticationMethods } from "@moon-x/core/sdk";
|
|
24
|
+
var getAuthenticationMethods = (bridge, publishableKey, emailConfig, setIsAuthenticated, setUser) => getHeadlessAuthenticationMethods({
|
|
25
|
+
transport: bridge,
|
|
26
|
+
publishableKey,
|
|
27
|
+
emailConfig,
|
|
28
|
+
setIsAuthenticated,
|
|
29
|
+
setUser
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// src/sdk/sdk-ethereum-methods.ts
|
|
33
|
+
import { getHeadlessEthereumMethods } from "@moon-x/core/sdk";
|
|
34
|
+
|
|
35
|
+
// src/platform/async-storage-kv.ts
|
|
36
|
+
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
37
|
+
var createAsyncStorageKv = (prefix = "moonkey:") => ({
|
|
38
|
+
get: async (key) => AsyncStorage.getItem(prefix + key),
|
|
39
|
+
set: async (key, value) => AsyncStorage.setItem(prefix + key, value),
|
|
40
|
+
remove: async (key) => AsyncStorage.removeItem(prefix + key)
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// src/platform/rn-passkey.ts
|
|
44
|
+
import { create as passkeyCreate, get as passkeyGet } from "react-native-passkeys";
|
|
45
|
+
import { toB64url } from "@moon-x/core/lib";
|
|
46
|
+
var createRNPasskey = () => ({
|
|
47
|
+
assert: async ({ rpId, challenge }) => {
|
|
48
|
+
const credential = await passkeyGet({
|
|
49
|
+
challenge,
|
|
50
|
+
rpId,
|
|
51
|
+
allowCredentials: [],
|
|
52
|
+
userVerification: "preferred"
|
|
53
|
+
});
|
|
54
|
+
if (!credential) throw new Error("passkey_assertion_cancelled");
|
|
55
|
+
const userHandle = credential.response.userHandle;
|
|
56
|
+
if (!userHandle) throw new Error("user_handle_missing");
|
|
57
|
+
return {
|
|
58
|
+
rawIdB64url: credential.rawId,
|
|
59
|
+
userHandleB64url: userHandle
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
create: async ({
|
|
63
|
+
rpId,
|
|
64
|
+
rpName,
|
|
65
|
+
userId,
|
|
66
|
+
userName,
|
|
67
|
+
userDisplayName,
|
|
68
|
+
challenge,
|
|
69
|
+
excludeCredentials
|
|
70
|
+
}) => {
|
|
71
|
+
const credential = await passkeyCreate({
|
|
72
|
+
challenge,
|
|
73
|
+
rp: { id: rpId, name: rpName },
|
|
74
|
+
user: {
|
|
75
|
+
id: toB64url(userId),
|
|
76
|
+
name: userName,
|
|
77
|
+
displayName: userDisplayName
|
|
78
|
+
},
|
|
79
|
+
pubKeyCredParams: [
|
|
80
|
+
{ type: "public-key", alg: -7 },
|
|
81
|
+
{ type: "public-key", alg: -257 }
|
|
82
|
+
],
|
|
83
|
+
authenticatorSelection: {
|
|
84
|
+
residentKey: "required",
|
|
85
|
+
userVerification: "preferred"
|
|
86
|
+
},
|
|
87
|
+
excludeCredentials: (excludeCredentials ?? []).map((id) => ({
|
|
88
|
+
id,
|
|
89
|
+
type: "public-key"
|
|
90
|
+
}))
|
|
91
|
+
});
|
|
92
|
+
if (!credential) throw new Error("passkey_registration_cancelled");
|
|
93
|
+
return {
|
|
94
|
+
rawIdB64url: credential.rawId,
|
|
95
|
+
clientDataJSONB64url: credential.response.clientDataJSON,
|
|
96
|
+
attestationObjectB64url: credential.response.attestationObject
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// src/sdk/sdk-ethereum-methods.ts
|
|
102
|
+
var getEthereumSDKMethods = (bridge, publishableKey) => getHeadlessEthereumMethods({
|
|
103
|
+
transport: bridge,
|
|
104
|
+
passkey: createRNPasskey(),
|
|
105
|
+
publishableKey
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// src/sdk/sdk-passkey-methods.ts
|
|
109
|
+
import { generatePasskeyUserId, toB64url as toB64url2 } from "@moon-x/core/lib";
|
|
110
|
+
import { assertPasskeyInParent } from "@moon-x/core/sdk";
|
|
111
|
+
import { PostMessageMethod, PostMessageType } from "@moon-x/core/utils";
|
|
112
|
+
import { create as passkeyCreate2 } from "react-native-passkeys";
|
|
113
|
+
var getPasskeyMethods = (bridge, publishableKey) => ({
|
|
114
|
+
registerPasskey: async (params) => {
|
|
115
|
+
const dekInfo = await bridge.send(
|
|
116
|
+
PostMessageType.AUTH,
|
|
117
|
+
PostMessageMethod.GET_DEK_INFO,
|
|
118
|
+
{ publishableKey }
|
|
119
|
+
);
|
|
120
|
+
if (!dekInfo.rp_id) {
|
|
121
|
+
throw new Error("webauthn_rp_id_not_configured");
|
|
122
|
+
}
|
|
123
|
+
const rpId = dekInfo.rp_id;
|
|
124
|
+
const relyingPartyOrigin = `https://${rpId}`;
|
|
125
|
+
const isAdditional = (dekInfo.allow_credential_ids_b64url ?? []).length > 0;
|
|
126
|
+
let unlock = null;
|
|
127
|
+
if (isAdditional) {
|
|
128
|
+
unlock = await assertPasskeyInParent({
|
|
129
|
+
transport: bridge,
|
|
130
|
+
passkey: createRNPasskey(),
|
|
131
|
+
publishableKey,
|
|
132
|
+
requireFreshAssertion: true
|
|
133
|
+
});
|
|
134
|
+
const allowList = dekInfo.allow_credential_ids_b64url ?? [];
|
|
135
|
+
if (!allowList.includes(unlock.credentialIdB64url)) {
|
|
136
|
+
throw new Error("unknown_passkey_for_this_account");
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const beginResult = await bridge.send(
|
|
140
|
+
PostMessageType.AUTH,
|
|
141
|
+
PostMessageMethod.PASSKEY_REGISTER_BEGIN,
|
|
142
|
+
{
|
|
143
|
+
publishableKey,
|
|
144
|
+
relyingPartyOrigin,
|
|
145
|
+
displayName: params?.displayName,
|
|
146
|
+
additional: isAdditional
|
|
147
|
+
}
|
|
148
|
+
);
|
|
149
|
+
const options = beginResult.optionsJSON;
|
|
150
|
+
const token = beginResult.token;
|
|
151
|
+
if (!options || !token) {
|
|
152
|
+
throw new Error("passkey_register_begin_no_options");
|
|
153
|
+
}
|
|
154
|
+
const { userIdB64url } = generatePasskeyUserId();
|
|
155
|
+
options.user.id = userIdB64url;
|
|
156
|
+
if (!options.challenge) {
|
|
157
|
+
const challengeBytes = crypto.getRandomValues(new Uint8Array(32));
|
|
158
|
+
options.challenge = toB64url2(challengeBytes);
|
|
159
|
+
}
|
|
160
|
+
const credential = await passkeyCreate2(
|
|
161
|
+
options
|
|
162
|
+
);
|
|
163
|
+
if (!credential) throw new Error("passkey_register_cancelled");
|
|
164
|
+
await bridge.send(
|
|
165
|
+
PostMessageType.AUTH,
|
|
166
|
+
PostMessageMethod.PASSKEY_REGISTER_VERIFY,
|
|
167
|
+
{
|
|
168
|
+
publishableKey,
|
|
169
|
+
relyingPartyOrigin,
|
|
170
|
+
token,
|
|
171
|
+
credential,
|
|
172
|
+
userIdB64url
|
|
173
|
+
}
|
|
174
|
+
);
|
|
175
|
+
if (isAdditional && unlock) {
|
|
176
|
+
await bridge.send(
|
|
177
|
+
PostMessageType.AUTH,
|
|
178
|
+
PostMessageMethod.ADD_PASSKEY_WRAP,
|
|
179
|
+
{
|
|
180
|
+
publishableKey,
|
|
181
|
+
unlockUserHandleB64url: unlock.userHandleB64url,
|
|
182
|
+
unlockCredentialIdB64url: unlock.credentialIdB64url,
|
|
183
|
+
newUserHandleB64url: userIdB64url,
|
|
184
|
+
newCredentialIdB64url: credential.rawId
|
|
185
|
+
}
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
return { credential_id: credential.rawId };
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// src/sdk/sdk-methods.ts
|
|
193
|
+
import { getHeadlessGeneralMethods } from "@moon-x/core/sdk";
|
|
194
|
+
var getSDKMethods = (bridge, publishableKey, setIsAuthenticated, setUser) => getHeadlessGeneralMethods({
|
|
195
|
+
transport: bridge,
|
|
196
|
+
passkey: createRNPasskey(),
|
|
197
|
+
storage: createAsyncStorageKv(),
|
|
198
|
+
publishableKey,
|
|
199
|
+
setIsAuthenticated,
|
|
200
|
+
setUser
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// src/sdk/sdk-solana-methods.ts
|
|
204
|
+
import { getHeadlessSolanaMethods } from "@moon-x/core/sdk";
|
|
205
|
+
var getSolanaSDKMethods = (bridge, publishableKey) => getHeadlessSolanaMethods({
|
|
206
|
+
transport: bridge,
|
|
207
|
+
passkey: createRNPasskey(),
|
|
208
|
+
publishableKey
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
// src/sdk/create-sdk-instance.ts
|
|
212
|
+
var createSdkInstance = ({
|
|
213
|
+
bridge,
|
|
214
|
+
publishableKey,
|
|
215
|
+
emailConfig,
|
|
216
|
+
isAuthenticated,
|
|
217
|
+
user,
|
|
218
|
+
ready,
|
|
219
|
+
setIsAuthenticated,
|
|
220
|
+
setUser,
|
|
221
|
+
refreshUser
|
|
222
|
+
}) => ({
|
|
223
|
+
publishableKey,
|
|
224
|
+
ready,
|
|
225
|
+
isAuthenticated,
|
|
226
|
+
user,
|
|
227
|
+
refreshUser,
|
|
228
|
+
...getAuthenticationMethods(
|
|
229
|
+
bridge,
|
|
230
|
+
publishableKey,
|
|
231
|
+
emailConfig,
|
|
232
|
+
setIsAuthenticated,
|
|
233
|
+
setUser
|
|
234
|
+
),
|
|
235
|
+
...getSDKMethods(bridge, publishableKey, setIsAuthenticated, setUser),
|
|
236
|
+
...getPasskeyMethods(bridge, publishableKey),
|
|
237
|
+
...getEthereumSDKMethods(bridge, publishableKey),
|
|
238
|
+
...getSolanaSDKMethods(bridge, publishableKey)
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
// src/provider.tsx
|
|
242
|
+
import { setAssertCacheTtlMs } from "@moon-x/core/lib";
|
|
243
|
+
import { PostMessageMethod as PostMessageMethod2, PostMessageType as PostMessageType2 } from "@moon-x/core/utils";
|
|
244
|
+
|
|
245
|
+
// src/transport/webview-bridge.ts
|
|
246
|
+
import {
|
|
247
|
+
FORWARDED_ERROR_FIELDS
|
|
248
|
+
} from "@moon-x/core/utils";
|
|
249
|
+
var REQUEST_TIMEOUT_MS = 3e4;
|
|
250
|
+
var generateUUID = () => {
|
|
251
|
+
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
252
|
+
return crypto.randomUUID();
|
|
253
|
+
}
|
|
254
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
255
|
+
const r = Math.random() * 16 | 0;
|
|
256
|
+
const v = c === "x" ? r : r & 3 | 8;
|
|
257
|
+
return v.toString(16);
|
|
258
|
+
});
|
|
259
|
+
};
|
|
260
|
+
var bigintReplacer = (_key, value) => typeof value === "bigint" ? value.toString() : value;
|
|
261
|
+
var buildInjectScript = (envelope) => {
|
|
262
|
+
const payload = JSON.stringify(envelope, bigintReplacer);
|
|
263
|
+
return `
|
|
264
|
+
(function() {
|
|
265
|
+
try {
|
|
266
|
+
var msg = JSON.parse(${JSON.stringify(payload)});
|
|
267
|
+
var event;
|
|
268
|
+
try {
|
|
269
|
+
event = new MessageEvent('message', {
|
|
270
|
+
data: msg,
|
|
271
|
+
origin: window.location.origin,
|
|
272
|
+
source: window
|
|
273
|
+
});
|
|
274
|
+
} catch (e) {
|
|
275
|
+
event = document.createEvent('MessageEvent');
|
|
276
|
+
event.initMessageEvent(
|
|
277
|
+
'message',
|
|
278
|
+
false,
|
|
279
|
+
false,
|
|
280
|
+
msg,
|
|
281
|
+
window.location.origin,
|
|
282
|
+
'',
|
|
283
|
+
window,
|
|
284
|
+
null
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
window.dispatchEvent(event);
|
|
288
|
+
} catch (e) {
|
|
289
|
+
if (window.ReactNativeWebView) {
|
|
290
|
+
window.ReactNativeWebView.postMessage(JSON.stringify({
|
|
291
|
+
__moonx_bridge_error: true,
|
|
292
|
+
error: String(e && e.message || e)
|
|
293
|
+
}));
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
})();
|
|
297
|
+
true;
|
|
298
|
+
`;
|
|
299
|
+
};
|
|
300
|
+
var WebViewBridge = class {
|
|
301
|
+
constructor(webViewRef) {
|
|
302
|
+
this.pending = /* @__PURE__ */ new Map();
|
|
303
|
+
this.oneWayHandlers = /* @__PURE__ */ new Map();
|
|
304
|
+
// Wire to <WebView onMessage={bridge.handleMessage}>. Demultiplexes
|
|
305
|
+
// by request id; one-way messages (no id, or unknown id) fall through
|
|
306
|
+
// to handlers registered via `register`.
|
|
307
|
+
this.handleMessage = (event) => {
|
|
308
|
+
let parsed;
|
|
309
|
+
try {
|
|
310
|
+
parsed = JSON.parse(event.nativeEvent.data);
|
|
311
|
+
} catch {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
if (!parsed || typeof parsed !== "object") return;
|
|
315
|
+
const message = parsed;
|
|
316
|
+
if (typeof message.id === "string" && this.pending.has(message.id)) {
|
|
317
|
+
const resolver = this.pending.get(message.id);
|
|
318
|
+
this.pending.delete(message.id);
|
|
319
|
+
clearTimeout(resolver.timeoutId);
|
|
320
|
+
if (message.success) {
|
|
321
|
+
resolver.resolve(message.data);
|
|
322
|
+
} else {
|
|
323
|
+
const err = new Error(message.error || "WebView bridge error");
|
|
324
|
+
for (const key of FORWARDED_ERROR_FIELDS) {
|
|
325
|
+
const value = message[key];
|
|
326
|
+
if (value !== void 0) {
|
|
327
|
+
err[key] = value;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
resolver.reject(err);
|
|
331
|
+
}
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
if (typeof message.type === "string" && typeof message.method === "string") {
|
|
335
|
+
const key = `${message.type}:${message.method}`;
|
|
336
|
+
const handler = this.oneWayHandlers.get(key);
|
|
337
|
+
if (handler) handler(message.payload);
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
this.webViewRef = webViewRef;
|
|
341
|
+
}
|
|
342
|
+
// Subscribe to a one-way message kind. Returns an unsubscribe fn.
|
|
343
|
+
register(type, method, handler) {
|
|
344
|
+
const key = `${type}:${method}`;
|
|
345
|
+
this.oneWayHandlers.set(key, handler);
|
|
346
|
+
return () => {
|
|
347
|
+
if (this.oneWayHandlers.get(key) === handler) {
|
|
348
|
+
this.oneWayHandlers.delete(key);
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
// Conforms to the `Transport.isReady()` half of @moon-x/core/sdk's
|
|
353
|
+
// interface — used by shared SDK factories to short-circuit before
|
|
354
|
+
// attempting to inject JS into a not-yet-mounted WebView.
|
|
355
|
+
isReady() {
|
|
356
|
+
return !!this.webViewRef.current;
|
|
357
|
+
}
|
|
358
|
+
// Request/response. Same envelope as @moon-x/core PostMessageClient.
|
|
359
|
+
send(type, method, payload) {
|
|
360
|
+
const ref = this.webViewRef.current;
|
|
361
|
+
if (!ref) {
|
|
362
|
+
return Promise.reject(new Error("webview_not_ready"));
|
|
363
|
+
}
|
|
364
|
+
const id = generateUUID();
|
|
365
|
+
return new Promise((resolve, reject) => {
|
|
366
|
+
const timeoutId = setTimeout(() => {
|
|
367
|
+
this.pending.delete(id);
|
|
368
|
+
reject(new Error("Timeout: No response from WebView"));
|
|
369
|
+
}, REQUEST_TIMEOUT_MS);
|
|
370
|
+
this.pending.set(id, {
|
|
371
|
+
resolve,
|
|
372
|
+
reject,
|
|
373
|
+
timeoutId
|
|
374
|
+
});
|
|
375
|
+
const envelope = { id, type, method, payload };
|
|
376
|
+
try {
|
|
377
|
+
ref.injectJavaScript(buildInjectScript(envelope));
|
|
378
|
+
} catch (error) {
|
|
379
|
+
this.pending.delete(id);
|
|
380
|
+
clearTimeout(timeoutId);
|
|
381
|
+
reject(
|
|
382
|
+
error instanceof Error ? error : new Error("inject_javascript_failed")
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
// Fire-and-forget. The iframe-app may or may not reply; we don't
|
|
388
|
+
// hold a resolver. Used for things like CLOSE / cleanup signals.
|
|
389
|
+
sendOneWay(type, method, payload) {
|
|
390
|
+
const ref = this.webViewRef.current;
|
|
391
|
+
if (!ref) return;
|
|
392
|
+
const id = generateUUID();
|
|
393
|
+
const envelope = { id, type, method, payload };
|
|
394
|
+
try {
|
|
395
|
+
ref.injectJavaScript(buildInjectScript(envelope));
|
|
396
|
+
} catch {
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
// Reject every outstanding request — wire on unmount so dangling
|
|
400
|
+
// promises don't dangle forever.
|
|
401
|
+
cleanup(reason = "webview_unmounted") {
|
|
402
|
+
for (const [id, resolver] of this.pending.entries()) {
|
|
403
|
+
clearTimeout(resolver.timeoutId);
|
|
404
|
+
resolver.reject(new Error(reason));
|
|
405
|
+
this.pending.delete(id);
|
|
406
|
+
}
|
|
407
|
+
this.oneWayHandlers.clear();
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
// src/provider.tsx
|
|
412
|
+
import { SDKProvider } from "@moon-x/core/react";
|
|
413
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
414
|
+
var DEFAULT_IFRAME_URL = "https://iframe.moonx-dev.com";
|
|
415
|
+
var MoonKeyContext = createContext(null);
|
|
416
|
+
var MoonKeyProvider = ({
|
|
417
|
+
publishableKey,
|
|
418
|
+
config,
|
|
419
|
+
children
|
|
420
|
+
}) => {
|
|
421
|
+
const iframeUrl = config?.iframeUrl ?? DEFAULT_IFRAME_URL;
|
|
422
|
+
setAssertCacheTtlMs(config?.security?.assertionCacheTtlMs ?? 0);
|
|
423
|
+
const webViewRef = useRef(null);
|
|
424
|
+
const bridge = useMemo(
|
|
425
|
+
() => new WebViewBridge(
|
|
426
|
+
webViewRef
|
|
427
|
+
),
|
|
428
|
+
[]
|
|
429
|
+
);
|
|
430
|
+
const [sensitiveVisible, setSensitiveVisible] = useState(false);
|
|
431
|
+
const cancelRef = useRef(null);
|
|
432
|
+
const [ready, setReady] = useState(false);
|
|
433
|
+
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
|
434
|
+
const [user, setUser] = useState(null);
|
|
435
|
+
const { height: screenHeight } = useWindowDimensions();
|
|
436
|
+
const SHEET_TOP_OFFSET = 80;
|
|
437
|
+
const sheetTranslateY = useRef(new Animated.Value(screenHeight)).current;
|
|
438
|
+
const backdropOpacity = useRef(new Animated.Value(0)).current;
|
|
439
|
+
useEffect(() => {
|
|
440
|
+
Animated.parallel([
|
|
441
|
+
Animated.timing(sheetTranslateY, {
|
|
442
|
+
toValue: sensitiveVisible ? 0 : screenHeight,
|
|
443
|
+
duration: 280,
|
|
444
|
+
useNativeDriver: true
|
|
445
|
+
}),
|
|
446
|
+
Animated.timing(backdropOpacity, {
|
|
447
|
+
toValue: sensitiveVisible ? 0.5 : 0,
|
|
448
|
+
duration: 280,
|
|
449
|
+
useNativeDriver: true
|
|
450
|
+
})
|
|
451
|
+
]).start();
|
|
452
|
+
}, [sensitiveVisible, sheetTranslateY, backdropOpacity, screenHeight]);
|
|
453
|
+
const initialSourceUri = useMemo(() => {
|
|
454
|
+
const base = iframeUrl.replace(/\/$/, "");
|
|
455
|
+
const sep = base.includes("?") ? "&" : "?";
|
|
456
|
+
return `${base}${sep}publishableKey=${encodeURIComponent(publishableKey)}`;
|
|
457
|
+
}, [iframeUrl, publishableKey]);
|
|
458
|
+
const handleMessage = useCallback(
|
|
459
|
+
(event) => {
|
|
460
|
+
bridge.handleMessage({ nativeEvent: { data: event.nativeEvent.data } });
|
|
461
|
+
},
|
|
462
|
+
[bridge]
|
|
463
|
+
);
|
|
464
|
+
const refreshUser = useCallback(async () => {
|
|
465
|
+
try {
|
|
466
|
+
const fetched = await sdkRef.current.getUser();
|
|
467
|
+
if (fetched) setUser(fetched);
|
|
468
|
+
} catch {
|
|
469
|
+
}
|
|
470
|
+
}, []);
|
|
471
|
+
const navigateWebView = useCallback(
|
|
472
|
+
(path) => {
|
|
473
|
+
const ref = webViewRef.current;
|
|
474
|
+
if (!ref) return;
|
|
475
|
+
const base = iframeUrl.replace(/\/$/, "");
|
|
476
|
+
const cleanPath = path.startsWith("/") ? path : `/${path}`;
|
|
477
|
+
const hasKey = /[?&]publishableKey=/.test(cleanPath);
|
|
478
|
+
const sep = cleanPath.includes("?") ? "&" : "?";
|
|
479
|
+
const target = hasKey ? `${base}${cleanPath}` : `${base}${cleanPath}${sep}publishableKey=${encodeURIComponent(publishableKey)}`;
|
|
480
|
+
ref.injectJavaScript(
|
|
481
|
+
`window.location.href = ${JSON.stringify(target)}; true;`
|
|
482
|
+
);
|
|
483
|
+
},
|
|
484
|
+
[iframeUrl, publishableKey]
|
|
485
|
+
);
|
|
486
|
+
const runSensitiveFlow = useCallback(
|
|
487
|
+
(options) => {
|
|
488
|
+
setSensitiveVisible(true);
|
|
489
|
+
navigateWebView(options.flowPath);
|
|
490
|
+
return new Promise((resolve) => {
|
|
491
|
+
let finished = false;
|
|
492
|
+
const finish = () => {
|
|
493
|
+
if (finished) return;
|
|
494
|
+
finished = true;
|
|
495
|
+
setSensitiveVisible(false);
|
|
496
|
+
navigateWebView("/");
|
|
497
|
+
if (cancelRef.current === finish) cancelRef.current = null;
|
|
498
|
+
resolve();
|
|
499
|
+
};
|
|
500
|
+
cancelRef.current = finish;
|
|
501
|
+
if (options.onMount) {
|
|
502
|
+
Promise.resolve(options.onMount({ bridge, cancel: finish })).catch(
|
|
503
|
+
() => {
|
|
504
|
+
}
|
|
505
|
+
);
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
},
|
|
509
|
+
[bridge, navigateWebView]
|
|
510
|
+
);
|
|
511
|
+
const sdk = useMemo(
|
|
512
|
+
() => createSdkInstance({
|
|
513
|
+
bridge,
|
|
514
|
+
publishableKey,
|
|
515
|
+
emailConfig: config?.emailConfig,
|
|
516
|
+
isAuthenticated,
|
|
517
|
+
user,
|
|
518
|
+
ready,
|
|
519
|
+
setIsAuthenticated,
|
|
520
|
+
setUser,
|
|
521
|
+
refreshUser
|
|
522
|
+
}),
|
|
523
|
+
[
|
|
524
|
+
bridge,
|
|
525
|
+
publishableKey,
|
|
526
|
+
config?.emailConfig,
|
|
527
|
+
isAuthenticated,
|
|
528
|
+
user,
|
|
529
|
+
ready,
|
|
530
|
+
refreshUser
|
|
531
|
+
]
|
|
532
|
+
);
|
|
533
|
+
const sdkRef = useRef(sdk);
|
|
534
|
+
useEffect(() => {
|
|
535
|
+
sdkRef.current = sdk;
|
|
536
|
+
}, [sdk]);
|
|
537
|
+
useEffect(() => {
|
|
538
|
+
return () => {
|
|
539
|
+
bridge.cleanup("provider_unmounted");
|
|
540
|
+
};
|
|
541
|
+
}, [bridge]);
|
|
542
|
+
useEffect(() => {
|
|
543
|
+
let cancelled = false;
|
|
544
|
+
const unregister = bridge.register(
|
|
545
|
+
PostMessageType2.INIT,
|
|
546
|
+
PostMessageMethod2.READY,
|
|
547
|
+
() => {
|
|
548
|
+
if (!cancelled) setReady(true);
|
|
549
|
+
}
|
|
550
|
+
);
|
|
551
|
+
const fallback = setTimeout(() => {
|
|
552
|
+
if (!cancelled) setReady(true);
|
|
553
|
+
}, 2e3);
|
|
554
|
+
return () => {
|
|
555
|
+
cancelled = true;
|
|
556
|
+
unregister();
|
|
557
|
+
clearTimeout(fallback);
|
|
558
|
+
};
|
|
559
|
+
}, [bridge]);
|
|
560
|
+
useEffect(() => {
|
|
561
|
+
if (!ready || isAuthenticated) return;
|
|
562
|
+
let cancelled = false;
|
|
563
|
+
let attempt = 0;
|
|
564
|
+
const maxAttempts = 4;
|
|
565
|
+
const tryRestore = async () => {
|
|
566
|
+
if (cancelled) return;
|
|
567
|
+
attempt += 1;
|
|
568
|
+
try {
|
|
569
|
+
const session = await sdkRef.current.getCurrentSession();
|
|
570
|
+
if (cancelled) return;
|
|
571
|
+
if (session) {
|
|
572
|
+
setIsAuthenticated(true);
|
|
573
|
+
try {
|
|
574
|
+
const u = await sdkRef.current.getUser();
|
|
575
|
+
if (!cancelled && u) setUser(u);
|
|
576
|
+
} catch {
|
|
577
|
+
}
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
if (attempt < maxAttempts) {
|
|
581
|
+
const delay = 200 * 2 ** (attempt - 1);
|
|
582
|
+
setTimeout(tryRestore, delay);
|
|
583
|
+
}
|
|
584
|
+
} catch {
|
|
585
|
+
if (!cancelled && attempt < maxAttempts) {
|
|
586
|
+
const delay = 200 * 2 ** (attempt - 1);
|
|
587
|
+
setTimeout(tryRestore, delay);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
tryRestore();
|
|
592
|
+
return () => {
|
|
593
|
+
cancelled = true;
|
|
594
|
+
};
|
|
595
|
+
}, [ready, isAuthenticated]);
|
|
596
|
+
const contextValue = useMemo(
|
|
597
|
+
() => ({
|
|
598
|
+
sdk,
|
|
599
|
+
_bridge: bridge,
|
|
600
|
+
_runSensitiveFlow: runSensitiveFlow,
|
|
601
|
+
_oauthRedirectUri: config?.oauthRedirectUri
|
|
602
|
+
}),
|
|
603
|
+
[sdk, bridge, runSensitiveFlow, config?.oauthRedirectUri]
|
|
604
|
+
);
|
|
605
|
+
const closeLabel = config?.sensitiveModalCopy?.closeLabel ?? "Done";
|
|
606
|
+
return /* @__PURE__ */ jsx(MoonKeyContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsxs(SDKProvider, { value: sdk, children: [
|
|
607
|
+
children,
|
|
608
|
+
/* @__PURE__ */ jsx(
|
|
609
|
+
Animated.View,
|
|
610
|
+
{
|
|
611
|
+
style: [styles.backdrop, { opacity: backdropOpacity }],
|
|
612
|
+
pointerEvents: sensitiveVisible ? "auto" : "none",
|
|
613
|
+
children: /* @__PURE__ */ jsx(
|
|
614
|
+
Pressable,
|
|
615
|
+
{
|
|
616
|
+
style: StyleSheet.absoluteFill,
|
|
617
|
+
onPress: () => cancelRef.current?.()
|
|
618
|
+
}
|
|
619
|
+
)
|
|
620
|
+
}
|
|
621
|
+
),
|
|
622
|
+
/* @__PURE__ */ jsxs(
|
|
623
|
+
Animated.View,
|
|
624
|
+
{
|
|
625
|
+
style: [
|
|
626
|
+
styles.sheetContainer,
|
|
627
|
+
{
|
|
628
|
+
top: SHEET_TOP_OFFSET,
|
|
629
|
+
transform: [{ translateY: sheetTranslateY }]
|
|
630
|
+
}
|
|
631
|
+
],
|
|
632
|
+
pointerEvents: sensitiveVisible ? "auto" : "none",
|
|
633
|
+
children: [
|
|
634
|
+
/* @__PURE__ */ jsx(View, { style: styles.sheetGrabber }),
|
|
635
|
+
/* @__PURE__ */ jsxs(View, { style: styles.modalChrome, children: [
|
|
636
|
+
/* @__PURE__ */ jsx(View, { style: styles.modalTitleSpacer }),
|
|
637
|
+
/* @__PURE__ */ jsx(
|
|
638
|
+
Pressable,
|
|
639
|
+
{
|
|
640
|
+
onPress: () => cancelRef.current?.(),
|
|
641
|
+
hitSlop: 12,
|
|
642
|
+
style: styles.closeButton,
|
|
643
|
+
children: /* @__PURE__ */ jsx(Text, { style: styles.closeButtonText, children: closeLabel })
|
|
644
|
+
}
|
|
645
|
+
)
|
|
646
|
+
] }),
|
|
647
|
+
/* @__PURE__ */ jsx(
|
|
648
|
+
WebView,
|
|
649
|
+
{
|
|
650
|
+
ref: webViewRef,
|
|
651
|
+
source: { uri: initialSourceUri },
|
|
652
|
+
onMessage: handleMessage,
|
|
653
|
+
webviewDebuggingEnabled: __DEV__,
|
|
654
|
+
javaScriptEnabled: true,
|
|
655
|
+
domStorageEnabled: true,
|
|
656
|
+
sharedCookiesEnabled: Platform.OS === "ios",
|
|
657
|
+
thirdPartyCookiesEnabled: Platform.OS === "android",
|
|
658
|
+
startInLoadingState: false,
|
|
659
|
+
originWhitelist: ["https://*", "http://*"],
|
|
660
|
+
style: styles.webView
|
|
661
|
+
}
|
|
662
|
+
)
|
|
663
|
+
]
|
|
664
|
+
}
|
|
665
|
+
)
|
|
666
|
+
] }) });
|
|
667
|
+
};
|
|
668
|
+
var useMoonKeyContext = () => {
|
|
669
|
+
const ctx = useContext(MoonKeyContext);
|
|
670
|
+
if (!ctx) {
|
|
671
|
+
throw new Error("useMoonKey must be used within a MoonKeyProvider");
|
|
672
|
+
}
|
|
673
|
+
return ctx;
|
|
674
|
+
};
|
|
675
|
+
var styles = StyleSheet.create({
|
|
676
|
+
backdrop: {
|
|
677
|
+
position: "absolute",
|
|
678
|
+
top: 0,
|
|
679
|
+
bottom: 0,
|
|
680
|
+
left: 0,
|
|
681
|
+
right: 0,
|
|
682
|
+
backgroundColor: "#000",
|
|
683
|
+
zIndex: 999
|
|
684
|
+
},
|
|
685
|
+
// Sheet-style container: rendered as a card that slides up from the
|
|
686
|
+
// bottom. `top` is set inline by the provider to leave room above
|
|
687
|
+
// for the host app's content visible through the dimmed backdrop.
|
|
688
|
+
// `bottom: 0` so the WebView fills the remaining height.
|
|
689
|
+
sheetContainer: {
|
|
690
|
+
position: "absolute",
|
|
691
|
+
bottom: 0,
|
|
692
|
+
left: 0,
|
|
693
|
+
right: 0,
|
|
694
|
+
backgroundColor: "#0a0a0a",
|
|
695
|
+
borderTopLeftRadius: 14,
|
|
696
|
+
borderTopRightRadius: 14,
|
|
697
|
+
overflow: "hidden",
|
|
698
|
+
zIndex: 1e3,
|
|
699
|
+
shadowColor: "#000",
|
|
700
|
+
shadowOpacity: 0.4,
|
|
701
|
+
shadowRadius: 20,
|
|
702
|
+
shadowOffset: { width: 0, height: -4 },
|
|
703
|
+
elevation: 16
|
|
704
|
+
},
|
|
705
|
+
sheetGrabber: {
|
|
706
|
+
width: 36,
|
|
707
|
+
height: 4,
|
|
708
|
+
borderRadius: 2,
|
|
709
|
+
backgroundColor: "#333",
|
|
710
|
+
alignSelf: "center",
|
|
711
|
+
marginTop: 8,
|
|
712
|
+
marginBottom: 4
|
|
713
|
+
},
|
|
714
|
+
webView: {
|
|
715
|
+
flex: 1
|
|
716
|
+
},
|
|
717
|
+
modalChrome: {
|
|
718
|
+
flexDirection: "row",
|
|
719
|
+
alignItems: "center",
|
|
720
|
+
justifyContent: "space-between",
|
|
721
|
+
paddingTop: 8,
|
|
722
|
+
paddingBottom: 12,
|
|
723
|
+
paddingHorizontal: 16,
|
|
724
|
+
backgroundColor: "#0a0a0a",
|
|
725
|
+
borderBottomWidth: StyleSheet.hairlineWidth,
|
|
726
|
+
borderBottomColor: "#222"
|
|
727
|
+
},
|
|
728
|
+
// Empty flex spacer keeps the close button right-aligned without
|
|
729
|
+
// a title taking up the left side. The iframe-app's flow page
|
|
730
|
+
// renders its own header inside the WebView, so a duplicate title
|
|
731
|
+
// here was redundant.
|
|
732
|
+
modalTitleSpacer: {
|
|
733
|
+
flex: 1
|
|
734
|
+
},
|
|
735
|
+
closeButton: {
|
|
736
|
+
paddingHorizontal: 8,
|
|
737
|
+
paddingVertical: 4
|
|
738
|
+
},
|
|
739
|
+
closeButtonText: {
|
|
740
|
+
color: "#3b82f6",
|
|
741
|
+
fontSize: 17,
|
|
742
|
+
fontWeight: "600"
|
|
743
|
+
}
|
|
744
|
+
});
|
|
745
|
+
|
|
746
|
+
// src/hooks/index.ts
|
|
747
|
+
import {
|
|
748
|
+
useAddPasskey,
|
|
749
|
+
useCreateWallet,
|
|
750
|
+
useImportKey,
|
|
751
|
+
useLoginWithEmail,
|
|
752
|
+
useLogout,
|
|
753
|
+
usePasskeyStatus,
|
|
754
|
+
useRegisterPasskey,
|
|
755
|
+
useRemovePasskey,
|
|
756
|
+
useUser,
|
|
757
|
+
useWallets
|
|
758
|
+
} from "@moon-x/core/react";
|
|
759
|
+
|
|
760
|
+
// src/hooks/use-attach-oauth.ts
|
|
761
|
+
import { PostMessageMethod as PostMessageMethod3, PostMessageType as PostMessageType3 } from "@moon-x/core/utils";
|
|
762
|
+
import { useState as useState2 } from "react";
|
|
763
|
+
|
|
764
|
+
// src/lib/oauth-deep-link.ts
|
|
765
|
+
var openOAuthFlow = async ({
|
|
766
|
+
authUrl,
|
|
767
|
+
redirectUri
|
|
768
|
+
}) => {
|
|
769
|
+
const [{ openAuthSessionAsync, dismissAuthSession }, Linking] = await Promise.all([
|
|
770
|
+
import("expo-web-browser"),
|
|
771
|
+
import("expo-linking")
|
|
772
|
+
]);
|
|
773
|
+
const result = await openAuthSessionAsync(authUrl, redirectUri);
|
|
774
|
+
if (result.type === "cancel" || result.type === "dismiss") {
|
|
775
|
+
throw new Error("oauth_cancelled");
|
|
776
|
+
}
|
|
777
|
+
if (result.type !== "success" || !result.url) {
|
|
778
|
+
dismissAuthSession?.();
|
|
779
|
+
throw new Error(`oauth_failed:${result.type}`);
|
|
780
|
+
}
|
|
781
|
+
const parsed = Linking.parse(result.url);
|
|
782
|
+
if (parsed && parsed.queryParams && parsed.queryParams.error) {
|
|
783
|
+
throw new Error(`oauth_provider_error:${String(parsed.queryParams.error)}`);
|
|
784
|
+
}
|
|
785
|
+
return { redirectUrl: result.url };
|
|
786
|
+
};
|
|
787
|
+
var parseOAuthRedirect = async (redirectUrl) => {
|
|
788
|
+
const Linking = await import("expo-linking");
|
|
789
|
+
const parsed = Linking.parse(redirectUrl);
|
|
790
|
+
const q = parsed.queryParams || {};
|
|
791
|
+
return {
|
|
792
|
+
code: typeof q.code === "string" ? q.code : void 0,
|
|
793
|
+
state: typeof q.state === "string" ? q.state : void 0,
|
|
794
|
+
token: typeof q.token === "string" ? q.token : void 0,
|
|
795
|
+
error: typeof q.error === "string" ? q.error : void 0,
|
|
796
|
+
raw: redirectUrl
|
|
797
|
+
};
|
|
798
|
+
};
|
|
799
|
+
|
|
800
|
+
// src/hooks/use-attach-oauth.ts
|
|
801
|
+
var useAttachOAuth = (params) => {
|
|
802
|
+
const { sdk, _bridge, _oauthRedirectUri } = useMoonKeyContext();
|
|
803
|
+
const [state, setState] = useState2({ status: "idle" });
|
|
804
|
+
return {
|
|
805
|
+
state,
|
|
806
|
+
attachOAuth: async (provider) => {
|
|
807
|
+
setState({ status: "starting" });
|
|
808
|
+
try {
|
|
809
|
+
const redirectUri = params?.redirectUri || _oauthRedirectUri;
|
|
810
|
+
if (!redirectUri) throw new Error("oauth_redirect_uri_missing");
|
|
811
|
+
const startResult = await _bridge.send(
|
|
812
|
+
PostMessageType3.AUTH,
|
|
813
|
+
PostMessageMethod3.START_OAUTH,
|
|
814
|
+
{
|
|
815
|
+
publishableKey: sdk.publishableKey,
|
|
816
|
+
provider,
|
|
817
|
+
attach: true,
|
|
818
|
+
login_redirect_url: redirectUri
|
|
819
|
+
}
|
|
820
|
+
);
|
|
821
|
+
if (!startResult.redirect_url) {
|
|
822
|
+
throw new Error("start_oauth_failed");
|
|
823
|
+
}
|
|
824
|
+
setState({ status: "redirecting" });
|
|
825
|
+
const { redirectUrl } = await openOAuthFlow({
|
|
826
|
+
authUrl: startResult.redirect_url,
|
|
827
|
+
redirectUri
|
|
828
|
+
});
|
|
829
|
+
const parsed = await parseOAuthRedirect(redirectUrl);
|
|
830
|
+
if (parsed.error) throw new Error(parsed.error);
|
|
831
|
+
setState({ status: "verifying" });
|
|
832
|
+
await _bridge.send(
|
|
833
|
+
PostMessageType3.AUTH,
|
|
834
|
+
PostMessageMethod3.ATTACH_OAUTH,
|
|
835
|
+
{
|
|
836
|
+
publishableKey: sdk.publishableKey,
|
|
837
|
+
provider,
|
|
838
|
+
code: parsed.code,
|
|
839
|
+
token: parsed.token,
|
|
840
|
+
state: parsed.state
|
|
841
|
+
}
|
|
842
|
+
);
|
|
843
|
+
setState({ status: "complete" });
|
|
844
|
+
} catch (e) {
|
|
845
|
+
const err = e instanceof Error ? e : new Error(String(e));
|
|
846
|
+
setState({ status: "error", error: err });
|
|
847
|
+
throw err;
|
|
848
|
+
}
|
|
849
|
+
},
|
|
850
|
+
reset: () => setState({ status: "idle" })
|
|
851
|
+
};
|
|
852
|
+
};
|
|
853
|
+
|
|
854
|
+
// src/hooks/use-detach-oauth.ts
|
|
855
|
+
import { PostMessageMethod as PostMessageMethod4, PostMessageType as PostMessageType4 } from "@moon-x/core/utils";
|
|
856
|
+
var useDetachOAuth = () => {
|
|
857
|
+
const { sdk, _bridge } = useMoonKeyContext();
|
|
858
|
+
return {
|
|
859
|
+
detachOAuth: async (idpUserId) => {
|
|
860
|
+
await _bridge.send(
|
|
861
|
+
PostMessageType4.AUTH,
|
|
862
|
+
PostMessageMethod4.DETACH_OAUTH,
|
|
863
|
+
{ publishableKey: sdk.publishableKey, idpUserId }
|
|
864
|
+
);
|
|
865
|
+
}
|
|
866
|
+
};
|
|
867
|
+
};
|
|
868
|
+
|
|
869
|
+
// src/hooks/use-export-key.ts
|
|
870
|
+
import { withClearOnFailure } from "@moon-x/core/lib";
|
|
871
|
+
import { assertPasskeyInParent as assertPasskeyInParent2 } from "@moon-x/core/sdk";
|
|
872
|
+
var useExportKey = () => {
|
|
873
|
+
const { sdk, _bridge, _runSensitiveFlow } = useMoonKeyContext();
|
|
874
|
+
return {
|
|
875
|
+
exportKey: (wallet) => withClearOnFailure(async () => {
|
|
876
|
+
const { userHandleB64url, credentialIdB64url } = await assertPasskeyInParent2({
|
|
877
|
+
transport: _bridge,
|
|
878
|
+
passkey: createRNPasskey(),
|
|
879
|
+
publishableKey: sdk.publishableKey,
|
|
880
|
+
requireFreshAssertion: true
|
|
881
|
+
});
|
|
882
|
+
const params = new URLSearchParams({
|
|
883
|
+
publishableKey: sdk.publishableKey,
|
|
884
|
+
wallet_id: wallet.id ?? "",
|
|
885
|
+
wallet_type: wallet.wallet_type ?? "",
|
|
886
|
+
user_handle_b64url: userHandleB64url,
|
|
887
|
+
credential_id_b64url: credentialIdB64url
|
|
888
|
+
});
|
|
889
|
+
await _runSensitiveFlow({
|
|
890
|
+
title: "Export key",
|
|
891
|
+
flowPath: `/export-key?${params.toString()}`,
|
|
892
|
+
// No completion postMessage in v1 — the user-tap-Done path
|
|
893
|
+
// resolves the promise via the modal's cancel handler. The
|
|
894
|
+
// promise simply waits for runSensitiveFlow to settle, which
|
|
895
|
+
// happens when the user dismisses.
|
|
896
|
+
onMount: () => Promise.resolve()
|
|
897
|
+
});
|
|
898
|
+
})
|
|
899
|
+
};
|
|
900
|
+
};
|
|
901
|
+
|
|
902
|
+
// src/hooks/use-login-with-oauth.ts
|
|
903
|
+
import { PostMessageMethod as PostMessageMethod5, PostMessageType as PostMessageType5 } from "@moon-x/core/utils";
|
|
904
|
+
import { useState as useState3 } from "react";
|
|
905
|
+
var useLoginWithOAuth = (params) => {
|
|
906
|
+
const { sdk, _bridge, _oauthRedirectUri } = useMoonKeyContext();
|
|
907
|
+
const [state, setState] = useState3({ status: "idle" });
|
|
908
|
+
return {
|
|
909
|
+
state,
|
|
910
|
+
loginWithOAuth: async (provider) => {
|
|
911
|
+
setState({ status: "starting" });
|
|
912
|
+
try {
|
|
913
|
+
const redirectUri = params?.redirectUri || _oauthRedirectUri;
|
|
914
|
+
if (!redirectUri) {
|
|
915
|
+
throw new Error("oauth_redirect_uri_missing");
|
|
916
|
+
}
|
|
917
|
+
const startResult = await _bridge.send(
|
|
918
|
+
PostMessageType5.AUTH,
|
|
919
|
+
PostMessageMethod5.START_OAUTH,
|
|
920
|
+
{
|
|
921
|
+
publishableKey: sdk.publishableKey,
|
|
922
|
+
provider,
|
|
923
|
+
login_redirect_url: redirectUri
|
|
924
|
+
}
|
|
925
|
+
);
|
|
926
|
+
if (!startResult.redirect_url) {
|
|
927
|
+
throw new Error("start_oauth_failed");
|
|
928
|
+
}
|
|
929
|
+
setState({ status: "redirecting" });
|
|
930
|
+
const { redirectUrl } = await openOAuthFlow({
|
|
931
|
+
authUrl: startResult.redirect_url,
|
|
932
|
+
redirectUri
|
|
933
|
+
});
|
|
934
|
+
const parsed = await parseOAuthRedirect(redirectUrl);
|
|
935
|
+
if (parsed.error) throw new Error(parsed.error);
|
|
936
|
+
if (!parsed.code && !parsed.token) {
|
|
937
|
+
throw new Error("oauth_no_code_or_token");
|
|
938
|
+
}
|
|
939
|
+
setState({ status: "verifying" });
|
|
940
|
+
const result = await sdk.verifyOAuth({
|
|
941
|
+
provider,
|
|
942
|
+
code: parsed.code,
|
|
943
|
+
token: parsed.token,
|
|
944
|
+
state: parsed.state
|
|
945
|
+
});
|
|
946
|
+
setState({ status: "complete", user: result?.user ?? result ?? null });
|
|
947
|
+
} catch (e) {
|
|
948
|
+
const err = e instanceof Error ? e : new Error(String(e));
|
|
949
|
+
setState({ status: "error", error: err });
|
|
950
|
+
throw err;
|
|
951
|
+
}
|
|
952
|
+
},
|
|
953
|
+
reset: () => setState({ status: "idle" })
|
|
954
|
+
};
|
|
955
|
+
};
|
|
956
|
+
|
|
957
|
+
// src/hooks/use-moonkey.ts
|
|
958
|
+
var useMoonKey = () => useMoonKeyContext().sdk;
|
|
959
|
+
|
|
960
|
+
// src/index.ts
|
|
961
|
+
import { PostMessageMethod as PostMessageMethod6, PostMessageType as PostMessageType6 } from "@moon-x/core/utils";
|
|
962
|
+
export {
|
|
963
|
+
MoonKeyProvider,
|
|
964
|
+
PostMessageMethod6 as PostMessageMethod,
|
|
965
|
+
PostMessageType6 as PostMessageType,
|
|
966
|
+
useAddPasskey,
|
|
967
|
+
useAttachOAuth,
|
|
968
|
+
useCreateWallet,
|
|
969
|
+
useDetachOAuth,
|
|
970
|
+
useExportKey,
|
|
971
|
+
useImportKey,
|
|
972
|
+
useLoginWithEmail,
|
|
973
|
+
useLoginWithOAuth,
|
|
974
|
+
useLogout,
|
|
975
|
+
useMoonKey,
|
|
976
|
+
useMoonKeyContext,
|
|
977
|
+
usePasskeyStatus,
|
|
978
|
+
useRegisterPasskey,
|
|
979
|
+
useRemovePasskey,
|
|
980
|
+
useUser,
|
|
981
|
+
useWallets
|
|
982
|
+
};
|