@coinbase/cdp-core
, it offers a React-friendly interface for end user authentication
and embedded wallet operations.
Quickstart
This guide will help you get started with @coinbase/cdp-hooks. You’ll learn how to install the package, set up the provider, and use the hooks in both web and React Native applications.Installation
Web Applications
For web applications, add the package to your project using your preferred package manager:React Native Applications
For React Native applications, you’ll need additional crypto polyfills and dependencies:index.js
or index.ts
):
react-native-quick-crypto
: Provides Web Crypto API compatibility for asymmetric key generation (ECDSA, RSA) required for JWT signing and encryptionreact-native-get-random-values
: Provides secure random number generation viacrypto.getRandomValues()
@ungap/structured-clone
: PolyfillsstructuredClone
for object cloning compatibility@react-native-async-storage/async-storage
: Provides persistent storage for auth tokens and secrets
Gather your CDP Project Information
- Sign in or create an account on the CDP Portal
- On your dashboard, select a project from the dropdown at the at the top, and copy the Project ID
Allowlist your local app
- Navigate to the Embedded Wallet Configuration in CDP Portal, and click Add origin to include your local app
- Enter the origin of your locally running app - e.g.,
http://localhost:3000
- Click Add origin again to save your changes
Setup Provider
Next, you need to wrap your application with the CDPHooksProvider, which provides the necessary context for hooks to work correctly.Web Applications
Update your main application file (e.g., main.tsx) to include the provider:React Native Applications
For React Native, the setup is identical.Smart Account Configuration
You can configure the provider to automatically create Smart Accounts for new users:- When
createAccountOnLogin
is set to"evm-smart"
, new users will automatically get both an EOA and a Smart Account.
Sign In a User
End user authentication proceeds in two steps:- The user inputs their email address to initiate the authentication flow, which will send the user a One Time Password (OTP) and return a
flowId
- The user submits the six-digit OTP and
flowId
, after which the user will be authenticated, returning a User object.
Web Applications
React Native Applications
For React Native, you’ll use native UI components and handle the sign-in flow similarly:View User Information
Once the end user has signed in, you can display their information in your application:Send a Transaction
We support signing and sending a Blockchain transaction in a single action on the following networks:- Base
- Base Sepolia
- Ethereum
- Ethereum Sepolia
- Avalanche
- Arbitrum
- Optimism
- Polygon
viem
to broadcast the transaction.
Sign Messages and Typed Data
End users can sign EVM messages, hashes, and typed data to generate signatures for various onchain applications.Export Private Key
End users can export their private keys from their embedded wallet, allowing them to import it into an EVM-compatible wallet of their choice.Smart Account Operations
Smart Accounts provide advanced account abstraction features with React hooks.Send User Operations
Send user operations from Smart Accounts with support for multiple calls and paymaster sponsorship. The hook returns a method to execute the user operation andstatus
, data
, and error
properties to read the result of the user operation:
Track User Operation Status
Use theuseWaitForUserOperation
hook to poll for user operation status and provide real-time updates. This hook immediately fires off a query to get the result of the user operation:
Conditional Polling
You can control when theuseWaitForUserOperation
hook should start polling using the enabled
parameter:
Functions
useConfig()
Returns
object
config
Example
useIsInitialized()
Returns
object
isInitialized
Example
useSignInWithEmail()
Returns
object
signInWithEmail()
Parameters
Parameter | Type |
---|---|
options | SignInWithEmailOptions |
Returns
Promise
<SignInWithEmailResult
>
Example
useSignInWithSms()
Returns
object
signInWithSms()
Parameters
Parameter | Type |
---|---|
options | SignInWithSmsOptions |
Returns
Promise
<SignInWithSmsResult
>
Example
useVerifyEmailOTP()
Returns
object
verifyEmailOTP()
Parameters
Parameter | Type |
---|---|
options | VerifyEmailOTPOptions |
Returns
Promise
<VerifyEmailOTPResult
>
Example
useVerifySmsOTP()
Returns
object
verifySmsOTP()
Parameters
Parameter | Type |
---|---|
options | VerifySmsOTPOptions |
Returns
Promise
<VerifySmsOTPResult
>
Example
useIsSignedIn()
Returns
object
isSignedIn
Example
useCurrentUser()
Returns
object
currentUser
Example
useSignOut()
Returns
object
signOut()
Returns
Promise
<void
>
Example
useGetAccessToken()
Returns
object
Function to get the access token for the current user
getAccessToken()
Returns
Promise
<null
| string
>
Example
useEvmAddress()
Returns
object
evmAddress
Example
useSignEvmHash()
Returns
object
signEvmHash()
Parameters
Parameter | Type |
---|---|
options | SignEvmHashOptions |
Returns
Promise
<SignEvmHashResult
>
Example
useSignEvmTransaction()
Returns
object
signEvmTransaction()
Parameters
Parameter | Type |
---|---|
options | SignEvmTransactionOptions |
Returns
Promise
<SignEvmTransactionResult
>
Example
useSendEvmTransaction()
data
discriminated union object.
Note: The data
returned from the hook only represents the last sent transaction. If you wish to
call one instance of the hook multiple times in quick succession it is recommended to save the
txHash returned from sendEvmTransaction
yourself and handle waiting for the receipt.
Returns
object
sendEvmTransaction()
Parameters
Parameter | Type |
---|---|
options | SendEvmTransactionOptions |
Returns
Promise
<SendEvmTransactionResult
>
data
Example
useSignEvmMessage()
Returns
object
signEvmMessage()
Parameters
Parameter | Type |
---|---|
options | SignEvmMessageOptions |
Returns
Promise
<SignEvmMessageResult
>
Example
useSignEvmTypedData()
Returns
object
signEvmTypedData()
Parameters
Parameter | Type |
---|---|
options | SignEvmTypedDataOptions |
Returns
Promise
<SignEvmTypedDataResult
>
Example
useExportEvmAccount()
Returns
object
exportEvmAccount()
Parameters
Parameter | Type |
---|---|
options | ExportEvmAccountOptions |
Returns
Promise
<ExportEvmAccountResult
>
Example
useEnforceAuthenticated()
Type Parameters
Type Parameter | Description |
---|---|
TArgs extends unknown [] | Array type representing the arguments of the callback |
TReturn | Type representing the return value of the callback |
Parameters
Parameter | Type | Description |
---|---|---|
callback | (…args ) => Promise <TReturn > | The async function to wrap with authentication check |
Returns
A wrapped version of the callback that checks authenticationParameters
Parameter | Type |
---|---|
…args | TArgs |
Returns
Promise
<TReturn
>
Throws
Throws an error if the user is not authenticated when the callback is invokeduseEnforceUnauthenticated()
Type Parameters
Type Parameter | Description |
---|---|
TArgs extends unknown [] | Array type representing the arguments of the callback |
TReturn | Type representing the return value of the callback |
Parameters
Parameter | Type | Description |
---|---|---|
callback | (…args ) => Promise <TReturn > | The async function to wrap with authentication check |
Returns
A wrapped version of the callback that checks authenticationParameters
Parameter | Type |
---|---|
…args | TArgs |
Returns
Promise
<TReturn
>
Throws
Throws an error if the user is authenticated when the callback is invokeduseSendUserOperation()
data
discriminated union object.
Note: The data
returned from the hook only represents the last sent user operation. If you wish to
call one instance of the hook multiple times in quick succession it is recommended to save the
userOperationHash returned from sendUserOperation
yourself and handle waiting for the result.
Returns
UseSendUserOperationReturnType
Example
useWaitForUserOperation()
data
returned from the hook only represents the last sent user operation. If you wish to
call one instance of the hook multiple times in quick succession it is recommended to save the
userOpHash returned from waitForUserOperation
yourself and handle waiting for the result.
Parameters
Parameter | Type | Description |
---|---|---|
parameters | UseWaitForUserOperationParameters | Configuration object for waiting for user operation |
Returns
UseWaitForUserOperationReturnType
Query result with data, loading state, and error information
Example
CDPHooksProvider()
Parameters
Parameter | Type | Description |
---|---|---|
props | CDPHooksProviderProps | The component props |
Returns
Element
A React context provider wrapping the children with CDP functionality
Example
Classes
APIError
Extends
Error
Constructors
Constructor
Parameters
Parameter | Type |
---|---|
statusCode | number |
errorType | APIErrorType |
errorMessage | string |
correlationId? | string |
errorLink? | string |
cause? | Error |
Returns
APIError
Overrides
Methods
toJSON()
Returns
object
errorLink?
correlationId?
name
statusCode
errorType
errorMessage
Properties
Property | Type |
---|---|
statusCode | number |
errorType | APIErrorType |
errorMessage | string |
correlationId? | string |
errorLink? | string |
Interfaces
CDPHooksProviderProps
Props for the CDP Provider componentProperties
Property | Type | Description |
---|---|---|
children | ReactNode | React children to be rendered within the provider |
config | Config | Configuration object for initializing the CDP SDK |
CDPContextValue
Core context value interface for the CDP (Coinbase Developer Platform) provider.Properties
Property | Type | Description |
---|---|---|
isInitialized | boolean | Whether the CDP SDK has completed initialization |
currentUser | null | User | The currently authenticated user, or null if not signed in |
isSignedIn | boolean | Convenience boolean indicating if there is an authenticated user |
config | Config | The CDP config |
EIP712TypedData
Properties
Property | Type |
---|---|
domain | EIP712Domain |
types | EIP712Types |
primaryType | string |
message | EIP712Message |
Type Aliases
Config
Type declaration
transports?
Param
The optional transports to use for the public clients. If not provided, the defaulthttp()
transport is used.
Returns
The config for the CDP hooks.TransactionState
- “idle”: No transaction in progress.
- “pending”: Transaction sent, waiting for confirmation.
- “success”: Transaction confirmed, includes receipt.
- “error”: Transaction failed, includes error details.
Status
- “idle”: No request in progress.
- “pending”: Request sent, waiting for confirmation.
- “success”: Request confirmed, includes result.
- “error”: Request failed, includes error details.
UseSendUserOperationReturnType
useSendUserOperation
hook.
Properties
Property | Type |
---|---|
sendUserOperation | (options ) => Promise <SendUserOperationResult > |
data | | GetUserOperationResult | undefined |
error | Error | undefined |
status | Status |
UseWaitForUserOperationParameters
useWaitForUserOperation
hook.
Properties
Property | Type |
---|---|
userOperationHash? | Hex |
evmSmartAccount? | EvmAddress |
network? | SendEvmTransactionWithEndUserAccountBodyNetwork |
enabled? | boolean |
UseWaitForUserOperationReturnType
useWaitForUserOperation
hook.
Properties
Property | Type |
---|---|
status | Status |
data | | GetUserOperationResult | undefined |
error | Error | undefined |
AllowedEvmTransactionType
APIErrorType
EvmAddress
ExportEvmAccountOptions
Properties
Property | Type |
---|---|
evmAccount | EvmAddress |
ExportEvmAccountResult
Properties
Property | Type |
---|---|
privateKey | string |
GetUserOperationResult
Hex
SendEvmTransactionOptions
Properties
Property | Type |
---|---|
evmAccount | EvmAddress |
network | SendEvmTransactionWithEndUserAccountBodyNetwork |
transaction | AllowedEvmTransactionType |
SendEvmTransactionResult
Properties
Property | Type |
---|---|
transactionHash | Hex |
SignEvmHashOptions
Properties
Property | Type |
---|---|
evmAccount | EvmAddress |
hash | Hex |
SignEvmHashResult
Properties
Property | Type |
---|---|
signature | Hex |
SignEvmMessageOptions
Properties
Property | Type |
---|---|
evmAccount | EvmAddress |
message | string |
SignEvmMessageResult
Properties
Property | Type |
---|---|
signature | Hex |
SignEvmTransactionOptions
Properties
Property | Type |
---|---|
evmAccount | EvmAddress |
transaction | AllowedEvmTransactionType |
SignEvmTransactionResult
Properties
Property | Type |
---|---|
signedTransaction | Hex |
SignEvmTypedDataOptions
Properties
Property | Type |
---|---|
evmAccount | EvmAddress |
typedData | EIP712TypedData |
SignEvmTypedDataResult
Properties
Property | Type |
---|---|
signature | Hex |
SignInWithEmailOptions
Properties
Property | Type |
---|---|
email | string |
SignInWithEmailResult
Properties
Property | Type |
---|---|
message | string |
flowId | string |
User
Properties
Property | Type |
---|---|
userId | string |
authenticationMethods | AuthenticationMethods |
evmAccounts? | EvmAddress [] |
evmSmartAccounts? | EvmAddress [] |
VerifyEmailOTPOptions
Properties
Property | Type |
---|---|
flowId | string |
otp | string |
VerifyEmailOTPResult
Properties
Property | Type |
---|---|
user | User |
message | string |
isNewUser | boolean |