rustywallet-lightning
Lightning Network utilities for Bitcoin wallets.
Features
- BOLT11 Invoices: Parse and create Lightning invoices
- BOLT12 Offers: Parse and create reusable payment offers
- Payment Hashes: Generate and verify payment hashes/preimages
- Node Identity: Derive node ID from HD seed
- Route Hints: Parse and create route hints for private channels
- Channel Points: Handle channel point references
Installation
[]
= "0.2"
Quick Start
Payment Hash/Preimage
use *;
// Generate a random payment preimage
let preimage = random;
// Compute the payment hash
let payment_hash = preimage.payment_hash;
println!;
// Verify a preimage matches a hash
assert!;
BOLT12 Offers
BOLT12 offers provide a more flexible and privacy-preserving way to request payments:
use ;
// Create an offer
let offer = new
.description
.amount_msats // 10 sats
.issuer
.expires_in // 30 days
.build
.unwrap;
// Encode to string
let encoded = offer.encode;
println!; // lno1...
// Parse an offer
let parsed = parse.unwrap;
println!;
println!;
println!;
Offer Builder Options
use OfferBuilder;
let offer = new
.description // Required
.amount_msats // Fixed amount (optional)
.amount_variable // Or variable amount
.issuer // Issuer name
.expires_in // Expiry in seconds
.quantity_max // Max quantity per payment
.build
.unwrap;
Offer Fields
| Field | Description |
|---|---|
description() |
Human-readable description |
amount() |
Payment amount (fixed, variable, or currency) |
expiry() |
Absolute expiry timestamp |
is_expired() |
Check if offer has expired |
issuer() |
Issuer name/identifier |
node_id() |
Recipient node public key |
offer_id() |
Unique offer identifier (hash) |
paths() |
Blinded paths for privacy |
quantity_max() |
Maximum quantity per payment |
Parse BOLT11 Invoice
use *;
let invoice = "lnbc1pvjluez..."; // Your invoice string
let parsed = parse.unwrap;
println!;
println!;
println!;
Create Invoice Data
use *;
let preimage = random;
let payment_hash = preimage.payment_hash;
let invoice_data = new
.amount_sats // 10,000 sats
.description
.payment_hash
.expiry // 1 hour
.build
.unwrap;
Node Identity
use NodeIdentity;
use Seed;
// Derive node identity from seed
let seed = random;
let identity = from_seed.unwrap;
println!;
// Sign a message
let signature = identity.sign.unwrap;
Channel Points
use *;
// Parse channel point from string
let cp = parse.unwrap;
println!;
println!;
// Short channel ID
let scid = new;
println!; // "700000x1234x0"
Route Hints
use *;
let node_id = from_bytes;
let scid = new;
let hint = new
.hop
.build;
// Calculate routing fee
let hop = &hint.hops;
let fee = hop.fee_for_amount; // fee for 1M msat
API Reference
Types
| Type | Description |
|---|---|
PaymentPreimage |
32-byte payment secret |
PaymentHash |
SHA256 hash of preimage |
Bolt11Invoice |
Parsed BOLT11 invoice |
Bolt12Offer |
BOLT12 offer for reusable payments |
OfferBuilder |
Builder for BOLT12 offers |
OfferAmount |
Amount type (fixed, variable, currency) |
InvoiceBuilder |
Builder for invoice data |
NodeIdentity |
Node keypair derived from seed |
NodeId |
33-byte compressed public key |
ChannelPoint |
Funding txid:output reference |
ShortChannelId |
Compact block:tx:output ID |
RouteHint |
Private channel routing info |
RouteHintHop |
Single hop in route hint |
BlindedPath |
Blinded path for receiver privacy |
Networks
Network::Mainnet- Bitcoin mainnet (lnbc)Network::Testnet- Bitcoin testnet (lntb)Network::Regtest- Bitcoin regtest (lnbcrt)
BOLT11 vs BOLT12
| Feature | BOLT11 | BOLT12 |
|---|---|---|
| Reusable | No | Yes |
| Expiry | Required | Optional |
| Privacy | Limited | Blinded paths |
| Amount | Fixed | Fixed/Variable |
| Encoding | Bech32 | Bech32m |
| Prefix | lnbc |
lno |
Security Notes
PaymentPreimagedebug output is redactedNodeIdentitysecret key is redacted in debug- Use secure random for preimage generation
- BOLT12 offers support blinded paths for receiver privacy
License
MIT