Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
ActiveSessionResource,
AuthenticateWithGoogleOneTapParams,
AuthenticateWithMetamaskParams,
AuthenticateWithWeb3Params,
Clerk as ClerkInterface,
ClerkAPIError,
ClerkOptions,
Expand Down Expand Up @@ -1321,25 +1322,31 @@ export class Clerk implements ClerkInterface {
}) as Promise<SignInResource | SignUpResource>;
};

public authenticateWithMetamask = async ({
public authenticateWithMetamask = async (props: AuthenticateWithMetamaskParams = {}): Promise<void> => {
await this.authenticateWithWeb3({ ...props, strategy: 'web3_metamask_signature' });
};

public authenticateWithWeb3 = async ({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ As we are introducing a new generic authenticateWithWeb3() function, we should also use it on the existing authenticateWithMetamask() method in order to DRY to code and avoid duplicating the logic

Also are we looking to introduce the corresponding authenticateWithCoinbase() method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have 2 options here:

  1. Deprecate authenticateWithMetamask and recommend transitioning to authenticateWithWeb3.
  2. Retain authenticateWithMetamask, introduce authenticateWithCoinbase and optionally mark authenticateWithWeb3 as private.

redirectUrl,
signUpContinueUrl,
customNavigate,
unsafeMetadata,
}: AuthenticateWithMetamaskParams = {}): Promise<void> => {
strategy,
}: AuthenticateWithWeb3Params = {}): Promise<void> => {
if (!this.client || !this.environment) {
return;
}

const provider =
(strategy === 'web3_metamask_signature' && 'metamask') || (strategy === 'web3_coinbase_signature' && 'coinbase');
const navigate = (to: string) =>
customNavigate && typeof customNavigate === 'function' ? customNavigate(to) : this.navigate(to);

let signInOrSignUp: SignInResource | SignUpResource;
try {
signInOrSignUp = await this.client.signIn.authenticateWithMetamask();
signInOrSignUp = await this.client.signIn.authenticateWeb3Provider(provider);
} catch (err) {
if (isError(err, ERROR_CODES.FORM_IDENTIFIER_NOT_FOUND)) {
signInOrSignUp = await this.client.signUp.authenticateWithMetamask({ unsafeMetadata });
signInOrSignUp = await this.client.signUp.authenticateWeb3Provider({ unsafeMetadata, provider });

if (
signUpContinueUrl &&
Expand Down
35 changes: 24 additions & 11 deletions packages/clerk-js/src/core/resources/SignIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import type {
Web3SignatureFactor,
} from '@clerk/types';

import { generateSignatureWithMetamask, getMetamaskIdentifier, windowNavigate } from '../../utils';
import { generateSignatureWithWeb3, getWeb3Identifier, windowNavigate } from '../../utils';
import {
ClerkWebAuthnError,
convertJSONToPublicKeyRequestOptions,
Expand Down Expand Up @@ -107,6 +107,9 @@ export class SignIn extends BaseResource implements SignInResource {
case 'web3_metamask_signature':
config = { web3WalletId: factor.web3WalletId } as Web3SignatureConfig;
break;
case 'web3_coinbase_signature':
config = { web3WalletId: factor.web3WalletId } as Web3SignatureConfig;
break;
case 'reset_password_phone_code':
config = { phoneNumberId: factor.phoneNumberId } as ResetPasswordPhoneCodeFactorConfig;
break;
Expand Down Expand Up @@ -223,16 +226,16 @@ export class SignIn extends BaseResource implements SignInResource {
};

public authenticateWithWeb3 = async (params: AuthenticateWithWeb3Params): Promise<SignInResource> => {
const { identifier, generateSignature } = params || {};
if (!(typeof generateSignature === 'function')) {
const { identifier, provider } = params || {};
const strategy = provider ? `web3_${provider}_signature` : 'web3_metamask_signature';

if (!(typeof generateSignatureWithWeb3 === 'function')) {
clerkMissingOptionError('generateSignature');
}

await this.create({ identifier });

const web3FirstFactor = this.supportedFirstFactors.find(
f => f.strategy === 'web3_metamask_signature',
) as Web3SignatureFactor;
const web3FirstFactor = this.supportedFirstFactors.find(f => f.strategy === strategy) as Web3SignatureFactor;

if (!web3FirstFactor) {
clerkVerifyWeb3WalletCalledBeforeCreate('SignIn');
Expand All @@ -241,25 +244,35 @@ export class SignIn extends BaseResource implements SignInResource {
await this.prepareFirstFactor(web3FirstFactor);

const { nonce } = this.firstFactorVerification;
const signature = await generateSignature({
const signature = await generateSignatureWithWeb3({
identifier: this.identifier!,
nonce: nonce!,
provider: provider,
});

return this.attemptFirstFactor({
signature,
strategy: 'web3_metamask_signature',
strategy,
});
};

public authenticateWithMetamask = async (): Promise<SignInResource> => {
const identifier = await getMetamaskIdentifier();
public authenticateWeb3Provider = async (provider: 'metamask' | 'coinbase'): Promise<SignInResource> => {
const identifier = await getWeb3Identifier(provider);

return this.authenticateWithWeb3({
identifier,
generateSignature: generateSignatureWithMetamask,
provider: provider,
});
};

public authenticateWithMetamask = async (): Promise<SignInResource> => {
return this.authenticateWeb3Provider('metamask');
};

public authenticateWithCoinbase = async (): Promise<SignInResource> => {
return this.authenticateWeb3Provider('coinbase');
};

public authenticateWithPasskey = async (params?: AuthenticateWithPasskeyParams): Promise<SignInResource> => {
const { flow } = params || {};

Expand Down
40 changes: 27 additions & 13 deletions packages/clerk-js/src/core/resources/SignUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ import type {
PrepareEmailAddressVerificationParams,
PreparePhoneNumberVerificationParams,
PrepareVerificationParams,
SignUpAuthenticateWithMetamaskParams,
PrepareWeb3WalletVerificationParams,
SignUpAuthenticateWithWeb3Params,
SignUpCreateParams,
SignUpField,
SignUpIdentificationField,
SignUpJSON,
SignUpResource,
SignUpStatus,
SignUpUnsafeMetadata,
SignUpUpdateParams,
StartEmailLinkFlowParams,
} from '@clerk/types';

import { generateSignatureWithMetamask, getMetamaskIdentifier, windowNavigate } from '../../utils';
import { generateSignatureWithWeb3, getWeb3Identifier, windowNavigate } from '../../utils';
import { getCaptchaToken, retrieveCaptchaInfo } from '../../utils/captcha';
import { createValidatePassword } from '../../utils/passwords/password';
import { normalizeUnsafeMetadata } from '../../utils/resourceParams';
Expand Down Expand Up @@ -170,40 +172,52 @@ export class SignUp extends BaseResource implements SignUpResource {
return this.attemptVerification({ ...params, strategy: 'phone_code' });
};

prepareWeb3WalletVerification = (): Promise<SignUpResource> => {
return this.prepareVerification({ strategy: 'web3_metamask_signature' });
prepareWeb3WalletVerification = (params?: PrepareWeb3WalletVerificationParams): Promise<SignUpResource> => {
return this.prepareVerification(params || { strategy: 'web3_metamask_signature' });
};

attemptWeb3WalletVerification = async (params: AttemptWeb3WalletVerificationParams): Promise<SignUpResource> => {
const { signature } = params;
return this.attemptVerification({ signature, strategy: 'web3_metamask_signature' });
const { signature, strategy } = params;
return this.attemptVerification({ signature, strategy });
};

public authenticateWithWeb3 = async (
params: AuthenticateWithWeb3Params & { unsafeMetadata?: SignUpUnsafeMetadata },
): Promise<SignUpResource> => {
const { generateSignature, identifier, unsafeMetadata } = params || {};
const { identifier, unsafeMetadata, provider } = params || {};
const strategy = provider ? `web3_${provider}_signature` : 'web3_metamask_signature';
const web3Wallet = identifier || this.web3wallet!;
await this.create({ web3Wallet, unsafeMetadata });
await this.prepareWeb3WalletVerification();
await this.prepareWeb3WalletVerification(strategy);

const { nonce } = this.verifications.web3Wallet;
if (!nonce) {
clerkVerifyWeb3WalletCalledBeforeCreate('SignUp');
}

const signature = await generateSignature({ identifier, nonce });
return this.attemptWeb3WalletVerification({ signature });
const signature = await generateSignatureWithWeb3({ identifier, nonce, provider });

return this.attemptWeb3WalletVerification({ signature, strategy });
};

public authenticateWithMetamask = async (params?: SignUpAuthenticateWithMetamaskParams): Promise<SignUpResource> => {
const identifier = await getMetamaskIdentifier();
public authenticateWeb3Provider = async (
provider: 'metamask' | 'coinbase',
params?: SignUpAuthenticateWithWeb3Params,
): Promise<SignUpResource> => {
const identifier = await getWeb3Identifier(provider);
return this.authenticateWithWeb3({
identifier,
generateSignature: generateSignatureWithMetamask,
unsafeMetadata: params?.unsafeMetadata,
provider,
});
};
public authenticateWithMetamask = async (params?: SignUpAuthenticateWithWeb3Params): Promise<SignUpResource> => {
return this.authenticateWeb3Provider('metamask', params);
};

public authenticateWithCoinbase = async (params?: SignUpAuthenticateWithWeb3Params): Promise<SignUpResource> => {
return this.authenticateWeb3Provider('coinbase', params);
};

public authenticateWithRedirect = async ({
redirectUrl,
Expand Down
4 changes: 4 additions & 0 deletions packages/clerk-js/src/ui/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export const WEB3_PROVIDERS: Web3Providers = Object.freeze({
id: 'metamask',
name: 'MetaMask',
},
coinbase: {
id: 'coinbase',
name: 'Coinbase Wallet'
}
});

export function getWeb3ProviderData(name: Web3Provider): Web3ProviderData | undefined | null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ export const SignInSocialButtons = React.memo((props: SocialButtonsProps) => {
.authenticateWithRedirect({ strategy, redirectUrl, redirectUrlComplete })
.catch(err => handleError(err, [], card.setError));
}}
web3Callback={() => {
web3Callback={strategy => {
return clerk
.authenticateWithMetamask({
.authenticateWithWeb3({
customNavigate: navigate,
redirectUrl: redirectUrlComplete,
signUpContinueUrl: ctx.signUpContinueUrl,
strategy,
})
.catch(err => handleError(err, [], card.setError));
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ export const SignUpSocialButtons = React.memo((props: SignUpSocialButtonsProps)
})
.catch(err => handleError(err, [], card.setError));
}}
web3Callback={() => {
web3Callback={strategy => {
return clerk
.authenticateWithMetamask({
.authenticateWithWeb3({
customNavigate: navigate,
redirectUrl: redirectUrlComplete,
signUpContinueUrl: 'continue',
unsafeMetadata: ctx.unsafeMetadata,
strategy: strategy,
})
.catch(err => handleError(err, [], card.setError));
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const AddWeb3WalletActionMenu = withCardStateProvider(() => {
web3Wallet = await web3Wallet.prepareVerification({ strategy: 'web3_metamask_signature' });
const nonce = web3Wallet.verification.nonce as string;
const signature = await generateSignatureWithMetamask({ identifier, nonce });
await web3Wallet.attemptVerification({ signature });
await web3Wallet.attemptVerification({ signature, strategy });
card.setIdle();
} catch (err) {
card.setIdle();
Expand Down
Loading