Expand description
Minimal offline Bitcoin key generator for cold storage.
Generates a secp256k1 private key from OS-provided cryptographic randomness and derives the corresponding WIF, compressed public key, and native SegWit (Bech32) address. Designed for air-gapped key ceremonies.
§Library usage
// 1. Generate a private key from OS randomness
let key = btc_keygen::generate()?;
// 2. Encode as WIF (for wallet import)
let wif = btc_keygen::encode_wif(&key);
println!("{}", wif.expose_str());
// 3. Derive the compressed public key
let pubkey = btc_keygen::derive_pubkey(&key);
// 4. Derive the Bitcoin address
let address = btc_keygen::derive_address(&pubkey);To use an existing key instead of OS randomness, see
PrivateKey::from_bytes and PrivateKey::from_hex.
§Security
- Entropy comes from the OS CSPRNG via
getrandom. - Private key bytes are zeroized in memory when
PrivateKeyis dropped. - Secret output never leaves this crate as a
String.encode_wifreturns aSecretWifandPrivateKey::to_hexreturns aSecretKeyHex: fixed-size buffers that zeroize on drop, redact theirDebug, and cannot be cloned, copied, or printed with{}. They are filled in place, so no secret-bearingString,Vec, orformat!temporary is allocated along the way. - Exposing a secret is explicit (
expose_bytes,expose_str) and is the point where copies become the caller’s responsibility: writing the bytes to a terminal, or copying them into aString, puts key material in memory this crate cannot erase. PrivateKey::as_bytesandPrivateKey::to_secret_keyexist for interoperability and hand out key material the crate no longer controls. In particularsecp256k1::SecretKeyisCopyand does not erase itself on drop; itsnon_secure_eraseis best-effort.- Erasure is best-effort in general. It covers the buffers this crate owns,
not memory the OS relocated to swap or a crash dump, not the stack
libsecp256k1 uses while deriving a public key, and not copies the optimizer
keeps alive. The
zeroizecrate documents that last limit for itself. - No networking code, so the crate cannot leak secrets over the network.
- Elliptic curve operations use Bitcoin Core’s
libsecp256k1.
Structs§
- Error
- Error returned when key generation fails.
- Private
Key - A validated secp256k1 private key that zeroizes its bytes on drop.
- Secret
Ascii - A fixed-length ASCII secret that zeroizes on drop and redacts its
Debug.
Functions§
- derive_
address - Derives a native SegWit (P2WPKH) Bech32 address from a compressed public key.
- derive_
pubkey - Derives the compressed public key (33 bytes) from a private key.
- encode_
wif - Encodes a private key as a Wallet Import Format (WIF) string.
- generate
- Generates a new Bitcoin private key using OS-provided cryptographic randomness.
Type Aliases§
- Secret
KeyHex - A raw private key in hexadecimal: exactly 64 lowercase ASCII bytes.
- Secret
Wif - A compressed mainnet WIF: exactly 52 Base58 ASCII bytes.