Skip to content

Using embedded wallets

To use embedded wallets, Privy implements an RpcProvider on the EmbeddedWallet class of the Unity SDK. This is an EIP1193 provider is responsible for managing RPC requests to a user's embedded wallet.

Supported RPC methods

Currently, Privy's RpcProvider only supports the personal_sign and eth_signTypedData_v4 RPCs. We are actively adding support for other methods.

Making an RPC request

1. Get the user's wallet

To make an RPC request to a user's wallet, first get the user's embedded wallet like so:

csharp
// Ensure user is authenticated / non null

PrivyUser privyUser = PrivyManager.Instance.User;

if ( privyUser != null ) {
	// Grab the embedded wallet from the embedded wallet list
	// For demonstration purposes we're just grabbing the first one.
	IEmbeddedWallet embeddedWallet = PrivyManager.Instance.User.EmbeddedWallet[0];

	//Ensure the Wallet is not null
	if ( embeddedWallet != null ) {
		//wallet operations
	}
}

2. Construct your RPC request

Next, construct the RPC request using the RpcRequest class from Privy. The class follows the interface below:

csharp
public class RpcRequest
{
    public string Method { get; set; }
    public string[] Params { get; set; }

}

As an example, you can construct a new RPC request like so.

csharp
var rpcRequest = new RpcRequest
{
    Method = "personal_sign", //a supported method
    Params = new string[] { "A message to sign", embeddedWallet.Address } //an array of strings, with the message + address
};

3. Execute the RPC request

Now, simply pass the rpcRequest you constructed to the RpcProvider's Request method to execute the request:

csharp
try {
	//Now that the response has been constructed, we try to execute the request
	RpcResponse personalSignResponse = await embeddedWallet.RpcProvider.Request(rpcRequest);

	//If response is successful, we can parse out the data
	Debug.Log(personalSignResponse.Data)
} catch (PrivyException.EmbeddedWalletException ex){
		//If the request method fails, we catch it here
    Debug.LogError($"Could not sign message due to error: {ex.Error} {ex.Message}");
} catch (Exception ex) {
		//If there's some other error, unrelated to the request, catch this here
    Debug.LogError($"Could not sign message exception {ex.Message}");
}

This will return an RpcResponse, which implements the interface below:

csharp
public class RpcResponse
{
    public string Method { get; set; }
    public string Data { get; set; }

}

Handling errors

The provider's Request method may error if:

  • the user is not authenticated
  • the user's wallet does not exist or has not loaded on their device
  • there is an issue with the RPC request that was sent to the wallet

These errors can be caught through a generic exception, or Privy's custom AuthenticationException or EmbeddedWalletException:

csharp
catch (PrivyException.AuthenticationException ex)
{
    Debug.LogError($"Error signing message, Type:{ex.Error}, Message:{ex.Message}");
}
catch (PrivyException.EmbeddedWalletException ex){
    Debug.LogError($"Could not sign message due to error: {ex.Error} {ex.Message}");
} catch {
	  Debug.LogError("Error signing message");
}

Full example

As a complete example, you can send an RPC request to a wallet and handle corresponding errors like so:

csharp
try {
    IEmbeddedWallet embeddedWallet = PrivyManager.Instance.User.EmbeddedWallets[0];

    var rpcRequest = new RpcRequest
    {
        Method = "personal_sign",
        Params = new string[] { "A message to sign", embeddedWallet.Address }  // Use the 'new' keyword here
    };

    RpcResponse personalSignResponse = await embeddedWallet.RpcProvider.Request(rpcRequest);

    Debug.Log(personalSignResponse.Data);
} catch (PrivyException.EmbeddedWalletException ex){
    Debug.LogError($"Could not sign message due to error: {ex.Error} {ex.Message}");
} catch (Exception ex) {
    Debug.LogError($"Could not sign message exception {ex.Message}");
}