useEmbeddedEthereumWallet
Manage embedded Ethereum wallets with creation, activation, recovery, and signing capabilities.
Usage
import { useEmbeddedEthereumWallet } from '@openfort/react-native';
function WalletManager() {
const ethereum = useEmbeddedEthereumWallet({ chainId: 1 });
if (ethereum.status === 'connecting' || ethereum.status === 'creating') {
return <ActivityIndicator />;
}
if (ethereum.status === 'connected') {
return (
<View>
<Text>{ethereum.activeWallet.address}</Text>
<Button
title="Send Transaction"
onPress={() => ethereum.provider.request({
method: 'eth_sendTransaction',
params: [{ from: ethereum.activeWallet.address, to: '0x...', value: '0x0' }]
})}
/>
</View>
);
}
if (ethereum.wallets.length === 0) {
return <Button title="Create Wallet" onPress={() => ethereum.create()} />;
}
return <Button title="Connect" onPress={() => ethereum.setActive({ address: ethereum.wallets[0].address })} />;
}Return type
Returns a discriminated union based on the status field:
type EmbeddedEthereumWalletState = {
status: 'disconnected' | 'connecting' | 'reconnecting' | 'creating' | 'needs-recovery' | 'connected' | 'error'
wallets: ConnectedEmbeddedEthereumWallet[]
activeWallet: ConnectedEmbeddedEthereumWallet | null
provider?: OpenfortEmbeddedEthereumWalletProvider // Available when connected
error?: string // Available when error
create: (options?: CreateOptions) => Promise<EmbeddedAccount>
setActive: (options: SetActiveOptions) => Promise<void>
setRecovery: (options: SetRecoveryOptions) => Promise<void>
exportPrivateKey: () => Promise<string>
}
type ConnectedEmbeddedEthereumWallet = {
address: string
ownerAddress?: string
chainType: 'EVM'
walletIndex: number
getProvider: () => Promise<OpenfortEmbeddedEthereumWalletProvider>
}
type OpenfortEmbeddedEthereumWalletProvider = {
request(args: { method: string; params?: any[] }): Promise<any>
on(event: string, handler: (...args: any[]) => void): void
removeListener(event: string, handler: (...args: any[]) => void): void
}Parameters
Hook options
type UseEmbeddedEthereumWalletOptions = {
chainId?: number
onCreateSuccess?: (account: EmbeddedAccount, provider: OpenfortEmbeddedEthereumWalletProvider) => void
onCreateError?: (error: OpenfortError) => void
onSetActiveSuccess?: (wallet: ConnectedEmbeddedEthereumWallet, provider: OpenfortEmbeddedEthereumWalletProvider) => void
onSetActiveError?: (error: OpenfortError) => void
}create
type CreateOptions = {
chainId?: number
recoveryPassword?: string
accountType?: 'Smart Account' | 'Externally Owned Account'
policyId?: string
onSuccess?: (data: CreateResult) => void
onError?: (error: OpenfortError) => void
}setActive
type SetActiveOptions = {
address: string
chainId?: number
recoveryPassword?: string
onSuccess?: (data: SetActiveResult) => void
onError?: (error: OpenfortError) => void
}setRecovery
type SetRecoveryOptions = {
previousRecovery: RecoveryParams
newRecovery: RecoveryParams
onSuccess?: (data: SetRecoveryResult) => void
onError?: (error: OpenfortError) => void
}
type RecoveryParams = {
recoveryMethod: 'automatic' | 'password'
password?: string
encryptionSession?: string
}