@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.
@@ -1,90 +1,93 @@
1
- import { useCrossmint } from "@/hooks";
2
- import { ReactNode, createContext, useEffect, useMemo, useState } from "react";
1
+ import { ReactNode, createContext, useMemo, useState } from "react";
3
2
 
4
- import { EVMSmartWallet, SmartWalletError, SmartWalletSDK } from "@crossmint/client-sdk-smart-wallet";
3
+ import {
4
+ EVMSmartWallet,
5
+ EVMSmartWalletChain,
6
+ SmartWalletError,
7
+ SmartWalletSDK,
8
+ WalletParams,
9
+ } from "@crossmint/client-sdk-smart-wallet";
5
10
 
6
- export type CrossmintWalletConfig = {
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: () => Promise<void>;
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: async () => {},
29
+ getOrCreateWallet: () => ({ startedCreation: false }),
30
+ clearWallet: () => {},
39
31
  });
40
32
 
41
- export function CrossmintWalletProvider({ children, config }: { config: CrossmintWalletConfig; children: ReactNode }) {
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 getOrCreateWalletInternal = async (jwt: string) => {
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
- if (state.status === "loaded" && crossmint.jwt == null) {
65
- console.log("Clearing wallet");
66
- setState({ status: "not-loaded" });
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
- console.log("No authenticated user, not creating wallet.");
74
- return;
54
+ return { startedCreation: false, reason: `Jwt not set in "CrossmintProvider".` };
75
55
  }
76
56
 
77
- if (state.status === "loaded" || state.status === "in-progress") {
78
- console.log("Wallet is already loaded, or is currently loading.");
79
- return;
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
- return getOrCreateWalletInternal(crossmint.jwt);
76
+ const clearWallet = () => {
77
+ setState({ status: "not-loaded" });
83
78
  };
84
79
 
85
80
  return (
86
- <WalletContext.Provider value={{ ...state, getOrCreateWallet: getOrCreateWalletExternal }}>
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
+ }
@@ -0,0 +1,5 @@
1
+ export const MOCK_API_KEY = "sk_development_12341234";
2
+ export const waitForSettledState = async (callback: () => void) => {
3
+ await new Promise((resolve) => setTimeout(resolve, 20));
4
+ callback();
5
+ };
@@ -0,0 +1 @@
1
+ export const SESSION_PREFIX = "crossmint-session";
@@ -0,0 +1,2 @@
1
+ export * from "./constants";
2
+ export * from "./jwt";
@@ -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
+ }