Skip to content
Developer Handbook

Wallets and Addresses

On this page

Your private key lets the wallet sign a spend. The corresponding public key lets other software verify the signature. Generate the private key as a secret 32-byte number in the valid secp256k1 range using a cryptographically secure random source.

Derive a receiving address

  1. Derive the compressed 33-byte secp256k1 public key from the private key.
  2. Apply SHA-256 and then RIPEMD-160 to the public key. The resulting 20 bytes are its HASH160.
  3. Encode the hash as a P2PKH CashAddr using the bitcoincash: prefix for mainnet. A sender decodes that address and places the hash in the new UTXO’s locking script.

This public example key derives the address for the 100,000-sat UTXO. Use it only in the exercise. Libauth P2PKH address API

Derive a P2PKH address with Libauth

import {
  binToHex, hash160, hexToBin, privateKeyToP2pkhCashAddress, secp256k1
} from '@bitauth/libauth';

const privateKey = hexToBin(
  'c1570ff31db1dec06a5dcacbba72735156bd42acf78fefc6717d174d842acc04'
);
const publicKey = secp256k1.derivePublicKeyCompressed(privateKey);
if (typeof publicKey === 'string') throw new Error(publicKey);

const { address } = privateKeyToP2pkhCashAddress({ privateKey, prefix: 'bitcoincash' });
console.log(binToHex(hash160(publicKey))); // b2557abfebaf263824b75ec473fd7b3db74957ea
console.log(address); // bitcoincash:qze9274lawhjvwpyka0vgula0v7mwj2hageduvrmrl

Try deriving the address in the walkthrough.

Derive many addresses from one backup

A hierarchical deterministic (HD) wallet can derive many keys from one seed. BIP32 adds a chain code to each key to derive its children. A derivation path identifies a branch or key in that tree. If your wallet uses BIP39, restore the same mnemonic and passphrase to reproduce the seed, then follow the original derivation paths to recover the keys.

Follow the receiving branch when you hand out an address and the change branch when a payment sends leftover sats back to your wallet.

  • First receiving key: m/44'/145'/0'/0/0
  • First change key: m/44'/145'/0'/1/0

These BIP44 paths use branch 0 for receiving and 1 for change. The last number selects an address within that branch. Apostrophes mark hardened steps, which require the parent private key.

BIP32 · BIP39 · BIP44 · Registered coin types

Import and export wallet keys

WIF moves one private key between wallets. To move or monitor a whole branch of an HD wallet, export an extended key, which bundles a key with the chain code used to derive its children.

  • Wallet Import Format (WIF) encodes one private key and its network and compression conventions for import into a compatible wallet. Keep it private. Anyone with the WIF can sign for that key. Libauth WIF decoding
  • xprv exports an extended private key, which can derive child signing keys below that node. Keep it private like the seed.
  • xpub exports an extended public key. A watch-only wallet can derive the account’s non-hardened receiving and change addresses from it. Anyone with the xpub can link those addresses. Share it only with services that need to watch them. BIP32 extended key formats

Mainnet addresses and network selection

Bitcoin Cash mainnet uses bitcoincash: addresses. Configure your wallet’s node or index to read and broadcast on the mainnet chain.

Keep the full prefix when passing an address to a wallet or library. It participates in the checksum. Decode the address and verify its prefix before building the output. An address can also tell the sender that a wallet accepts CashTokens. This changes the address type while preserving the same spending script. CashAddr specification

Run the examples on Mocknet or Chipnet

Mocknet uses the mainnet address encoding with an isolated browser chain and its own UTXOs. Chipnet is a separate live BCH test chain. It uses bchtest:, a prefix shared by several BCH test networks.

For Chipnet, open the lab in Chipnet mode or configure your client to use wss://chipnet.bch.ninja:50004. For testnet3, testnet4, or another public test chain, select its own node or Electrum endpoint. Verify the server's network and a known header after the networks diverge when they share earlier history. BCHN test network parameters · Connect to Electrum

Payment and identity formats

A receiving address is enough for the example payment. These formats cover reusable payment destinations, names, and sign-in.

  • CashCode is a shareable reusable payment code. A compatible sender derives a fresh payment destination and the recipient scans for payments it can spend. Reusable Payment Addresses specification
  • Cash Account is a human-readable name that resolves to payment information through an on-chain registration. Cash Accounts specification
  • Nostr keys sign Nostr events. NIP-19 encodes the public key as npub and the private key as nsec. Use a separate Nostr key for identity and keep nsec private. NIP-19

Continue to wallet sync to find UTXOs paid to your receiving and change addresses.

See signing the payment for the transition from private key to unlocking bytecode.