Skip to main content

Rust

Rust is rapidly becoming the language of choice for new Bitcoin projects. Its memory safety guarantees, performance, and strong type system make it ideal for financial software where correctness is critical.

rust-bitcoin

rust-bitcoin provides fundamental Bitcoin data structures and serialization. It's the foundation that BDK and other Rust libraries build on.

cargo add bitcoin
use bitcoin::{Address, Network, PublicKey, CompressedPublicKey};
use bitcoin::secp256k1::{Secp256k1, rand};

fn main() {
let secp = Secp256k1::new();

// Generate a random key pair
let (secret_key, public_key) = secp.generate_keypair(&mut rand::thread_rng());

// Create a SegWit address
let compressed = CompressedPublicKey::from(public_key);
let address = Address::p2wpkh(compressed, Network::Regtest);

println!("Address: {}", address);
println!("Secret key: {}", secret_key.display_secret());
}

BDK (Bitcoin Dev Kit)

BDK is the recommended library for building wallets in Rust. It handles descriptors, coin selection, fee estimation, and PSBT construction.

cargo add bdk_wallet bdk_esplora bdk_file_store
use bdk_wallet::{Wallet, KeychainKind};
use bitcoin::Network;

fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a wallet with descriptor
let descriptor = "wpkh(tprv8ZgxMBicQKsPd.../84'/1'/0'/0/*)";
let change_descriptor = "wpkh(tprv8ZgxMBicQKsPd.../84'/1'/0'/1/*)";

let mut wallet = Wallet::create(descriptor, change_descriptor)
.network(Network::Regtest)
.create_wallet_no_persist()?;

// Get a new address
let address = wallet.reveal_next_address(KeychainKind::External);
println!("Address: {}", address.address);

Ok(())
}

LDK (Lightning Dev Kit)

LDK lets you build custom Lightning implementations. Unlike running a full Lightning node (LND, CLN), LDK gives you components you can assemble as needed.

cargo add lightning lightning-invoice lightning-net-tokio

LDK's key components:

ComponentPurpose
ChannelManagerManage payment channels
PeerManagerHandle P2P connections
RouterFind payment routes
KeysManagerKey derivation and signing
ChainMonitorWatch for on-chain channel events

rust-miniscript

Miniscript is a structured representation of Bitcoin Script that enables composable spending policies:

use miniscript::policy::Concrete;
use miniscript::Descriptor;

// A 2-of-3 multisig policy
let policy_str = "thresh(2,pk(Alice),pk(Bob),pk(Carol))";
let policy = Concrete::<String>::from_str(policy_str)?;

// Compile to a descriptor
let descriptor = policy.compile::<miniscript::Segwitv0>()?;
println!("Descriptor: {}", descriptor);

Ecosystem Overview

CratePurpose
bitcoinCore data structures, serialization
bdk_walletHigh-level wallet operations
lightningLightning Network protocol
miniscriptComposable spending policies
secp256k1Elliptic curve operations
bip39Mnemonic seed phrases