Derive Stacks address from keys
Generate Stacks addresses from private or public keys using multiple methods
import { getPublicKeyFromPrivate } from "@stacks/encryption";import {getAddressFromPrivateKey,getAddressFromPublicKey} from "@stacks/transactions";// Derive address from private keyconst privateKey = process.env.PRIVATE_KEY; // Keep this secret!const addressFromPrivate = getAddressFromPrivateKey(privateKey, "testnet");// Derive public key and addressconst publicKey = getPublicKeyFromPrivate(privateKey);const addressFromPublic = getAddressFromPublicKey(publicKey, "testnet");console.log("Address:", addressFromPrivate);console.log("Public key:", publicKey);console.log("Same address:", addressFromPrivate === addressFromPublic); // true
Use cases
- Wallet address generation
- Key pair validation
- Address recovery from backup keys
- Multi-signature wallet setup
Key concepts
Stacks addresses are derived through:
- Private key: 32-byte random number (keep secret!)
- Public key: Derived from private key using ECDSA
- Address: Base58check-encoded hash of public key
- Network: Different prefixes for mainnet (SP/SM) vs testnet (ST/SN)