Appearance
Creating embedded wallets
To create an embedded wallet for your user, call the CreateWallet
method on the PrivyUser
.
csharp
try {
PrivyUser privyUser = PrivyManager.Instance.User;
if (privyUser != null) {
IEmbeddedWallet wallet = await PrivyManager.Instance.User.CreateWallet();
Debug.Log("New wallet created with address: " + wallet.address);
}
} catch {
Debug.Log("Error creating embedded wallet.");
}
This method will throw an error if:
- the user is not authenticated
- the user already has an embedded wallet
- wallet creation fails on the user's device
Creating additional wallets
Privy embedded wallets are hierarchical deterministic (HD) wallets. An HD wallet allows you to generate multiple Ethereum addresses and private keys from a shared source of entropy: the wallet seed (or equivalently, a BIP-39 mnemonic encoding the seed, known as a seed phrase).
In kind, Privy can be used to provision multiple embedded wallets for a single user.
In order to create additional wallets for your user, after the first wallet has been created, simply set the optional allowAdditional
parameter of CreateWallet
to true
.
csharp
try {
PrivyUser privyUser = PrivyManager.Instance.User;
if (privyUser != null) {
IEmbeddedWallet wallet = await PrivyManager.Instance.User.CreateWallet(allowAdditional: true);
Debug.Log($"Wallet address: {wallet.Address}");
}
} catch {
Debug.Log("Error creating embedded wallet.");
}
If you do not set allowAdditional
to true
, CreateWallet
will error when creating additional embedded wallets for the user.
Specifying the HD wallet index
Create a wallet at a specified HD wallet index by calling CreateWalletAtHdIndex
and pass in the preferred hdWalletIndex
. This method will either create a new wallet, or return the existing one if one already exists at the specified index.
INFO
A wallet with HD index 0 must be created before creating a wallet at greater HD indices.
csharp
try {
PrivyUser privyUser = PrivyManager.Instance.User;
if (privyUser != null) {
// Create an HD wallet at index 5
IEmbeddedWallet wallet = await PrivyManager.Instance.User.CreateWalletAtHdIndex(hdWalletIndex: 5);
Debug.Log($"Wallet address: {wallet.Address}, index: {wallet.HdWalletIndex}");
}
} catch {
Debug.Log("Error creating embedded wallet.");
}
An exception can be thrown if:
- Wallet creation fails or the wallet cannot be added to the user's account.
- An invalid HD wallet index is supplied, i.e.
hdWalletIndex
is less than 0, or ifhdWalletIndex
is greater than 0 while user has no wallet with HD index 0.