-
Notifications
You must be signed in to change notification settings - Fork 410
WIP: feat(clerk-js,react,shared): Add sign in with Solana functionality to clerk-js ui components #7293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
WIP: feat(clerk-js,react,shared): Add sign in with Solana functionality to clerk-js ui components #7293
Changes from all commits
505ac6a
a5f9016
0e178ea
f66590a
44ef411
6007a65
e5f180c
1c13a4a
5ad0053
5ac37e0
a1f884e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import type { | |
| AuthenticateWithPasskeyParams, | ||
| AuthenticateWithPopupParams, | ||
| AuthenticateWithRedirectParams, | ||
| AuthenticateWithSolanaParams, | ||
| AuthenticateWithWeb3Params, | ||
| ClientTrustState, | ||
| CreateEmailLinkFlowReturn, | ||
|
|
@@ -15,6 +16,7 @@ import type { | |
| EmailLinkConfig, | ||
| EmailLinkFactor, | ||
| EnterpriseSSOConfig, | ||
| GenerateSignature, | ||
| PassKeyConfig, | ||
| PasskeyFactor, | ||
| PhoneCodeConfig, | ||
|
|
@@ -69,12 +71,14 @@ import { | |
| generateSignatureWithCoinbaseWallet, | ||
| generateSignatureWithMetamask, | ||
| generateSignatureWithOKXWallet, | ||
| generateSignatureWithSolana, | ||
| getBaseIdentifier, | ||
| getBrowserLocale, | ||
| getClerkQueryParam, | ||
| getCoinbaseWalletIdentifier, | ||
| getMetamaskIdentifier, | ||
| getOKXWalletIdentifier, | ||
| getSolanaIdentifier, | ||
| windowNavigate, | ||
| } from '../../utils'; | ||
| import { | ||
|
|
@@ -212,6 +216,7 @@ export class SignIn extends BaseResource implements SignInResource { | |
| case 'web3_base_signature': | ||
| case 'web3_coinbase_wallet_signature': | ||
| case 'web3_okx_wallet_signature': | ||
| case 'web3_solana_signature': | ||
| config = { web3WalletId: params.web3WalletId } as Web3SignatureConfig; | ||
| break; | ||
| case 'reset_password_phone_code': | ||
|
|
@@ -379,13 +384,17 @@ export class SignIn extends BaseResource implements SignInResource { | |
| }; | ||
|
|
||
| public authenticateWithWeb3 = async (params: AuthenticateWithWeb3Params): Promise<SignInResource> => { | ||
| const { identifier, generateSignature, strategy = 'web3_metamask_signature' } = params || {}; | ||
| const { identifier, generateSignature, strategy = 'web3_metamask_signature', walletName } = params || {}; | ||
| const provider = strategy.replace('web3_', '').replace('_signature', '') as Web3Provider; | ||
|
|
||
| if (!(typeof generateSignature === 'function')) { | ||
| clerkMissingOptionError('generateSignature'); | ||
| } | ||
|
|
||
| if (provider === 'solana' && !walletName) { | ||
| clerkMissingOptionError('walletName'); | ||
| } | ||
|
|
||
| await this.create({ identifier }); | ||
|
|
||
| const web3FirstFactor = this.supportedFirstFactors?.find(f => f.strategy === strategy) as Web3SignatureFactor; | ||
|
|
@@ -403,7 +412,7 @@ export class SignIn extends BaseResource implements SignInResource { | |
|
|
||
| let signature: string; | ||
| try { | ||
| signature = await generateSignature({ identifier, nonce: message, provider }); | ||
| signature = await generateSignature({ identifier, nonce: message, walletName, provider }); | ||
| } catch (err) { | ||
| // There is a chance that as a user when you try to setup and use the Coinbase Wallet with an existing | ||
| // Passkey in order to authenticate, the initial generate signature request to be rejected. For this | ||
|
|
@@ -412,7 +421,7 @@ export class SignIn extends BaseResource implements SignInResource { | |
| // error code 4001 means the user rejected the request | ||
| // Reference: https://docs.cdp.coinbase.com/wallet-sdk/docs/errors | ||
| if (provider === 'coinbase_wallet' && err.code === 4001) { | ||
| signature = await generateSignature({ identifier, nonce: message, provider }); | ||
| signature = await generateSignature({ identifier, nonce: message, provider, walletName }); | ||
| } else { | ||
| throw err; | ||
| } | ||
|
|
@@ -460,6 +469,16 @@ export class SignIn extends BaseResource implements SignInResource { | |
| }); | ||
| }; | ||
|
|
||
| public authenticateWithSolana = async (params: AuthenticateWithSolanaParams): Promise<SignInResource> => { | ||
| const identifier = await getSolanaIdentifier(params.walletName); | ||
| return this.authenticateWithWeb3({ | ||
| identifier, | ||
| generateSignature: generateSignatureWithSolana, | ||
| strategy: 'web3_solana_signature', | ||
| walletName: params?.walletName, | ||
| }); | ||
| }; | ||
|
|
||
| public authenticateWithPasskey = async (params?: AuthenticateWithPasskeyParams): Promise<SignInResource> => { | ||
| const { flow } = params || {}; | ||
|
|
||
|
|
@@ -968,7 +987,7 @@ class SignInFuture implements SignInFutureResource { | |
|
|
||
| return runAsyncResourceTask(this.resource, async () => { | ||
| let identifier; | ||
| let generateSignature; | ||
| let generateSignature: GenerateSignature; | ||
| switch (provider) { | ||
| case 'metamask': | ||
| identifier = await getMetamaskIdentifier(); | ||
|
|
@@ -986,6 +1005,13 @@ class SignInFuture implements SignInFutureResource { | |
| identifier = await getOKXWalletIdentifier(); | ||
| generateSignature = generateSignatureWithOKXWallet; | ||
| break; | ||
| case 'solana': | ||
| if (!params.walletName) { | ||
| throw new Error('walletName is required for solana web3 authentication'); | ||
| } | ||
|
Comment on lines
+1009
to
+1011
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i would expect this error to be thrown by |
||
| identifier = await getSolanaIdentifier(params.walletName); | ||
| generateSignature = generateSignatureWithSolana; | ||
| break; | ||
| default: | ||
| throw new Error(`Unsupported Web3 provider: ${provider}`); | ||
| } | ||
|
|
@@ -1011,7 +1037,12 @@ class SignInFuture implements SignInFutureResource { | |
|
|
||
| let signature: string; | ||
| try { | ||
| signature = await generateSignature({ identifier, nonce: message }); | ||
| signature = await generateSignature({ | ||
| identifier, | ||
| nonce: message, | ||
| walletName: params?.walletName, | ||
| provider, | ||
| }); | ||
| } catch (err) { | ||
| // There is a chance that as a user when you try to setup and use the Coinbase Wallet with an existing | ||
| // Passkey in order to authenticate, the initial generate signature request to be rejected. For this | ||
|
|
@@ -1020,7 +1051,11 @@ class SignInFuture implements SignInFutureResource { | |
| // error code 4001 means the user rejected the request | ||
| // Reference: https://docs.cdp.coinbase.com/wallet-sdk/docs/errors | ||
| if (provider === 'coinbase_wallet' && err.code === 4001) { | ||
| signature = await generateSignature({ identifier, nonce: message }); | ||
| signature = await generateSignature({ | ||
| identifier, | ||
| nonce: message, | ||
| provider, | ||
| }); | ||
| } else { | ||
| throw err; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { useClerk } from '@clerk/shared/react'; | ||
|
|
||
| import { withRedirectToAfterSignIn, withRedirectToSignInTask } from '@/ui/common/withRedirect'; | ||
| import { descriptors, Flex, Flow } from '@/ui/customizables'; | ||
| import { BackLink } from '@/ui/elements/BackLink'; | ||
| import { Card } from '@/ui/elements/Card'; | ||
| import { useCardState, withCardStateProvider } from '@/ui/elements/contexts'; | ||
| import { Header } from '@/ui/elements/Header'; | ||
| import { Web3WalletButtons } from '@/ui/elements/Web3WalletButtons'; | ||
| import { handleError } from '@/ui/utils/errorHandler'; | ||
|
|
||
| import { useSignInContext } from '../../contexts'; | ||
| import { useRouter } from '../../router'; | ||
|
|
||
| const SignInFactorOneSolanaWalletsCardInner = () => { | ||
| const clerk = useClerk(); | ||
| const card = useCardState(); | ||
| const router = useRouter(); | ||
| const ctx = useSignInContext(); | ||
|
|
||
| const onSelect = async ({ walletName }: { walletName: string }) => { | ||
| card.setLoading(walletName); | ||
| try { | ||
| await clerk.authenticateWithWeb3({ | ||
| strategy: 'web3_solana_signature', | ||
| redirectUrl: ctx.afterSignInUrl || '/', | ||
| signUpContinueUrl: ctx.isCombinedFlow ? '../create/continue' : ctx.signUpContinueUrl, | ||
| customNavigate: router.navigate, | ||
| secondFactorUrl: 'factor-two', | ||
| walletName, | ||
| }); | ||
| } catch (err) { | ||
| handleError(err as Error, [], card.setError); | ||
| card.setIdle(); | ||
| } | ||
| }; | ||
|
|
||
| const onBackLinkClick = () => { | ||
| void router.navigate('../'); | ||
| }; | ||
|
|
||
| return ( | ||
| <Flow.Part part='choose-wallet'> | ||
| <Card.Root> | ||
| <Card.Content> | ||
| <Header.Root showLogo> | ||
| <Header.Title>Continue with Solana Wallet</Header.Title> | ||
| <Header.Subtitle>Select a wallet below to sign in</Header.Subtitle> | ||
| </Header.Root> | ||
| <Card.Alert>{card.error}</Card.Alert> | ||
| <Flex | ||
| direction='col' | ||
| gap={4} | ||
| > | ||
| <Web3WalletButtons onSelect={onSelect} /> | ||
|
|
||
| <BackLink | ||
| boxElementDescriptor={descriptors.backRow} | ||
| linkElementDescriptor={descriptors.backLink} | ||
| onClick={onBackLinkClick} | ||
| /> | ||
| </Flex> | ||
| </Card.Content> | ||
| <Card.Footer /> | ||
| </Card.Root> | ||
| </Flow.Part> | ||
| ); | ||
| }; | ||
|
|
||
| export const SignInFactorOneSolanaWalletsCard = withRedirectToSignInTask( | ||
| withRedirectToAfterSignIn(withCardStateProvider(SignInFactorOneSolanaWalletsCardInner)), | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,6 +58,10 @@ export const SignInSocialButtons = React.memo((props: SignInSocialButtonsProps) | |
| .catch(err => handleError(err, [], card.setError)); | ||
| }} | ||
| web3Callback={strategy => { | ||
| if (strategy === 'web3_solana_signature') { | ||
| return navigate(`choose-wallet?strategy=${strategy}`); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this the proper way to navigate to the next screen? In general, why we need to pass along the strategy? Doesn't than mean that the rest of the query parameters will be lost after the navigate. For example, if somebody navigates to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After speaking with @dstaley regarding this implementation strategy, we agreed that this would pave the way for the future of supporting Web3 chains, rather than specific wallets, which would result in the user needing to select their wallet of choice, and inturn require the persistance of the strategy to listen in the browser window for the proper wallet extension providers that support the chosen strategy. @panteliselef - Please do let me know if the query param will cause issues in this flow and if it should be reworked. |
||
| } | ||
|
|
||
| return clerk | ||
| .authenticateWithWeb3({ | ||
| customNavigate: navigate, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
params should not be optional right?