@crossmint/client-sdk-react-ui 1.7.1 → 1.9.1
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.d.cts +20 -7
- package/dist/index.d.ts +20 -7
- package/dist/index.js +1 -1
- package/package.json +17 -7
- package/src/components/auth/AuthForm.tsx +50 -0
- package/src/components/auth/AuthFormBackButton.tsx +26 -0
- package/src/components/auth/AuthFormDialog.tsx +33 -0
- package/src/components/auth/EmbeddedAuthForm.tsx +5 -0
- package/src/components/auth/methods/email/EmailAuthFlow.tsx +19 -0
- package/src/components/auth/methods/email/EmailOTPInput.tsx +123 -0
- package/src/components/auth/methods/email/EmailSignIn.tsx +113 -0
- package/src/components/auth/methods/farcaster/FarcasterSignIn.tsx +170 -0
- package/src/components/auth/methods/google/GoogleSignIn.tsx +62 -0
- package/src/components/common/Dialog.tsx +141 -0
- package/src/components/common/Divider.tsx +25 -0
- package/src/components/common/InputOTP.tsx +89 -0
- package/src/components/common/PoweredByCrossmint.tsx +4 -9
- package/src/components/common/Spinner.tsx +22 -0
- package/src/components/dynamic-xyz/DynamicContextProviderWrapper.tsx +31 -0
- package/src/components/embed/v3/CrossmintEmbeddedCheckoutV3.tsx +7 -0
- package/src/components/embed/v3/EmbeddedCheckoutV3IFrame.tsx +74 -0
- package/src/components/embed/v3/crypto/CryptoWalletConnectionHandler.tsx +138 -0
- package/src/components/embed/v3/crypto/utils/handleEvmTransaction.ts +65 -0
- package/src/components/embed/v3/crypto/utils/handleSendTransaction.ts +31 -0
- package/src/components/embed/v3/crypto/utils/handleSolanaTransaction.ts +51 -0
- package/src/components/embed/v3/index.ts +1 -0
- package/src/components/index.ts +3 -0
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useAuthSignIn.ts +117 -0
- package/src/hooks/useCrossmintCheckout.tsx +54 -0
- package/src/hooks/useOAuthWindowListener.ts +87 -0
- package/src/hooks/useRefreshToken.test.ts +21 -8
- package/src/hooks/useRefreshToken.ts +5 -4
- package/src/icons/alert.tsx +19 -0
- package/src/icons/discord.tsx +18 -0
- package/src/icons/emailOTP.tsx +147 -0
- package/src/icons/farcaster.tsx +26 -0
- package/src/icons/google.tsx +30 -0
- package/src/icons/leftArrow.tsx +20 -0
- package/src/icons/poweredByLeaf.tsx +2 -2
- package/src/providers/CrossmintAuthProvider.test.tsx +4 -3
- package/src/providers/CrossmintAuthProvider.tsx +36 -32
- package/src/providers/CrossmintWalletProvider.tsx +3 -3
- package/src/providers/auth/AuthFormProvider.test.tsx +105 -0
- package/src/providers/auth/AuthFormProvider.tsx +116 -0
- package/src/providers/auth/FarcasterProvider.tsx +12 -0
- package/src/twind.config.ts +101 -1
- package/src/types/auth.ts +4 -0
- package/src/utils/authCookies.ts +0 -3
- package/src/utils/createCrossmintApiClient.ts +17 -0
- package/src/components/auth/AuthModal.tsx +0 -207
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { OTPInput, OTPInputContext } from "input-otp";
|
|
5
|
+
import { createContext, useContext } from "react";
|
|
6
|
+
import { classNames } from "@/utils/classNames";
|
|
7
|
+
|
|
8
|
+
// Define the type for customStyles
|
|
9
|
+
type CustomStyles = {
|
|
10
|
+
textPrimary: string;
|
|
11
|
+
inputBackground: string;
|
|
12
|
+
buttonBackground: string;
|
|
13
|
+
accent: string;
|
|
14
|
+
danger: string;
|
|
15
|
+
border: string;
|
|
16
|
+
borderRadius?: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// Create a context for customStyles
|
|
20
|
+
const CustomStylesContext = createContext<CustomStyles | undefined>(undefined);
|
|
21
|
+
|
|
22
|
+
const InputOTP = React.forwardRef<
|
|
23
|
+
React.ElementRef<typeof OTPInput>,
|
|
24
|
+
React.ComponentPropsWithoutRef<typeof OTPInput> & { customStyles?: CustomStyles }
|
|
25
|
+
>(({ className, containerClassName, customStyles, ...props }, ref) => (
|
|
26
|
+
<CustomStylesContext.Provider value={customStyles}>
|
|
27
|
+
<OTPInput
|
|
28
|
+
autoFocus
|
|
29
|
+
ref={ref}
|
|
30
|
+
containerClassName={classNames("flex items-center gap-2 has-[:disabled]:opacity-50", containerClassName)}
|
|
31
|
+
className={classNames("disabled:cursor-not-allowed", className)}
|
|
32
|
+
{...props}
|
|
33
|
+
/>
|
|
34
|
+
</CustomStylesContext.Provider>
|
|
35
|
+
));
|
|
36
|
+
InputOTP.displayName = "InputOTP";
|
|
37
|
+
|
|
38
|
+
const InputOTPGroup = React.forwardRef<React.ElementRef<"div">, React.ComponentPropsWithoutRef<"div">>(
|
|
39
|
+
({ className, ...props }, ref) => (
|
|
40
|
+
<div ref={ref} className={classNames("flex gap-2 items-center", className)} {...props} />
|
|
41
|
+
)
|
|
42
|
+
);
|
|
43
|
+
InputOTPGroup.displayName = "InputOTPGroup";
|
|
44
|
+
|
|
45
|
+
const InputOTPSlot = React.forwardRef<
|
|
46
|
+
React.ElementRef<"div">,
|
|
47
|
+
React.ComponentPropsWithoutRef<"div"> & {
|
|
48
|
+
index: number;
|
|
49
|
+
hasError?: boolean;
|
|
50
|
+
}
|
|
51
|
+
>(({ index, className, hasError, ...props }, ref) => {
|
|
52
|
+
const inputOTPContext = React.useContext(OTPInputContext);
|
|
53
|
+
const { char, hasFakeCaret, isActive } = inputOTPContext.slots[index];
|
|
54
|
+
const customStyles = useContext(CustomStylesContext);
|
|
55
|
+
|
|
56
|
+
return (
|
|
57
|
+
<div
|
|
58
|
+
ref={ref}
|
|
59
|
+
className={classNames(
|
|
60
|
+
"relative flex h-14 w-12 items-center justify-center border text-lg transition-all rounded-md text-cm-text-primary",
|
|
61
|
+
isActive && `z-10 ring-2 ring-offset-background`,
|
|
62
|
+
className
|
|
63
|
+
)}
|
|
64
|
+
style={{
|
|
65
|
+
borderRadius: customStyles?.borderRadius,
|
|
66
|
+
borderColor: hasError ? customStyles?.danger : customStyles?.border,
|
|
67
|
+
boxShadow: isActive ? `0 0 0 2px ${customStyles?.accent}` : "none",
|
|
68
|
+
backgroundColor: char ? customStyles?.buttonBackground : customStyles?.inputBackground,
|
|
69
|
+
}}
|
|
70
|
+
{...props}
|
|
71
|
+
>
|
|
72
|
+
{char}
|
|
73
|
+
{hasFakeCaret && (
|
|
74
|
+
<div className="pointer-events-none absolute inset-0 flex items-center justify-center">
|
|
75
|
+
<div
|
|
76
|
+
className="h-4 w-px animate-caret-blink duration-1000"
|
|
77
|
+
style={{
|
|
78
|
+
height: "18px",
|
|
79
|
+
backgroundColor: customStyles?.textPrimary,
|
|
80
|
+
}}
|
|
81
|
+
/>
|
|
82
|
+
</div>
|
|
83
|
+
)}
|
|
84
|
+
</div>
|
|
85
|
+
);
|
|
86
|
+
});
|
|
87
|
+
InputOTPSlot.displayName = "InputOTPSlot";
|
|
88
|
+
|
|
89
|
+
export { InputOTP, InputOTPGroup, InputOTPSlot };
|
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
import { PoweredByLeaf } from "@/icons/poweredByLeaf";
|
|
2
|
+
import { classNames } from "@/utils/classNames";
|
|
2
3
|
|
|
3
|
-
export function PoweredByCrossmint({ color }: { color?: string }) {
|
|
4
|
+
export function PoweredByCrossmint({ color, className }: { color?: string; className?: string }) {
|
|
4
5
|
return (
|
|
5
6
|
<p
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
fontSize: "0.75rem",
|
|
9
|
-
fontWeight: "400",
|
|
10
|
-
letterSpacing: "-0.2px",
|
|
11
|
-
padding: "0.5rem",
|
|
12
|
-
color: color || "#67797F",
|
|
13
|
-
}}
|
|
7
|
+
className={classNames("flex text-xs font-normal tracking-tight p-2 items-center", className)}
|
|
8
|
+
style={{ color: color || "#67797F" }}
|
|
14
9
|
>
|
|
15
10
|
Powered by
|
|
16
11
|
<span className="flex self-center pl-1 gap-1 items-center font-semibold">
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type React from "react";
|
|
2
|
+
import { classNames } from "@/utils/classNames";
|
|
3
|
+
|
|
4
|
+
export const Spinner = ({ className, style }: { className?: string; style?: React.CSSProperties }) => (
|
|
5
|
+
<svg
|
|
6
|
+
aria-hidden="true"
|
|
7
|
+
className={classNames("w-6 h-6 fill-cm-text-primary text-cm-text-secondary animate-spin", className)}
|
|
8
|
+
style={style}
|
|
9
|
+
viewBox="0 0 100 101"
|
|
10
|
+
fill="none"
|
|
11
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
12
|
+
>
|
|
13
|
+
<path
|
|
14
|
+
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
|
|
15
|
+
fill="currentColor"
|
|
16
|
+
/>
|
|
17
|
+
<path
|
|
18
|
+
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
|
|
19
|
+
fill="currentFill"
|
|
20
|
+
/>
|
|
21
|
+
</svg>
|
|
22
|
+
);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { APIKeyEnvironmentPrefix } from "@crossmint/common-sdk-base";
|
|
2
|
+
import { type DynamicContextProps, DynamicContextProvider } from "@dynamic-labs/sdk-react-core";
|
|
3
|
+
import type { ReactNode } from "react";
|
|
4
|
+
|
|
5
|
+
export interface DynamicContextProviderWrapperProps {
|
|
6
|
+
children?: ReactNode;
|
|
7
|
+
settings: Omit<DynamicContextProps["settings"], "initialAuthenticationMode" | "environmentId">;
|
|
8
|
+
apiKeyEnvironment: APIKeyEnvironmentPrefix;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default function DynamicContextProviderWrapper({
|
|
12
|
+
children,
|
|
13
|
+
settings,
|
|
14
|
+
apiKeyEnvironment,
|
|
15
|
+
}: DynamicContextProviderWrapperProps) {
|
|
16
|
+
return (
|
|
17
|
+
<DynamicContextProvider
|
|
18
|
+
settings={{
|
|
19
|
+
initialAuthenticationMode: "connect-only",
|
|
20
|
+
environmentId:
|
|
21
|
+
apiKeyEnvironment === "production"
|
|
22
|
+
? "3fc6c24e-6a8e-45f8-aae1-a87d7a027e12"
|
|
23
|
+
: "cd53135a-b32b-4704-bfca-324b665e9329",
|
|
24
|
+
cssOverrides: `.powered-by-dynamic { display: none !important; }`,
|
|
25
|
+
...settings,
|
|
26
|
+
}}
|
|
27
|
+
>
|
|
28
|
+
{children}
|
|
29
|
+
</DynamicContextProvider>
|
|
30
|
+
);
|
|
31
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { CrossmintEmbeddedCheckoutV3Props } from "@crossmint/client-sdk-base";
|
|
2
|
+
|
|
3
|
+
import { EmbeddedCheckoutV3IFrame } from "./EmbeddedCheckoutV3IFrame";
|
|
4
|
+
|
|
5
|
+
export function CrossmintEmbeddedCheckout_Alpha(props: CrossmintEmbeddedCheckoutV3Props) {
|
|
6
|
+
return <EmbeddedCheckoutV3IFrame {...props} />;
|
|
7
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { useCrossmint } from "@/hooks";
|
|
2
|
+
import { useEffect, useRef, useState } from "react";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
type CrossmintEmbeddedCheckoutV3Props,
|
|
6
|
+
type EmbeddedCheckoutV3IFrameEmitter,
|
|
7
|
+
crossmintEmbeddedCheckoutV3Service,
|
|
8
|
+
} from "@crossmint/client-sdk-base";
|
|
9
|
+
|
|
10
|
+
import { CryptoWalletConnectionHandler } from "./crypto/CryptoWalletConnectionHandler";
|
|
11
|
+
import { createCrossmintApiClient } from "@/utils/createCrossmintApiClient";
|
|
12
|
+
|
|
13
|
+
export function EmbeddedCheckoutV3IFrame(props: CrossmintEmbeddedCheckoutV3Props) {
|
|
14
|
+
const [iframeClient, setIframeClient] = useState<EmbeddedCheckoutV3IFrameEmitter | null>(null);
|
|
15
|
+
const [height, setHeight] = useState(0);
|
|
16
|
+
|
|
17
|
+
const { crossmint } = useCrossmint();
|
|
18
|
+
const apiClient = createCrossmintApiClient(crossmint);
|
|
19
|
+
const embedV3Service = crossmintEmbeddedCheckoutV3Service({ apiClient });
|
|
20
|
+
|
|
21
|
+
const ref = useRef<HTMLIFrameElement>(null);
|
|
22
|
+
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
const iframe = ref.current;
|
|
25
|
+
if (!iframe || iframeClient) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
setIframeClient(embedV3Service.iframe.createClient(iframe));
|
|
29
|
+
}, [ref.current, iframeClient]);
|
|
30
|
+
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
if (iframeClient == null) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
iframeClient.on("ui:height.changed", (data) => setHeight(data.height));
|
|
36
|
+
|
|
37
|
+
return () => {
|
|
38
|
+
iframeClient.off("ui:height.changed");
|
|
39
|
+
};
|
|
40
|
+
}, [iframeClient]);
|
|
41
|
+
|
|
42
|
+
return (
|
|
43
|
+
<>
|
|
44
|
+
<iframe
|
|
45
|
+
ref={ref}
|
|
46
|
+
src={embedV3Service.iframe.getUrl(props)}
|
|
47
|
+
id="crossmint-embedded-checkout.iframe"
|
|
48
|
+
role="crossmint-embedded-checkout.iframe"
|
|
49
|
+
allow="payment *"
|
|
50
|
+
style={{
|
|
51
|
+
boxShadow: "none",
|
|
52
|
+
border: "none",
|
|
53
|
+
padding: "0px",
|
|
54
|
+
width: "100%",
|
|
55
|
+
minWidth: "100%",
|
|
56
|
+
overflow: "hidden",
|
|
57
|
+
display: "block",
|
|
58
|
+
userSelect: "none",
|
|
59
|
+
transform: "translate(0px)",
|
|
60
|
+
opacity: "1",
|
|
61
|
+
transition: "ease 0s, opacity 0.4s ease 0.1s",
|
|
62
|
+
height: `${height}px`,
|
|
63
|
+
backgroundColor: "transparent",
|
|
64
|
+
}}
|
|
65
|
+
/>
|
|
66
|
+
{props.payment.crypto.enabled ? (
|
|
67
|
+
<CryptoWalletConnectionHandler
|
|
68
|
+
iframeClient={iframeClient}
|
|
69
|
+
apiKeyEnvironment={apiClient["parsedAPIKey"].environment}
|
|
70
|
+
/>
|
|
71
|
+
) : null}
|
|
72
|
+
</>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import DynamicContextProviderWrapper from "@/components/dynamic-xyz/DynamicContextProviderWrapper";
|
|
2
|
+
import type { EmbeddedCheckoutV3IFrameEmitter } from "@crossmint/client-sdk-base";
|
|
3
|
+
import {
|
|
4
|
+
type APIKeyEnvironmentPrefix,
|
|
5
|
+
type BlockchainIncludingTestnet,
|
|
6
|
+
chainIdToBlockchain,
|
|
7
|
+
} from "@crossmint/common-sdk-base";
|
|
8
|
+
import { EthereumWalletConnectors } from "@dynamic-labs/ethereum";
|
|
9
|
+
import { type HandleConnectedWallet, useDynamicContext } from "@dynamic-labs/sdk-react-core";
|
|
10
|
+
import { SolanaWalletConnectors } from "@dynamic-labs/solana";
|
|
11
|
+
import { useEffect } from "react";
|
|
12
|
+
import { handleSendTransaction } from "./utils/handleSendTransaction";
|
|
13
|
+
|
|
14
|
+
export function CryptoWalletConnectionHandler(props: {
|
|
15
|
+
iframeClient: EmbeddedCheckoutV3IFrameEmitter | null;
|
|
16
|
+
apiKeyEnvironment: APIKeyEnvironmentPrefix;
|
|
17
|
+
}) {
|
|
18
|
+
const { iframeClient, apiKeyEnvironment } = props;
|
|
19
|
+
|
|
20
|
+
return (
|
|
21
|
+
<DynamicContextProviderWrapper
|
|
22
|
+
apiKeyEnvironment={apiKeyEnvironment}
|
|
23
|
+
settings={{
|
|
24
|
+
walletConnectors: [EthereumWalletConnectors, SolanaWalletConnectors],
|
|
25
|
+
events: {
|
|
26
|
+
onAuthFlowCancel() {
|
|
27
|
+
console.log("[CryptoWalletConnectionHandler] onAuthFlowCancel");
|
|
28
|
+
iframeClient?.send("crypto:connect-wallet.failed", {
|
|
29
|
+
error: "cancelled",
|
|
30
|
+
});
|
|
31
|
+
},
|
|
32
|
+
onAuthFlowClose() {
|
|
33
|
+
console.log("[CryptoWalletConnectionHandler] onAuthFlowClose");
|
|
34
|
+
},
|
|
35
|
+
onAuthFailure(data, reason) {
|
|
36
|
+
console.error("[CryptoWalletConnectionHandler] onAuthFailure", data, reason);
|
|
37
|
+
},
|
|
38
|
+
onAuthSuccess(data) {
|
|
39
|
+
console.log("[CryptoWalletConnectionHandler] onAuthSuccess", data);
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
handlers: {
|
|
43
|
+
handleConnectedWallet: async (wallet) => {
|
|
44
|
+
console.log("[CryptoWalletConnectionHandler] handleConnectedWallet", wallet);
|
|
45
|
+
|
|
46
|
+
const address = wallet.address;
|
|
47
|
+
if (!address) {
|
|
48
|
+
console.error("[CryptoWalletConnectionHandler] handleConnectedWallet: address is missing");
|
|
49
|
+
iframeClient?.send("crypto:connect-wallet.failed", {
|
|
50
|
+
error: "address is missing",
|
|
51
|
+
});
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const chain = await dynamicChainToCrossmintChain(wallet);
|
|
56
|
+
|
|
57
|
+
iframeClient?.send("crypto:connect-wallet.success", {
|
|
58
|
+
address,
|
|
59
|
+
chain,
|
|
60
|
+
walletProviderKey: wallet.connector?.key,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
return true;
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
}}
|
|
67
|
+
>
|
|
68
|
+
<_CryptoWalletConnectionHandler {...props} />
|
|
69
|
+
</DynamicContextProviderWrapper>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function _CryptoWalletConnectionHandler({ iframeClient }: Parameters<typeof CryptoWalletConnectionHandler>[0]) {
|
|
74
|
+
const { setShowAuthFlow, primaryWallet, handleLogOut } = useDynamicContext();
|
|
75
|
+
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
if (iframeClient == null) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const showAuthFlowListener = iframeClient.on("crypto:connect-wallet.show", async ({ show }) => {
|
|
81
|
+
await handleLogOut();
|
|
82
|
+
setShowAuthFlow(show);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
return () => {
|
|
86
|
+
iframeClient.off(showAuthFlowListener);
|
|
87
|
+
};
|
|
88
|
+
}, [iframeClient, handleLogOut, setShowAuthFlow]);
|
|
89
|
+
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
if (iframeClient == null) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const signTransactionListener = iframeClient.on(
|
|
95
|
+
"crypto:send-transaction",
|
|
96
|
+
async ({ chain, serializedTransaction }) => {
|
|
97
|
+
if (primaryWallet == null) {
|
|
98
|
+
console.error("[CryptoWalletConnectionHandler] signTransaction: primaryWallet is missing");
|
|
99
|
+
iframeClient.send("crypto:send-transaction:failed", {
|
|
100
|
+
error: "primaryWallet is missing",
|
|
101
|
+
});
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
await handleSendTransaction(
|
|
106
|
+
primaryWallet,
|
|
107
|
+
chain as BlockchainIncludingTestnet,
|
|
108
|
+
serializedTransaction,
|
|
109
|
+
iframeClient
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
return () => {
|
|
115
|
+
iframeClient.off(signTransactionListener);
|
|
116
|
+
};
|
|
117
|
+
}, [iframeClient, primaryWallet]);
|
|
118
|
+
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async function dynamicChainToCrossmintChain(
|
|
123
|
+
wallet: Parameters<HandleConnectedWallet>[0]
|
|
124
|
+
): Promise<BlockchainIncludingTestnet> {
|
|
125
|
+
const chain = wallet.chain;
|
|
126
|
+
if (chain === "SOL") {
|
|
127
|
+
return "solana";
|
|
128
|
+
}
|
|
129
|
+
const chainId = await wallet.connector?.getNetwork();
|
|
130
|
+
if (typeof chainId !== "number") {
|
|
131
|
+
throw new Error("chainId is not a number");
|
|
132
|
+
}
|
|
133
|
+
const chainFromChainId = chainIdToBlockchain(chainId);
|
|
134
|
+
if (!chainFromChainId) {
|
|
135
|
+
throw new Error(`ChainId ${chainId} is not supported`);
|
|
136
|
+
}
|
|
137
|
+
return chainFromChainId as BlockchainIncludingTestnet;
|
|
138
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { EmbeddedCheckoutV3IFrameEmitter } from "@crossmint/client-sdk-base";
|
|
2
|
+
import {
|
|
3
|
+
type BlockchainIncludingTestnet,
|
|
4
|
+
blockchainToChainId,
|
|
5
|
+
type EVMBlockchainIncludingTestnet,
|
|
6
|
+
} from "@crossmint/common-sdk-base";
|
|
7
|
+
import type { EthereumWallet } from "@dynamic-labs/ethereum-core";
|
|
8
|
+
import { parseTransaction, type TransactionSerializableEIP1559 } from "viem";
|
|
9
|
+
|
|
10
|
+
export async function handleEvmTransaction({
|
|
11
|
+
primaryWallet,
|
|
12
|
+
chain,
|
|
13
|
+
serializedTransaction,
|
|
14
|
+
iframeClient,
|
|
15
|
+
}: {
|
|
16
|
+
primaryWallet: EthereumWallet;
|
|
17
|
+
chain: BlockchainIncludingTestnet;
|
|
18
|
+
serializedTransaction: string;
|
|
19
|
+
iframeClient: EmbeddedCheckoutV3IFrameEmitter;
|
|
20
|
+
}) {
|
|
21
|
+
try {
|
|
22
|
+
await primaryWallet.switchNetwork(blockchainToChainId(chain as EVMBlockchainIncludingTestnet));
|
|
23
|
+
} catch (error) {
|
|
24
|
+
console.error("[CryptoWalletConnectionHandler] failed to switch network", error);
|
|
25
|
+
iframeClient.send("crypto:send-transaction:failed", {
|
|
26
|
+
error: (error as Error).message,
|
|
27
|
+
});
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
let walletClient: Awaited<ReturnType<typeof primaryWallet.getWalletClient>>;
|
|
32
|
+
try {
|
|
33
|
+
walletClient = await primaryWallet.getWalletClient();
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.error("[CryptoWalletConnectionHandler] failed to get wallet client", error);
|
|
36
|
+
iframeClient.send("crypto:send-transaction:failed", {
|
|
37
|
+
error: (error as Error).message,
|
|
38
|
+
});
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let parsedTransaction: TransactionSerializableEIP1559;
|
|
43
|
+
try {
|
|
44
|
+
parsedTransaction = parseTransaction(serializedTransaction as `0x${string}`) as TransactionSerializableEIP1559;
|
|
45
|
+
} catch (error) {
|
|
46
|
+
console.error("[CryptoWalletConnectionHandler] failed to parse transaction", error);
|
|
47
|
+
iframeClient.send("crypto:send-transaction:failed", {
|
|
48
|
+
error: (error as Error).message,
|
|
49
|
+
});
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
const txId = await walletClient.sendTransaction(parsedTransaction);
|
|
55
|
+
console.log("[CryptoWalletConnectionHandler] txId", txId);
|
|
56
|
+
iframeClient.send("crypto:send-transaction:success", {
|
|
57
|
+
txId,
|
|
58
|
+
});
|
|
59
|
+
} catch (error) {
|
|
60
|
+
console.error("[CryptoWalletConnectionHandler] failed to send transaction", error);
|
|
61
|
+
iframeClient.send("crypto:send-transaction:failed", {
|
|
62
|
+
error: (error as Error).message,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Wallet } from "@dynamic-labs/sdk-react-core";
|
|
2
|
+
|
|
3
|
+
import type { BlockchainIncludingTestnet } from "@crossmint/common-sdk-base";
|
|
4
|
+
|
|
5
|
+
import { handleEvmTransaction } from "./handleEvmTransaction";
|
|
6
|
+
|
|
7
|
+
import { isSolanaWallet } from "@dynamic-labs/solana";
|
|
8
|
+
import { handleSolanaTransaction } from "./handleSolanaTransaction";
|
|
9
|
+
import { isEthereumWallet } from "@dynamic-labs/ethereum";
|
|
10
|
+
import type { EmbeddedCheckoutV3IFrameEmitter } from "@crossmint/client-sdk-base";
|
|
11
|
+
|
|
12
|
+
export async function handleSendTransaction(
|
|
13
|
+
primaryWallet: Wallet,
|
|
14
|
+
chain: BlockchainIncludingTestnet,
|
|
15
|
+
serializedTransaction: string,
|
|
16
|
+
iframeClient: EmbeddedCheckoutV3IFrameEmitter
|
|
17
|
+
) {
|
|
18
|
+
const commonParams = {
|
|
19
|
+
chain,
|
|
20
|
+
serializedTransaction,
|
|
21
|
+
iframeClient,
|
|
22
|
+
};
|
|
23
|
+
if (isSolanaWallet(primaryWallet)) {
|
|
24
|
+
return await handleSolanaTransaction({
|
|
25
|
+
...commonParams,
|
|
26
|
+
primaryWallet,
|
|
27
|
+
});
|
|
28
|
+
} else if (isEthereumWallet(primaryWallet)) {
|
|
29
|
+
return await handleEvmTransaction({ ...commonParams, primaryWallet });
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { EmbeddedCheckoutV3IFrameEmitter } from "@crossmint/client-sdk-base";
|
|
2
|
+
import type { SolanaWallet } from "@dynamic-labs/solana-core";
|
|
3
|
+
import { Transaction } from "@solana/web3.js";
|
|
4
|
+
import base58 from "bs58";
|
|
5
|
+
|
|
6
|
+
export async function handleSolanaTransaction({
|
|
7
|
+
primaryWallet,
|
|
8
|
+
serializedTransaction,
|
|
9
|
+
iframeClient,
|
|
10
|
+
}: {
|
|
11
|
+
primaryWallet: SolanaWallet;
|
|
12
|
+
serializedTransaction: string;
|
|
13
|
+
iframeClient: EmbeddedCheckoutV3IFrameEmitter;
|
|
14
|
+
}) {
|
|
15
|
+
// TODO: Handle switch network
|
|
16
|
+
|
|
17
|
+
let signer: Awaited<ReturnType<typeof primaryWallet.getSigner>>;
|
|
18
|
+
try {
|
|
19
|
+
signer = await primaryWallet.getSigner();
|
|
20
|
+
} catch (error) {
|
|
21
|
+
console.error("[CryptoWalletConnectionHandler] failed to get signer", error);
|
|
22
|
+
iframeClient.send("crypto:send-transaction:failed", {
|
|
23
|
+
error: "Failed to get signer",
|
|
24
|
+
});
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let deserializedTransaction: Transaction;
|
|
29
|
+
try {
|
|
30
|
+
deserializedTransaction = Transaction.from(base58.decode(serializedTransaction));
|
|
31
|
+
} catch (error) {
|
|
32
|
+
console.error("[CryptoWalletConnectionHandler] failed to deserialize transaction", error);
|
|
33
|
+
iframeClient.send("crypto:send-transaction:failed", {
|
|
34
|
+
error: "Failed to deserialize transaction",
|
|
35
|
+
});
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
const { signature: txId } = await signer.signAndSendTransaction(deserializedTransaction);
|
|
41
|
+
console.log("[CryptoWalletConnectionHandler] txId", txId);
|
|
42
|
+
iframeClient.send("crypto:send-transaction:success", {
|
|
43
|
+
txId,
|
|
44
|
+
});
|
|
45
|
+
} catch (error) {
|
|
46
|
+
console.error("[CryptoWalletConnectionHandler] failed to send transaction", error);
|
|
47
|
+
iframeClient.send("crypto:send-transaction:failed", {
|
|
48
|
+
error: (error as Error).message,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./CrossmintEmbeddedCheckoutV3";
|
package/src/components/index.ts
CHANGED
package/src/hooks/index.ts
CHANGED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import type { UseSignInData } from "@farcaster/auth-kit";
|
|
2
|
+
|
|
3
|
+
export function useAuthSignIn() {
|
|
4
|
+
return {
|
|
5
|
+
onEmailSignIn,
|
|
6
|
+
onConfirmEmailOtp,
|
|
7
|
+
onFarcasterSignIn,
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async function onEmailSignIn(email: string, options: { baseUrl: string; apiKey: string }) {
|
|
12
|
+
try {
|
|
13
|
+
const queryParams = new URLSearchParams({ apiKey: options.apiKey });
|
|
14
|
+
const response = await fetch(`${options.baseUrl}api/2024-09-26/session/sdk/auth/otps/send?${queryParams}`, {
|
|
15
|
+
headers: {
|
|
16
|
+
"Content-Type": "application/json",
|
|
17
|
+
"x-api-key": options.apiKey,
|
|
18
|
+
},
|
|
19
|
+
credentials: "same-origin",
|
|
20
|
+
cache: "no-cache",
|
|
21
|
+
mode: "cors",
|
|
22
|
+
method: "POST",
|
|
23
|
+
body: JSON.stringify({ email }),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
if (!response?.ok) {
|
|
27
|
+
throw new Error("Failed to send email. Please try again or contact support.");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return await response.json();
|
|
31
|
+
} catch (err) {
|
|
32
|
+
console.error("Error signing in via email ", err);
|
|
33
|
+
throw new Error("Error signing in via email " + err);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function onConfirmEmailOtp(
|
|
38
|
+
email: string,
|
|
39
|
+
emailId: string,
|
|
40
|
+
token: string,
|
|
41
|
+
options: { baseUrl: string; apiKey: string }
|
|
42
|
+
) {
|
|
43
|
+
try {
|
|
44
|
+
const queryParams = new URLSearchParams({
|
|
45
|
+
email,
|
|
46
|
+
signinAuthenticationMethod: "email",
|
|
47
|
+
apiKey: options.apiKey,
|
|
48
|
+
token,
|
|
49
|
+
locale: "en",
|
|
50
|
+
state: emailId,
|
|
51
|
+
callbackUrl: `${options.baseUrl}api/2024-09-26/session/sdk/auth/we-dont-actually-use-this-anymore`,
|
|
52
|
+
});
|
|
53
|
+
const response = await fetch(`${options.baseUrl}api/2024-09-26/session/sdk/auth/authenticate?${queryParams}`, {
|
|
54
|
+
headers: {
|
|
55
|
+
"Content-Type": "application/json",
|
|
56
|
+
"x-api-key": options.apiKey,
|
|
57
|
+
},
|
|
58
|
+
credentials: "same-origin",
|
|
59
|
+
cache: "no-cache",
|
|
60
|
+
mode: "cors",
|
|
61
|
+
method: "POST",
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
if (!response?.ok) {
|
|
65
|
+
throw new Error("Failed to confirm email otp. Please try again or contact support.");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const data = await response.json();
|
|
69
|
+
const callbackUrl = new URL(data.callbackUrl);
|
|
70
|
+
|
|
71
|
+
// parse the oneTimeSecret from the callbackUrl response
|
|
72
|
+
return callbackUrl.searchParams.get("oneTimeSecret");
|
|
73
|
+
} catch (err) {
|
|
74
|
+
console.error("Error confirming email otp ", err);
|
|
75
|
+
throw new Error("Error confirming email otp " + err);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async function onFarcasterSignIn(data: UseSignInData, options: { baseUrl: string; apiKey: string }) {
|
|
80
|
+
try {
|
|
81
|
+
const queryParams = new URLSearchParams({
|
|
82
|
+
signinAuthenticationMethod: "farcaster",
|
|
83
|
+
apiKey: options.apiKey,
|
|
84
|
+
callbackUrl: `${options.baseUrl}sdk/2024-09-26/auth/callback?isPopup=false`,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const response = await fetch(`${options.baseUrl}api/2024-09-26/session/sdk/auth/authenticate?${queryParams}`, {
|
|
88
|
+
headers: {
|
|
89
|
+
"Content-Type": "application/json",
|
|
90
|
+
"x-api-key": options.apiKey,
|
|
91
|
+
},
|
|
92
|
+
body: JSON.stringify({
|
|
93
|
+
...data,
|
|
94
|
+
domain: data.signatureParams.domain,
|
|
95
|
+
redirect: true,
|
|
96
|
+
callbackUrl: `${options.baseUrl}sdk/2024-09-26/auth/callback?isPopup=false`,
|
|
97
|
+
}),
|
|
98
|
+
credentials: "same-origin",
|
|
99
|
+
cache: "no-cache",
|
|
100
|
+
mode: "cors",
|
|
101
|
+
method: "POST",
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
if (!response?.ok) {
|
|
105
|
+
throw new Error("Failed to sign in via farcaster. Please try again or contact support.");
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const resData = await response.json();
|
|
109
|
+
const callbackUrl = new URL(resData.callbackUrl);
|
|
110
|
+
|
|
111
|
+
// parse the oneTimeSecret from the callbackUrl response
|
|
112
|
+
return callbackUrl.searchParams.get("oneTimeSecret");
|
|
113
|
+
} catch (err) {
|
|
114
|
+
console.error("Error signing in via farcaster ", err);
|
|
115
|
+
throw new Error("Error signing in via farcaster " + err);
|
|
116
|
+
}
|
|
117
|
+
}
|