Skip to content

Creating an Ethereum wallet

To create an EVM embedded wallet for your user, call the createEthereumWallet method on your PrivyUser instance.

kotlin
public interface PrivyUser {
  // Other privy user methods

  public suspend fun createEthereumWallet(allowAdditional: Boolean = false):  Result<EmbeddedEthereumWallet>

  // ...
}
FieldTypeDescription
allowAdditionalBool(Optional, Ethereum only) Ethereum embedded wallets are hierarchical deterministic (HD) wallets, and a user's seed entropy can support multiple separate embedded wallets. If a user already has a wallet and you'd like to create additional HD wallets for them, pass in true for the allowAdditional parameter. Defaults to false.

If a wallet is successfully created for the user, an EmbeddedEthereumWallet object is returned as an encapsulated value of Result.success.

This method will Result.failure if:

  • The user is not authenticated
  • If a user already has 9 or more wallets
  • If the network call to create the wallet fails
  • If a user already has an embedded wallet and allowAdditional is not set to true.

The Ethereum wallet

An EmbeddedEthereumWallet is defined as follows:

kotlin
public interface EmbeddedEthereumWallet {
 // The wallet's address
 public val address: String

 // The chain id
 public val chainId: String?

 // Recovery method type
 public val recoveryMethod: String?

 // HD wallet index
 public val hdWalletIndex: Int

 // Chain type - in this case, ChainType.Ethereum
  public val chainType: ChainType

 // A hook to an EmbeddedEthereumWalletProvider instance for this wallet
 public val provider: EmbeddedEthereumWalletProvider
}

Using the wallet

To enable your app to request signatures and transactions from the embedded wallet, Privy Ethereum embedded wallets expose a provider inspired by the EIP-1193 provider standard. This allows you request signatures and transactions from the wallet via a familiar JSON-RPC API (e.g. personal_sign).

Once you have an instance of an EmbeddedEthereumWallet, you can make RPC requests by using the provider: EmbeddedEthereumWalletProvider hook and using its request method. For example, wallet.provider.request(request: rpcRequest). This request method will suspend and await if the embedded wallet needs to wait for any internal ready state.

kotlin
public interface EmbeddedEthereumWalletProvider {
 /**
  * Sends a request to the Ethereum provider
  *
  * @param The RPC request
  * @return The response received
  */
 public suspend fun request(request: EthereumRpcRequest): Result<EthereumRpcResponse>
}

As a parameter to this method, to this method, pass an EthereumRpcRequest object that contains:

  • method: the name of the JSON-RPC method for the wallet to execute (e.g. 'personal_sign')
  • params: an array of parameters required by your specified method

By default, embedded wallets are connected to the Ethereum mainnet. To send a transaction on a different network, simply set the wallet's chainId in the transaction request.

kotlin
public data class EthereumRpcRequest(
 // Ethereum JSON RPC method, reference can be found in the [JSON RPC
 // API](https://ethereum.org/en/developers/docs/apis/json-rpc/).
 val method: String,

 // Ethereum JSON RPC method params, param reference for each method can
 // be found in the [JSON RPC API]
 // (https://ethereum.org/en/developers/docs/apis/json-rpc/).
 val params: List<String>
)

The response of the RPC request is as follows:

kotlin
public data class EthereumRpcResponse (
 // The RPC request method
 val method: String,

 // The response data
 val data: String
)
Supported Ethereum JSON-RPC Methods

The following JSON-RPC methods are supported for the embedded wallet:

JSON-RPC MethodSupported?
personal_sign
eth_sign
eth_signTypedData_v4
eth_signTransaction
eth_sendTransaction
eth_sendRawTransaction

Retrieving a user's Ethereum wallets

To retrieve all of a user's EVM wallets:

  1. Ensure the user is authenticated
  2. Grab the user's embedded EVM wallets.
kotlin
// Get current auth state
val user = privy.user

// check if user is authenticated
if (user != null) {
    // Retrieve list of user's embedded Ethereum wallets
    val ethereumWallets = user.embeddedEthereumWallets

    if (ethereumWallets.isNotEmpty()) {
        // Grab the desired wallet. Here, we retrieve the first wallet
        val ethereumWallet = ethereumWallets.first()

        // Make an rpc request
        ethereumWallet.provider.request(rpcRequest: ...)
    }
}