Appearance
Creating an Ethereum wallet
To create an EVM embedded wallet for your user, call the createEthereumWallet
method on the PrivyUser
instance.
dart
abstract class PrivyUser {
// Other privy user methods
/// Creates an Ethereum embedded wallet for the user.
Future<Result<EmbeddedEthereumWallet>> createEthereumWallet({bool allowAdditional = false});
}
Field | Type | Description |
---|---|---|
allowAdditional | Bool | (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:
dart
abstract class EmbeddedEthereumWallet {
/// The wallet's address.
final String address;
/// The chain ID.
final String? chainId;
/// Recovery method type.
final String? recoveryMethod;
/// HD wallet index.
final int hdWalletIndex;
/// Chain type - in this case ChainType.ethereum
final ChainType chainType;
// A hook to an EmbeddedEthereumWalletProvider instance for this wallet
final EmbeddedEthereumWalletProvider provider;
}
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.
dart
/// Defines the Ethereum Wallet Provider interface for sending RPC requests.
abstract class EmbeddedEthereumWalletProvider {
/**
* Sends a request to the Ethereum provider
*
* @param The RPC request
* @return The response received
*/
Future<Result<EthereumRpcResponse>> request(EthereumRpcRequest request);
}
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 specifiedmethod
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.
dart
/// Represents a JSON-RPC request to the Ethereum wallet provider.
class EthereumRpcRequest {
// Ethereum JSON RPC method, reference can be found in the [JSON RPC
// API](https://ethereum.org/en/developers/docs/apis/json-rpc/).
final String method;
// 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/).
final List<dynamic> params;
}
The response of the RPC request is as follows:
dart
/// Represents a JSON-RPC response from the Ethereum wallet provider.
class EthereumRpcResponse {
// The RPC request method
final String method;
// The response data
final String data;
}
Supported Ethereum JSON-RPC Methods
The following JSON-RPC methods are supported for the embedded wallet:
JSON-RPC Method | Supported? |
---|---|
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:
- Ensure the user is authenticated
- Grab the user's embedded EVM wallets.
kotlin
// Get the current user
final user = privy.user;
// Check if the user is authenticated
if (user != null) {
// Retrieve the list of user's embedded Ethereum wallets
final ethereumWallets = user.embeddedEthereumWallets;
if (ethereumWallets.isNotEmpty) {
// Grab the desired wallet. Here, we retrieve the first wallet.
final ethereumWallet = ethereumWallets.first;
// Make an rpc request
ethereumWallet.provider.request(rpcRequest: ...)
}
}