@moon-x/react-native-sdk 0.24.0 → 0.26.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 +105 -5
- package/dist/ethereum.js +91 -15
- package/dist/ethereum.mjs +105 -14
- package/dist/index.d.mts +120 -4
- package/dist/index.d.ts +120 -4
- package/dist/index.js +1127 -254
- package/dist/index.mjs +1101 -213
- package/dist/solana.js +91 -15
- package/dist/solana.mjs +105 -14
- package/dist/tron.js +91 -15
- package/dist/tron.mjs +105 -14
- package/package.json +3 -2
package/dist/index.js
CHANGED
|
@@ -30,44 +30,45 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
-
MOONPAY_CONSUMER_SESSION_ERROR_CODES: () =>
|
|
33
|
+
MOONPAY_CONSUMER_SESSION_ERROR_CODES: () => import_sdk12.MOONPAY_CONSUMER_SESSION_ERROR_CODES,
|
|
34
34
|
MoonKeyProvider: () => MoonXProvider,
|
|
35
35
|
MoonXProvider: () => MoonXProvider,
|
|
36
|
-
PostMessageMethod: () =>
|
|
37
|
-
PostMessageType: () =>
|
|
36
|
+
PostMessageMethod: () => import_utils11.PostMessageMethod,
|
|
37
|
+
PostMessageType: () => import_utils11.PostMessageType,
|
|
38
38
|
connectWalletConnect: () => connectWalletConnect,
|
|
39
39
|
listWalletConnectSessions: () => listWalletConnectSessions,
|
|
40
|
-
registerPresenceOtpCodeProvider: () =>
|
|
40
|
+
registerPresenceOtpCodeProvider: () => import_sdk13.registerPresenceOtpCodeProvider,
|
|
41
41
|
restoreWalletConnectSession: () => restoreWalletConnectSession,
|
|
42
|
-
useAddPasskey: () =>
|
|
42
|
+
useAddPasskey: () => import_react9.useAddPasskey,
|
|
43
43
|
useAppConfig: () => useAppConfig,
|
|
44
44
|
useAttachOAuth: () => useAttachOAuth,
|
|
45
|
-
useBulkImport: () =>
|
|
46
|
-
|
|
45
|
+
useBulkImport: () => import_react9.useBulkImport,
|
|
46
|
+
useChangeEmail: () => import_react9.useChangeEmail,
|
|
47
|
+
useCreateWallet: () => import_react9.useCreateWallet,
|
|
47
48
|
useDetachOAuth: () => useDetachOAuth,
|
|
48
|
-
useEphemeralSigner: () =>
|
|
49
|
+
useEphemeralSigner: () => import_react9.useEphemeralSigner,
|
|
49
50
|
useExportKey: () => useExportKey,
|
|
50
51
|
useExternalWallets: () => useExternalWallets,
|
|
51
52
|
useFundWallet: () => useFundWallet,
|
|
52
|
-
useImportKey: () =>
|
|
53
|
-
useLinkEmail: () =>
|
|
53
|
+
useImportKey: () => import_react9.useImportKey,
|
|
54
|
+
useLinkEmail: () => import_react9.useLinkEmail,
|
|
54
55
|
useLoginWithEmail: () => useLoginWithEmail,
|
|
55
56
|
useLoginWithOAuth: () => useLoginWithOAuth,
|
|
56
|
-
useLogout: () =>
|
|
57
|
+
useLogout: () => import_react9.useLogout,
|
|
57
58
|
useMoonKey: () => useMoonX,
|
|
58
59
|
useMoonX: () => useMoonX,
|
|
59
60
|
useMoonXContext: () => useMoonXContext,
|
|
60
|
-
usePasskeyStatus: () =>
|
|
61
|
-
useRegisterPasskey: () =>
|
|
62
|
-
useRemovePasskey: () =>
|
|
63
|
-
useUser: () =>
|
|
64
|
-
useWallets: () =>
|
|
61
|
+
usePasskeyStatus: () => import_react9.usePasskeyStatus,
|
|
62
|
+
useRegisterPasskey: () => import_react9.useRegisterPasskey,
|
|
63
|
+
useRemovePasskey: () => import_react9.useRemovePasskey,
|
|
64
|
+
useUser: () => import_react9.useUser,
|
|
65
|
+
useWallets: () => import_react9.useWallets
|
|
65
66
|
});
|
|
66
67
|
module.exports = __toCommonJS(index_exports);
|
|
67
68
|
|
|
68
69
|
// src/provider.tsx
|
|
69
|
-
var
|
|
70
|
-
var
|
|
70
|
+
var import_react2 = require("react");
|
|
71
|
+
var import_react_native2 = require("react-native");
|
|
71
72
|
var import_react_native_webview = require("react-native-webview");
|
|
72
73
|
|
|
73
74
|
// src/sdk/create-sdk-instance.ts
|
|
@@ -245,7 +246,8 @@ var getPasskeyMethods = (bridge, publishableKey, configuredRelyingPartyOrigin) =
|
|
|
245
246
|
passkey: createRNPasskey(),
|
|
246
247
|
publishableKey,
|
|
247
248
|
relyingPartyOrigin,
|
|
248
|
-
scopes: import_sdk4.SCOPES_ADD_PASSKEY
|
|
249
|
+
scopes: import_sdk4.SCOPES_ADD_PASSKEY,
|
|
250
|
+
requestOtpCode: params?.requestOtpCode
|
|
249
251
|
});
|
|
250
252
|
if (result.credentialIdB64url && !allowList.includes(result.credentialIdB64url)) {
|
|
251
253
|
throw new Error("unknown_passkey_for_this_account");
|
|
@@ -494,6 +496,67 @@ var getTronSDKMethods = (bridge, publishableKey, relyingPartyOrigin) => (0, impo
|
|
|
494
496
|
relyingPartyOrigin
|
|
495
497
|
});
|
|
496
498
|
|
|
499
|
+
// src/sdk/session-methods.ts
|
|
500
|
+
function getSessionMethods(bridge, publishableKey, relyingPartyOrigin, session, sessionResume) {
|
|
501
|
+
const auth = (callbacks) => getAuthenticationMethods(
|
|
502
|
+
bridge,
|
|
503
|
+
publishableKey,
|
|
504
|
+
callbacks?.setIsAuthenticated
|
|
505
|
+
);
|
|
506
|
+
const general = (callbacks, setUser) => getSDKMethods(
|
|
507
|
+
bridge,
|
|
508
|
+
publishableKey,
|
|
509
|
+
relyingPartyOrigin,
|
|
510
|
+
callbacks?.setIsAuthenticated,
|
|
511
|
+
setUser
|
|
512
|
+
);
|
|
513
|
+
const mutate = async (operation, profile) => {
|
|
514
|
+
const callbacks = session.beginAuthOperation();
|
|
515
|
+
try {
|
|
516
|
+
const result = await operation(callbacks);
|
|
517
|
+
if (profile === "await") await callbacks.refreshUser();
|
|
518
|
+
if (profile === "background") void callbacks.refreshUser();
|
|
519
|
+
return result;
|
|
520
|
+
} finally {
|
|
521
|
+
callbacks.finish();
|
|
522
|
+
}
|
|
523
|
+
};
|
|
524
|
+
const login = (operation, profile) => mutate(async (callbacks) => {
|
|
525
|
+
const release = await sessionResume?.beginLogin();
|
|
526
|
+
let succeeded = false;
|
|
527
|
+
try {
|
|
528
|
+
const result = await operation(callbacks);
|
|
529
|
+
succeeded = true;
|
|
530
|
+
return result;
|
|
531
|
+
} finally {
|
|
532
|
+
release?.(succeeded);
|
|
533
|
+
}
|
|
534
|
+
}, profile);
|
|
535
|
+
return {
|
|
536
|
+
loginWithCode: (params) => login((callbacks) => auth(callbacks).loginWithCode(params), "await"),
|
|
537
|
+
loginWithMoonPayCustomerToken: (params) => login((callbacks) => auth(callbacks).loginWithMoonPayCustomerToken(params), "background"),
|
|
538
|
+
verifyOAuth: (params) => login((callbacks) => auth(callbacks).verifyOAuth(params), "await"),
|
|
539
|
+
// Drop the resume key first, so a failed iframe logout cannot leave a key
|
|
540
|
+
// that brings the session back on the next launch.
|
|
541
|
+
logout: () => mutate((callbacks) => sessionResume ? sessionResume.logout(() => general(callbacks).logout()) : general(callbacks).logout(), "none"),
|
|
542
|
+
getUser: () => general(void 0, session.captureUserWrite()).getUser(),
|
|
543
|
+
linkEmailWithCode: async (params) => {
|
|
544
|
+
const refreshUser = session.captureUserRefresh();
|
|
545
|
+
const result = await auth().linkEmailWithCode(params);
|
|
546
|
+
await refreshUser();
|
|
547
|
+
return result;
|
|
548
|
+
},
|
|
549
|
+
// The account's email changes, so the provider's user is read again
|
|
550
|
+
// before the change resolves, the same as a link.
|
|
551
|
+
confirmEmailChangeNewCode: async (params) => {
|
|
552
|
+
const refreshUser = session.captureUserRefresh();
|
|
553
|
+
const result = await auth().confirmEmailChangeNewCode(params);
|
|
554
|
+
await refreshUser();
|
|
555
|
+
return result;
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
|
|
497
560
|
// src/sdk/create-sdk-instance.ts
|
|
498
561
|
var makeRelyingPartyOriginResolver = (bridge, publishableKey, configuredOrigin) => {
|
|
499
562
|
let cached = configuredOrigin || null;
|
|
@@ -521,7 +584,9 @@ var createSdkInstance = ({
|
|
|
521
584
|
revalidatedAt,
|
|
522
585
|
setIsAuthenticated,
|
|
523
586
|
setUser,
|
|
524
|
-
refreshUser
|
|
587
|
+
refreshUser,
|
|
588
|
+
session,
|
|
589
|
+
sessionResume
|
|
525
590
|
}) => ({
|
|
526
591
|
publishableKey,
|
|
527
592
|
ready,
|
|
@@ -529,6 +594,11 @@ var createSdkInstance = ({
|
|
|
529
594
|
isAuthenticated,
|
|
530
595
|
user,
|
|
531
596
|
refreshUser,
|
|
597
|
+
sessionStatus: session?.getSnapshot().sessionStatus ?? (isAuthenticated ? "authenticated" : "restoring"),
|
|
598
|
+
sessionError: session?.getSnapshot().sessionError ?? null,
|
|
599
|
+
retrySession: session?.retrySession ?? (() => {
|
|
600
|
+
throw new Error("session_recovery_unavailable");
|
|
601
|
+
}),
|
|
532
602
|
...getAuthenticationMethods(
|
|
533
603
|
bridge,
|
|
534
604
|
publishableKey,
|
|
@@ -547,11 +617,348 @@ var createSdkInstance = ({
|
|
|
547
617
|
...getFundingSDKMethods(bridge, publishableKey),
|
|
548
618
|
...getSolanaSDKMethods(bridge, publishableKey, relyingPartyOrigin),
|
|
549
619
|
...getBitcoinSDKMethods(bridge, publishableKey, relyingPartyOrigin),
|
|
550
|
-
...getTronSDKMethods(bridge, publishableKey, relyingPartyOrigin)
|
|
620
|
+
...getTronSDKMethods(bridge, publishableKey, relyingPartyOrigin),
|
|
621
|
+
...session ? getSessionMethods(bridge, publishableKey, relyingPartyOrigin, session, sessionResume) : {}
|
|
551
622
|
});
|
|
552
623
|
|
|
553
624
|
// src/provider.tsx
|
|
554
|
-
var
|
|
625
|
+
var import_utils5 = require("@moon-x/core/utils");
|
|
626
|
+
|
|
627
|
+
// src/components/presence-otp-prompt.tsx
|
|
628
|
+
var import_react = require("react");
|
|
629
|
+
var import_react_native = require("react-native");
|
|
630
|
+
var import_sdk9 = require("@moon-x/core/sdk");
|
|
631
|
+
|
|
632
|
+
// src/components/presence-otp-gate.ts
|
|
633
|
+
var createPresenceOtpGate = () => {
|
|
634
|
+
let cancelledChallengeId = null;
|
|
635
|
+
return {
|
|
636
|
+
evaluate(request) {
|
|
637
|
+
if (request.attempt === 1) {
|
|
638
|
+
cancelledChallengeId = null;
|
|
639
|
+
}
|
|
640
|
+
if (request.challengeId && cancelledChallengeId === request.challengeId) {
|
|
641
|
+
return "cancelled";
|
|
642
|
+
}
|
|
643
|
+
return "prompt";
|
|
644
|
+
},
|
|
645
|
+
markCancelled(challengeId) {
|
|
646
|
+
cancelledChallengeId = challengeId ?? null;
|
|
647
|
+
}
|
|
648
|
+
};
|
|
649
|
+
};
|
|
650
|
+
|
|
651
|
+
// src/components/presence-otp-controller.ts
|
|
652
|
+
var IDLE_PRESENCE_OTP_STATE = {
|
|
653
|
+
request: null,
|
|
654
|
+
submitting: false,
|
|
655
|
+
invalidCode: false
|
|
656
|
+
};
|
|
657
|
+
var createPresenceOtpController = (onChange) => {
|
|
658
|
+
const gate = createPresenceOtpGate();
|
|
659
|
+
let active;
|
|
660
|
+
let pending = null;
|
|
661
|
+
const waiting = [];
|
|
662
|
+
let state = IDLE_PRESENCE_OTP_STATE;
|
|
663
|
+
let disposed = false;
|
|
664
|
+
const set = (next) => {
|
|
665
|
+
state = next;
|
|
666
|
+
if (!disposed) onChange(next);
|
|
667
|
+
};
|
|
668
|
+
const takePending = () => {
|
|
669
|
+
const current = pending;
|
|
670
|
+
pending = null;
|
|
671
|
+
return current;
|
|
672
|
+
};
|
|
673
|
+
const show = (next) => {
|
|
674
|
+
active = { challengeId: next.request.challengeId };
|
|
675
|
+
if (gate.evaluate(next.request) === "cancelled") {
|
|
676
|
+
next.reject(new Error("presence_otp_cancelled"));
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
takePending()?.reject(new Error("presence_otp_replaced"));
|
|
680
|
+
pending = next;
|
|
681
|
+
set({
|
|
682
|
+
request: next.request,
|
|
683
|
+
submitting: false,
|
|
684
|
+
invalidCode: next.request.error === "invalid code"
|
|
685
|
+
});
|
|
686
|
+
};
|
|
687
|
+
return {
|
|
688
|
+
getState: () => state,
|
|
689
|
+
requestCode: (request) => new Promise((resolve, reject) => {
|
|
690
|
+
if (disposed) {
|
|
691
|
+
reject(new Error("presence_otp_cancelled"));
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
const next = { request, resolve, reject };
|
|
695
|
+
if (!active || active.challengeId === request.challengeId) {
|
|
696
|
+
show(next);
|
|
697
|
+
} else {
|
|
698
|
+
waiting.push(next);
|
|
699
|
+
}
|
|
700
|
+
}),
|
|
701
|
+
submit: (code) => {
|
|
702
|
+
const current = takePending();
|
|
703
|
+
if (!current) return false;
|
|
704
|
+
current.resolve(code);
|
|
705
|
+
set({ ...state, submitting: true, invalidCode: false });
|
|
706
|
+
return true;
|
|
707
|
+
},
|
|
708
|
+
cancel: () => {
|
|
709
|
+
if (state.submitting || !pending) return false;
|
|
710
|
+
gate.markCancelled(pending.request.challengeId);
|
|
711
|
+
takePending()?.reject(new Error("presence_otp_cancelled"));
|
|
712
|
+
set(IDLE_PRESENCE_OTP_STATE);
|
|
713
|
+
return true;
|
|
714
|
+
},
|
|
715
|
+
expire: (request) => {
|
|
716
|
+
if (pending?.request !== request) return;
|
|
717
|
+
takePending()?.reject(new Error("presence_otp_expired"));
|
|
718
|
+
set(IDLE_PRESENCE_OTP_STATE);
|
|
719
|
+
},
|
|
720
|
+
settled: (request) => {
|
|
721
|
+
if (!active || active.challengeId !== request.challengeId) return;
|
|
722
|
+
active = void 0;
|
|
723
|
+
takePending()?.reject(new Error("presence_otp_cancelled"));
|
|
724
|
+
const next = waiting.shift();
|
|
725
|
+
if (next) {
|
|
726
|
+
show(next);
|
|
727
|
+
} else {
|
|
728
|
+
set(IDLE_PRESENCE_OTP_STATE);
|
|
729
|
+
}
|
|
730
|
+
},
|
|
731
|
+
dispose: () => {
|
|
732
|
+
const error = new Error("presence_otp_cancelled");
|
|
733
|
+
takePending()?.reject(error);
|
|
734
|
+
for (const next of waiting.splice(0)) next.reject(error);
|
|
735
|
+
active = void 0;
|
|
736
|
+
state = IDLE_PRESENCE_OTP_STATE;
|
|
737
|
+
disposed = true;
|
|
738
|
+
}
|
|
739
|
+
};
|
|
740
|
+
};
|
|
741
|
+
|
|
742
|
+
// src/components/presence-otp-prompt.tsx
|
|
743
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
744
|
+
var DEFAULT_COPY = {
|
|
745
|
+
title: "Check your email",
|
|
746
|
+
description: "Enter the verification code we sent to confirm this wallet action.",
|
|
747
|
+
hint: "Didn't get it? Check spam, or cancel and try again in a minute.",
|
|
748
|
+
verify: "Verify",
|
|
749
|
+
verifying: "Verifying...",
|
|
750
|
+
cancel: "Cancel",
|
|
751
|
+
invalidCode: "That code was incorrect. Try again.",
|
|
752
|
+
incompleteCode: "Enter the 6-digit code."
|
|
753
|
+
};
|
|
754
|
+
var DEFAULT_COLORS = {
|
|
755
|
+
background: "#0a0a0a",
|
|
756
|
+
surface: "#141414",
|
|
757
|
+
border: "#333",
|
|
758
|
+
text: "#fafafa",
|
|
759
|
+
mutedText: "#a1a1aa",
|
|
760
|
+
accent: "#3b82f6",
|
|
761
|
+
accentText: "#fff",
|
|
762
|
+
error: "#f87171"
|
|
763
|
+
};
|
|
764
|
+
var OTP_LENGTH = 6;
|
|
765
|
+
var PresenceOtpPrompt = ({
|
|
766
|
+
publishableKey,
|
|
767
|
+
options
|
|
768
|
+
}) => {
|
|
769
|
+
const [state, setState] = (0, import_react.useState)(
|
|
770
|
+
IDLE_PRESENCE_OTP_STATE
|
|
771
|
+
);
|
|
772
|
+
const [code, setCode] = (0, import_react.useState)("");
|
|
773
|
+
const [incomplete, setIncomplete] = (0, import_react.useState)(false);
|
|
774
|
+
const controllerRef = (0, import_react.useRef)(null);
|
|
775
|
+
const copy = { ...DEFAULT_COPY, ...options?.copy };
|
|
776
|
+
const colors = { ...DEFAULT_COLORS, ...options?.colors };
|
|
777
|
+
(0, import_react.useEffect)(() => {
|
|
778
|
+
const controller = createPresenceOtpController(setState);
|
|
779
|
+
controllerRef.current = controller;
|
|
780
|
+
const unregister = (0, import_sdk9.registerPresenceOtpCodeProvider)(
|
|
781
|
+
publishableKey,
|
|
782
|
+
controller.requestCode,
|
|
783
|
+
controller.settled
|
|
784
|
+
);
|
|
785
|
+
return () => {
|
|
786
|
+
unregister();
|
|
787
|
+
controller.dispose();
|
|
788
|
+
controllerRef.current = null;
|
|
789
|
+
setState(IDLE_PRESENCE_OTP_STATE);
|
|
790
|
+
};
|
|
791
|
+
}, [publishableKey]);
|
|
792
|
+
(0, import_react.useEffect)(() => {
|
|
793
|
+
setCode("");
|
|
794
|
+
setIncomplete(false);
|
|
795
|
+
}, [state.request]);
|
|
796
|
+
(0, import_react.useEffect)(() => {
|
|
797
|
+
const request = state.request;
|
|
798
|
+
if (!request?.expiresAt || state.submitting) return;
|
|
799
|
+
const expire = () => controllerRef.current?.expire(request);
|
|
800
|
+
const remainingMs = request.expiresAt * 1e3 - Date.now();
|
|
801
|
+
if (remainingMs <= 0) {
|
|
802
|
+
expire();
|
|
803
|
+
return;
|
|
804
|
+
}
|
|
805
|
+
const timer = setTimeout(expire, remainingMs);
|
|
806
|
+
return () => clearTimeout(timer);
|
|
807
|
+
}, [state.request, state.submitting]);
|
|
808
|
+
const close = () => {
|
|
809
|
+
controllerRef.current?.cancel();
|
|
810
|
+
};
|
|
811
|
+
const submit = () => {
|
|
812
|
+
const normalized = code.trim();
|
|
813
|
+
if (!/^\d{6}$/.test(normalized)) {
|
|
814
|
+
setIncomplete(true);
|
|
815
|
+
return;
|
|
816
|
+
}
|
|
817
|
+
controllerRef.current?.submit(normalized);
|
|
818
|
+
};
|
|
819
|
+
const submitting = state.submitting;
|
|
820
|
+
const error = incomplete ? copy.incompleteCode : state.invalidCode ? copy.invalidCode : null;
|
|
821
|
+
const complete = code.length === OTP_LENGTH;
|
|
822
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
823
|
+
import_react_native.Modal,
|
|
824
|
+
{
|
|
825
|
+
visible: state.request !== null,
|
|
826
|
+
transparent: true,
|
|
827
|
+
animationType: "fade",
|
|
828
|
+
onRequestClose: close,
|
|
829
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.View, { style: styles.backdrop, children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native.View, { style: [styles.card, { backgroundColor: colors.background }], children: [
|
|
830
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { style: [styles.title, { color: colors.text }], children: copy.title }),
|
|
831
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { style: [styles.body, { color: colors.mutedText }], children: copy.description }),
|
|
832
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
833
|
+
import_react_native.TextInput,
|
|
834
|
+
{
|
|
835
|
+
autoFocus: true,
|
|
836
|
+
style: [
|
|
837
|
+
styles.input,
|
|
838
|
+
{
|
|
839
|
+
borderColor: colors.border,
|
|
840
|
+
backgroundColor: colors.surface,
|
|
841
|
+
color: colors.text
|
|
842
|
+
}
|
|
843
|
+
],
|
|
844
|
+
value: code,
|
|
845
|
+
onChangeText: (next) => {
|
|
846
|
+
setCode(next.replace(/\D/g, "").slice(0, OTP_LENGTH));
|
|
847
|
+
setIncomplete(false);
|
|
848
|
+
},
|
|
849
|
+
onSubmitEditing: submit,
|
|
850
|
+
editable: !submitting,
|
|
851
|
+
keyboardType: "number-pad",
|
|
852
|
+
textContentType: "oneTimeCode",
|
|
853
|
+
autoComplete: "sms-otp",
|
|
854
|
+
maxLength: OTP_LENGTH,
|
|
855
|
+
placeholder: "123456",
|
|
856
|
+
placeholderTextColor: colors.mutedText,
|
|
857
|
+
accessibilityLabel: "Verification code",
|
|
858
|
+
returnKeyType: "done"
|
|
859
|
+
}
|
|
860
|
+
),
|
|
861
|
+
error !== null && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
862
|
+
import_react_native.Text,
|
|
863
|
+
{
|
|
864
|
+
style: [styles.error, { color: colors.error }],
|
|
865
|
+
accessibilityLiveRegion: "polite",
|
|
866
|
+
children: error
|
|
867
|
+
}
|
|
868
|
+
),
|
|
869
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
870
|
+
import_react_native.Pressable,
|
|
871
|
+
{
|
|
872
|
+
onPress: submit,
|
|
873
|
+
disabled: submitting || !complete,
|
|
874
|
+
style: [
|
|
875
|
+
styles.primaryButton,
|
|
876
|
+
{ backgroundColor: colors.accent },
|
|
877
|
+
(submitting || !complete) && styles.primaryButtonDisabled
|
|
878
|
+
],
|
|
879
|
+
accessibilityRole: "button",
|
|
880
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { style: [styles.primaryButtonText, { color: colors.accentText }], children: submitting ? copy.verifying : copy.verify })
|
|
881
|
+
}
|
|
882
|
+
),
|
|
883
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
884
|
+
import_react_native.Pressable,
|
|
885
|
+
{
|
|
886
|
+
onPress: close,
|
|
887
|
+
disabled: submitting,
|
|
888
|
+
style: [styles.ghostButton, submitting && styles.primaryButtonDisabled],
|
|
889
|
+
accessibilityRole: "button",
|
|
890
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { style: [styles.ghostButtonText, { color: colors.mutedText }], children: copy.cancel })
|
|
891
|
+
}
|
|
892
|
+
),
|
|
893
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Text, { style: [styles.hint, { color: colors.mutedText }], children: copy.hint })
|
|
894
|
+
] }) })
|
|
895
|
+
}
|
|
896
|
+
);
|
|
897
|
+
};
|
|
898
|
+
var styles = import_react_native.StyleSheet.create({
|
|
899
|
+
backdrop: {
|
|
900
|
+
flex: 1,
|
|
901
|
+
alignItems: "center",
|
|
902
|
+
justifyContent: "center",
|
|
903
|
+
padding: 16,
|
|
904
|
+
backgroundColor: "rgba(0, 0, 0, 0.45)"
|
|
905
|
+
},
|
|
906
|
+
card: {
|
|
907
|
+
width: "100%",
|
|
908
|
+
maxWidth: 384,
|
|
909
|
+
padding: 24,
|
|
910
|
+
borderRadius: 14
|
|
911
|
+
},
|
|
912
|
+
title: {
|
|
913
|
+
fontSize: 20,
|
|
914
|
+
fontWeight: "600"
|
|
915
|
+
},
|
|
916
|
+
body: {
|
|
917
|
+
fontSize: 14,
|
|
918
|
+
marginTop: 8
|
|
919
|
+
},
|
|
920
|
+
input: {
|
|
921
|
+
marginTop: 16,
|
|
922
|
+
paddingVertical: 12,
|
|
923
|
+
paddingHorizontal: 16,
|
|
924
|
+
borderRadius: 12,
|
|
925
|
+
borderWidth: import_react_native.StyleSheet.hairlineWidth,
|
|
926
|
+
fontSize: 24,
|
|
927
|
+
letterSpacing: 8,
|
|
928
|
+
textAlign: "center"
|
|
929
|
+
},
|
|
930
|
+
error: {
|
|
931
|
+
fontSize: 13,
|
|
932
|
+
marginTop: 8
|
|
933
|
+
},
|
|
934
|
+
primaryButton: {
|
|
935
|
+
marginTop: 16,
|
|
936
|
+
paddingVertical: 12,
|
|
937
|
+
borderRadius: 12,
|
|
938
|
+
alignItems: "center"
|
|
939
|
+
},
|
|
940
|
+
primaryButtonDisabled: {
|
|
941
|
+
opacity: 0.5
|
|
942
|
+
},
|
|
943
|
+
primaryButtonText: {
|
|
944
|
+
fontSize: 16,
|
|
945
|
+
fontWeight: "600"
|
|
946
|
+
},
|
|
947
|
+
ghostButton: {
|
|
948
|
+
marginTop: 8,
|
|
949
|
+
paddingVertical: 12,
|
|
950
|
+
alignItems: "center"
|
|
951
|
+
},
|
|
952
|
+
ghostButtonText: {
|
|
953
|
+
fontSize: 15,
|
|
954
|
+
fontWeight: "500"
|
|
955
|
+
},
|
|
956
|
+
hint: {
|
|
957
|
+
fontSize: 12,
|
|
958
|
+
marginTop: 4,
|
|
959
|
+
textAlign: "center"
|
|
960
|
+
}
|
|
961
|
+
});
|
|
555
962
|
|
|
556
963
|
// src/transport/webview-bridge.ts
|
|
557
964
|
var import_utils3 = require("@moon-x/core/utils");
|
|
@@ -711,7 +1118,8 @@ var WebViewBridge = class {
|
|
|
711
1118
|
// ride out the iframe-app mount race; everything else makes a single attempt.
|
|
712
1119
|
async send(type, method, payload) {
|
|
713
1120
|
const isSigningPreparation = method === "GET_DEK_INFO" && payload !== null && typeof payload === "object" && "signingWalletId" in payload && payload.signingWalletId !== void 0;
|
|
714
|
-
|
|
1121
|
+
const isResumeCheck = method === "IS_AUTHENTICATED" && payload !== null && typeof payload === "object" && "resumeSessionId" in payload && payload.resumeSessionId !== void 0;
|
|
1122
|
+
if (isSigningPreparation || isResumeCheck || !RETRYABLE_METHODS.has(method)) {
|
|
715
1123
|
return this.sendOnce(
|
|
716
1124
|
type,
|
|
717
1125
|
method,
|
|
@@ -816,7 +1224,7 @@ var WebViewBridge = class {
|
|
|
816
1224
|
};
|
|
817
1225
|
|
|
818
1226
|
// src/provider.tsx
|
|
819
|
-
var
|
|
1227
|
+
var import_react3 = require("@moon-x/core/react");
|
|
820
1228
|
var import_lib2 = require("@moon-x/core/lib");
|
|
821
1229
|
|
|
822
1230
|
// src/webview-lifecycle.ts
|
|
@@ -838,55 +1246,545 @@ var webViewLifecycleReducer = (state, action) => {
|
|
|
838
1246
|
}
|
|
839
1247
|
};
|
|
840
1248
|
|
|
841
|
-
// src/session
|
|
842
|
-
|
|
843
|
-
let
|
|
1249
|
+
// src/native-session.ts
|
|
1250
|
+
function createNativeSession(reads) {
|
|
1251
|
+
let snapshot = {
|
|
1252
|
+
isAuthenticated: false,
|
|
1253
|
+
user: null,
|
|
1254
|
+
revalidatedAt: 0,
|
|
1255
|
+
sessionStatus: "restoring",
|
|
1256
|
+
sessionError: null
|
|
1257
|
+
};
|
|
1258
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
1259
|
+
const holds = /* @__PURE__ */ new Set();
|
|
1260
|
+
let holdSequence = 0;
|
|
1261
|
+
let replaceableFinish = null;
|
|
1262
|
+
let mounted = false;
|
|
1263
|
+
let ready = false;
|
|
1264
|
+
let pending = true;
|
|
844
1265
|
let generation = 0;
|
|
1266
|
+
let profileSequence = 0;
|
|
1267
|
+
let run = null;
|
|
1268
|
+
let timer;
|
|
1269
|
+
const publish = (patch) => {
|
|
1270
|
+
snapshot = { ...snapshot, ...patch };
|
|
1271
|
+
listeners.forEach((listener) => listener());
|
|
1272
|
+
};
|
|
1273
|
+
const cancelRestore = () => {
|
|
1274
|
+
run = null;
|
|
1275
|
+
clearTimeout(timer);
|
|
1276
|
+
timer = void 0;
|
|
1277
|
+
};
|
|
1278
|
+
const confirmedAt = () => Math.max(Date.now(), snapshot.revalidatedAt + 1);
|
|
1279
|
+
const captureUserWrite = () => {
|
|
1280
|
+
const capturedGeneration = generation;
|
|
1281
|
+
const sequence = ++profileSequence;
|
|
1282
|
+
return (user) => {
|
|
1283
|
+
if (!mounted || generation !== capturedGeneration || profileSequence !== sequence || !snapshot.isAuthenticated) return;
|
|
1284
|
+
if (user && typeof user === "object") publish({ user });
|
|
1285
|
+
};
|
|
1286
|
+
};
|
|
1287
|
+
const refreshUser = async () => {
|
|
1288
|
+
const write = captureUserWrite();
|
|
1289
|
+
try {
|
|
1290
|
+
write(await reads.getUser());
|
|
1291
|
+
} catch {
|
|
1292
|
+
}
|
|
1293
|
+
};
|
|
1294
|
+
const retrySession = () => {
|
|
1295
|
+
if (!mounted || run) return;
|
|
1296
|
+
pending = true;
|
|
1297
|
+
if (!ready || holds.size > 0) return;
|
|
1298
|
+
pending = false;
|
|
1299
|
+
const current = {};
|
|
1300
|
+
run = current;
|
|
1301
|
+
const capturedGeneration = generation;
|
|
1302
|
+
const isCurrent = () => mounted && ready && run === current && generation === capturedGeneration && holds.size === 0;
|
|
1303
|
+
let attempts = 0;
|
|
1304
|
+
let missing = 0;
|
|
1305
|
+
let lastError = null;
|
|
1306
|
+
let resumeSettled = !reads.resume;
|
|
1307
|
+
publish({ sessionStatus: "restoring", sessionError: null });
|
|
1308
|
+
const restore = async () => {
|
|
1309
|
+
if (!isCurrent()) return;
|
|
1310
|
+
attempts++;
|
|
1311
|
+
try {
|
|
1312
|
+
const session = await reads.getCurrentSession();
|
|
1313
|
+
if (!isCurrent()) return;
|
|
1314
|
+
if (session === null && !resumeSettled && reads.resume) {
|
|
1315
|
+
const resumed = await reads.resume();
|
|
1316
|
+
if (!isCurrent()) return;
|
|
1317
|
+
resumeSettled = true;
|
|
1318
|
+
if (resumed) {
|
|
1319
|
+
attempts--;
|
|
1320
|
+
void restore();
|
|
1321
|
+
return;
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
if (session === null) {
|
|
1325
|
+
missing++;
|
|
1326
|
+
} else {
|
|
1327
|
+
if (!session || typeof session !== "object" || !("accessToken" in session) || typeof session.accessToken !== "string" || !session.accessToken) {
|
|
1328
|
+
throw new Error("The session response is invalid. Retry the session check.");
|
|
1329
|
+
}
|
|
1330
|
+
publish({ isAuthenticated: true, sessionStatus: "authenticated", sessionError: null, revalidatedAt: confirmedAt() });
|
|
1331
|
+
await refreshUser();
|
|
1332
|
+
if (isCurrent()) run = null;
|
|
1333
|
+
return;
|
|
1334
|
+
}
|
|
1335
|
+
} catch (error) {
|
|
1336
|
+
if (!isCurrent()) return;
|
|
1337
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
1338
|
+
}
|
|
1339
|
+
if (!isCurrent()) return;
|
|
1340
|
+
if (attempts < 5) {
|
|
1341
|
+
timer = setTimeout(() => {
|
|
1342
|
+
timer = void 0;
|
|
1343
|
+
void restore();
|
|
1344
|
+
}, 200 * 2 ** (attempts - 1));
|
|
1345
|
+
return;
|
|
1346
|
+
}
|
|
1347
|
+
run = null;
|
|
1348
|
+
if (missing === 5) {
|
|
1349
|
+
generation++;
|
|
1350
|
+
profileSequence++;
|
|
1351
|
+
publish({ isAuthenticated: false, user: null, sessionStatus: "signed-out", sessionError: null, revalidatedAt: confirmedAt() });
|
|
1352
|
+
} else {
|
|
1353
|
+
publish({ sessionStatus: "error", sessionError: lastError ?? new Error("The session check failed. Retry the check.") });
|
|
1354
|
+
}
|
|
1355
|
+
};
|
|
1356
|
+
void restore();
|
|
1357
|
+
};
|
|
1358
|
+
const beginAuthFlow = ({ replaceSession = false } = {}) => {
|
|
1359
|
+
if (!mounted) throw new Error("provider_unmounted");
|
|
1360
|
+
if (replaceSession) {
|
|
1361
|
+
generation++;
|
|
1362
|
+
profileSequence++;
|
|
1363
|
+
}
|
|
1364
|
+
const capturedGeneration = generation;
|
|
1365
|
+
const id = ++holdSequence;
|
|
1366
|
+
holds.add(id);
|
|
1367
|
+
cancelRestore();
|
|
1368
|
+
pending = true;
|
|
1369
|
+
const finish = () => {
|
|
1370
|
+
if (!holds.delete(id)) return;
|
|
1371
|
+
if (holds.size === 0 && pending) retrySession();
|
|
1372
|
+
};
|
|
1373
|
+
if (replaceSession) {
|
|
1374
|
+
const previous = replaceableFinish;
|
|
1375
|
+
replaceableFinish = finish;
|
|
1376
|
+
previous?.();
|
|
1377
|
+
}
|
|
1378
|
+
return Object.assign(finish, {
|
|
1379
|
+
assertCurrent: () => {
|
|
1380
|
+
if (!mounted || generation !== capturedGeneration) throw new Error("session_changed");
|
|
1381
|
+
}
|
|
1382
|
+
});
|
|
1383
|
+
};
|
|
845
1384
|
return {
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
return
|
|
1385
|
+
getSnapshot: () => snapshot,
|
|
1386
|
+
subscribe: (listener) => {
|
|
1387
|
+
listeners.add(listener);
|
|
1388
|
+
return () => {
|
|
1389
|
+
listeners.delete(listener);
|
|
1390
|
+
};
|
|
1391
|
+
},
|
|
1392
|
+
mount: () => {
|
|
1393
|
+
mounted = true;
|
|
1394
|
+
if (pending) retrySession();
|
|
1395
|
+
},
|
|
1396
|
+
unmount: () => {
|
|
1397
|
+
mounted = false;
|
|
1398
|
+
generation++;
|
|
1399
|
+
profileSequence++;
|
|
1400
|
+
holds.clear();
|
|
1401
|
+
cancelRestore();
|
|
1402
|
+
pending = true;
|
|
1403
|
+
},
|
|
1404
|
+
setReady: (value) => {
|
|
1405
|
+
if (ready === value) return;
|
|
1406
|
+
ready = value;
|
|
1407
|
+
if (value) {
|
|
1408
|
+
retrySession();
|
|
1409
|
+
return;
|
|
1410
|
+
}
|
|
1411
|
+
generation++;
|
|
1412
|
+
profileSequence++;
|
|
1413
|
+
cancelRestore();
|
|
1414
|
+
pending = true;
|
|
1415
|
+
publish({ sessionStatus: "restoring", sessionError: null });
|
|
850
1416
|
},
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
1417
|
+
retrySession,
|
|
1418
|
+
/** Replaces an active check: a suspended WebView can drop requests it had in flight. */
|
|
1419
|
+
restartSession: () => {
|
|
1420
|
+
cancelRestore();
|
|
1421
|
+
retrySession();
|
|
856
1422
|
},
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
1423
|
+
beginAuthFlow,
|
|
1424
|
+
captureUserWrite,
|
|
1425
|
+
captureUserRefresh: () => {
|
|
1426
|
+
const capturedGeneration = generation;
|
|
1427
|
+
return async () => {
|
|
1428
|
+
if (mounted && generation === capturedGeneration) await refreshUser();
|
|
1429
|
+
};
|
|
1430
|
+
},
|
|
1431
|
+
refreshUser,
|
|
1432
|
+
beginAuthOperation: () => {
|
|
1433
|
+
const finish = beginAuthFlow();
|
|
1434
|
+
const replaced = replaceableFinish;
|
|
1435
|
+
replaceableFinish = null;
|
|
1436
|
+
replaced?.();
|
|
1437
|
+
let capturedGeneration = ++generation;
|
|
1438
|
+
profileSequence++;
|
|
1439
|
+
const current = () => mounted && generation === capturedGeneration;
|
|
1440
|
+
return {
|
|
1441
|
+
finish,
|
|
1442
|
+
setIsAuthenticated: (value) => {
|
|
1443
|
+
if (!current()) return;
|
|
1444
|
+
capturedGeneration = ++generation;
|
|
1445
|
+
profileSequence++;
|
|
1446
|
+
pending = false;
|
|
1447
|
+
publish({
|
|
1448
|
+
isAuthenticated: value,
|
|
1449
|
+
user: null,
|
|
1450
|
+
sessionError: null,
|
|
1451
|
+
sessionStatus: value ? "authenticated" : "signed-out",
|
|
1452
|
+
revalidatedAt: confirmedAt()
|
|
1453
|
+
});
|
|
1454
|
+
},
|
|
1455
|
+
refreshUser: async () => {
|
|
1456
|
+
if (current()) await refreshUser();
|
|
1457
|
+
}
|
|
1458
|
+
};
|
|
1459
|
+
}
|
|
1460
|
+
};
|
|
1461
|
+
}
|
|
1462
|
+
|
|
1463
|
+
// src/session-resume.ts
|
|
1464
|
+
var import_sdk10 = require("@moon-x/core/sdk");
|
|
1465
|
+
var import_utils4 = require("@moon-x/core/utils");
|
|
1466
|
+
var LOGOUT_TOMBSTONE = '{"loggedOut":true}';
|
|
1467
|
+
var sharedStates = /* @__PURE__ */ new Map();
|
|
1468
|
+
var sharedState = (publishableKey) => {
|
|
1469
|
+
let state = sharedStates.get(publishableKey);
|
|
1470
|
+
if (!state) {
|
|
1471
|
+
state = {
|
|
1472
|
+
queue: Promise.resolve(),
|
|
1473
|
+
blockCache: void 0,
|
|
1474
|
+
installId: void 0,
|
|
1475
|
+
logoutGeneration: 0,
|
|
1476
|
+
bindSequence: 0,
|
|
1477
|
+
loginsInProgress: 0,
|
|
1478
|
+
bindDeferred: false
|
|
1479
|
+
};
|
|
1480
|
+
sharedStates.set(publishableKey, state);
|
|
1481
|
+
}
|
|
1482
|
+
return state;
|
|
1483
|
+
};
|
|
1484
|
+
var storageKey = (publishableKey) => (
|
|
1485
|
+
// expo-secure-store keys allow only alphanumerics, ".", "-" and "_".
|
|
1486
|
+
`moonx.session_resume.${publishableKey.replace(/[^A-Za-z0-9._-]/g, "_")}`
|
|
1487
|
+
);
|
|
1488
|
+
var parseRecord = (raw) => {
|
|
1489
|
+
if (!raw) return null;
|
|
1490
|
+
try {
|
|
1491
|
+
const value = JSON.parse(raw);
|
|
1492
|
+
if (typeof value.privateKey !== "string" || typeof value.publicKey !== "string") return null;
|
|
1493
|
+
if ((0, import_sdk10.sessionResumePublicKeyOf)(value.privateKey) !== value.publicKey) return null;
|
|
1494
|
+
const sessionId = typeof value.sessionId === "string" && import_sdk10.SESSION_RESUME_SESSION_ID_PATTERN.test(value.sessionId) ? value.sessionId : void 0;
|
|
1495
|
+
return {
|
|
1496
|
+
privateKey: value.privateKey,
|
|
1497
|
+
publicKey: value.publicKey,
|
|
1498
|
+
...typeof value.installId === "string" ? { installId: value.installId } : {},
|
|
1499
|
+
...sessionId ? { sessionId } : {}
|
|
1500
|
+
};
|
|
1501
|
+
} catch {
|
|
1502
|
+
return null;
|
|
1503
|
+
}
|
|
1504
|
+
};
|
|
1505
|
+
var createNativeSessionResume = ({
|
|
1506
|
+
bridge,
|
|
1507
|
+
publishableKey,
|
|
1508
|
+
storage,
|
|
1509
|
+
blockStore
|
|
1510
|
+
}) => {
|
|
1511
|
+
const key = storageKey(publishableKey);
|
|
1512
|
+
const blockKey = `session_resume_blocked:${publishableKey}`;
|
|
1513
|
+
const shared = sharedState(publishableKey);
|
|
1514
|
+
const withLock = (run) => {
|
|
1515
|
+
const next = shared.queue.then(run, run);
|
|
1516
|
+
shared.queue = next.catch(() => void 0);
|
|
1517
|
+
return next;
|
|
1518
|
+
};
|
|
1519
|
+
const blockState = async () => {
|
|
1520
|
+
if (shared.blockCache === void 0) {
|
|
1521
|
+
const stored = await blockStore.get(blockKey);
|
|
1522
|
+
shared.blockCache = stored === null ? null : stored === "logout" ? "logout" : "blocked";
|
|
1523
|
+
}
|
|
1524
|
+
return shared.blockCache;
|
|
1525
|
+
};
|
|
1526
|
+
const isBlocked = async () => await blockState() !== null;
|
|
1527
|
+
const isLoggedOut = async () => await blockState() === "logout" || await storage.getItem(key) === LOGOUT_TOMBSTONE;
|
|
1528
|
+
const writeBlock = async (state) => {
|
|
1529
|
+
shared.blockCache = state;
|
|
1530
|
+
try {
|
|
1531
|
+
if (state) await blockStore.set(blockKey, state === "logout" ? "logout" : "1");
|
|
1532
|
+
else await blockStore.remove(blockKey);
|
|
1533
|
+
return true;
|
|
1534
|
+
} catch {
|
|
1535
|
+
return false;
|
|
1536
|
+
}
|
|
1537
|
+
};
|
|
1538
|
+
const setBlocked = async (value) => {
|
|
1539
|
+
let current;
|
|
1540
|
+
try {
|
|
1541
|
+
current = await blockState();
|
|
1542
|
+
} catch {
|
|
1543
|
+
return value ? writeBlock("logout") : false;
|
|
1544
|
+
}
|
|
1545
|
+
if (current === "logout") return value ? writeBlock("logout") : true;
|
|
1546
|
+
return writeBlock(value ? "blocked" : null);
|
|
1547
|
+
};
|
|
1548
|
+
const installKey = `session_resume_install:${publishableKey}`;
|
|
1549
|
+
const installId = async () => {
|
|
1550
|
+
if (shared.installId) return shared.installId;
|
|
1551
|
+
const stored = await blockStore.get(installKey);
|
|
1552
|
+
if (stored) return shared.installId = stored;
|
|
1553
|
+
const bytes = crypto.getRandomValues(new Uint8Array(16));
|
|
1554
|
+
const created = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
1555
|
+
await blockStore.set(installKey, created);
|
|
1556
|
+
return shared.installId = created;
|
|
1557
|
+
};
|
|
1558
|
+
const read = async () => {
|
|
1559
|
+
const record = parseRecord(await storage.getItem(key));
|
|
1560
|
+
if (!record) return null;
|
|
1561
|
+
if (record.installId === await installId()) return record;
|
|
1562
|
+
await clearKey();
|
|
1563
|
+
return null;
|
|
1564
|
+
};
|
|
1565
|
+
const write = async (record) => storage.setItem(key, JSON.stringify({ ...record, installId: await installId() }));
|
|
1566
|
+
const clearKey = async () => {
|
|
1567
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
1568
|
+
try {
|
|
1569
|
+
await storage.removeItem(key);
|
|
1570
|
+
await setBlocked(false);
|
|
1571
|
+
return "deleted";
|
|
1572
|
+
} catch {
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
let tombstoned = false;
|
|
1576
|
+
try {
|
|
1577
|
+
await storage.setItem(key, "{}");
|
|
1578
|
+
tombstoned = true;
|
|
1579
|
+
} catch {
|
|
1580
|
+
}
|
|
1581
|
+
const markerPersisted = await setBlocked(true);
|
|
1582
|
+
if (!tombstoned && !markerPersisted) {
|
|
1583
|
+
console.warn(
|
|
1584
|
+
"MoonX: could not invalidate the session resume key in either store; it is blocked until the app restarts."
|
|
1585
|
+
);
|
|
1586
|
+
return "unrecorded";
|
|
1587
|
+
}
|
|
1588
|
+
return "invalidated";
|
|
1589
|
+
};
|
|
1590
|
+
const invalidateUnreadable = async () => {
|
|
1591
|
+
if (await setBlocked(true)) return true;
|
|
1592
|
+
try {
|
|
1593
|
+
await storage.setItem(key, LOGOUT_TOMBSTONE);
|
|
1594
|
+
return true;
|
|
1595
|
+
} catch {
|
|
1596
|
+
return false;
|
|
1597
|
+
}
|
|
1598
|
+
};
|
|
1599
|
+
const replace = async (record) => {
|
|
1600
|
+
try {
|
|
1601
|
+
await write(record);
|
|
1602
|
+
return true;
|
|
1603
|
+
} catch {
|
|
1604
|
+
await clearKey();
|
|
1605
|
+
return false;
|
|
1606
|
+
}
|
|
1607
|
+
};
|
|
1608
|
+
const forgetRecordedSession = async (record) => {
|
|
1609
|
+
try {
|
|
1610
|
+
await write({ privateKey: record.privateKey, publicKey: record.publicKey });
|
|
1611
|
+
return true;
|
|
1612
|
+
} catch {
|
|
1613
|
+
return await clearKey() !== "unrecorded";
|
|
860
1614
|
}
|
|
861
1615
|
};
|
|
1616
|
+
const bind = async () => {
|
|
1617
|
+
if (shared.loginsInProgress > 0) {
|
|
1618
|
+
shared.bindDeferred = true;
|
|
1619
|
+
return;
|
|
1620
|
+
}
|
|
1621
|
+
const sequence = ++shared.bindSequence;
|
|
1622
|
+
const isLatest = () => shared.bindSequence === sequence;
|
|
1623
|
+
await withLock(async () => {
|
|
1624
|
+
if (!isLatest()) return;
|
|
1625
|
+
try {
|
|
1626
|
+
if (await isLoggedOut()) return;
|
|
1627
|
+
} catch {
|
|
1628
|
+
await invalidateUnreadable();
|
|
1629
|
+
return;
|
|
1630
|
+
}
|
|
1631
|
+
let record;
|
|
1632
|
+
try {
|
|
1633
|
+
record = await read();
|
|
1634
|
+
} catch {
|
|
1635
|
+
await invalidateUnreadable();
|
|
1636
|
+
return;
|
|
1637
|
+
}
|
|
1638
|
+
if (!record) {
|
|
1639
|
+
record = (0, import_sdk10.createSessionResumeKeyPair)();
|
|
1640
|
+
try {
|
|
1641
|
+
await write(record);
|
|
1642
|
+
} catch {
|
|
1643
|
+
return;
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
let result = null;
|
|
1647
|
+
try {
|
|
1648
|
+
result = await bridge.send(
|
|
1649
|
+
import_utils4.PostMessageType.AUTH,
|
|
1650
|
+
import_utils4.PostMessageMethod.SESSION_RESUME_BIND,
|
|
1651
|
+
{ publishableKey, publicKey: record.publicKey }
|
|
1652
|
+
);
|
|
1653
|
+
} catch {
|
|
1654
|
+
}
|
|
1655
|
+
if (!isLatest()) return;
|
|
1656
|
+
if (result?.bound && import_sdk10.SESSION_RESUME_SESSION_ID_PATTERN.test(result.sessionId)) {
|
|
1657
|
+
if (record.sessionId === result.sessionId || await replace({ ...record, sessionId: result.sessionId })) {
|
|
1658
|
+
await setBlocked(false);
|
|
1659
|
+
}
|
|
1660
|
+
return;
|
|
1661
|
+
}
|
|
1662
|
+
if (record.sessionId && result?.sessionId !== record.sessionId) {
|
|
1663
|
+
await replace({ privateKey: record.privateKey, publicKey: record.publicKey });
|
|
1664
|
+
}
|
|
1665
|
+
});
|
|
1666
|
+
};
|
|
1667
|
+
const resume = () => withLock(async () => {
|
|
1668
|
+
if (await isBlocked()) return false;
|
|
1669
|
+
const record = await read();
|
|
1670
|
+
const sessionId = record?.sessionId;
|
|
1671
|
+
if (!record || !sessionId) return false;
|
|
1672
|
+
const check = await bridge.send(
|
|
1673
|
+
import_utils4.PostMessageType.AUTH,
|
|
1674
|
+
import_utils4.PostMessageMethod.IS_AUTHENTICATED,
|
|
1675
|
+
{ publishableKey, resumeSessionId: sessionId }
|
|
1676
|
+
);
|
|
1677
|
+
if (check?.isAuthenticated === true) return true;
|
|
1678
|
+
if (check?.resumeRejected) {
|
|
1679
|
+
await clearKey();
|
|
1680
|
+
return false;
|
|
1681
|
+
}
|
|
1682
|
+
const challenge = check?.resumeChallenge;
|
|
1683
|
+
if (typeof challenge !== "string" || !import_sdk10.SESSION_RESUME_CHALLENGE_PATTERN.test(challenge)) return false;
|
|
1684
|
+
const signature = (0, import_sdk10.signSessionResume)(record.privateKey, sessionId, challenge);
|
|
1685
|
+
if (!signature) return false;
|
|
1686
|
+
const resumed = await bridge.send(
|
|
1687
|
+
import_utils4.PostMessageType.AUTH,
|
|
1688
|
+
import_utils4.PostMessageMethod.SESSION_RESUME,
|
|
1689
|
+
{ publishableKey, sessionId, challenge, signature }
|
|
1690
|
+
);
|
|
1691
|
+
if (resumed?.resumeRejected) {
|
|
1692
|
+
await clearKey();
|
|
1693
|
+
return false;
|
|
1694
|
+
}
|
|
1695
|
+
return resumed?.isAuthenticated === true;
|
|
1696
|
+
});
|
|
1697
|
+
const logout = (run) => withLock(async () => {
|
|
1698
|
+
shared.logoutGeneration++;
|
|
1699
|
+
if (await clearKey() !== "deleted") {
|
|
1700
|
+
console.warn("MoonX: could not delete the session resume key; continuing logout to revoke the session.");
|
|
1701
|
+
}
|
|
1702
|
+
if (!await writeBlock("logout")) {
|
|
1703
|
+
try {
|
|
1704
|
+
await storage.setItem(key, LOGOUT_TOMBSTONE);
|
|
1705
|
+
} catch {
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
return run();
|
|
1709
|
+
});
|
|
1710
|
+
const cancelPendingBinds = () => {
|
|
1711
|
+
shared.bindSequence++;
|
|
1712
|
+
};
|
|
1713
|
+
const forgetSession = () => withLock(async () => {
|
|
1714
|
+
let record;
|
|
1715
|
+
try {
|
|
1716
|
+
record = await read();
|
|
1717
|
+
} catch {
|
|
1718
|
+
if (!await invalidateUnreadable()) throw new Error("session_resume_storage_unavailable");
|
|
1719
|
+
return;
|
|
1720
|
+
}
|
|
1721
|
+
if (record?.sessionId && !await forgetRecordedSession(record) && !await invalidateUnreadable()) {
|
|
1722
|
+
throw new Error("session_resume_storage_unavailable");
|
|
1723
|
+
}
|
|
1724
|
+
});
|
|
1725
|
+
const clearLogoutBlock = (generation) => withLock(async () => {
|
|
1726
|
+
if (shared.logoutGeneration !== generation) return;
|
|
1727
|
+
try {
|
|
1728
|
+
if (await blockState() === "logout") await writeBlock(null);
|
|
1729
|
+
if (await storage.getItem(key) === LOGOUT_TOMBSTONE) await storage.removeItem(key);
|
|
1730
|
+
} catch {
|
|
1731
|
+
}
|
|
1732
|
+
});
|
|
1733
|
+
const beginLogin = async () => {
|
|
1734
|
+
shared.loginsInProgress++;
|
|
1735
|
+
const generation = shared.logoutGeneration;
|
|
1736
|
+
cancelPendingBinds();
|
|
1737
|
+
let released = false;
|
|
1738
|
+
const release = (succeeded = false) => {
|
|
1739
|
+
if (released) return;
|
|
1740
|
+
released = true;
|
|
1741
|
+
shared.loginsInProgress--;
|
|
1742
|
+
const cleared = succeeded ? clearLogoutBlock(generation) : Promise.resolve();
|
|
1743
|
+
if (shared.loginsInProgress === 0 && shared.bindDeferred) {
|
|
1744
|
+
shared.bindDeferred = false;
|
|
1745
|
+
void cleared.then(() => bind());
|
|
1746
|
+
}
|
|
1747
|
+
};
|
|
1748
|
+
try {
|
|
1749
|
+
await forgetSession();
|
|
1750
|
+
} catch (error) {
|
|
1751
|
+
release();
|
|
1752
|
+
throw error;
|
|
1753
|
+
}
|
|
1754
|
+
return release;
|
|
1755
|
+
};
|
|
1756
|
+
return { bind, beginLogin, cancelPendingBinds, resume, logout };
|
|
862
1757
|
};
|
|
863
1758
|
|
|
864
1759
|
// src/provider.tsx
|
|
865
|
-
var
|
|
1760
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
866
1761
|
var DEFAULT_IFRAME_URL = "https://x-iframe.moonpay.com";
|
|
867
|
-
var MoonXContext = (0,
|
|
1762
|
+
var MoonXContext = (0, import_react2.createContext)(null);
|
|
868
1763
|
var MoonXProvider = ({
|
|
869
1764
|
publishableKey,
|
|
870
1765
|
config,
|
|
871
1766
|
children
|
|
872
1767
|
}) => {
|
|
873
1768
|
(0, import_lib2.validatePublishableKey)(publishableKey);
|
|
1769
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(NativeProvider, { publishableKey, config, children }, JSON.stringify([publishableKey, config?.iframeUrl ?? DEFAULT_IFRAME_URL]));
|
|
1770
|
+
};
|
|
1771
|
+
var NativeProvider = ({ publishableKey, config, children }) => {
|
|
874
1772
|
const iframeUrl = config?.iframeUrl ?? DEFAULT_IFRAME_URL;
|
|
875
1773
|
const relyingPartyOrigin = config?.relyingPartyOrigin ?? "";
|
|
876
|
-
const webViewRef = (0,
|
|
877
|
-
const bridge = (0,
|
|
1774
|
+
const webViewRef = (0, import_react2.useRef)(null);
|
|
1775
|
+
const bridge = (0, import_react2.useMemo)(
|
|
878
1776
|
() => new WebViewBridge(
|
|
879
1777
|
webViewRef
|
|
880
1778
|
),
|
|
881
1779
|
[]
|
|
882
1780
|
);
|
|
883
|
-
const relyingPartyOriginResolver = (0,
|
|
1781
|
+
const relyingPartyOriginResolver = (0, import_react2.useMemo)(
|
|
884
1782
|
() => makeRelyingPartyOriginResolver(bridge, publishableKey, relyingPartyOrigin),
|
|
885
1783
|
[bridge, publishableKey, relyingPartyOrigin]
|
|
886
1784
|
);
|
|
887
|
-
const [sensitiveVisible, setSensitiveVisible] = (0,
|
|
888
|
-
const [captchaChallengeVisible, setCaptchaChallengeVisible] = (0,
|
|
889
|
-
const cancelRef = (0,
|
|
1785
|
+
const [sensitiveVisible, setSensitiveVisible] = (0, import_react2.useState)(false);
|
|
1786
|
+
const [captchaChallengeVisible, setCaptchaChallengeVisible] = (0, import_react2.useState)(false);
|
|
1787
|
+
const cancelRef = (0, import_react2.useRef)(null);
|
|
890
1788
|
const webViewVisible = sensitiveVisible || captchaChallengeVisible;
|
|
891
1789
|
const [
|
|
892
1790
|
{
|
|
@@ -895,76 +1793,78 @@ var MoonXProvider = ({
|
|
|
895
1793
|
key: webViewKey
|
|
896
1794
|
},
|
|
897
1795
|
dispatchWebViewLifecycle
|
|
898
|
-
] = (0,
|
|
899
|
-
const
|
|
900
|
-
const
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
1796
|
+
] = (0, import_react2.useReducer)(webViewLifecycleReducer, initialWebViewLifecycleState);
|
|
1797
|
+
const sessionResumeStorage = config?.sessionResumeStorage;
|
|
1798
|
+
const sessionResume = (0, import_react2.useMemo)(
|
|
1799
|
+
() => sessionResumeStorage ? createNativeSessionResume({
|
|
1800
|
+
bridge,
|
|
1801
|
+
publishableKey,
|
|
1802
|
+
storage: sessionResumeStorage,
|
|
1803
|
+
blockStore: createAsyncStorageKv()
|
|
1804
|
+
}) : void 0,
|
|
1805
|
+
[bridge, publishableKey, sessionResumeStorage]
|
|
907
1806
|
);
|
|
908
|
-
const
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
}, []);
|
|
923
|
-
(0,
|
|
924
|
-
|
|
1807
|
+
const session = (0, import_react2.useMemo)(
|
|
1808
|
+
() => createNativeSession({
|
|
1809
|
+
getCurrentSession: () => bridge.send(import_utils5.PostMessageType.AUTH, import_utils5.PostMessageMethod.GET_CURRENT_SESSION, { publishableKey }),
|
|
1810
|
+
getUser: () => bridge.send(import_utils5.PostMessageType.AUTH, import_utils5.PostMessageMethod.GET_USER, { publishableKey }),
|
|
1811
|
+
resume: sessionResume?.resume
|
|
1812
|
+
}),
|
|
1813
|
+
[bridge, publishableKey, sessionResume]
|
|
1814
|
+
);
|
|
1815
|
+
const sessionSnapshot = (0, import_react2.useSyncExternalStore)(session.subscribe, session.getSnapshot, session.getSnapshot);
|
|
1816
|
+
const { isAuthenticated, user, revalidatedAt } = sessionSnapshot;
|
|
1817
|
+
const { beginAuthFlow, refreshUser } = session;
|
|
1818
|
+
(0, import_react2.useLayoutEffect)(() => {
|
|
1819
|
+
session.mount();
|
|
1820
|
+
return session.unmount;
|
|
1821
|
+
}, [session]);
|
|
1822
|
+
(0, import_react2.useEffect)(() => {
|
|
1823
|
+
session.setReady(ready);
|
|
1824
|
+
}, [session, ready]);
|
|
1825
|
+
const { sessionStatus } = sessionSnapshot;
|
|
1826
|
+
(0, import_react2.useEffect)(() => {
|
|
1827
|
+
if (!sessionResume || !ready || sessionStatus !== "authenticated") return;
|
|
1828
|
+
void sessionResume.bind();
|
|
1829
|
+
}, [sessionResume, ready, sessionStatus, revalidatedAt]);
|
|
1830
|
+
(0, import_react2.useEffect)(() => {
|
|
1831
|
+
const subscription = import_react_native2.AppState.addEventListener("change", (nextState) => {
|
|
925
1832
|
if (nextState === "active") {
|
|
926
|
-
|
|
1833
|
+
session.restartSession();
|
|
927
1834
|
}
|
|
928
1835
|
});
|
|
929
1836
|
return () => subscription.remove();
|
|
930
|
-
}, []);
|
|
931
|
-
const { height: screenHeight } = (0,
|
|
1837
|
+
}, [session]);
|
|
1838
|
+
const { height: screenHeight } = (0, import_react_native2.useWindowDimensions)();
|
|
932
1839
|
const SHEET_TOP_OFFSET = 80;
|
|
933
|
-
const sheetTranslateY = (0,
|
|
934
|
-
const backdropOpacity = (0,
|
|
935
|
-
(0,
|
|
936
|
-
|
|
937
|
-
|
|
1840
|
+
const sheetTranslateY = (0, import_react2.useRef)(new import_react_native2.Animated.Value(screenHeight)).current;
|
|
1841
|
+
const backdropOpacity = (0, import_react2.useRef)(new import_react_native2.Animated.Value(0)).current;
|
|
1842
|
+
(0, import_react2.useEffect)(() => {
|
|
1843
|
+
import_react_native2.Animated.parallel([
|
|
1844
|
+
import_react_native2.Animated.timing(sheetTranslateY, {
|
|
938
1845
|
toValue: webViewVisible ? 0 : screenHeight,
|
|
939
1846
|
duration: 280,
|
|
940
1847
|
useNativeDriver: true
|
|
941
1848
|
}),
|
|
942
|
-
|
|
1849
|
+
import_react_native2.Animated.timing(backdropOpacity, {
|
|
943
1850
|
toValue: webViewVisible ? 0.5 : 0,
|
|
944
1851
|
duration: 280,
|
|
945
1852
|
useNativeDriver: true
|
|
946
1853
|
})
|
|
947
1854
|
]).start();
|
|
948
1855
|
}, [webViewVisible, sheetTranslateY, backdropOpacity, screenHeight]);
|
|
949
|
-
const initialSourceUri = (0,
|
|
1856
|
+
const initialSourceUri = (0, import_react2.useMemo)(() => {
|
|
950
1857
|
const base = iframeUrl.replace(/\/$/, "");
|
|
951
1858
|
const sep = base.includes("?") ? "&" : "?";
|
|
952
1859
|
return `${base}${sep}publishableKey=${encodeURIComponent(publishableKey)}`;
|
|
953
1860
|
}, [iframeUrl, publishableKey]);
|
|
954
|
-
const handleMessage = (0,
|
|
1861
|
+
const handleMessage = (0, import_react2.useCallback)(
|
|
955
1862
|
(event) => {
|
|
956
1863
|
bridge.handleMessage({ nativeEvent: { data: event.nativeEvent.data } });
|
|
957
1864
|
},
|
|
958
1865
|
[bridge]
|
|
959
1866
|
);
|
|
960
|
-
const
|
|
961
|
-
try {
|
|
962
|
-
const fetched = await sdkRef.current.getUser();
|
|
963
|
-
if (fetched) setUser(fetched);
|
|
964
|
-
} catch {
|
|
965
|
-
}
|
|
966
|
-
}, []);
|
|
967
|
-
const navigateWebView = (0, import_react.useCallback)(
|
|
1867
|
+
const navigateWebView = (0, import_react2.useCallback)(
|
|
968
1868
|
(path) => {
|
|
969
1869
|
const ref = webViewRef.current;
|
|
970
1870
|
if (!ref) return;
|
|
@@ -979,7 +1879,7 @@ var MoonXProvider = ({
|
|
|
979
1879
|
},
|
|
980
1880
|
[iframeUrl, publishableKey]
|
|
981
1881
|
);
|
|
982
|
-
const runSensitiveFlow = (0,
|
|
1882
|
+
const runSensitiveFlow = (0, import_react2.useCallback)(
|
|
983
1883
|
(options) => {
|
|
984
1884
|
setSensitiveVisible(true);
|
|
985
1885
|
navigateWebView(options.flowPath);
|
|
@@ -1021,7 +1921,7 @@ var MoonXProvider = ({
|
|
|
1021
1921
|
},
|
|
1022
1922
|
[bridge, navigateWebView]
|
|
1023
1923
|
);
|
|
1024
|
-
const sdk = (0,
|
|
1924
|
+
const sdk = (0, import_react2.useMemo)(
|
|
1025
1925
|
() => createSdkInstance({
|
|
1026
1926
|
bridge,
|
|
1027
1927
|
publishableKey,
|
|
@@ -1035,9 +1935,9 @@ var MoonXProvider = ({
|
|
|
1035
1935
|
user,
|
|
1036
1936
|
ready,
|
|
1037
1937
|
revalidatedAt,
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1938
|
+
refreshUser,
|
|
1939
|
+
session,
|
|
1940
|
+
sessionResume
|
|
1041
1941
|
}),
|
|
1042
1942
|
[
|
|
1043
1943
|
bridge,
|
|
@@ -1047,22 +1947,24 @@ var MoonXProvider = ({
|
|
|
1047
1947
|
user,
|
|
1048
1948
|
ready,
|
|
1049
1949
|
revalidatedAt,
|
|
1050
|
-
refreshUser
|
|
1950
|
+
refreshUser,
|
|
1951
|
+
session,
|
|
1952
|
+
sessionResume,
|
|
1953
|
+
sessionSnapshot
|
|
1051
1954
|
]
|
|
1052
1955
|
);
|
|
1053
|
-
const
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
}, [sdk]);
|
|
1057
|
-
(0, import_react.useEffect)(() => {
|
|
1956
|
+
const sessionResumeRef = (0, import_react2.useRef)(sessionResume);
|
|
1957
|
+
sessionResumeRef.current = sessionResume;
|
|
1958
|
+
(0, import_react2.useEffect)(() => {
|
|
1058
1959
|
return () => {
|
|
1960
|
+
sessionResumeRef.current?.cancelPendingBinds();
|
|
1059
1961
|
bridge.cleanup("provider_unmounted");
|
|
1060
1962
|
};
|
|
1061
1963
|
}, [bridge]);
|
|
1062
|
-
(0,
|
|
1964
|
+
(0, import_react2.useEffect)(
|
|
1063
1965
|
() => bridge.register(
|
|
1064
|
-
|
|
1065
|
-
|
|
1966
|
+
import_utils5.PostMessageType.INIT,
|
|
1967
|
+
import_utils5.PostMessageMethod.CAPTCHA_CHALLENGE_VISIBILITY,
|
|
1066
1968
|
(payload) => {
|
|
1067
1969
|
if (typeof payload?.visible === "boolean") {
|
|
1068
1970
|
setCaptchaChallengeVisible(payload.visible);
|
|
@@ -1071,21 +1973,17 @@ var MoonXProvider = ({
|
|
|
1071
1973
|
),
|
|
1072
1974
|
[bridge]
|
|
1073
1975
|
);
|
|
1074
|
-
(0,
|
|
1976
|
+
(0, import_react2.useEffect)(() => {
|
|
1075
1977
|
const unsubscribe = bridge.onResponseError((error) => {
|
|
1076
|
-
|
|
1077
|
-
error,
|
|
1078
|
-
authFlowsInFlight: authFlowsInFlightRef.current,
|
|
1079
|
-
revalidate: () => setRestoreNonce((value) => value + 1)
|
|
1080
|
-
});
|
|
1978
|
+
if (error.message === "no_session") session.retrySession();
|
|
1081
1979
|
});
|
|
1082
1980
|
return unsubscribe;
|
|
1083
|
-
}, [bridge,
|
|
1084
|
-
(0,
|
|
1981
|
+
}, [bridge, session]);
|
|
1982
|
+
(0, import_react2.useEffect)(() => {
|
|
1085
1983
|
let cancelled = false;
|
|
1086
1984
|
const unregister = bridge.register(
|
|
1087
|
-
|
|
1088
|
-
|
|
1985
|
+
import_utils5.PostMessageType.INIT,
|
|
1986
|
+
import_utils5.PostMessageMethod.READY,
|
|
1089
1987
|
() => {
|
|
1090
1988
|
if (!cancelled) dispatchWebViewLifecycle({ type: "ready" });
|
|
1091
1989
|
}
|
|
@@ -1099,65 +1997,7 @@ var MoonXProvider = ({
|
|
|
1099
1997
|
clearTimeout(fallback);
|
|
1100
1998
|
};
|
|
1101
1999
|
}, [bridge, webViewRecoveryNonce]);
|
|
1102
|
-
(0,
|
|
1103
|
-
if (!ready) return;
|
|
1104
|
-
const revalidationGeneration = sessionRevalidationGate.begin();
|
|
1105
|
-
let cancelled = false;
|
|
1106
|
-
let attempt = 0;
|
|
1107
|
-
const maxAttempts = 5;
|
|
1108
|
-
const timers = /* @__PURE__ */ new Set();
|
|
1109
|
-
const schedule = (callback, delay) => {
|
|
1110
|
-
const timer = setTimeout(() => {
|
|
1111
|
-
timers.delete(timer);
|
|
1112
|
-
callback();
|
|
1113
|
-
}, delay);
|
|
1114
|
-
timers.add(timer);
|
|
1115
|
-
};
|
|
1116
|
-
const tryRestore = async () => {
|
|
1117
|
-
if (cancelled || authFlowsInFlightRef.current > 0) return;
|
|
1118
|
-
attempt += 1;
|
|
1119
|
-
try {
|
|
1120
|
-
const session = await sdkRef.current.getCurrentSession();
|
|
1121
|
-
if (cancelled || authFlowsInFlightRef.current > 0) return;
|
|
1122
|
-
if (session) {
|
|
1123
|
-
setIsAuthenticated(true);
|
|
1124
|
-
try {
|
|
1125
|
-
const u = await sdkRef.current.getUser();
|
|
1126
|
-
if (!cancelled && u) setUser(u);
|
|
1127
|
-
} catch {
|
|
1128
|
-
}
|
|
1129
|
-
if (!cancelled) setRevalidatedAt(Date.now());
|
|
1130
|
-
sessionRevalidationGate.finish(revalidationGeneration);
|
|
1131
|
-
return;
|
|
1132
|
-
}
|
|
1133
|
-
if (attempt < maxAttempts) {
|
|
1134
|
-
const delay = 200 * 2 ** (attempt - 1);
|
|
1135
|
-
schedule(tryRestore, delay);
|
|
1136
|
-
} else if (!cancelled) {
|
|
1137
|
-
setIsAuthenticated(false);
|
|
1138
|
-
setUser(null);
|
|
1139
|
-
setRevalidatedAt(Date.now());
|
|
1140
|
-
sessionRevalidationGate.finish(revalidationGeneration);
|
|
1141
|
-
}
|
|
1142
|
-
} catch {
|
|
1143
|
-
if (authFlowsInFlightRef.current > 0) return;
|
|
1144
|
-
if (!cancelled && attempt < maxAttempts) {
|
|
1145
|
-
const delay = 200 * 2 ** (attempt - 1);
|
|
1146
|
-
schedule(tryRestore, delay);
|
|
1147
|
-
} else if (!cancelled) {
|
|
1148
|
-
setRevalidatedAt(Date.now());
|
|
1149
|
-
sessionRevalidationGate.finish(revalidationGeneration);
|
|
1150
|
-
}
|
|
1151
|
-
}
|
|
1152
|
-
};
|
|
1153
|
-
schedule(tryRestore, restoreNonce > 0 ? 150 : 0);
|
|
1154
|
-
return () => {
|
|
1155
|
-
cancelled = true;
|
|
1156
|
-
timers.forEach(clearTimeout);
|
|
1157
|
-
timers.clear();
|
|
1158
|
-
};
|
|
1159
|
-
}, [ready, restoreNonce, sessionRevalidationGate]);
|
|
1160
|
-
const contextValue = (0, import_react.useMemo)(
|
|
2000
|
+
const contextValue = (0, import_react2.useMemo)(
|
|
1161
2001
|
() => ({
|
|
1162
2002
|
sdk,
|
|
1163
2003
|
_bridge: bridge,
|
|
@@ -1176,17 +2016,24 @@ var MoonXProvider = ({
|
|
|
1176
2016
|
]
|
|
1177
2017
|
);
|
|
1178
2018
|
const closeLabel = config?.sensitiveModalCopy?.closeLabel ?? "Done";
|
|
1179
|
-
return /* @__PURE__ */ (0,
|
|
2019
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(MoonXContext.Provider, { value: contextValue, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_react3.SDKProvider, { value: sdk, children: [
|
|
1180
2020
|
children,
|
|
1181
|
-
/* @__PURE__ */ (0,
|
|
1182
|
-
|
|
2021
|
+
config?.presenceOtpPrompt !== false && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
2022
|
+
PresenceOtpPrompt,
|
|
1183
2023
|
{
|
|
1184
|
-
|
|
2024
|
+
publishableKey,
|
|
2025
|
+
options: config?.presenceOtpPrompt
|
|
2026
|
+
}
|
|
2027
|
+
),
|
|
2028
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
2029
|
+
import_react_native2.Animated.View,
|
|
2030
|
+
{
|
|
2031
|
+
style: [styles2.backdrop, { opacity: backdropOpacity }],
|
|
1185
2032
|
pointerEvents: webViewVisible ? "auto" : "none",
|
|
1186
|
-
children: /* @__PURE__ */ (0,
|
|
1187
|
-
|
|
2033
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
2034
|
+
import_react_native2.Pressable,
|
|
1188
2035
|
{
|
|
1189
|
-
style:
|
|
2036
|
+
style: import_react_native2.StyleSheet.absoluteFill,
|
|
1190
2037
|
onPress: () => {
|
|
1191
2038
|
if (sensitiveVisible) cancelRef.current?.();
|
|
1192
2039
|
}
|
|
@@ -1194,11 +2041,11 @@ var MoonXProvider = ({
|
|
|
1194
2041
|
)
|
|
1195
2042
|
}
|
|
1196
2043
|
),
|
|
1197
|
-
/* @__PURE__ */ (0,
|
|
1198
|
-
|
|
2044
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
2045
|
+
import_react_native2.Animated.View,
|
|
1199
2046
|
{
|
|
1200
2047
|
style: [
|
|
1201
|
-
|
|
2048
|
+
styles2.sheetContainer,
|
|
1202
2049
|
{
|
|
1203
2050
|
top: captchaChallengeVisible ? 0 : SHEET_TOP_OFFSET,
|
|
1204
2051
|
transform: [{ translateY: sheetTranslateY }]
|
|
@@ -1206,44 +2053,48 @@ var MoonXProvider = ({
|
|
|
1206
2053
|
],
|
|
1207
2054
|
pointerEvents: webViewVisible ? "auto" : "none",
|
|
1208
2055
|
children: [
|
|
1209
|
-
sensitiveVisible && /* @__PURE__ */ (0,
|
|
1210
|
-
/* @__PURE__ */ (0,
|
|
1211
|
-
/* @__PURE__ */ (0,
|
|
1212
|
-
/* @__PURE__ */ (0,
|
|
1213
|
-
/* @__PURE__ */ (0,
|
|
1214
|
-
|
|
2056
|
+
sensitiveVisible && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_jsx_runtime2.Fragment, { children: [
|
|
2057
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native2.View, { style: styles2.sheetGrabber }),
|
|
2058
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_react_native2.View, { style: styles2.modalChrome, children: [
|
|
2059
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native2.View, { style: styles2.modalTitleSpacer }),
|
|
2060
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
2061
|
+
import_react_native2.Pressable,
|
|
1215
2062
|
{
|
|
1216
2063
|
onPress: () => cancelRef.current?.(),
|
|
1217
2064
|
hitSlop: 12,
|
|
1218
|
-
style:
|
|
1219
|
-
children: /* @__PURE__ */ (0,
|
|
2065
|
+
style: styles2.closeButton,
|
|
2066
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native2.Text, { style: styles2.closeButtonText, children: closeLabel })
|
|
1220
2067
|
}
|
|
1221
2068
|
)
|
|
1222
2069
|
] })
|
|
1223
2070
|
] }),
|
|
1224
|
-
/* @__PURE__ */ (0,
|
|
2071
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
1225
2072
|
import_react_native_webview.WebView,
|
|
1226
2073
|
{
|
|
1227
2074
|
ref: webViewRef,
|
|
1228
2075
|
source: { uri: initialSourceUri },
|
|
1229
2076
|
onMessage: handleMessage,
|
|
1230
2077
|
onContentProcessDidTerminate: () => {
|
|
2078
|
+
session.setReady(false);
|
|
1231
2079
|
dispatchWebViewLifecycle({ type: "recover", remount: false });
|
|
2080
|
+
sessionResume?.cancelPendingBinds();
|
|
1232
2081
|
bridge.rejectAllPending("webview_process_terminated");
|
|
1233
2082
|
webViewRef.current?.reload();
|
|
1234
2083
|
},
|
|
1235
2084
|
onRenderProcessGone: () => {
|
|
2085
|
+
session.setReady(false);
|
|
1236
2086
|
dispatchWebViewLifecycle({ type: "recover", remount: true });
|
|
2087
|
+
sessionResume?.cancelPendingBinds();
|
|
1237
2088
|
bridge.rejectAllPending("webview_process_terminated");
|
|
1238
2089
|
},
|
|
1239
2090
|
webviewDebuggingEnabled: __DEV__,
|
|
1240
2091
|
javaScriptEnabled: true,
|
|
1241
2092
|
domStorageEnabled: true,
|
|
1242
|
-
sharedCookiesEnabled:
|
|
1243
|
-
thirdPartyCookiesEnabled:
|
|
2093
|
+
sharedCookiesEnabled: import_react_native2.Platform.OS === "ios",
|
|
2094
|
+
thirdPartyCookiesEnabled: import_react_native2.Platform.OS === "android",
|
|
1244
2095
|
startInLoadingState: false,
|
|
1245
2096
|
originWhitelist: ["https://*", "http://*"],
|
|
1246
|
-
style:
|
|
2097
|
+
style: styles2.webView
|
|
1247
2098
|
},
|
|
1248
2099
|
webViewKey
|
|
1249
2100
|
)
|
|
@@ -1253,13 +2104,13 @@ var MoonXProvider = ({
|
|
|
1253
2104
|
] }) });
|
|
1254
2105
|
};
|
|
1255
2106
|
var useMoonXContext = () => {
|
|
1256
|
-
const ctx = (0,
|
|
2107
|
+
const ctx = (0, import_react2.useContext)(MoonXContext);
|
|
1257
2108
|
if (!ctx) {
|
|
1258
2109
|
throw new Error("useMoonX must be used within a MoonXProvider");
|
|
1259
2110
|
}
|
|
1260
2111
|
return ctx;
|
|
1261
2112
|
};
|
|
1262
|
-
var
|
|
2113
|
+
var styles2 = import_react_native2.StyleSheet.create({
|
|
1263
2114
|
backdrop: {
|
|
1264
2115
|
position: "absolute",
|
|
1265
2116
|
top: 0,
|
|
@@ -1309,7 +2160,7 @@ var styles = import_react_native.StyleSheet.create({
|
|
|
1309
2160
|
paddingBottom: 12,
|
|
1310
2161
|
paddingHorizontal: 16,
|
|
1311
2162
|
backgroundColor: "#0a0a0a",
|
|
1312
|
-
borderBottomWidth:
|
|
2163
|
+
borderBottomWidth: import_react_native2.StyleSheet.hairlineWidth,
|
|
1313
2164
|
borderBottomColor: "#222"
|
|
1314
2165
|
},
|
|
1315
2166
|
// Empty flex spacer keeps the close button right-aligned without
|
|
@@ -1331,19 +2182,19 @@ var styles = import_react_native.StyleSheet.create({
|
|
|
1331
2182
|
});
|
|
1332
2183
|
|
|
1333
2184
|
// src/hooks/index.ts
|
|
1334
|
-
var
|
|
2185
|
+
var import_react9 = require("@moon-x/core/react");
|
|
1335
2186
|
|
|
1336
2187
|
// src/hooks/use-app-config.ts
|
|
1337
|
-
var
|
|
1338
|
-
var
|
|
2188
|
+
var import_utils6 = require("@moon-x/core/utils");
|
|
2189
|
+
var import_react4 = require("react");
|
|
1339
2190
|
var useAppConfig = (publishableKey) => {
|
|
1340
2191
|
const { sdk, _bridge } = useMoonXContext();
|
|
1341
2192
|
const resolvedKey = publishableKey ?? sdk.publishableKey;
|
|
1342
2193
|
const ready = sdk.ready;
|
|
1343
|
-
const [appConfig, setAppConfig] = (0,
|
|
1344
|
-
const [isLoading, setIsLoading] = (0,
|
|
1345
|
-
const [error, setError] = (0,
|
|
1346
|
-
(0,
|
|
2194
|
+
const [appConfig, setAppConfig] = (0, import_react4.useState)(null);
|
|
2195
|
+
const [isLoading, setIsLoading] = (0, import_react4.useState)(true);
|
|
2196
|
+
const [error, setError] = (0, import_react4.useState)(null);
|
|
2197
|
+
(0, import_react4.useEffect)(() => {
|
|
1347
2198
|
if (!resolvedKey) {
|
|
1348
2199
|
setIsLoading(false);
|
|
1349
2200
|
return;
|
|
@@ -1357,7 +2208,7 @@ var useAppConfig = (publishableKey) => {
|
|
|
1357
2208
|
setError(null);
|
|
1358
2209
|
setAppConfig(null);
|
|
1359
2210
|
try {
|
|
1360
|
-
const config = await _bridge.send(
|
|
2211
|
+
const config = await _bridge.send(import_utils6.PostMessageType.AUTH, import_utils6.PostMessageMethod.GET_APP_CONFIG, {
|
|
1361
2212
|
publishableKey: resolvedKey
|
|
1362
2213
|
});
|
|
1363
2214
|
if (!cancelled) setAppConfig(config);
|
|
@@ -1378,8 +2229,8 @@ var useAppConfig = (publishableKey) => {
|
|
|
1378
2229
|
};
|
|
1379
2230
|
|
|
1380
2231
|
// src/hooks/use-attach-oauth.ts
|
|
1381
|
-
var
|
|
1382
|
-
var
|
|
2232
|
+
var import_utils7 = require("@moon-x/core/utils");
|
|
2233
|
+
var import_react5 = require("react");
|
|
1383
2234
|
|
|
1384
2235
|
// src/lib/oauth-deep-link.ts
|
|
1385
2236
|
var openOAuthFlow = async ({
|
|
@@ -1420,7 +2271,7 @@ var parseOAuthRedirect = async (redirectUrl) => {
|
|
|
1420
2271
|
// src/hooks/use-attach-oauth.ts
|
|
1421
2272
|
var useAttachOAuth = (params) => {
|
|
1422
2273
|
const { sdk, _bridge, _oauthRedirectUri } = useMoonXContext();
|
|
1423
|
-
const [state, setState] = (0,
|
|
2274
|
+
const [state, setState] = (0, import_react5.useState)({ status: "idle" });
|
|
1424
2275
|
return {
|
|
1425
2276
|
state,
|
|
1426
2277
|
attachOAuth: async (provider) => {
|
|
@@ -1429,8 +2280,8 @@ var useAttachOAuth = (params) => {
|
|
|
1429
2280
|
const redirectUri = params?.redirectUri || _oauthRedirectUri;
|
|
1430
2281
|
if (!redirectUri) throw new Error("oauth_redirect_uri_missing");
|
|
1431
2282
|
const startResult = await _bridge.send(
|
|
1432
|
-
|
|
1433
|
-
|
|
2283
|
+
import_utils7.PostMessageType.AUTH,
|
|
2284
|
+
import_utils7.PostMessageMethod.START_OAUTH,
|
|
1434
2285
|
{
|
|
1435
2286
|
publishableKey: sdk.publishableKey,
|
|
1436
2287
|
provider,
|
|
@@ -1450,8 +2301,8 @@ var useAttachOAuth = (params) => {
|
|
|
1450
2301
|
if (parsed.error) throw new Error(parsed.error);
|
|
1451
2302
|
setState({ status: "verifying" });
|
|
1452
2303
|
await _bridge.send(
|
|
1453
|
-
|
|
1454
|
-
|
|
2304
|
+
import_utils7.PostMessageType.AUTH,
|
|
2305
|
+
import_utils7.PostMessageMethod.ATTACH_OAUTH,
|
|
1455
2306
|
{
|
|
1456
2307
|
publishableKey: sdk.publishableKey,
|
|
1457
2308
|
provider,
|
|
@@ -1472,14 +2323,14 @@ var useAttachOAuth = (params) => {
|
|
|
1472
2323
|
};
|
|
1473
2324
|
|
|
1474
2325
|
// src/hooks/use-detach-oauth.ts
|
|
1475
|
-
var
|
|
2326
|
+
var import_utils8 = require("@moon-x/core/utils");
|
|
1476
2327
|
var useDetachOAuth = () => {
|
|
1477
2328
|
const { sdk, _bridge } = useMoonXContext();
|
|
1478
2329
|
return {
|
|
1479
2330
|
detachOAuth: async (idpUserId) => {
|
|
1480
2331
|
await _bridge.send(
|
|
1481
|
-
|
|
1482
|
-
|
|
2332
|
+
import_utils8.PostMessageType.AUTH,
|
|
2333
|
+
import_utils8.PostMessageMethod.DETACH_OAUTH,
|
|
1483
2334
|
{ publishableKey: sdk.publishableKey, idpUserId }
|
|
1484
2335
|
);
|
|
1485
2336
|
}
|
|
@@ -1487,8 +2338,8 @@ var useDetachOAuth = () => {
|
|
|
1487
2338
|
};
|
|
1488
2339
|
|
|
1489
2340
|
// src/hooks/use-export-key.ts
|
|
1490
|
-
var
|
|
1491
|
-
var
|
|
2341
|
+
var import_sdk11 = require("@moon-x/core/sdk");
|
|
2342
|
+
var import_utils9 = require("@moon-x/core/utils");
|
|
1492
2343
|
var parseExportKeyResult = (value) => {
|
|
1493
2344
|
if (!value || typeof value !== "object" || !("status" in value)) {
|
|
1494
2345
|
return { status: "failed", error: "invalid_export_key_result" };
|
|
@@ -1507,12 +2358,12 @@ var useExportKey = () => {
|
|
|
1507
2358
|
return {
|
|
1508
2359
|
exportKey: async (wallet) => {
|
|
1509
2360
|
try {
|
|
1510
|
-
const { userHandleB64url, credentialIdB64url, tokens } = await (0,
|
|
2361
|
+
const { userHandleB64url, credentialIdB64url, tokens } = await (0, import_sdk11.getInternalPresenceTokens)({
|
|
1511
2362
|
transport: _bridge,
|
|
1512
2363
|
passkey: createRNPasskey(),
|
|
1513
2364
|
publishableKey: sdk.publishableKey,
|
|
1514
2365
|
relyingPartyOrigin: _relyingPartyOrigin,
|
|
1515
|
-
scopes:
|
|
2366
|
+
scopes: import_sdk11.SCOPES_EXPORT_KEY
|
|
1516
2367
|
});
|
|
1517
2368
|
const keyshareReadToken = tokens.keyshare_read?.token ?? "";
|
|
1518
2369
|
const keyExportToken = tokens.key_export?.token ?? "";
|
|
@@ -1532,8 +2383,8 @@ var useExportKey = () => {
|
|
|
1532
2383
|
title: "Export key",
|
|
1533
2384
|
flowPath: `/export-key?${params.toString()}#${secrets.toString()}`,
|
|
1534
2385
|
onMount: ({ bridge, setResult }) => bridge.register(
|
|
1535
|
-
|
|
1536
|
-
|
|
2386
|
+
import_utils9.PostMessageType.WALLET,
|
|
2387
|
+
import_utils9.PostMessageMethod.EXPORT_KEY_RESULT,
|
|
1537
2388
|
(result) => {
|
|
1538
2389
|
setResult(parseExportKeyResult(result));
|
|
1539
2390
|
}
|
|
@@ -1556,13 +2407,13 @@ var useFundWallet = () => {
|
|
|
1556
2407
|
};
|
|
1557
2408
|
|
|
1558
2409
|
// src/hooks/use-login-with-email.ts
|
|
1559
|
-
var
|
|
1560
|
-
var
|
|
2410
|
+
var import_react6 = require("@moon-x/core/react");
|
|
2411
|
+
var import_react7 = require("react");
|
|
1561
2412
|
var useLoginWithEmail = (callbacks) => {
|
|
1562
|
-
const login = (0,
|
|
2413
|
+
const login = (0, import_react6.useLoginWithEmail)(callbacks);
|
|
1563
2414
|
const { _beginAuthFlow } = useMoonXContext();
|
|
1564
2415
|
const coreLoginWithCode = login.loginWithCode;
|
|
1565
|
-
const loginWithCode = (0,
|
|
2416
|
+
const loginWithCode = (0, import_react7.useCallback)(
|
|
1566
2417
|
async (params) => {
|
|
1567
2418
|
const finishAuthFlow = _beginAuthFlow();
|
|
1568
2419
|
try {
|
|
@@ -1577,56 +2428,77 @@ var useLoginWithEmail = (callbacks) => {
|
|
|
1577
2428
|
};
|
|
1578
2429
|
|
|
1579
2430
|
// src/hooks/use-login-with-oauth.ts
|
|
1580
|
-
var
|
|
1581
|
-
var
|
|
2431
|
+
var import_utils10 = require("@moon-x/core/utils");
|
|
2432
|
+
var import_react8 = require("react");
|
|
1582
2433
|
var useLoginWithOAuth = (params) => {
|
|
1583
2434
|
const { sdk, _bridge, _oauthRedirectUri, _beginAuthFlow } = useMoonXContext();
|
|
1584
|
-
const [state, setState] = (0,
|
|
2435
|
+
const [state, setState] = (0, import_react8.useState)({ status: "idle" });
|
|
2436
|
+
const activeFlow = (0, import_react8.useRef)(null);
|
|
2437
|
+
const mounted = (0, import_react8.useRef)(true);
|
|
2438
|
+
(0, import_react8.useEffect)(() => {
|
|
2439
|
+
mounted.current = true;
|
|
2440
|
+
return () => {
|
|
2441
|
+
mounted.current = false;
|
|
2442
|
+
};
|
|
2443
|
+
}, []);
|
|
1585
2444
|
return {
|
|
1586
2445
|
state,
|
|
1587
2446
|
loginWithOAuth: async (provider) => {
|
|
1588
|
-
const finishAuthFlow = _beginAuthFlow();
|
|
1589
|
-
|
|
2447
|
+
const finishAuthFlow = _beginAuthFlow({ replaceSession: true });
|
|
2448
|
+
const previousFlow = activeFlow.current;
|
|
2449
|
+
activeFlow.current = finishAuthFlow;
|
|
2450
|
+
previousFlow?.();
|
|
2451
|
+
const assertCurrent = () => {
|
|
2452
|
+
if (activeFlow.current !== finishAuthFlow) throw new Error("session_changed");
|
|
2453
|
+
finishAuthFlow.assertCurrent();
|
|
2454
|
+
};
|
|
2455
|
+
const update = (next) => {
|
|
2456
|
+
if (mounted.current && activeFlow.current === finishAuthFlow) setState(next);
|
|
2457
|
+
};
|
|
2458
|
+
update({ status: "starting" });
|
|
1590
2459
|
try {
|
|
1591
2460
|
const redirectUri = params?.redirectUri || _oauthRedirectUri;
|
|
1592
2461
|
if (!redirectUri) {
|
|
1593
2462
|
throw new Error("oauth_redirect_uri_missing");
|
|
1594
2463
|
}
|
|
1595
2464
|
const startResult = await _bridge.send(
|
|
1596
|
-
|
|
1597
|
-
|
|
2465
|
+
import_utils10.PostMessageType.AUTH,
|
|
2466
|
+
import_utils10.PostMessageMethod.START_OAUTH,
|
|
1598
2467
|
{
|
|
1599
2468
|
publishableKey: sdk.publishableKey,
|
|
1600
2469
|
provider,
|
|
1601
2470
|
login_redirect_url: redirectUri
|
|
1602
2471
|
}
|
|
1603
2472
|
);
|
|
2473
|
+
assertCurrent();
|
|
1604
2474
|
if (!startResult.redirect_url) {
|
|
1605
2475
|
throw new Error("start_oauth_failed");
|
|
1606
2476
|
}
|
|
1607
|
-
|
|
2477
|
+
update({ status: "redirecting" });
|
|
1608
2478
|
const { redirectUrl } = await openOAuthFlow({
|
|
1609
2479
|
authUrl: startResult.redirect_url,
|
|
1610
2480
|
redirectUri
|
|
1611
2481
|
});
|
|
1612
2482
|
const parsed = await parseOAuthRedirect(redirectUrl);
|
|
2483
|
+
assertCurrent();
|
|
1613
2484
|
if (parsed.error) throw new Error(parsed.error);
|
|
1614
2485
|
if (!parsed.code && !parsed.token) {
|
|
1615
2486
|
throw new Error("oauth_no_code_or_token");
|
|
1616
2487
|
}
|
|
1617
|
-
|
|
2488
|
+
update({ status: "verifying" });
|
|
1618
2489
|
const result = await sdk.verifyOAuth({
|
|
1619
2490
|
provider,
|
|
1620
2491
|
code: parsed.code,
|
|
1621
2492
|
token: parsed.token,
|
|
1622
2493
|
state: parsed.state
|
|
1623
2494
|
});
|
|
1624
|
-
|
|
2495
|
+
update({ status: "complete", user: result?.user ?? result ?? null });
|
|
1625
2496
|
} catch (e) {
|
|
1626
2497
|
const err = e instanceof Error ? e : new Error(String(e));
|
|
1627
|
-
|
|
2498
|
+
update({ status: "error", error: err });
|
|
1628
2499
|
throw err;
|
|
1629
2500
|
} finally {
|
|
2501
|
+
if (activeFlow.current === finishAuthFlow) activeFlow.current = null;
|
|
1630
2502
|
finishAuthFlow();
|
|
1631
2503
|
}
|
|
1632
2504
|
},
|
|
@@ -1638,17 +2510,17 @@ var useLoginWithOAuth = (params) => {
|
|
|
1638
2510
|
var useMoonX = () => useMoonXContext().sdk;
|
|
1639
2511
|
|
|
1640
2512
|
// src/external-wallets/use-external-wallets.ts
|
|
1641
|
-
var
|
|
2513
|
+
var import_react10 = require("react");
|
|
1642
2514
|
var useExternalWallets = (connectors) => {
|
|
1643
|
-
const [wallets, setWallets] = (0,
|
|
1644
|
-
const [connected, setConnected] = (0,
|
|
2515
|
+
const [wallets, setWallets] = (0, import_react10.useState)([]);
|
|
2516
|
+
const [connected, setConnected] = (0, import_react10.useState)(
|
|
1645
2517
|
null
|
|
1646
2518
|
);
|
|
1647
|
-
const [status, setStatus] = (0,
|
|
1648
|
-
const [error, setError] = (0,
|
|
1649
|
-
const [refreshNonce, setRefreshNonce] = (0,
|
|
1650
|
-
const unsubscribeRef = (0,
|
|
1651
|
-
(0,
|
|
2519
|
+
const [status, setStatus] = (0, import_react10.useState)("idle");
|
|
2520
|
+
const [error, setError] = (0, import_react10.useState)(null);
|
|
2521
|
+
const [refreshNonce, setRefreshNonce] = (0, import_react10.useState)(0);
|
|
2522
|
+
const unsubscribeRef = (0, import_react10.useRef)(null);
|
|
2523
|
+
(0, import_react10.useEffect)(() => {
|
|
1652
2524
|
let cancelled = false;
|
|
1653
2525
|
void Promise.all(
|
|
1654
2526
|
connectors.map(async (connector) => ({
|
|
@@ -1662,13 +2534,13 @@ var useExternalWallets = (connectors) => {
|
|
|
1662
2534
|
cancelled = true;
|
|
1663
2535
|
};
|
|
1664
2536
|
}, [connectors, refreshNonce]);
|
|
1665
|
-
(0,
|
|
2537
|
+
(0, import_react10.useEffect)(
|
|
1666
2538
|
() => () => {
|
|
1667
2539
|
unsubscribeRef.current?.();
|
|
1668
2540
|
},
|
|
1669
2541
|
[]
|
|
1670
2542
|
);
|
|
1671
|
-
const connect = (0,
|
|
2543
|
+
const connect = (0, import_react10.useCallback)(
|
|
1672
2544
|
async (id) => {
|
|
1673
2545
|
const connector = connectors.find((candidate) => candidate.info.id === id);
|
|
1674
2546
|
if (!connector) throw new Error(`External wallet "${id}" not found`);
|
|
@@ -1699,7 +2571,7 @@ var useExternalWallets = (connectors) => {
|
|
|
1699
2571
|
},
|
|
1700
2572
|
[connectors]
|
|
1701
2573
|
);
|
|
1702
|
-
const switchAccount = (0,
|
|
2574
|
+
const switchAccount = (0, import_react10.useCallback)(
|
|
1703
2575
|
async (address, chainId) => {
|
|
1704
2576
|
if (!connected) throw new Error("No external wallet connected");
|
|
1705
2577
|
const activeAccount = await connected.switchAccount(address, chainId);
|
|
@@ -1709,7 +2581,7 @@ var useExternalWallets = (connectors) => {
|
|
|
1709
2581
|
},
|
|
1710
2582
|
[connected]
|
|
1711
2583
|
);
|
|
1712
|
-
const disconnect = (0,
|
|
2584
|
+
const disconnect = (0, import_react10.useCallback)(async () => {
|
|
1713
2585
|
try {
|
|
1714
2586
|
await connected?.disconnect();
|
|
1715
2587
|
} finally {
|
|
@@ -1995,9 +2867,9 @@ var restoreWalletConnectSession = (options) => {
|
|
|
1995
2867
|
};
|
|
1996
2868
|
|
|
1997
2869
|
// src/index.ts
|
|
1998
|
-
var
|
|
1999
|
-
var
|
|
2000
|
-
var
|
|
2870
|
+
var import_sdk12 = require("@moon-x/core/sdk");
|
|
2871
|
+
var import_sdk13 = require("@moon-x/core/sdk");
|
|
2872
|
+
var import_utils11 = require("@moon-x/core/utils");
|
|
2001
2873
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2002
2874
|
0 && (module.exports = {
|
|
2003
2875
|
MOONPAY_CONSUMER_SESSION_ERROR_CODES,
|
|
@@ -2013,6 +2885,7 @@ var import_utils10 = require("@moon-x/core/utils");
|
|
|
2013
2885
|
useAppConfig,
|
|
2014
2886
|
useAttachOAuth,
|
|
2015
2887
|
useBulkImport,
|
|
2888
|
+
useChangeEmail,
|
|
2016
2889
|
useCreateWallet,
|
|
2017
2890
|
useDetachOAuth,
|
|
2018
2891
|
useEphemeralSigner,
|