From 8d31b3305265fd54e4cc8844ac77d86f71d95335 Mon Sep 17 00:00:00 2001 From: Vladimir Fomene Date: Wed, 11 Oct 2023 11:16:38 +0300 Subject: [PATCH] feat(example): add RPC wallet example --- Cargo.toml | 1 + .../example_bitcoind_rpc_polling/README.md | 68 +++++++++++++ example-crates/wallet_rpc/Cargo.toml | 11 +++ example-crates/wallet_rpc/README.md | 13 +++ example-crates/wallet_rpc/src/main.rs | 96 +++++++++++++++++++ 5 files changed, 189 insertions(+) create mode 100644 example-crates/example_bitcoind_rpc_polling/README.md create mode 100644 example-crates/wallet_rpc/Cargo.toml create mode 100644 example-crates/wallet_rpc/README.md create mode 100644 example-crates/wallet_rpc/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index 0e1efc902f..77fc7226cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ members = [ "example-crates/wallet_electrum", "example-crates/wallet_esplora_blocking", "example-crates/wallet_esplora_async", + "example-crates/wallet_rpc", "nursery/tmp_plan", "nursery/coin_select" ] diff --git a/example-crates/example_bitcoind_rpc_polling/README.md b/example-crates/example_bitcoind_rpc_polling/README.md new file mode 100644 index 0000000000..fef82ab1cb --- /dev/null +++ b/example-crates/example_bitcoind_rpc_polling/README.md @@ -0,0 +1,68 @@ +# Example RPC CLI + +### Simple Regtest Test + +1. Start local regtest bitcoind. + ``` + mkdir -p /tmp/regtest/bitcoind + bitcoind -regtest -server -fallbackfee=0.0002 -rpcuser= -rpcpassword= -datadir=/tmp/regtest/bitcoind -daemon + ``` +2. Create a test bitcoind wallet and set bitcoind env. + ``` + bitcoin-cli -datadir=/tmp/regtest/bitcoind -regtest -rpcuser= -rpcpassword= -named createwallet wallet_name="test" + export RPC_URL=127.0.0.1:18443 + export RPC_USER= + export RPC_PASS= + ``` +3. Get test bitcoind wallet info. + ``` + bitcoin-cli -rpcwallet="test" -rpcuser= -rpcpassword= -datadir=/tmp/regtest/bitcoind -regtest getwalletinfo + ``` +4. Get new test bitcoind wallet address. + ``` + BITCOIND_ADDRESS=$(bitcoin-cli -rpcwallet="test" -datadir=/tmp/regtest/bitcoind -regtest -rpcuser= -rpcpassword= getnewaddress) + echo $BITCOIND_ADDRESS + ``` +5. Generate 101 blocks with reward to test bitcoind wallet address. + ``` + bitcoin-cli -datadir=/tmp/regtest/bitcoind -regtest -rpcuser= -rpcpassword= generatetoaddress 101 $BITCOIND_ADDRESS + ``` +6. Verify test bitcoind wallet balance. + ``` + bitcoin-cli -rpcwallet="test" -datadir=/tmp/regtest/bitcoind -regtest -rpcuser= -rpcpassword= getbalances + ``` +7. Set descriptor env and get address from RPC CLI wallet. + ``` + export DESCRIPTOR="wpkh(tprv8ZgxMBicQKsPfK9BTf82oQkHhawtZv19CorqQKPFeaHDMA4dXYX6eWsJGNJ7VTQXWmoHdrfjCYuDijcRmNFwSKcVhswzqs4fugE8turndGc/1/*)" + cargo run -- --network regtest address next + ``` +8. Send 5 test bitcoin to RPC CLI wallet. + ``` + bitcoin-cli -rpcwallet="test" -datadir=/tmp/regtest/bitcoind -regtest -rpcuser= -rpcpassword= sendtoaddress
5 + ``` +9. Sync blockchain with RPC CLI wallet. + ``` + cargo run -- --network regtest sync + + ``` +10. Get RPC CLI wallet unconfirmed balances. + ``` + cargo run -- --network regtest balance + ``` +11. Generate 1 block with reward to test bitcoind wallet address. + ``` + bitcoin-cli -datadir=/tmp/regtest/bitcoind -rpcuser= -rpcpassword= -regtest generatetoaddress 10 $BITCOIND_ADDRESS + ``` +12. Sync the blockchain with RPC CLI wallet. + ``` + cargo run -- --network regtest sync + + ``` +13. Get RPC CLI wallet confirmed balances. + ``` + cargo run -- --network regtest balance + ``` +14. Get RPC CLI wallet transactions. + ``` + cargo run -- --network regtest txout list + ``` \ No newline at end of file diff --git a/example-crates/wallet_rpc/Cargo.toml b/example-crates/wallet_rpc/Cargo.toml new file mode 100644 index 0000000000..127ed82198 --- /dev/null +++ b/example-crates/wallet_rpc/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "wallet_rpc" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +bdk = { path = "../../crates/bdk" } +bdk_file_store = { path = "../../crates/file_store" } +bdk_bitcoind_rpc = { path = "../../crates/bitcoind_rpc" } diff --git a/example-crates/wallet_rpc/README.md b/example-crates/wallet_rpc/README.md new file mode 100644 index 0000000000..11bac45596 --- /dev/null +++ b/example-crates/wallet_rpc/README.md @@ -0,0 +1,13 @@ +# Wallet RPC Example + +# To run the wallet example, execute the following code (replace arguments with values that match your setup) + +``` +cargo run -- +``` + +Here is the command we used during testing + +``` +cargo run -- 127.0.0.1:18332 bitcoin password 20 2532323 +``` \ No newline at end of file diff --git a/example-crates/wallet_rpc/src/main.rs b/example-crates/wallet_rpc/src/main.rs new file mode 100644 index 0000000000..2207868a5a --- /dev/null +++ b/example-crates/wallet_rpc/src/main.rs @@ -0,0 +1,96 @@ +use bdk::{ + bitcoin::{Address, Network}, + wallet::{AddressIndex, Wallet}, + SignOptions, +}; +use bdk_bitcoind_rpc::{ + bitcoincore_rpc::{Auth, Client, RpcApi}, + Emitter, +}; +use bdk_file_store::Store; +use std::str::FromStr; + +const DB_MAGIC: &str = "bdk-rpc-wallet-example"; +const SEND_AMOUNT: u64 = 5000; + +fn main() -> Result<(), Box> { + let args = std::env::args().collect::>(); + let db_path = std::env::temp_dir().join("bdk-rpc-example"); + let db = Store::::new_from_path(DB_MAGIC.as_bytes(), db_path)?; + + let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)"; + let internal_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)"; + + if args.len() < 6 { + println!("Usage: wallet_rpc "); + std::process::exit(1); + } + + let mut wallet = Wallet::new( + external_descriptor, + Some(internal_descriptor), + db, + Network::Testnet, + )?; + + let address = wallet.get_address(AddressIndex::New); + println!("Generated Address: {}", address); + + let balance = wallet.get_balance(); + println!("Wallet balance before syncing: {} sats", balance.total()); + + let rpc_client = Client::new(&args[1], Auth::UserPass(args[2].clone(), args[3].clone()))?; + + println!( + "Connected to Bitcoin Core RPC at {:?}", + rpc_client.get_blockchain_info().unwrap() + ); + + wallet.set_lookahead_for_all(args[4].parse::()?)?; + + let chain_tip = wallet.latest_checkpoint(); + let mut emitter = match chain_tip { + Some(cp) => Emitter::from_checkpoint(&rpc_client, cp), + None => Emitter::from_height(&rpc_client, args[5].parse::()?), + }; + + while let Some((height, block)) = emitter.next_block()? { + println!("Applying block {} at height {}", block.block_hash(), height); + wallet.apply_block_relevant(block, height)?; + wallet.commit()?; + } + + let unconfirmed_txs = emitter.mempool()?; + println!("Applying unconfirmed transactions: ..."); + wallet.batch_insert_relevant_unconfirmed(unconfirmed_txs.iter().map(|(tx, time)| (tx, *time))); + wallet.commit()?; + + let balance = wallet.get_balance(); + println!("Wallet balance after syncing: {} sats", balance.total()); + + if balance.total() < SEND_AMOUNT { + println!( + "Please send at least {} sats to the receiving address", + SEND_AMOUNT + ); + std::process::exit(1); + } + + let faucet_address = Address::from_str("tb1qw2c3lxufxqe2x9s4rdzh65tpf4d7fssjgh8nv6")? + .require_network(Network::Testnet)?; + + let mut tx_builder = wallet.build_tx(); + tx_builder + .add_recipient(faucet_address.script_pubkey(), SEND_AMOUNT) + .enable_rbf(); + + let mut psbt = tx_builder.finish()?; + let finalized = wallet.sign(&mut psbt, SignOptions::default())?; + assert!(finalized); + + let tx = psbt.extract_tx(); + rpc_client.send_raw_transaction(&tx)?; + println!("Tx broadcasted! Txid: {}", tx.txid()); + + Ok(()) +}