@crossmint/client-sdk-react-ui 1.4.1 → 1.5.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/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -5
- package/dist/index.d.ts +9 -5
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/src/components/auth/AuthModal.tsx +15 -39
- package/src/components/auth/PasskeyPrompt.tsx +251 -0
- package/src/components/common/PoweredByCrossmint.tsx +19 -0
- package/src/icons/fingerprint.tsx +25 -0
- package/src/icons/passkey.tsx +18 -0
- package/src/icons/passkeyPromptLogo.tsx +76 -0
- package/src/icons/poweredByLeaf.tsx +84 -0
- package/src/providers/CrossmintAuthProvider.tsx +13 -2
- package/src/providers/CrossmintWalletProvider.tsx +85 -23
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type ReactNode, createContext, useMemo, useState } from "react";
|
|
2
|
+
import { createPortal } from "react-dom";
|
|
2
3
|
|
|
3
4
|
import {
|
|
4
5
|
type EVMSmartWallet,
|
|
@@ -6,11 +7,30 @@ import {
|
|
|
6
7
|
SmartWalletError,
|
|
7
8
|
SmartWalletSDK,
|
|
8
9
|
type WalletParams,
|
|
10
|
+
type PasskeySigner,
|
|
9
11
|
} from "@crossmint/client-sdk-smart-wallet";
|
|
10
12
|
|
|
11
13
|
import { useCrossmint } from "../hooks";
|
|
14
|
+
import type { UIConfig } from "@crossmint/common-sdk-base";
|
|
15
|
+
import { PasskeyPrompt } from "@/components/auth/PasskeyPrompt";
|
|
12
16
|
|
|
13
17
|
type WalletStatus = "not-loaded" | "in-progress" | "loaded" | "loading-error";
|
|
18
|
+
|
|
19
|
+
type ValidPasskeyPromptType =
|
|
20
|
+
| "create-wallet"
|
|
21
|
+
| "transaction"
|
|
22
|
+
| "not-supported"
|
|
23
|
+
| "create-wallet-error"
|
|
24
|
+
| "transaction-error";
|
|
25
|
+
type PasskeyPromptState =
|
|
26
|
+
| {
|
|
27
|
+
type: ValidPasskeyPromptType;
|
|
28
|
+
open: true;
|
|
29
|
+
primaryActionOnClick: () => void;
|
|
30
|
+
secondaryActionOnClick?: () => void;
|
|
31
|
+
}
|
|
32
|
+
| { open: false };
|
|
33
|
+
|
|
14
34
|
type ValidWalletState =
|
|
15
35
|
| { status: "not-loaded" | "in-progress" }
|
|
16
36
|
| { status: "loaded"; wallet: EVMSmartWallet }
|
|
@@ -20,13 +40,15 @@ type WalletContext = {
|
|
|
20
40
|
status: WalletStatus;
|
|
21
41
|
wallet?: EVMSmartWallet;
|
|
22
42
|
error?: SmartWalletError;
|
|
23
|
-
getOrCreateWallet: (
|
|
43
|
+
getOrCreateWallet: (
|
|
44
|
+
config?: Pick<WalletConfig, "signer" | "type">
|
|
45
|
+
) => Promise<{ startedCreation: boolean; reason?: string }>;
|
|
24
46
|
clearWallet: () => void;
|
|
25
47
|
};
|
|
26
48
|
|
|
27
49
|
export const WalletContext = createContext<WalletContext>({
|
|
28
50
|
status: "not-loaded",
|
|
29
|
-
getOrCreateWallet: () => ({ startedCreation: false }),
|
|
51
|
+
getOrCreateWallet: () => Promise.resolve({ startedCreation: false }),
|
|
30
52
|
clearWallet: () => {},
|
|
31
53
|
});
|
|
32
54
|
|
|
@@ -35,17 +57,24 @@ export type WalletConfig = WalletParams & { type: "evm-smart-wallet" };
|
|
|
35
57
|
export function CrossmintWalletProvider({
|
|
36
58
|
children,
|
|
37
59
|
defaultChain,
|
|
60
|
+
showWalletModals = true, // enabled by default
|
|
61
|
+
appearance,
|
|
38
62
|
}: {
|
|
39
63
|
children: ReactNode;
|
|
40
64
|
defaultChain: EVMSmartWalletChain;
|
|
65
|
+
showWalletModals?: boolean;
|
|
66
|
+
appearance?: UIConfig;
|
|
41
67
|
}) {
|
|
42
68
|
const { crossmint } = useCrossmint("CrossmintWalletProvider must be used within CrossmintProvider");
|
|
43
69
|
const smartWalletSDK = useMemo(() => SmartWalletSDK.init({ clientApiKey: crossmint.apiKey }), [crossmint.apiKey]);
|
|
44
70
|
|
|
45
|
-
const [
|
|
71
|
+
const [walletState, setWalletState] = useState<ValidWalletState>({ status: "not-loaded" });
|
|
72
|
+
const [passkeyPromptState, setPasskeyPromptState] = useState<PasskeyPromptState>({ open: false });
|
|
46
73
|
|
|
47
|
-
const getOrCreateWallet = (
|
|
48
|
-
|
|
74
|
+
const getOrCreateWallet = async (
|
|
75
|
+
config: WalletConfig = { type: "evm-smart-wallet", signer: { type: "PASSKEY" } }
|
|
76
|
+
) => {
|
|
77
|
+
if (walletState.status == "in-progress") {
|
|
49
78
|
console.log("Wallet already loading");
|
|
50
79
|
return { startedCreation: false, reason: "Wallet is already loading." };
|
|
51
80
|
}
|
|
@@ -54,31 +83,64 @@ export function CrossmintWalletProvider({
|
|
|
54
83
|
return { startedCreation: false, reason: `Jwt not set in "CrossmintProvider".` };
|
|
55
84
|
}
|
|
56
85
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
internalCall();
|
|
86
|
+
try {
|
|
87
|
+
setWalletState({ status: "in-progress" });
|
|
88
|
+
const wallet = await smartWalletSDK.getOrCreateWallet(
|
|
89
|
+
{ jwt: crossmint.jwt as string },
|
|
90
|
+
defaultChain,
|
|
91
|
+
enhanceConfigWithPasskeyPrompts(config)
|
|
92
|
+
);
|
|
93
|
+
setWalletState({ status: "loaded", wallet });
|
|
94
|
+
} catch (error: unknown) {
|
|
95
|
+
console.error("There was an error creating a wallet ", error);
|
|
96
|
+
setWalletState(deriveErrorState(error));
|
|
97
|
+
}
|
|
73
98
|
return { startedCreation: true };
|
|
74
99
|
};
|
|
75
100
|
|
|
101
|
+
const enhanceConfigWithPasskeyPrompts = (config: WalletConfig) => {
|
|
102
|
+
if (showWalletModals && (config.signer as PasskeySigner).type === "PASSKEY") {
|
|
103
|
+
return {
|
|
104
|
+
...config,
|
|
105
|
+
signer: {
|
|
106
|
+
...config.signer,
|
|
107
|
+
onPrePasskeyRegistration: createPasskeyPrompt("create-wallet"),
|
|
108
|
+
onPasskeyRegistrationError: createPasskeyPrompt("create-wallet-error"),
|
|
109
|
+
onFirstTimePasskeySigning: createPasskeyPrompt("transaction"),
|
|
110
|
+
onFirstTimePasskeySigningError: createPasskeyPrompt("transaction-error"),
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
return config;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const createPasskeyPrompt = (type: ValidPasskeyPromptType) => () =>
|
|
118
|
+
new Promise<void>((resolve) => {
|
|
119
|
+
setPasskeyPromptState({
|
|
120
|
+
type,
|
|
121
|
+
open: true,
|
|
122
|
+
primaryActionOnClick: () => {
|
|
123
|
+
setPasskeyPromptState({ open: false });
|
|
124
|
+
resolve();
|
|
125
|
+
},
|
|
126
|
+
secondaryActionOnClick: () => {
|
|
127
|
+
setPasskeyPromptState({ open: false });
|
|
128
|
+
resolve();
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
76
133
|
const clearWallet = () => {
|
|
77
|
-
|
|
134
|
+
setWalletState({ status: "not-loaded" });
|
|
78
135
|
};
|
|
79
136
|
|
|
80
137
|
return (
|
|
81
|
-
<WalletContext.Provider value={{ ...
|
|
138
|
+
<WalletContext.Provider value={{ ...walletState, getOrCreateWallet, clearWallet }}>
|
|
139
|
+
{children}
|
|
140
|
+
{passkeyPromptState.open
|
|
141
|
+
? createPortal(<PasskeyPrompt state={passkeyPromptState} appearance={appearance} />, document.body)
|
|
142
|
+
: null}
|
|
143
|
+
</WalletContext.Provider>
|
|
82
144
|
);
|
|
83
145
|
}
|
|
84
146
|
|