Get account details from wallet
Extract addresses, keys, and other account information from a wallet instance
import { STACKS_TESTNET, STACKS_MAINNET } from "@stacks/network";import { generateWallet, getStxAddress } from "@stacks/wallet-sdk";// Generate or restore walletconst wallet = await generateWallet({secretKey: "your twenty four word mnemonic phrase goes here...",password: "wallet-password",});// Get first accountconst account = wallet.accounts[0];// Get addresses for different networksconst testnetAddress = getStxAddress({account,transactionVersion: 0 // testnet});const mainnetAddress = getStxAddress({account,transactionVersion: 1 // mainnet});// Get keysconst privateKey = account.stxPrivateKey;const publicKey = account.stxPublicKey;console.log("Testnet address:", testnetAddress);console.log("Mainnet address:", mainnetAddress);console.log("Public key:", publicKey);
Use cases
- Displaying user addresses in wallet UIs
- Getting private keys for transaction signing
- Deriving addresses for different networks
- Building wallet management tools
Key concepts
Wallet accounts contain:
- Private key: Used for signing transactions
- Public key: Derived from private key
- Addresses: Network-specific (mainnet vs testnet)
- Derivation path: BIP44 path used to generate the account
Multiple accounts from same wallet
import { generateNewAccount } from "@stacks/wallet-sdk";// Get multiple accountsconst firstAccount = wallet.accounts[0];const secondAccount = wallet.accounts[1] || generateNewAccount(wallet);const thirdAccount = wallet.accounts[2] || generateNewAccount(wallet);// Each account has unique addressesconst addresses = wallet.accounts.map((account, index) => ({index,address: getStxAddress({ account, transactionVersion: 1 }),publicKey: account.stxPublicKey,}));console.log("All addresses:", addresses);
Account details interface
// Full account structureinterface Account {stxPrivateKey: string;stxPublicKey: string;index: number;// BIP44 derivation path: m/44'/5757'/account'/0/0}// Get all account detailsfunction getAccountDetails(account: Account, network: 'mainnet' | 'testnet') {const transactionVersion = network === 'mainnet' ? 1 : 0;return {address: getStxAddress({ account, transactionVersion }),publicKey: account.stxPublicKey,privateKey: account.stxPrivateKey,derivationPath: `m/44'/5757'/${account.index}'/0/0`,network,};}
Export wallet for backup
function exportWalletData(wallet: Wallet) {return {// Never expose the mnemonic in production!encryptedSeed: wallet.encryptedSecretKey,accounts: wallet.accounts.map(account => ({index: account.index,publicKey: account.stxPublicKey,testnetAddress: getStxAddress({account,transactionVersion: 0}),mainnetAddress: getStxAddress({account,transactionVersion: 1}),})),};}// Usageconst backupData = exportWalletData(wallet);// Save backupData securely (encrypted)
Security warning
Never log or expose private keys in production applications. Store them securely in memory and clear them after use.
Package installation
Terminal
$npm install @stacks/network @stacks/wallet-sdk