|
| 1 | +--- |
| 2 | +title: Account Management |
| 3 | +icon: users |
| 4 | +description: Learn how to create and manage accounts in the Avalanche Client SDK with support for EVM, X-Chain, and P-Chain operations. |
| 5 | +--- |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +Avalanche accounts work across all three chains—P-Chain, X-Chain, and C-Chain—with a single account. Each account provides both EVM addresses (for C-Chain) and XP addresses (for X/P-Chain), so you can interact with the entire Avalanche network without managing separate accounts. |
| 10 | + |
| 11 | +## Account Structure |
| 12 | + |
| 13 | +Every Avalanche account has an EVM account for C-Chain and an optional XP account for X/P-Chain: |
| 14 | + |
| 15 | +```typescript |
| 16 | +type AvalancheAccount = { |
| 17 | + evmAccount: Account; // C-Chain |
| 18 | + xpAccount?: XPAccount; // X/P-Chain |
| 19 | + getEVMAddress: () => Address; |
| 20 | + getXPAddress: (chain?: "X" | "P" | "C", hrp?: string) => XPAddress; |
| 21 | +}; |
| 22 | +``` |
| 23 | + |
| 24 | +### Quick Start |
| 25 | + |
| 26 | +```typescript |
| 27 | +import { privateKeyToAvalancheAccount } from "@avalanche-sdk/client/accounts"; |
| 28 | +import { createAvalancheWalletClient } from "@avalanche-sdk/client"; |
| 29 | +import { avalanche } from "@avalanche-sdk/client/chains"; |
| 30 | + |
| 31 | +const account = privateKeyToAvalancheAccount("0x..."); |
| 32 | + |
| 33 | +const walletClient = createAvalancheWalletClient({ |
| 34 | + account, |
| 35 | + chain: avalanche, |
| 36 | + transport: { type: "http" }, |
| 37 | +}); |
| 38 | + |
| 39 | +// Get addresses for all chains |
| 40 | +const evmAddress = account.getEVMAddress(); // 0x742d35Cc... |
| 41 | +const xChainAddress = account.getXPAddress("X"); // X-avax1... |
| 42 | +const pChainAddress = account.getXPAddress("P"); // P-avax1... |
| 43 | +``` |
| 44 | + |
| 45 | +## Account Types |
| 46 | + |
| 47 | +### Local Accounts |
| 48 | + |
| 49 | +Local accounts store keys on your machine and sign transactions before broadcasting. Use these for server-side apps, bots, or when you need full control. |
| 50 | + |
| 51 | +- [Private Key Accounts](accounts/local/private-key) - Simple and direct |
| 52 | +- [Mnemonic Accounts](accounts/local/mnemonic) - Easy recovery with seed phrases |
| 53 | +- [HD Key Accounts](accounts/local/hd-key) - Advanced key derivation |
| 54 | + |
| 55 | +### JSON-RPC Accounts |
| 56 | + |
| 57 | +JSON-RPC accounts use external wallets (MetaMask, Core, etc.) for signing. Perfect for browser-based dApps where users control their own keys. |
| 58 | + |
| 59 | +[Learn more about JSON-RPC accounts →](accounts/json-rpc) |
| 60 | + |
| 61 | +## Working with Accounts |
| 62 | + |
| 63 | +### EVM Account |
| 64 | + |
| 65 | +The `evmAccount` handles all C-Chain operations—smart contracts, ERC-20 transfers, and standard EVM interactions. |
| 66 | + |
| 67 | +```typescript |
| 68 | +const evmAccount = account.evmAccount; |
| 69 | +console.log(evmAccount.address); // 0x742d35Cc... |
| 70 | +``` |
| 71 | + |
| 72 | +### XP Account |
| 73 | + |
| 74 | +The `xpAccount` handles X-Chain and P-Chain operations—UTXO transactions, asset transfers, and staking. |
| 75 | + |
| 76 | +```typescript |
| 77 | +if (account.xpAccount) { |
| 78 | + const xpAccount = account.xpAccount; |
| 79 | + console.log(xpAccount.publicKey); |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +### Getting Addresses |
| 84 | + |
| 85 | +```typescript |
| 86 | +// C-Chain address |
| 87 | +const evmAddress = account.getEVMAddress(); // 0x742d35Cc... |
| 88 | + |
| 89 | +// X/P-Chain addresses |
| 90 | +const xChainAddress = account.getXPAddress("X"); // X-avax1... |
| 91 | +const pChainAddress = account.getXPAddress("P"); // P-avax1... |
| 92 | + |
| 93 | +// Network-specific (mainnet vs testnet) |
| 94 | +const mainnet = account.getXPAddress("X", "avax"); |
| 95 | +const testnet = account.getXPAddress("X", "fuji"); |
| 96 | +``` |
| 97 | + |
| 98 | +## Creating Accounts |
| 99 | + |
| 100 | +### Private Key |
| 101 | + |
| 102 | +```typescript |
| 103 | +import { privateKeyToAvalancheAccount } from "@avalanche-sdk/client/accounts"; |
| 104 | + |
| 105 | +const account = privateKeyToAvalancheAccount("0x..."); |
| 106 | +``` |
| 107 | + |
| 108 | +[Private Key Accounts →](accounts/local/private-key) |
| 109 | + |
| 110 | +### Mnemonic |
| 111 | + |
| 112 | +```typescript |
| 113 | +import { mnemonicsToAvalancheAccount } from "@avalanche-sdk/client/accounts"; |
| 114 | + |
| 115 | +const account = mnemonicsToAvalancheAccount("abandon abandon abandon..."); |
| 116 | +``` |
| 117 | + |
| 118 | +[Mnemonic Accounts →](accounts/local/mnemonic) |
| 119 | + |
| 120 | +### HD Key |
| 121 | + |
| 122 | +```typescript |
| 123 | +import { hdKeyToAvalancheAccount, HDKey } from "@avalanche-sdk/client/accounts"; |
| 124 | + |
| 125 | +const hdKey = HDKey.fromMasterSeed(seed); |
| 126 | +const account = hdKeyToAvalancheAccount(hdKey, { accountIndex: 0 }); |
| 127 | +``` |
| 128 | + |
| 129 | +[HD Key Accounts →](accounts/local/hd-key) |
| 130 | + |
| 131 | +## Address Formats |
| 132 | + |
| 133 | +- **C-Chain:** `0x...` (Ethereum-compatible) |
| 134 | +- **X/P-Chain:** `avax1...` or `X-avax1...` / `P-avax1...` (Bech32-encoded) |
| 135 | + |
| 136 | +[Network-Specific Addresses →](accounts/local/addresses) |
| 137 | + |
| 138 | +## Security |
| 139 | + |
| 140 | +<Callout> |
| 141 | + **Never expose private keys or mnemonics in client-side code or commit them to |
| 142 | + version control. Use environment variables.** |
| 143 | +</Callout> |
| 144 | + |
| 145 | +```typescript |
| 146 | +// ✅ Good |
| 147 | +const account = privateKeyToAvalancheAccount(process.env.PRIVATE_KEY!); |
| 148 | + |
| 149 | +// ❌ Bad |
| 150 | +const account = privateKeyToAvalancheAccount("0x1234..."); |
| 151 | +``` |
| 152 | + |
| 153 | +## Comparison Table |
| 154 | + |
| 155 | +| Feature | Private Key | Mnemonic | HD Key | JSON-RPC | |
| 156 | +| ----------------- | ----------- | --------- | -------- | ------------ | |
| 157 | +| **Ease of Use** | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ | |
| 158 | +| **Recovery** | ❌ | ✅ | ✅ | ❌ | |
| 159 | +| **Multi-Account** | ❌ | ✅ | ✅ | ❌ | |
| 160 | +| **Security** | ⚠️ High | ✅ High | ✅ High | ✅ Very High | |
| 161 | +| **Use Case** | Server/Bots | User Apps | Advanced | User Apps | |
| 162 | + |
| 163 | +## Next Steps |
| 164 | + |
| 165 | +- [Private Key Accounts](accounts/local/private-key) - Simple and direct |
| 166 | +- [Mnemonic Accounts](accounts/local/mnemonic) - Easy recovery |
| 167 | +- [HD Key Accounts](accounts/local/hd-key) - Advanced key derivation |
| 168 | +- [JSON-RPC Accounts](accounts/json-rpc) - Browser wallet integration |
| 169 | +- [Account Utilities](accounts/local/utilities) - Helper functions |
| 170 | +- [Wallet Operations](methods/wallet-methods/wallet) - Send transactions |
0 commit comments