@moon-x/react-native-sdk 0.11.0 → 0.13.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 +138 -32
- package/dist/ethereum.d.mts +10 -2
- package/dist/ethereum.d.ts +10 -2
- package/dist/ethereum.js +296 -2
- package/dist/ethereum.mjs +316 -2
- package/dist/index.d.mts +156 -21
- package/dist/index.d.ts +156 -21
- package/dist/index.js +695 -121
- package/dist/index.mjs +678 -102
- package/dist/solana.d.mts +10 -1
- package/dist/solana.d.ts +10 -1
- package/dist/solana.js +298 -2
- package/dist/solana.mjs +318 -2
- package/dist/tron.d.mts +11 -0
- package/dist/tron.d.ts +11 -0
- package/dist/tron.js +330 -0
- package/dist/tron.mjs +327 -0
- package/dist/use-chain-export-key-C4vTffjx.d.mts +11 -0
- package/dist/use-chain-export-key-C7zBt98X.d.ts +11 -0
- package/dist/use-export-key-BIz_tV0k.d.mts +23 -0
- package/dist/use-export-key-BIz_tV0k.d.ts +23 -0
- package/package.json +12 -4
package/dist/index.mjs
CHANGED
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
} from "react";
|
|
11
11
|
import {
|
|
12
12
|
Animated,
|
|
13
|
+
AppState,
|
|
13
14
|
Platform,
|
|
14
15
|
Pressable,
|
|
15
16
|
StyleSheet,
|
|
@@ -24,12 +25,24 @@ import { PostMessageMethod as PostMessageMethod2, PostMessageType as PostMessage
|
|
|
24
25
|
|
|
25
26
|
// src/sdk/sdk-authentication-methods.ts
|
|
26
27
|
import { getHeadlessAuthenticationMethods } from "@moon-x/core/sdk";
|
|
27
|
-
var getAuthenticationMethods = (bridge, publishableKey, setIsAuthenticated, setUser) =>
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
var getAuthenticationMethods = (bridge, publishableKey, setIsAuthenticated, setUser) => {
|
|
29
|
+
const headless = getHeadlessAuthenticationMethods({
|
|
30
|
+
transport: bridge,
|
|
31
|
+
publishableKey,
|
|
32
|
+
setIsAuthenticated,
|
|
33
|
+
setUser
|
|
34
|
+
});
|
|
35
|
+
return {
|
|
36
|
+
...headless,
|
|
37
|
+
// The verification response contains session credentials that belong
|
|
38
|
+
// inside the WebView. Keep the public RN contract aligned with the web
|
|
39
|
+
// SDK: callers only observe successful completion, never the raw token
|
|
40
|
+
// bundle returned by VERIFY_EMAIL_OTP.
|
|
41
|
+
loginWithCode: async (params) => {
|
|
42
|
+
await headless.loginWithCode(params);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
};
|
|
33
46
|
|
|
34
47
|
// src/sdk/sdk-ethereum-methods.ts
|
|
35
48
|
import {
|
|
@@ -124,6 +137,13 @@ var getEthereumSDKMethods = (bridge, publishableKey, relyingPartyOrigin) => getH
|
|
|
124
137
|
relyingPartyOrigin
|
|
125
138
|
});
|
|
126
139
|
|
|
140
|
+
// src/sdk/sdk-funding-methods.ts
|
|
141
|
+
import { getHeadlessFundingMethods } from "@moon-x/core/sdk";
|
|
142
|
+
var getFundingSDKMethods = (bridge, publishableKey) => getHeadlessFundingMethods({
|
|
143
|
+
transport: bridge,
|
|
144
|
+
publishableKey
|
|
145
|
+
});
|
|
146
|
+
|
|
127
147
|
// src/sdk/sdk-passkey-methods.ts
|
|
128
148
|
import { generatePasskeyUserId, toB64url } from "@moon-x/core/lib";
|
|
129
149
|
import {
|
|
@@ -134,8 +154,8 @@ import {
|
|
|
134
154
|
} from "@moon-x/core/sdk";
|
|
135
155
|
import { PostMessageMethod, PostMessageType } from "@moon-x/core/utils";
|
|
136
156
|
import { create as passkeyCreate } from "react-native-passkeys";
|
|
137
|
-
var getPasskeyMethods = (bridge, publishableKey) =>
|
|
138
|
-
|
|
157
|
+
var getPasskeyMethods = (bridge, publishableKey) => {
|
|
158
|
+
const enrollPasskey = async (params) => {
|
|
139
159
|
const dekInfo = await bridge.send(
|
|
140
160
|
PostMessageType.AUTH,
|
|
141
161
|
PostMessageMethod.GET_DEK_INFO,
|
|
@@ -218,38 +238,45 @@ var getPasskeyMethods = (bridge, publishableKey) => ({
|
|
|
218
238
|
);
|
|
219
239
|
}
|
|
220
240
|
return { credential_id: credential.rawId, passkey_registered: true };
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
241
|
+
};
|
|
242
|
+
return {
|
|
243
|
+
registerPasskey: enrollPasskey,
|
|
244
|
+
// Additional and first-time enrollment share the same implementation.
|
|
245
|
+
// The SDK derives which branch is required from GET_DEK_INFO, so exposing
|
|
246
|
+
// both public method names does not duplicate ceremony logic.
|
|
247
|
+
addPasskey: enrollPasskey,
|
|
248
|
+
// Parent-side passkey step-up. Mirrors the web SDK's
|
|
249
|
+
// `sdk.getPresenceToken()` — orchestrator is platform-agnostic; only
|
|
250
|
+
// the WebAuthn primitive differs (createRNPasskey() uses
|
|
251
|
+
// react-native-passkeys instead of @simplewebauthn/browser).
|
|
252
|
+
//
|
|
253
|
+
// DEK hygiene strip lives inside `createRNPasskey().assertForServer`,
|
|
254
|
+
// so the orchestrator never touches userHandle. Server gate
|
|
255
|
+
// (requireUserHandleStripped) catches any regression.
|
|
256
|
+
//
|
|
257
|
+
// The relying-party origin for RN apps is reconstructed from the
|
|
258
|
+
// app's configured rp_id (fetched via the iframe's GET_DEK_INFO) —
|
|
259
|
+
// RN has no window.location.origin to use. Same pattern as
|
|
260
|
+
// sdk-passkey-methods.ts::registerPasskey.
|
|
261
|
+
getPresenceToken: async (options) => {
|
|
262
|
+
const dekInfo = await bridge.send(
|
|
263
|
+
PostMessageType.AUTH,
|
|
264
|
+
PostMessageMethod.GET_DEK_INFO,
|
|
265
|
+
{ publishableKey }
|
|
266
|
+
);
|
|
267
|
+
if (!dekInfo.rp_id) {
|
|
268
|
+
throw new Error("webauthn_rp_id_not_configured");
|
|
269
|
+
}
|
|
270
|
+
return getPresenceToken({
|
|
271
|
+
transport: bridge,
|
|
272
|
+
passkey: createRNPasskey(),
|
|
273
|
+
publishableKey,
|
|
274
|
+
relyingPartyOrigin: `https://${dekInfo.rp_id}`,
|
|
275
|
+
expiresInSecs: options?.expiresInSecs
|
|
276
|
+
});
|
|
243
277
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
passkey: createRNPasskey(),
|
|
247
|
-
publishableKey,
|
|
248
|
-
relyingPartyOrigin: `https://${dekInfo.rp_id}`,
|
|
249
|
-
expiresInSecs: options?.expiresInSecs
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
});
|
|
278
|
+
};
|
|
279
|
+
};
|
|
253
280
|
|
|
254
281
|
// src/sdk/sdk-methods.ts
|
|
255
282
|
import {
|
|
@@ -276,6 +303,17 @@ var getSolanaSDKMethods = (bridge, publishableKey, relyingPartyOrigin) => getHea
|
|
|
276
303
|
relyingPartyOrigin
|
|
277
304
|
});
|
|
278
305
|
|
|
306
|
+
// src/sdk/sdk-tron-methods.ts
|
|
307
|
+
import {
|
|
308
|
+
getHeadlessTronMethods
|
|
309
|
+
} from "@moon-x/core/sdk";
|
|
310
|
+
var getTronSDKMethods = (bridge, publishableKey, relyingPartyOrigin) => getHeadlessTronMethods({
|
|
311
|
+
transport: bridge,
|
|
312
|
+
passkey: createRNPasskey(),
|
|
313
|
+
publishableKey,
|
|
314
|
+
relyingPartyOrigin
|
|
315
|
+
});
|
|
316
|
+
|
|
279
317
|
// src/sdk/create-sdk-instance.ts
|
|
280
318
|
var makeRelyingPartyOriginResolver = (bridge, publishableKey, configuredOrigin) => {
|
|
281
319
|
let cached = configuredOrigin || null;
|
|
@@ -300,12 +338,14 @@ var createSdkInstance = ({
|
|
|
300
338
|
isAuthenticated,
|
|
301
339
|
user,
|
|
302
340
|
ready,
|
|
341
|
+
revalidatedAt,
|
|
303
342
|
setIsAuthenticated,
|
|
304
343
|
setUser,
|
|
305
344
|
refreshUser
|
|
306
345
|
}) => ({
|
|
307
346
|
publishableKey,
|
|
308
347
|
ready,
|
|
348
|
+
revalidatedAt,
|
|
309
349
|
isAuthenticated,
|
|
310
350
|
user,
|
|
311
351
|
refreshUser,
|
|
@@ -324,7 +364,9 @@ var createSdkInstance = ({
|
|
|
324
364
|
),
|
|
325
365
|
...getPasskeyMethods(bridge, publishableKey),
|
|
326
366
|
...getEthereumSDKMethods(bridge, publishableKey, relyingPartyOrigin),
|
|
327
|
-
...
|
|
367
|
+
...getFundingSDKMethods(bridge, publishableKey),
|
|
368
|
+
...getSolanaSDKMethods(bridge, publishableKey, relyingPartyOrigin),
|
|
369
|
+
...getTronSDKMethods(bridge, publishableKey, relyingPartyOrigin)
|
|
328
370
|
});
|
|
329
371
|
|
|
330
372
|
// src/provider.tsx
|
|
@@ -579,6 +621,32 @@ var MoonXProvider = ({
|
|
|
579
621
|
const [ready, setReady] = useState(false);
|
|
580
622
|
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
|
581
623
|
const [user, setUser] = useState(null);
|
|
624
|
+
const [revalidatedAt, setRevalidatedAt] = useState(0);
|
|
625
|
+
const [restoreNonce, setRestoreNonce] = useState(0);
|
|
626
|
+
const authFlowsInFlightRef = useRef(0);
|
|
627
|
+
const beginAuthFlow = useCallback(() => {
|
|
628
|
+
authFlowsInFlightRef.current += 1;
|
|
629
|
+
let finished = false;
|
|
630
|
+
return () => {
|
|
631
|
+
if (finished) return;
|
|
632
|
+
finished = true;
|
|
633
|
+
authFlowsInFlightRef.current = Math.max(
|
|
634
|
+
0,
|
|
635
|
+
authFlowsInFlightRef.current - 1
|
|
636
|
+
);
|
|
637
|
+
if (authFlowsInFlightRef.current === 0) {
|
|
638
|
+
setRestoreNonce((value) => value + 1);
|
|
639
|
+
}
|
|
640
|
+
};
|
|
641
|
+
}, []);
|
|
642
|
+
useEffect(() => {
|
|
643
|
+
const subscription = AppState.addEventListener("change", (nextState) => {
|
|
644
|
+
if (nextState === "active") {
|
|
645
|
+
setRestoreNonce((value) => value + 1);
|
|
646
|
+
}
|
|
647
|
+
});
|
|
648
|
+
return () => subscription.remove();
|
|
649
|
+
}, []);
|
|
582
650
|
const { height: screenHeight } = useWindowDimensions();
|
|
583
651
|
const SHEET_TOP_OFFSET = 80;
|
|
584
652
|
const sheetTranslateY = useRef(new Animated.Value(screenHeight)).current;
|
|
@@ -636,20 +704,37 @@ var MoonXProvider = ({
|
|
|
636
704
|
navigateWebView(options.flowPath);
|
|
637
705
|
return new Promise((resolve) => {
|
|
638
706
|
let finished = false;
|
|
707
|
+
let result = null;
|
|
708
|
+
let cleanup;
|
|
639
709
|
const finish = () => {
|
|
640
710
|
if (finished) return;
|
|
641
711
|
finished = true;
|
|
712
|
+
cleanup?.();
|
|
642
713
|
setSensitiveVisible(false);
|
|
643
714
|
navigateWebView("/");
|
|
644
715
|
if (cancelRef.current === finish) cancelRef.current = null;
|
|
645
|
-
resolve();
|
|
716
|
+
resolve(result ?? { status: "cancelled" });
|
|
646
717
|
};
|
|
647
718
|
cancelRef.current = finish;
|
|
648
719
|
if (options.onMount) {
|
|
649
|
-
Promise.resolve(
|
|
650
|
-
(
|
|
651
|
-
|
|
652
|
-
|
|
720
|
+
Promise.resolve(
|
|
721
|
+
options.onMount({
|
|
722
|
+
bridge,
|
|
723
|
+
setResult: (next) => {
|
|
724
|
+
result = next;
|
|
725
|
+
}
|
|
726
|
+
})
|
|
727
|
+
).then((onCleanup) => {
|
|
728
|
+
if (typeof onCleanup !== "function") return;
|
|
729
|
+
if (finished) onCleanup();
|
|
730
|
+
else cleanup = onCleanup;
|
|
731
|
+
}).catch((error) => {
|
|
732
|
+
if (finished) return;
|
|
733
|
+
result = {
|
|
734
|
+
status: "failed",
|
|
735
|
+
error: error instanceof Error ? error.message : "sensitive_flow_setup_failed"
|
|
736
|
+
};
|
|
737
|
+
});
|
|
653
738
|
}
|
|
654
739
|
});
|
|
655
740
|
},
|
|
@@ -668,6 +753,7 @@ var MoonXProvider = ({
|
|
|
668
753
|
isAuthenticated,
|
|
669
754
|
user,
|
|
670
755
|
ready,
|
|
756
|
+
revalidatedAt,
|
|
671
757
|
setIsAuthenticated,
|
|
672
758
|
setUser,
|
|
673
759
|
refreshUser
|
|
@@ -679,6 +765,7 @@ var MoonXProvider = ({
|
|
|
679
765
|
isAuthenticated,
|
|
680
766
|
user,
|
|
681
767
|
ready,
|
|
768
|
+
revalidatedAt,
|
|
682
769
|
refreshUser
|
|
683
770
|
]
|
|
684
771
|
);
|
|
@@ -710,16 +797,24 @@ var MoonXProvider = ({
|
|
|
710
797
|
};
|
|
711
798
|
}, [bridge]);
|
|
712
799
|
useEffect(() => {
|
|
713
|
-
if (!ready
|
|
800
|
+
if (!ready) return;
|
|
714
801
|
let cancelled = false;
|
|
715
802
|
let attempt = 0;
|
|
716
|
-
const maxAttempts =
|
|
803
|
+
const maxAttempts = 5;
|
|
804
|
+
const timers = /* @__PURE__ */ new Set();
|
|
805
|
+
const schedule = (callback, delay) => {
|
|
806
|
+
const timer = setTimeout(() => {
|
|
807
|
+
timers.delete(timer);
|
|
808
|
+
callback();
|
|
809
|
+
}, delay);
|
|
810
|
+
timers.add(timer);
|
|
811
|
+
};
|
|
717
812
|
const tryRestore = async () => {
|
|
718
|
-
if (cancelled) return;
|
|
813
|
+
if (cancelled || authFlowsInFlightRef.current > 0) return;
|
|
719
814
|
attempt += 1;
|
|
720
815
|
try {
|
|
721
816
|
const session = await sdkRef.current.getCurrentSession();
|
|
722
|
-
if (cancelled) return;
|
|
817
|
+
if (cancelled || authFlowsInFlightRef.current > 0) return;
|
|
723
818
|
if (session) {
|
|
724
819
|
setIsAuthenticated(true);
|
|
725
820
|
try {
|
|
@@ -727,38 +822,50 @@ var MoonXProvider = ({
|
|
|
727
822
|
if (!cancelled && u) setUser(u);
|
|
728
823
|
} catch {
|
|
729
824
|
}
|
|
825
|
+
if (!cancelled) setRevalidatedAt(Date.now());
|
|
730
826
|
return;
|
|
731
827
|
}
|
|
732
828
|
if (attempt < maxAttempts) {
|
|
733
829
|
const delay = 200 * 2 ** (attempt - 1);
|
|
734
|
-
|
|
830
|
+
schedule(tryRestore, delay);
|
|
831
|
+
} else if (!cancelled) {
|
|
832
|
+
setIsAuthenticated(false);
|
|
833
|
+
setUser(null);
|
|
834
|
+
setRevalidatedAt(Date.now());
|
|
735
835
|
}
|
|
736
836
|
} catch {
|
|
837
|
+
if (authFlowsInFlightRef.current > 0) return;
|
|
737
838
|
if (!cancelled && attempt < maxAttempts) {
|
|
738
839
|
const delay = 200 * 2 ** (attempt - 1);
|
|
739
|
-
|
|
840
|
+
schedule(tryRestore, delay);
|
|
841
|
+
} else if (!cancelled) {
|
|
842
|
+
setRevalidatedAt(Date.now());
|
|
740
843
|
}
|
|
741
844
|
}
|
|
742
845
|
};
|
|
743
|
-
tryRestore
|
|
846
|
+
schedule(tryRestore, restoreNonce > 0 ? 150 : 0);
|
|
744
847
|
return () => {
|
|
745
848
|
cancelled = true;
|
|
849
|
+
timers.forEach(clearTimeout);
|
|
850
|
+
timers.clear();
|
|
746
851
|
};
|
|
747
|
-
}, [ready,
|
|
852
|
+
}, [ready, restoreNonce]);
|
|
748
853
|
const contextValue = useMemo(
|
|
749
854
|
() => ({
|
|
750
855
|
sdk,
|
|
751
856
|
_bridge: bridge,
|
|
752
857
|
_relyingPartyOrigin: relyingPartyOriginResolver,
|
|
753
858
|
_runSensitiveFlow: runSensitiveFlow,
|
|
754
|
-
_oauthRedirectUri: config?.oauthRedirectUri
|
|
859
|
+
_oauthRedirectUri: config?.oauthRedirectUri,
|
|
860
|
+
_beginAuthFlow: beginAuthFlow
|
|
755
861
|
}),
|
|
756
862
|
[
|
|
757
863
|
sdk,
|
|
758
864
|
bridge,
|
|
759
865
|
relyingPartyOriginResolver,
|
|
760
866
|
runSensitiveFlow,
|
|
761
|
-
config?.oauthRedirectUri
|
|
867
|
+
config?.oauthRedirectUri,
|
|
868
|
+
beginAuthFlow
|
|
762
869
|
]
|
|
763
870
|
);
|
|
764
871
|
const closeLabel = config?.sensitiveModalCopy?.closeLabel ?? "Done";
|
|
@@ -908,7 +1015,6 @@ import {
|
|
|
908
1015
|
useCreateWallet,
|
|
909
1016
|
useEphemeralSigner,
|
|
910
1017
|
useImportKey,
|
|
911
|
-
useLoginWithEmail,
|
|
912
1018
|
useLogout,
|
|
913
1019
|
usePasskeyStatus,
|
|
914
1020
|
useRegisterPasskey,
|
|
@@ -917,9 +1023,53 @@ import {
|
|
|
917
1023
|
useWallets
|
|
918
1024
|
} from "@moon-x/core/react";
|
|
919
1025
|
|
|
920
|
-
// src/hooks/use-
|
|
1026
|
+
// src/hooks/use-app-config.ts
|
|
921
1027
|
import { PostMessageMethod as PostMessageMethod4, PostMessageType as PostMessageType4 } from "@moon-x/core/utils";
|
|
922
|
-
import { useState as useState2 } from "react";
|
|
1028
|
+
import { useEffect as useEffect2, useState as useState2 } from "react";
|
|
1029
|
+
var useAppConfig = (publishableKey) => {
|
|
1030
|
+
const { sdk, _bridge } = useMoonXContext();
|
|
1031
|
+
const resolvedKey = publishableKey ?? sdk.publishableKey;
|
|
1032
|
+
const ready = sdk.ready;
|
|
1033
|
+
const [appConfig, setAppConfig] = useState2(null);
|
|
1034
|
+
const [isLoading, setIsLoading] = useState2(true);
|
|
1035
|
+
const [error, setError] = useState2(null);
|
|
1036
|
+
useEffect2(() => {
|
|
1037
|
+
if (!resolvedKey) {
|
|
1038
|
+
setIsLoading(false);
|
|
1039
|
+
return;
|
|
1040
|
+
}
|
|
1041
|
+
if (!ready) {
|
|
1042
|
+
return;
|
|
1043
|
+
}
|
|
1044
|
+
let cancelled = false;
|
|
1045
|
+
const fetchAppConfig = async () => {
|
|
1046
|
+
setIsLoading(true);
|
|
1047
|
+
setError(null);
|
|
1048
|
+
setAppConfig(null);
|
|
1049
|
+
try {
|
|
1050
|
+
const config = await _bridge.send(PostMessageType4.AUTH, PostMessageMethod4.GET_APP_CONFIG, {
|
|
1051
|
+
publishableKey: resolvedKey
|
|
1052
|
+
});
|
|
1053
|
+
if (!cancelled) setAppConfig(config);
|
|
1054
|
+
} catch (err) {
|
|
1055
|
+
if (!cancelled) {
|
|
1056
|
+
setError(err instanceof Error ? err.message : "Unknown error");
|
|
1057
|
+
}
|
|
1058
|
+
} finally {
|
|
1059
|
+
if (!cancelled) setIsLoading(false);
|
|
1060
|
+
}
|
|
1061
|
+
};
|
|
1062
|
+
fetchAppConfig();
|
|
1063
|
+
return () => {
|
|
1064
|
+
cancelled = true;
|
|
1065
|
+
};
|
|
1066
|
+
}, [resolvedKey, ready, _bridge]);
|
|
1067
|
+
return { appConfig, isLoading, error };
|
|
1068
|
+
};
|
|
1069
|
+
|
|
1070
|
+
// src/hooks/use-attach-oauth.ts
|
|
1071
|
+
import { PostMessageMethod as PostMessageMethod5, PostMessageType as PostMessageType5 } from "@moon-x/core/utils";
|
|
1072
|
+
import { useState as useState3 } from "react";
|
|
923
1073
|
|
|
924
1074
|
// src/lib/oauth-deep-link.ts
|
|
925
1075
|
var openOAuthFlow = async ({
|
|
@@ -960,7 +1110,7 @@ var parseOAuthRedirect = async (redirectUrl) => {
|
|
|
960
1110
|
// src/hooks/use-attach-oauth.ts
|
|
961
1111
|
var useAttachOAuth = (params) => {
|
|
962
1112
|
const { sdk, _bridge, _oauthRedirectUri } = useMoonXContext();
|
|
963
|
-
const [state, setState] =
|
|
1113
|
+
const [state, setState] = useState3({ status: "idle" });
|
|
964
1114
|
return {
|
|
965
1115
|
state,
|
|
966
1116
|
attachOAuth: async (provider) => {
|
|
@@ -969,8 +1119,8 @@ var useAttachOAuth = (params) => {
|
|
|
969
1119
|
const redirectUri = params?.redirectUri || _oauthRedirectUri;
|
|
970
1120
|
if (!redirectUri) throw new Error("oauth_redirect_uri_missing");
|
|
971
1121
|
const startResult = await _bridge.send(
|
|
972
|
-
|
|
973
|
-
|
|
1122
|
+
PostMessageType5.AUTH,
|
|
1123
|
+
PostMessageMethod5.START_OAUTH,
|
|
974
1124
|
{
|
|
975
1125
|
publishableKey: sdk.publishableKey,
|
|
976
1126
|
provider,
|
|
@@ -990,8 +1140,8 @@ var useAttachOAuth = (params) => {
|
|
|
990
1140
|
if (parsed.error) throw new Error(parsed.error);
|
|
991
1141
|
setState({ status: "verifying" });
|
|
992
1142
|
await _bridge.send(
|
|
993
|
-
|
|
994
|
-
|
|
1143
|
+
PostMessageType5.AUTH,
|
|
1144
|
+
PostMessageMethod5.ATTACH_OAUTH,
|
|
995
1145
|
{
|
|
996
1146
|
publishableKey: sdk.publishableKey,
|
|
997
1147
|
provider,
|
|
@@ -1012,14 +1162,14 @@ var useAttachOAuth = (params) => {
|
|
|
1012
1162
|
};
|
|
1013
1163
|
|
|
1014
1164
|
// src/hooks/use-detach-oauth.ts
|
|
1015
|
-
import { PostMessageMethod as
|
|
1165
|
+
import { PostMessageMethod as PostMessageMethod6, PostMessageType as PostMessageType6 } from "@moon-x/core/utils";
|
|
1016
1166
|
var useDetachOAuth = () => {
|
|
1017
1167
|
const { sdk, _bridge } = useMoonXContext();
|
|
1018
1168
|
return {
|
|
1019
1169
|
detachOAuth: async (idpUserId) => {
|
|
1020
1170
|
await _bridge.send(
|
|
1021
|
-
|
|
1022
|
-
|
|
1171
|
+
PostMessageType6.AUTH,
|
|
1172
|
+
PostMessageMethod6.DETACH_OAUTH,
|
|
1023
1173
|
{ publishableKey: sdk.publishableKey, idpUserId }
|
|
1024
1174
|
);
|
|
1025
1175
|
}
|
|
@@ -1028,50 +1178,103 @@ var useDetachOAuth = () => {
|
|
|
1028
1178
|
|
|
1029
1179
|
// src/hooks/use-export-key.ts
|
|
1030
1180
|
import { getInternalPresenceTokens as getInternalPresenceTokens2, SCOPES_EXPORT_KEY } from "@moon-x/core/sdk";
|
|
1181
|
+
import { PostMessageMethod as PostMessageMethod7, PostMessageType as PostMessageType7 } from "@moon-x/core/utils";
|
|
1182
|
+
var parseExportKeyResult = (value) => {
|
|
1183
|
+
if (!value || typeof value !== "object" || !("status" in value)) {
|
|
1184
|
+
return { status: "failed", error: "invalid_export_key_result" };
|
|
1185
|
+
}
|
|
1186
|
+
const candidate = value;
|
|
1187
|
+
if (candidate.status === "completed" || candidate.status === "cancelled") {
|
|
1188
|
+
return { status: candidate.status };
|
|
1189
|
+
}
|
|
1190
|
+
if (candidate.status === "failed" && typeof candidate.error === "string") {
|
|
1191
|
+
return { status: "failed", error: candidate.error };
|
|
1192
|
+
}
|
|
1193
|
+
return { status: "failed", error: "invalid_export_key_result" };
|
|
1194
|
+
};
|
|
1031
1195
|
var useExportKey = () => {
|
|
1032
1196
|
const { sdk, _bridge, _relyingPartyOrigin, _runSensitiveFlow } = useMoonXContext();
|
|
1033
1197
|
return {
|
|
1034
1198
|
exportKey: async (wallet) => {
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1199
|
+
try {
|
|
1200
|
+
const { userHandleB64url, credentialIdB64url, tokens } = await getInternalPresenceTokens2({
|
|
1201
|
+
transport: _bridge,
|
|
1202
|
+
passkey: createRNPasskey(),
|
|
1203
|
+
publishableKey: sdk.publishableKey,
|
|
1204
|
+
relyingPartyOrigin: _relyingPartyOrigin,
|
|
1205
|
+
scopes: SCOPES_EXPORT_KEY
|
|
1206
|
+
});
|
|
1207
|
+
const keyshareReadToken = tokens.keyshare_read?.token ?? "";
|
|
1208
|
+
const params = new URLSearchParams({
|
|
1209
|
+
publishableKey: sdk.publishableKey,
|
|
1210
|
+
wallet_id: wallet.id ?? "",
|
|
1211
|
+
wallet_type: wallet.wallet_type ?? ""
|
|
1212
|
+
});
|
|
1213
|
+
const secrets = new URLSearchParams({
|
|
1214
|
+
user_handle_b64url: userHandleB64url,
|
|
1215
|
+
credential_id_b64url: credentialIdB64url,
|
|
1216
|
+
keyshare_read_token: keyshareReadToken
|
|
1217
|
+
});
|
|
1218
|
+
return await _runSensitiveFlow({
|
|
1219
|
+
title: "Export key",
|
|
1220
|
+
flowPath: `/export-key?${params.toString()}#${secrets.toString()}`,
|
|
1221
|
+
onMount: ({ bridge, setResult }) => bridge.register(
|
|
1222
|
+
PostMessageType7.WALLET,
|
|
1223
|
+
PostMessageMethod7.EXPORT_KEY_RESULT,
|
|
1224
|
+
(result) => {
|
|
1225
|
+
setResult(parseExportKeyResult(result));
|
|
1226
|
+
}
|
|
1227
|
+
)
|
|
1228
|
+
});
|
|
1229
|
+
} catch (error) {
|
|
1230
|
+
const message = error instanceof Error ? error.message : "export_key_failed";
|
|
1231
|
+
return /cancel/i.test(message) ? { status: "cancelled" } : { status: "failed", error: message };
|
|
1232
|
+
}
|
|
1062
1233
|
}
|
|
1063
1234
|
};
|
|
1064
1235
|
};
|
|
1065
1236
|
|
|
1237
|
+
// src/hooks/use-fund-wallet.ts
|
|
1238
|
+
var useFundWallet = () => {
|
|
1239
|
+
const { sdk } = useMoonXContext();
|
|
1240
|
+
return {
|
|
1241
|
+
fundWallet: (params) => sdk.prepareFunding(params)
|
|
1242
|
+
};
|
|
1243
|
+
};
|
|
1244
|
+
|
|
1245
|
+
// src/hooks/use-login-with-email.ts
|
|
1246
|
+
import {
|
|
1247
|
+
useLoginWithEmail as useCoreLoginWithEmail
|
|
1248
|
+
} from "@moon-x/core/react";
|
|
1249
|
+
import { useCallback as useCallback2 } from "react";
|
|
1250
|
+
var useLoginWithEmail = (callbacks) => {
|
|
1251
|
+
const login = useCoreLoginWithEmail(callbacks);
|
|
1252
|
+
const { _beginAuthFlow } = useMoonXContext();
|
|
1253
|
+
const coreLoginWithCode = login.loginWithCode;
|
|
1254
|
+
const loginWithCode = useCallback2(
|
|
1255
|
+
async (params) => {
|
|
1256
|
+
const finishAuthFlow = _beginAuthFlow();
|
|
1257
|
+
try {
|
|
1258
|
+
return await coreLoginWithCode(params);
|
|
1259
|
+
} finally {
|
|
1260
|
+
finishAuthFlow();
|
|
1261
|
+
}
|
|
1262
|
+
},
|
|
1263
|
+
[_beginAuthFlow, coreLoginWithCode]
|
|
1264
|
+
);
|
|
1265
|
+
return { ...login, loginWithCode };
|
|
1266
|
+
};
|
|
1267
|
+
|
|
1066
1268
|
// src/hooks/use-login-with-oauth.ts
|
|
1067
|
-
import { PostMessageMethod as
|
|
1068
|
-
import { useState as
|
|
1269
|
+
import { PostMessageMethod as PostMessageMethod8, PostMessageType as PostMessageType8 } from "@moon-x/core/utils";
|
|
1270
|
+
import { useState as useState4 } from "react";
|
|
1069
1271
|
var useLoginWithOAuth = (params) => {
|
|
1070
|
-
const { sdk, _bridge, _oauthRedirectUri } = useMoonXContext();
|
|
1071
|
-
const [state, setState] =
|
|
1272
|
+
const { sdk, _bridge, _oauthRedirectUri, _beginAuthFlow } = useMoonXContext();
|
|
1273
|
+
const [state, setState] = useState4({ status: "idle" });
|
|
1072
1274
|
return {
|
|
1073
1275
|
state,
|
|
1074
1276
|
loginWithOAuth: async (provider) => {
|
|
1277
|
+
const finishAuthFlow = _beginAuthFlow();
|
|
1075
1278
|
setState({ status: "starting" });
|
|
1076
1279
|
try {
|
|
1077
1280
|
const redirectUri = params?.redirectUri || _oauthRedirectUri;
|
|
@@ -1079,8 +1282,8 @@ var useLoginWithOAuth = (params) => {
|
|
|
1079
1282
|
throw new Error("oauth_redirect_uri_missing");
|
|
1080
1283
|
}
|
|
1081
1284
|
const startResult = await _bridge.send(
|
|
1082
|
-
|
|
1083
|
-
|
|
1285
|
+
PostMessageType8.AUTH,
|
|
1286
|
+
PostMessageMethod8.START_OAUTH,
|
|
1084
1287
|
{
|
|
1085
1288
|
publishableKey: sdk.publishableKey,
|
|
1086
1289
|
provider,
|
|
@@ -1112,6 +1315,8 @@ var useLoginWithOAuth = (params) => {
|
|
|
1112
1315
|
const err = e instanceof Error ? e : new Error(String(e));
|
|
1113
1316
|
setState({ status: "error", error: err });
|
|
1114
1317
|
throw err;
|
|
1318
|
+
} finally {
|
|
1319
|
+
finishAuthFlow();
|
|
1115
1320
|
}
|
|
1116
1321
|
},
|
|
1117
1322
|
reset: () => setState({ status: "idle" })
|
|
@@ -1121,22 +1326,393 @@ var useLoginWithOAuth = (params) => {
|
|
|
1121
1326
|
// src/hooks/use-moonx.ts
|
|
1122
1327
|
var useMoonX = () => useMoonXContext().sdk;
|
|
1123
1328
|
|
|
1329
|
+
// src/external-wallets/use-external-wallets.ts
|
|
1330
|
+
import { useCallback as useCallback3, useEffect as useEffect3, useRef as useRef2, useState as useState5 } from "react";
|
|
1331
|
+
var useExternalWallets = (connectors) => {
|
|
1332
|
+
const [wallets, setWallets] = useState5([]);
|
|
1333
|
+
const [connected, setConnected] = useState5(
|
|
1334
|
+
null
|
|
1335
|
+
);
|
|
1336
|
+
const [status, setStatus] = useState5("idle");
|
|
1337
|
+
const [error, setError] = useState5(null);
|
|
1338
|
+
const [refreshNonce, setRefreshNonce] = useState5(0);
|
|
1339
|
+
const unsubscribeRef = useRef2(null);
|
|
1340
|
+
useEffect3(() => {
|
|
1341
|
+
let cancelled = false;
|
|
1342
|
+
void Promise.all(
|
|
1343
|
+
connectors.map(async (connector) => ({
|
|
1344
|
+
...connector.info,
|
|
1345
|
+
available: connector.isAvailable ? await Promise.resolve(connector.isAvailable()).catch(() => false) : true
|
|
1346
|
+
}))
|
|
1347
|
+
).then((discovered) => {
|
|
1348
|
+
if (!cancelled) setWallets(discovered);
|
|
1349
|
+
});
|
|
1350
|
+
return () => {
|
|
1351
|
+
cancelled = true;
|
|
1352
|
+
};
|
|
1353
|
+
}, [connectors, refreshNonce]);
|
|
1354
|
+
useEffect3(
|
|
1355
|
+
() => () => {
|
|
1356
|
+
unsubscribeRef.current?.();
|
|
1357
|
+
},
|
|
1358
|
+
[]
|
|
1359
|
+
);
|
|
1360
|
+
const connect = useCallback3(
|
|
1361
|
+
async (id) => {
|
|
1362
|
+
const connector = connectors.find((candidate) => candidate.info.id === id);
|
|
1363
|
+
if (!connector) throw new Error(`External wallet "${id}" not found`);
|
|
1364
|
+
setStatus("connecting");
|
|
1365
|
+
setError(null);
|
|
1366
|
+
try {
|
|
1367
|
+
const connection = await connector.connect();
|
|
1368
|
+
unsubscribeRef.current?.();
|
|
1369
|
+
unsubscribeRef.current = connection.subscribeAccounts?.((accounts, activeAccount) => {
|
|
1370
|
+
if (!activeAccount) {
|
|
1371
|
+
setConnected(null);
|
|
1372
|
+
setStatus("idle");
|
|
1373
|
+
return;
|
|
1374
|
+
}
|
|
1375
|
+
setConnected(
|
|
1376
|
+
(current) => current ? { ...current, accounts, activeAccount, chain: activeAccount.chain } : current
|
|
1377
|
+
);
|
|
1378
|
+
}) ?? null;
|
|
1379
|
+
setConnected(connection);
|
|
1380
|
+
setStatus("connected");
|
|
1381
|
+
return connection;
|
|
1382
|
+
} catch (cause) {
|
|
1383
|
+
const nextError = cause instanceof Error ? cause : new Error("Wallet connection failed");
|
|
1384
|
+
setError(nextError);
|
|
1385
|
+
setStatus("error");
|
|
1386
|
+
throw nextError;
|
|
1387
|
+
}
|
|
1388
|
+
},
|
|
1389
|
+
[connectors]
|
|
1390
|
+
);
|
|
1391
|
+
const switchAccount = useCallback3(
|
|
1392
|
+
async (address, chainId) => {
|
|
1393
|
+
if (!connected) throw new Error("No external wallet connected");
|
|
1394
|
+
const activeAccount = await connected.switchAccount(address, chainId);
|
|
1395
|
+
setConnected(
|
|
1396
|
+
(current) => current?.id === connected.id ? { ...current, activeAccount, chain: activeAccount.chain } : current
|
|
1397
|
+
);
|
|
1398
|
+
},
|
|
1399
|
+
[connected]
|
|
1400
|
+
);
|
|
1401
|
+
const disconnect = useCallback3(async () => {
|
|
1402
|
+
try {
|
|
1403
|
+
await connected?.disconnect();
|
|
1404
|
+
} finally {
|
|
1405
|
+
unsubscribeRef.current?.();
|
|
1406
|
+
unsubscribeRef.current = null;
|
|
1407
|
+
setConnected(null);
|
|
1408
|
+
setStatus("idle");
|
|
1409
|
+
}
|
|
1410
|
+
}, [connected]);
|
|
1411
|
+
return {
|
|
1412
|
+
wallets,
|
|
1413
|
+
connected,
|
|
1414
|
+
status,
|
|
1415
|
+
error,
|
|
1416
|
+
connect,
|
|
1417
|
+
disconnect,
|
|
1418
|
+
switchAccount,
|
|
1419
|
+
refresh: () => setRefreshNonce((value) => value + 1)
|
|
1420
|
+
};
|
|
1421
|
+
};
|
|
1422
|
+
|
|
1423
|
+
// src/external-wallets/wallet-connect.ts
|
|
1424
|
+
import {
|
|
1425
|
+
beginWalletConnectSession,
|
|
1426
|
+
disconnectWalletConnectSession,
|
|
1427
|
+
getWalletConnectAccounts,
|
|
1428
|
+
parseWalletConnectAccount,
|
|
1429
|
+
requestWalletConnect
|
|
1430
|
+
} from "@moon-x/core/lib";
|
|
1431
|
+
var DEFAULT_REQUIRED_NAMESPACES = {
|
|
1432
|
+
eip155: {
|
|
1433
|
+
chains: ["eip155:1"],
|
|
1434
|
+
methods: [
|
|
1435
|
+
"eth_sendTransaction",
|
|
1436
|
+
"eth_signTransaction",
|
|
1437
|
+
"personal_sign",
|
|
1438
|
+
"eth_signTypedData_v4"
|
|
1439
|
+
],
|
|
1440
|
+
events: ["accountsChanged", "chainChanged"]
|
|
1441
|
+
}
|
|
1442
|
+
};
|
|
1443
|
+
var toExternalAccount = (account) => {
|
|
1444
|
+
const chain = account.namespace === "eip155" ? "ethereum" : account.namespace === "solana" ? "solana" : null;
|
|
1445
|
+
return chain ? { address: account.address, chain, chainId: account.chainId } : null;
|
|
1446
|
+
};
|
|
1447
|
+
var addressesMatch = (left, right, chain) => chain === "ethereum" ? left.toLowerCase() === right.toLowerCase() : left === right;
|
|
1448
|
+
var bytesToHex = (message) => {
|
|
1449
|
+
let result = "0x";
|
|
1450
|
+
for (const byte of message) result += byte.toString(16).padStart(2, "0");
|
|
1451
|
+
return result;
|
|
1452
|
+
};
|
|
1453
|
+
var stringToHex = (message) => {
|
|
1454
|
+
if (message.startsWith("0x")) return message;
|
|
1455
|
+
const encoded = encodeURIComponent(message);
|
|
1456
|
+
let result = "0x";
|
|
1457
|
+
for (let index = 0; index < encoded.length; index += 1) {
|
|
1458
|
+
if (encoded[index] === "%") {
|
|
1459
|
+
result += encoded.slice(index + 1, index + 3).toLowerCase();
|
|
1460
|
+
index += 2;
|
|
1461
|
+
} else {
|
|
1462
|
+
result += encoded.charCodeAt(index).toString(16).padStart(2, "0");
|
|
1463
|
+
}
|
|
1464
|
+
}
|
|
1465
|
+
return result;
|
|
1466
|
+
};
|
|
1467
|
+
var createConnection = ({
|
|
1468
|
+
client,
|
|
1469
|
+
session,
|
|
1470
|
+
walletClientType = "walletconnect",
|
|
1471
|
+
chain,
|
|
1472
|
+
address
|
|
1473
|
+
}) => {
|
|
1474
|
+
let accounts = getWalletConnectAccounts(session).map(toExternalAccount).filter((account) => account !== null);
|
|
1475
|
+
if (chain) accounts = accounts.filter((account) => account.chain === chain);
|
|
1476
|
+
if (accounts.length === 0) {
|
|
1477
|
+
throw new Error("WalletConnect session has no supported accounts");
|
|
1478
|
+
}
|
|
1479
|
+
let active = accounts.find(
|
|
1480
|
+
(account) => account.address === address && (!chain || account.chain === chain)
|
|
1481
|
+
) ?? accounts[0];
|
|
1482
|
+
const requireActiveAccount = () => {
|
|
1483
|
+
if (!active) throw new Error("WalletConnect session disconnected");
|
|
1484
|
+
return active;
|
|
1485
|
+
};
|
|
1486
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
1487
|
+
const metadata = session.peer?.metadata;
|
|
1488
|
+
const connection = {
|
|
1489
|
+
id: session.topic,
|
|
1490
|
+
name: metadata?.name ?? "WalletConnect",
|
|
1491
|
+
chain: active.chain,
|
|
1492
|
+
walletClientType,
|
|
1493
|
+
accounts,
|
|
1494
|
+
activeAccount: active,
|
|
1495
|
+
request: async (method, params, chainId) => {
|
|
1496
|
+
const account = requireActiveAccount();
|
|
1497
|
+
return requestWalletConnect(client, {
|
|
1498
|
+
topic: session.topic,
|
|
1499
|
+
chainId: chainId ?? account.chainId ?? "eip155:1",
|
|
1500
|
+
method,
|
|
1501
|
+
requestParams: params
|
|
1502
|
+
});
|
|
1503
|
+
},
|
|
1504
|
+
signMessage: async (message) => {
|
|
1505
|
+
const account = requireActiveAccount();
|
|
1506
|
+
if (account.chain !== "ethereum") {
|
|
1507
|
+
throw new Error(
|
|
1508
|
+
"Use request() with the wallet's supported Solana signing method"
|
|
1509
|
+
);
|
|
1510
|
+
}
|
|
1511
|
+
return requestWalletConnect(client, {
|
|
1512
|
+
topic: session.topic,
|
|
1513
|
+
chainId: account.chainId ?? "eip155:1",
|
|
1514
|
+
method: "personal_sign",
|
|
1515
|
+
requestParams: [
|
|
1516
|
+
typeof message === "string" ? stringToHex(message) : bytesToHex(message),
|
|
1517
|
+
account.address
|
|
1518
|
+
]
|
|
1519
|
+
});
|
|
1520
|
+
},
|
|
1521
|
+
signTypedData: async (typedData) => {
|
|
1522
|
+
const account = requireActiveAccount();
|
|
1523
|
+
if (account.chain !== "ethereum") {
|
|
1524
|
+
throw new Error("Typed-data signing is only available for Ethereum");
|
|
1525
|
+
}
|
|
1526
|
+
return requestWalletConnect(client, {
|
|
1527
|
+
topic: session.topic,
|
|
1528
|
+
chainId: account.chainId ?? "eip155:1",
|
|
1529
|
+
method: "eth_signTypedData_v4",
|
|
1530
|
+
requestParams: [account.address, JSON.stringify(typedData)]
|
|
1531
|
+
});
|
|
1532
|
+
},
|
|
1533
|
+
signTransaction: async (transaction) => {
|
|
1534
|
+
const account = requireActiveAccount();
|
|
1535
|
+
if (account.chain !== "ethereum") {
|
|
1536
|
+
throw new Error(
|
|
1537
|
+
"Use request() with the wallet's supported Solana signing method"
|
|
1538
|
+
);
|
|
1539
|
+
}
|
|
1540
|
+
return requestWalletConnect(client, {
|
|
1541
|
+
topic: session.topic,
|
|
1542
|
+
chainId: account.chainId ?? "eip155:1",
|
|
1543
|
+
method: "eth_signTransaction",
|
|
1544
|
+
requestParams: [transaction]
|
|
1545
|
+
});
|
|
1546
|
+
},
|
|
1547
|
+
sendTransaction: async (transaction) => {
|
|
1548
|
+
const account = requireActiveAccount();
|
|
1549
|
+
if (account.chain !== "ethereum") {
|
|
1550
|
+
throw new Error(
|
|
1551
|
+
"Use request() with the wallet's supported Solana send method"
|
|
1552
|
+
);
|
|
1553
|
+
}
|
|
1554
|
+
return requestWalletConnect(client, {
|
|
1555
|
+
topic: session.topic,
|
|
1556
|
+
chainId: account.chainId ?? "eip155:1",
|
|
1557
|
+
method: "eth_sendTransaction",
|
|
1558
|
+
requestParams: [transaction]
|
|
1559
|
+
});
|
|
1560
|
+
},
|
|
1561
|
+
switchAccount: async (nextAddress, nextChainId) => {
|
|
1562
|
+
requireActiveAccount();
|
|
1563
|
+
const next = accounts.find(
|
|
1564
|
+
(account) => account.address === nextAddress && (!nextChainId || account.chainId === nextChainId)
|
|
1565
|
+
);
|
|
1566
|
+
if (!next) throw new Error("Account is not approved in this session");
|
|
1567
|
+
active = next;
|
|
1568
|
+
connection.activeAccount = next;
|
|
1569
|
+
connection.chain = next.chain;
|
|
1570
|
+
listeners.forEach((listener) => listener(accounts, active));
|
|
1571
|
+
return next;
|
|
1572
|
+
},
|
|
1573
|
+
subscribeAccounts: (listener) => {
|
|
1574
|
+
listeners.add(listener);
|
|
1575
|
+
return () => listeners.delete(listener);
|
|
1576
|
+
},
|
|
1577
|
+
disconnect: async () => {
|
|
1578
|
+
cleanup();
|
|
1579
|
+
publish([]);
|
|
1580
|
+
await disconnectWalletConnectSession(client, session.topic);
|
|
1581
|
+
},
|
|
1582
|
+
raw: { client, session }
|
|
1583
|
+
};
|
|
1584
|
+
const publish = (nextAccounts) => {
|
|
1585
|
+
accounts = chain ? nextAccounts.filter((account) => account.chain === chain) : nextAccounts;
|
|
1586
|
+
const previousActive = active;
|
|
1587
|
+
const stillApproved = previousActive ? accounts.find(
|
|
1588
|
+
(account) => account.address === previousActive.address && account.chainId === previousActive.chainId
|
|
1589
|
+
) : void 0;
|
|
1590
|
+
active = stillApproved ?? accounts[0] ?? null;
|
|
1591
|
+
connection.accounts = accounts;
|
|
1592
|
+
connection.activeAccount = active;
|
|
1593
|
+
if (active) {
|
|
1594
|
+
connection.chain = active.chain;
|
|
1595
|
+
}
|
|
1596
|
+
listeners.forEach((listener) => listener(accounts, active ?? null));
|
|
1597
|
+
};
|
|
1598
|
+
const onSessionUpdate = (event) => {
|
|
1599
|
+
if (event.topic !== session.topic || !event.params?.namespaces) return;
|
|
1600
|
+
session.namespaces = event.params.namespaces;
|
|
1601
|
+
publish(
|
|
1602
|
+
getWalletConnectAccounts(session).map(toExternalAccount).filter((account) => account !== null)
|
|
1603
|
+
);
|
|
1604
|
+
};
|
|
1605
|
+
const onSessionEvent = (event) => {
|
|
1606
|
+
if (event.topic !== session.topic || event.params?.event?.name !== "accountsChanged" || !Array.isArray(event.params.event.data)) {
|
|
1607
|
+
return;
|
|
1608
|
+
}
|
|
1609
|
+
if (!active) return;
|
|
1610
|
+
const currentAccount = active;
|
|
1611
|
+
const values = event.params.event.data;
|
|
1612
|
+
if (!values.every((value) => typeof value === "string")) {
|
|
1613
|
+
return;
|
|
1614
|
+
}
|
|
1615
|
+
if (values.length === 0) {
|
|
1616
|
+
publish([]);
|
|
1617
|
+
return;
|
|
1618
|
+
}
|
|
1619
|
+
const approvedAccounts = getWalletConnectAccounts(session).map(toExternalAccount).filter(
|
|
1620
|
+
(account) => account !== null && (!chain || account.chain === chain)
|
|
1621
|
+
);
|
|
1622
|
+
const eventChainId = event.params.chainId;
|
|
1623
|
+
const next = values.map((value) => {
|
|
1624
|
+
if (value.includes(":")) {
|
|
1625
|
+
const parsed = parseWalletConnectAccount(value);
|
|
1626
|
+
const candidate = parsed ? toExternalAccount(parsed) : null;
|
|
1627
|
+
if (!candidate || eventChainId && candidate.chainId !== eventChainId) {
|
|
1628
|
+
return null;
|
|
1629
|
+
}
|
|
1630
|
+
return approvedAccounts.find(
|
|
1631
|
+
(account) => account.chain === candidate.chain && account.chainId === candidate.chainId && addressesMatch(account.address, candidate.address, account.chain)
|
|
1632
|
+
) ?? null;
|
|
1633
|
+
}
|
|
1634
|
+
return approvedAccounts.find(
|
|
1635
|
+
(account) => (eventChainId ? account.chainId === eventChainId : account.chain === currentAccount.chain && account.chainId === currentAccount.chainId) && addressesMatch(account.address, value, account.chain)
|
|
1636
|
+
) ?? null;
|
|
1637
|
+
});
|
|
1638
|
+
const validated = next.filter(
|
|
1639
|
+
(account) => account !== null
|
|
1640
|
+
);
|
|
1641
|
+
if (validated.length !== values.length) return;
|
|
1642
|
+
publish(validated);
|
|
1643
|
+
};
|
|
1644
|
+
const onSessionEnded = (event) => {
|
|
1645
|
+
if (event.topic !== session.topic) return;
|
|
1646
|
+
cleanup();
|
|
1647
|
+
publish([]);
|
|
1648
|
+
};
|
|
1649
|
+
const cleanup = () => {
|
|
1650
|
+
client.off?.("session_update", onSessionUpdate);
|
|
1651
|
+
client.off?.("session_event", onSessionEvent);
|
|
1652
|
+
client.off?.("session_delete", onSessionEnded);
|
|
1653
|
+
client.off?.("session_expire", onSessionEnded);
|
|
1654
|
+
};
|
|
1655
|
+
client.on?.("session_update", onSessionUpdate);
|
|
1656
|
+
client.on?.("session_event", onSessionEvent);
|
|
1657
|
+
client.on?.("session_delete", onSessionEnded);
|
|
1658
|
+
client.on?.("session_expire", onSessionEnded);
|
|
1659
|
+
return connection;
|
|
1660
|
+
};
|
|
1661
|
+
var connectWalletConnect = async (options) => {
|
|
1662
|
+
const { uri, approval } = await beginWalletConnectSession(options.client, {
|
|
1663
|
+
requiredNamespaces: options.requiredNamespaces ?? DEFAULT_REQUIRED_NAMESPACES,
|
|
1664
|
+
optionalNamespaces: options.optionalNamespaces
|
|
1665
|
+
});
|
|
1666
|
+
return {
|
|
1667
|
+
uri,
|
|
1668
|
+
approval: async () => createConnection({
|
|
1669
|
+
client: options.client,
|
|
1670
|
+
session: await approval(),
|
|
1671
|
+
walletClientType: options.walletClientType,
|
|
1672
|
+
chain: options.chain,
|
|
1673
|
+
address: options.address
|
|
1674
|
+
})
|
|
1675
|
+
};
|
|
1676
|
+
};
|
|
1677
|
+
var listWalletConnectSessions = (client) => client.session?.getAll() ?? [];
|
|
1678
|
+
var restoreWalletConnectSession = (options) => {
|
|
1679
|
+
const session = listWalletConnectSessions(options.client).find(
|
|
1680
|
+
(candidate) => candidate.topic === options.topic
|
|
1681
|
+
);
|
|
1682
|
+
if (!session) throw new Error("WalletConnect session not found");
|
|
1683
|
+
return createConnection({
|
|
1684
|
+
client: options.client,
|
|
1685
|
+
session,
|
|
1686
|
+
walletClientType: options.walletClientType,
|
|
1687
|
+
chain: options.chain,
|
|
1688
|
+
address: options.address
|
|
1689
|
+
});
|
|
1690
|
+
};
|
|
1691
|
+
|
|
1124
1692
|
// src/index.ts
|
|
1125
|
-
import { PostMessageMethod as
|
|
1693
|
+
import { PostMessageMethod as PostMessageMethod9, PostMessageType as PostMessageType9 } from "@moon-x/core/utils";
|
|
1126
1694
|
export {
|
|
1695
|
+
MoonXProvider as MoonKeyProvider,
|
|
1127
1696
|
MoonXProvider,
|
|
1128
|
-
|
|
1129
|
-
|
|
1697
|
+
PostMessageMethod9 as PostMessageMethod,
|
|
1698
|
+
PostMessageType9 as PostMessageType,
|
|
1699
|
+
connectWalletConnect,
|
|
1700
|
+
listWalletConnectSessions,
|
|
1701
|
+
restoreWalletConnectSession,
|
|
1130
1702
|
useAddPasskey,
|
|
1703
|
+
useAppConfig,
|
|
1131
1704
|
useAttachOAuth,
|
|
1132
1705
|
useCreateWallet,
|
|
1133
1706
|
useDetachOAuth,
|
|
1134
1707
|
useEphemeralSigner,
|
|
1135
1708
|
useExportKey,
|
|
1709
|
+
useExternalWallets,
|
|
1710
|
+
useFundWallet,
|
|
1136
1711
|
useImportKey,
|
|
1137
1712
|
useLoginWithEmail,
|
|
1138
1713
|
useLoginWithOAuth,
|
|
1139
1714
|
useLogout,
|
|
1715
|
+
useMoonX as useMoonKey,
|
|
1140
1716
|
useMoonX,
|
|
1141
1717
|
useMoonXContext,
|
|
1142
1718
|
usePasskeyStatus,
|