rust-bitcoin
The foundation Rust crate for Bitcoin. Used by BDK, LDK, miniscript-rs,
electrs, etc. Stable, comprehensive.
Repo: github.com/rust-bitcoin/rust-bitcoin.
Install
[dependencies]
bitcoin = "0.32" # check current; API stabilizing
Key types
Transaction, TxIn, TxOut, OutPoint.
Script, ScriptBuf, Address, Network.
Block, BlockHash, BlockHeader.
secp256k1::SecretKey, secp256k1::PublicKey.
bip32::ExtendedPrivKey, ExtendedPubKey, DerivationPath.
psbt::Psbt (BIP174).
Quick examples
Address generation
use bitcoin::{Address, Network};
use bitcoin::secp256k1::{Secp256k1, SecretKey, PublicKey};
use bitcoin::PrivateKey;
let secp = Secp256k1::new();
let sk = SecretKey::from_slice(&[1u8; 32]).unwrap();
let pk = PublicKey::from_secret_key(&secp, &sk);
let addr = Address::p2wpkh(&PublicKey::new(pk), Network::Bitcoin).unwrap();
println!("{}", addr); // bc1q...
Parse + construct tx
use bitcoin::{Transaction, consensus::encode};
let raw = hex::decode("01000000...").unwrap();
let tx: Transaction = encode::deserialize(&raw).unwrap();
println!("txid: {}", tx.compute_txid());
let bytes = encode::serialize(&tx);
Sign with secp256k1
let secp = Secp256k1::new();
let msg = bitcoin::secp256k1::Message::from_digest_slice(&hash).unwrap();
let sig = secp.sign_ecdsa(&msg, &sk);
Modules
consensus — wire format encode/decode.
blockdata — block / tx / script.
network — message types, magic bytes.
psbt — partially-signed tx.
bip32 — HD derivation.
taproot — Taproot (TaprootBuilder, ControlBlock).
address — addr parsing + generation.
Use cases
- Wallet / signer / explorer code in Rust.
- Backend services consuming Bitcoin data.
- Foundation for higher-level libs (BDK, LDK).
Common pitfalls
- API churn between minor versions in 0.x range. Pin carefully.
Script (zero-cost wrapper) vs ScriptBuf (owned) — most APIs
take &Script.
- Network mismatch errors when building cross-network apps.
See also
1---2name: bitcoin-libraries-rust-bitcoin3description: rust-bitcoin: Rust crate for Bitcoin primitives. Tx, Block, Script, Address, Network, key types, PSBT, BIP32. Foundation for BDK, LDK, miniscript-rs. USE WHEN: building Bitcoin apps in Rust, parsing/constructing txs, working with addresses + scripts.4---56# rust-bitcoin78The foundation Rust crate for Bitcoin. Used by BDK, LDK, miniscript-rs,9electrs, etc. Stable, comprehensive.1011Repo: `github.com/rust-bitcoin/rust-bitcoin`.1213## Install1415```toml16[dependencies]17bitcoin = "0.32" # check current; API stabilizing18```1920## Key types2122- `Transaction`, `TxIn`, `TxOut`, `OutPoint`.23- `Script`, `ScriptBuf`, `Address`, `Network`.24- `Block`, `BlockHash`, `BlockHeader`.25- `secp256k1::SecretKey`, `secp256k1::PublicKey`.26- `bip32::ExtendedPrivKey`, `ExtendedPubKey`, `DerivationPath`.27- `psbt::Psbt` (BIP174).2829## Quick examples3031### Address generation32```rust33use bitcoin::{Address, Network};34use bitcoin::secp256k1::{Secp256k1, SecretKey, PublicKey};35use bitcoin::PrivateKey;3637let secp = Secp256k1::new();38let sk = SecretKey::from_slice(&[1u8; 32]).unwrap();39let pk = PublicKey::from_secret_key(&secp, &sk);40let addr = Address::p2wpkh(&PublicKey::new(pk), Network::Bitcoin).unwrap();41println!("{}", addr); // bc1q...42```4344### Parse + construct tx45```rust46use bitcoin::{Transaction, consensus::encode};4748let raw = hex::decode("01000000...").unwrap();49let tx: Transaction = encode::deserialize(&raw).unwrap();50println!("txid: {}", tx.compute_txid());5152let bytes = encode::serialize(&tx);53```5455### Sign with secp256k156```rust57let secp = Secp256k1::new();58let msg = bitcoin::secp256k1::Message::from_digest_slice(&hash).unwrap();59let sig = secp.sign_ecdsa(&msg, &sk);60```6162## Modules6364- `consensus` — wire format encode/decode.65- `blockdata` — block / tx / script.66- `network` — message types, magic bytes.67- `psbt` — partially-signed tx.68- `bip32` — HD derivation.69- `taproot` — Taproot (TaprootBuilder, ControlBlock).70- `address` — addr parsing + generation.7172## Use cases7374- Wallet / signer / explorer code in Rust.75- Backend services consuming Bitcoin data.76- Foundation for higher-level libs (BDK, LDK).7778## Common pitfalls7980- API churn between minor versions in 0.x range. Pin carefully.81- `Script` (zero-cost wrapper) vs `ScriptBuf` (owned) — most APIs82 take `&Script`.83- Network mismatch errors when building cross-network apps.8485## See also8687- [bdk/SKILL.md](../bdk/SKILL.md)88- [ldk/SKILL.md](../ldk/SKILL.md)89- [miniscript-rs/SKILL.md](../miniscript-rs/SKILL.md)90- [secp256k1-rs/SKILL.md](../secp256k1-rs/SKILL.md)