Skip to content

Commit 2dc9575

Browse files
feat: add RPC wallet example
1 parent 529e8b5 commit 2dc9575

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

example-crates/wallet_rpc/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9+
bdk = { path = "../../crates/bdk" }
10+
bdk_file_store = { path = "../../crates/file_store" }
11+
bdk_bitcoind_rpc = { path = "../../crates/bitcoind_rpc" }
Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,99 @@
1-
fn main() {
2-
println!("Hello, world!");
1+
use std::str::FromStr;
2+
use bdk_file_store::Store;
3+
use bdk::{
4+
bitcoin::{ Address, Network},
5+
wallet::{AddressIndex, Wallet, ChangeSet },
6+
SignOptions,
7+
};
8+
use bdk_bitcoind_rpc::{
9+
Emitter,
10+
bitcoincore_rpc::{Auth, Client, RpcApi},
11+
};
12+
13+
const DB_MAGIC: &str = "bdk-rpc-wallet-example";
14+
const FALLBACK_HEIGHT: u32 = 2532323;
15+
const SEND_AMOUNT: u64 = 5000;
16+
const LOOKAHEAD: u32 = 20;
17+
const RPC_USER: &str = "bitcoin";
18+
const RPC_PASS: &str = "password";
19+
const RPC_URL : &str = "127.0.0.1:18332";
20+
21+
22+
fn main() -> Result<(), Box<dyn std::error::Error>> {
23+
let db_path = std::env::temp_dir().join("bdk-rpc-example");
24+
let db = Store::<bdk::wallet::ChangeSet>::new_from_path(DB_MAGIC.as_bytes(), db_path)?;
25+
26+
let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)";
27+
let internal_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)";
28+
29+
let mut wallet = Wallet::new(
30+
external_descriptor,
31+
Some(internal_descriptor),
32+
db,
33+
Network::Testnet,
34+
)?;
35+
36+
let address = wallet.get_address(AddressIndex::New);
37+
println!("Generated Address: {}", address);
38+
39+
let balance = wallet.get_balance();
40+
println!("Wallet balance before syncing: {} sats", balance.total());
41+
42+
let rpc_client = Client::new(
43+
RPC_URL,
44+
Auth::UserPass(RPC_USER.to_string(), RPC_PASS.to_string()),
45+
)?;
46+
47+
println!("Connected to Bitcoin Core RPC at {:?}", rpc_client.get_blockchain_info().unwrap());
48+
49+
wallet.set_lookahead_for_all(LOOKAHEAD);
50+
51+
let chain_tip = wallet.latest_checkpoint();
52+
let mut emitter = match chain_tip {
53+
Some(cp) => Emitter::from_checkpoint(&rpc_client, cp),
54+
None => Emitter::from_height(&rpc_client, FALLBACK_HEIGHT),
55+
};
56+
57+
while let Some((height, block)) = emitter.next_block()? {
58+
println!("Applying block {} at height {}", block.block_hash(), height);
59+
let wallet_changeset = wallet.apply_block_relevant(block, height)?;
60+
wallet.stage(wallet_changeset);
61+
wallet.commit()?;
62+
}
63+
64+
let unconfirmed_txs = emitter.mempool()?;
65+
let mempool_changeset = wallet.batch_insert_relevant_unconfirmed(unconfirmed_txs.iter().map(|(tx, time)| (tx, *time)));
66+
println!("Applying unconfirmed transactions: {:?}", mempool_changeset);
67+
let wallet_changeset = ChangeSet::from(mempool_changeset);
68+
wallet.stage(wallet_changeset);
69+
wallet.commit()?;
70+
71+
let balance = wallet.get_balance();
72+
println!("Wallet balance after syncing: {} sats", balance.total());
73+
74+
if balance.total() < SEND_AMOUNT {
75+
println!(
76+
"Please send at least {} sats to the receiving address",
77+
SEND_AMOUNT
78+
);
79+
std::process::exit(0);
80+
}
81+
82+
let faucet_address = Address::from_str("tb1qw2c3lxufxqe2x9s4rdzh65tpf4d7fssjgh8nv6")?
83+
.require_network(Network::Testnet)?;
84+
85+
let mut tx_builder = wallet.build_tx();
86+
tx_builder
87+
.add_recipient(faucet_address.script_pubkey(), SEND_AMOUNT)
88+
.enable_rbf();
89+
90+
let mut psbt = tx_builder.finish()?;
91+
let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
92+
assert!(finalized);
93+
94+
let tx = psbt.extract_tx();
95+
rpc_client.send_raw_transaction(&tx)?;
96+
println!("Tx broadcasted! Txid: {}", tx.txid());
97+
98+
Ok(())
399
}

0 commit comments

Comments
 (0)