forked from bitcoindevkit/bdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e58d0b
commit 9c7344c
Showing
3 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 -- <RPC_URL> <RPC_USER> <RPC_PASS> <LOOKAHEAD> <FALLBACK_HEIGHT> | ||
``` | ||
|
||
Here is the command we used during testing | ||
|
||
``` | ||
cargo run -- 127.0.0.1:18332 bitcoin password 20 2532323 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,91 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
use bdk::{ | ||
bitcoin::{Address, Network}, | ||
wallet::{AddressIndex, ChangeSet, 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<dyn std::error::Error>> { | ||
let args = std::env::args().collect::<Vec<_>>(); | ||
let db_path = std::env::temp_dir().join("bdk-rpc-example"); | ||
let db = Store::<bdk::wallet::ChangeSet>::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/*)"; | ||
|
||
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[0], Auth::UserPass(args[1].clone(), args[2].clone()))?; | ||
|
||
println!( | ||
"Connected to Bitcoin Core RPC at {:?}", | ||
rpc_client.get_blockchain_info().unwrap() | ||
); | ||
|
||
wallet.set_lookahead_for_all(args[3].parse::<u32>()?); | ||
|
||
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[4].parse::<u32>()?), | ||
}; | ||
|
||
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(0); | ||
} | ||
|
||
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(()) | ||
} |