Appearance
Hierarchical deterministic (HD) 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. Read more below to learn how!
Read more about how HD wallets work.
HD wallets use a shared source of entropy to derive the wallet seed; this entropy is protected by the Privy cryptosystem.
Each wallet is generated from the seed and a unique path parameter, which has the format:
m / purpose' / coin_type' / account' / change / address_index
For Privy's embedded wallets, the path used for the i
-th wallet is:
m/44'/60'/0'/0/i
where i
is 0-indexed. An HD wallet is said to have an index of i
if it is derived from the i
-th path above. You can read more about these derivation paths here.
Creating multiple HD wallets
To create multiple Ethereum wallets for a user, use the create
method from the useEmbeddedEthereumWallet
hook:
tsx
import {useEmbeddedEthereumWallet} from '@privy-io/expo';
const {create} = useEmbeddedEthereumWallet();
As a parameter to create
, pass an object containing a createAdditional
boolean specifying if you would like to create an additional wallet, even if the user has an existing one.
Prop | Type | Description |
---|---|---|
createAdditional | boolean | If true , will allow the user to create a Ethereum wallet regardless if it is their first Ethereum wallet or an additional Ethereum wallet. If false , create will succeed only if the use is creating their first wallet. Defaults to false . |
Once invoked, create
will return a Promise that resolves to the newly created wallet, if it was successful. This method will reject with an error if:
- the user is not
authenticated
. - the user already has an embedded Ethereum wallet and
createAdditional
was not set totrue
. - if there is another error during wallet creation, such as the user exiting prematurely.
tsx
// Creating additional embedded wallets for the user
// You can also create the first wallet for the user using this syntax
await create({createAdditional: true});
Using multiple HD wallets
Once a user has one or more embedded wallets, the wallets are added to the wallets
array returned by useEmbeddedEthereumWallet
:
tsx
import {useEmbeddedEthereumWallet} from '@privy-io/expo';
...
const {wallets} = useEmbeddedEthereumWallet();
Refer to the requests section to learn how to interact with the wallets you create.
Getting a specific embedded wallet
To find a specific embedded wallet for the user, search the wallets
array for a wallet with the address
that matches your desired address:
tsx
const desiredAddress = 'insert-your-desired-wallet-address';
const {wallets} = useEmbeddedEthereumWallet();
const desiredWallet = wallets.find((wallet) => wallet.address === desiredAddress);
You can alternatively search the wallets array by your desired HD index:
tsx
// Replace this with your desired HD index
const desiredHdIndex = 0;
const {wallets} = useEmbeddedEthereumWallet();
const desiredWallet = wallets.find((wallet) => wallet.walletIndex === desiredHdIndex);