@crossmint/client-sdk-react-ui 1.3.13 → 1.3.14
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/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +45 -20
- package/dist/index.d.ts +45 -20
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +13 -9
- package/src/components/auth/AuthModal.tsx +196 -0
- package/src/consts/version.ts +1 -1
- package/src/hooks/index.ts +1 -1
- package/src/hooks/useAuth.ts +11 -0
- package/src/hooks/useCrossmint.tsx +2 -1
- package/src/hooks/useWallet.ts +2 -1
- package/src/icons/x.tsx +19 -0
- package/src/providers/CrossmintAuthProvider.test.tsx +198 -0
- package/src/providers/CrossmintAuthProvider.tsx +126 -11
- package/src/providers/CrossmintWalletProvider.test.tsx +206 -0
- package/src/providers/CrossmintWalletProvider.tsx +59 -56
- package/src/testUtils.ts +5 -0
- package/src/utils/constants.ts +1 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/jwt.ts +9 -0
|
@@ -1,90 +1,93 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ReactNode, createContext, useEffect, useMemo, useState } from "react";
|
|
1
|
+
import { ReactNode, createContext, useMemo, useState } from "react";
|
|
3
2
|
|
|
4
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
EVMSmartWallet,
|
|
5
|
+
EVMSmartWalletChain,
|
|
6
|
+
SmartWalletError,
|
|
7
|
+
SmartWalletSDK,
|
|
8
|
+
WalletParams,
|
|
9
|
+
} from "@crossmint/client-sdk-smart-wallet";
|
|
5
10
|
|
|
6
|
-
|
|
7
|
-
type: "evm-smart-wallet";
|
|
8
|
-
defaultChain: "polygon-amoy" | "base-sepolia" | "optimism-sepolia" | "arbitrum-sepolia";
|
|
9
|
-
createOnLogin: "all-users" | "off";
|
|
10
|
-
};
|
|
11
|
+
import { useCrossmint } from "../hooks";
|
|
11
12
|
|
|
12
13
|
type WalletStatus = "not-loaded" | "in-progress" | "loaded" | "loading-error";
|
|
13
|
-
|
|
14
14
|
type ValidWalletState =
|
|
15
15
|
| { status: "not-loaded" | "in-progress" }
|
|
16
16
|
| { status: "loaded"; wallet: EVMSmartWallet }
|
|
17
17
|
| { status: "loading-error"; error: SmartWalletError };
|
|
18
18
|
|
|
19
|
-
function deriveErrorState(error: unknown): { status: "loading-error"; error: SmartWalletError } {
|
|
20
|
-
if (error instanceof SmartWalletError) {
|
|
21
|
-
return { status: "loading-error", error };
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
25
|
-
const stack = error instanceof Error ? error.stack : undefined;
|
|
26
|
-
return { status: "loading-error", error: new SmartWalletError(`Unknown Wallet Error: ${message}`, stack) };
|
|
27
|
-
}
|
|
28
|
-
|
|
29
19
|
type WalletContext = {
|
|
30
20
|
status: WalletStatus;
|
|
31
21
|
wallet?: EVMSmartWallet;
|
|
32
22
|
error?: SmartWalletError;
|
|
33
|
-
getOrCreateWallet: () =>
|
|
23
|
+
getOrCreateWallet: (config?: WalletConfig) => { startedCreation: boolean; reason?: string };
|
|
24
|
+
clearWallet: () => void;
|
|
34
25
|
};
|
|
35
26
|
|
|
36
27
|
export const WalletContext = createContext<WalletContext>({
|
|
37
28
|
status: "not-loaded",
|
|
38
|
-
getOrCreateWallet:
|
|
29
|
+
getOrCreateWallet: () => ({ startedCreation: false }),
|
|
30
|
+
clearWallet: () => {},
|
|
39
31
|
});
|
|
40
32
|
|
|
41
|
-
export
|
|
33
|
+
export type WalletConfig = WalletParams & { type: "evm-smart-wallet" };
|
|
34
|
+
|
|
35
|
+
export function CrossmintWalletProvider({
|
|
36
|
+
children,
|
|
37
|
+
defaultChain,
|
|
38
|
+
}: {
|
|
39
|
+
children: ReactNode;
|
|
40
|
+
defaultChain: EVMSmartWalletChain;
|
|
41
|
+
}) {
|
|
42
42
|
const { crossmint } = useCrossmint("CrossmintWalletProvider must be used within CrossmintProvider");
|
|
43
|
-
const [state, setState] = useState<ValidWalletState>({ status: "not-loaded" });
|
|
44
43
|
const smartWalletSDK = useMemo(() => SmartWalletSDK.init({ clientApiKey: crossmint.apiKey }), [crossmint.apiKey]);
|
|
45
44
|
|
|
46
|
-
const
|
|
47
|
-
try {
|
|
48
|
-
setState({ status: "in-progress" });
|
|
49
|
-
const wallet = await smartWalletSDK.getOrCreateWallet({ jwt }, config.defaultChain);
|
|
50
|
-
setState({ status: "loaded", wallet });
|
|
51
|
-
} catch (error: unknown) {
|
|
52
|
-
console.error("There was an error creating a wallet ", error);
|
|
53
|
-
setState(deriveErrorState(error));
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
useEffect(() => {
|
|
58
|
-
if (config.createOnLogin === "all-users" && crossmint.jwt != null && state.status === "not-loaded") {
|
|
59
|
-
console.log("Getting or Creating wallet");
|
|
60
|
-
getOrCreateWalletInternal(crossmint.jwt);
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
45
|
+
const [state, setState] = useState<ValidWalletState>({ status: "not-loaded" });
|
|
63
46
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return;
|
|
47
|
+
const getOrCreateWallet = (config: WalletConfig = { type: "evm-smart-wallet", signer: { type: "PASSKEY" } }) => {
|
|
48
|
+
if (state.status == "in-progress") {
|
|
49
|
+
console.log("Wallet already loading");
|
|
50
|
+
return { startedCreation: false, reason: "Wallet is already loading." };
|
|
68
51
|
}
|
|
69
|
-
}, [crossmint.jwt, config.createOnLogin, state.status]);
|
|
70
52
|
|
|
71
|
-
const getOrCreateWalletExternal = async () => {
|
|
72
53
|
if (crossmint.jwt == null) {
|
|
73
|
-
|
|
74
|
-
return;
|
|
54
|
+
return { startedCreation: false, reason: `Jwt not set in "CrossmintProvider".` };
|
|
75
55
|
}
|
|
76
56
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
57
|
+
const internalCall = async () => {
|
|
58
|
+
try {
|
|
59
|
+
setState({ status: "in-progress" });
|
|
60
|
+
const wallet = await smartWalletSDK.getOrCreateWallet(
|
|
61
|
+
{ jwt: crossmint.jwt as string },
|
|
62
|
+
defaultChain,
|
|
63
|
+
config
|
|
64
|
+
);
|
|
65
|
+
setState({ status: "loaded", wallet });
|
|
66
|
+
} catch (error: unknown) {
|
|
67
|
+
console.error("There was an error creating a wallet ", error);
|
|
68
|
+
setState(deriveErrorState(error));
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
internalCall();
|
|
73
|
+
return { startedCreation: true };
|
|
74
|
+
};
|
|
81
75
|
|
|
82
|
-
|
|
76
|
+
const clearWallet = () => {
|
|
77
|
+
setState({ status: "not-loaded" });
|
|
83
78
|
};
|
|
84
79
|
|
|
85
80
|
return (
|
|
86
|
-
<WalletContext.Provider value={{ ...state, getOrCreateWallet
|
|
87
|
-
{children}
|
|
88
|
-
</WalletContext.Provider>
|
|
81
|
+
<WalletContext.Provider value={{ ...state, getOrCreateWallet, clearWallet }}>{children}</WalletContext.Provider>
|
|
89
82
|
);
|
|
90
83
|
}
|
|
84
|
+
|
|
85
|
+
function deriveErrorState(error: unknown): { status: "loading-error"; error: SmartWalletError } {
|
|
86
|
+
if (error instanceof SmartWalletError) {
|
|
87
|
+
return { status: "loading-error", error };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
91
|
+
const stack = error instanceof Error ? error.stack : undefined;
|
|
92
|
+
return { status: "loading-error", error: new SmartWalletError(`Unknown Wallet Error: ${message}`, stack) };
|
|
93
|
+
}
|
package/src/testUtils.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const SESSION_PREFIX = "crossmint-session";
|
package/src/utils/jwt.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { SESSION_PREFIX } from "./constants";
|
|
2
|
+
|
|
3
|
+
export function getCachedJwt(): string | undefined {
|
|
4
|
+
if (typeof document === "undefined") {
|
|
5
|
+
return undefined; // Check if we're on the client-side
|
|
6
|
+
}
|
|
7
|
+
const crossmintSession = document.cookie.split("; ").find((row) => row.startsWith(SESSION_PREFIX));
|
|
8
|
+
return crossmintSession ? crossmintSession.split("=")[1] : undefined;
|
|
9
|
+
}
|