|
| 1 | +use std::str::FromStr; |
| 2 | + |
| 3 | +use bdk::blockchain::Blockchain; |
| 4 | +use bdk::{ |
| 5 | + blockchain::esplora::EsploraBlockchain, |
| 6 | + database::MemoryDatabase, |
| 7 | + template::Bip84, |
| 8 | + wallet::{export::FullyNodedExport, AddressIndex}, |
| 9 | + KeychainKind, SyncOptions, Wallet, |
| 10 | +}; |
| 11 | +use bitcoin::{ |
| 12 | + util::bip32::{self, ExtendedPrivKey}, |
| 13 | + Network, |
| 14 | +}; |
| 15 | + |
| 16 | +pub mod utils; |
| 17 | + |
| 18 | +use crate::utils::tx::build_signed_tx; |
| 19 | + |
| 20 | +/// This will create a wallet from an xpriv and get the balance by connecting to an Esplora server. |
| 21 | +/// If enough amount is available, this will send a transaction to an address. |
| 22 | +/// Otherwise, this will display a wallet address to receive funds. |
| 23 | +/// |
| 24 | +/// This can be run with `cargo run --features="use-esplora-reqwest, reqwest-default-tls" --example esplora_backend` |
| 25 | +/// in the root folder. |
| 26 | +/// |
| 27 | +/// Note: The configuration above uses asynchronous HTTP calls. |
| 28 | +/// To run it in a synchronous way, the user should change `required-features` to |
| 29 | +/// `required-features = ["use-esplora-ureq"] in `esplora_backend` target in Cargo.toml and run |
| 30 | +/// `cargo run --features="use-esplora-ureq" --example esplora_backend` |
| 31 | +fn main() { |
| 32 | + let network = Network::Signet; |
| 33 | + |
| 34 | + let xpriv = "tprv8ZgxMBicQKsPcx5nBGsR63Pe8KnRUqmbJNENAfGftF3yuXoMMoVJJcYeUw5eVkm9WBPjWYt6HMWYJNesB5HaNVBaFc1M6dRjWSYnmewUMYy"; |
| 35 | + |
| 36 | + let esplora_url = "https://explorer.bc-2.jp/api"; |
| 37 | + |
| 38 | + run(&network, esplora_url, xpriv); |
| 39 | +} |
| 40 | + |
| 41 | +fn create_wallet(network: &Network, xpriv: &ExtendedPrivKey) -> Wallet<MemoryDatabase> { |
| 42 | + let wallet = Wallet::new( |
| 43 | + Bip84(*xpriv, KeychainKind::External), |
| 44 | + Some(Bip84(*xpriv, KeychainKind::Internal)), |
| 45 | + *network, |
| 46 | + MemoryDatabase::default(), |
| 47 | + ) |
| 48 | + .unwrap(); |
| 49 | + |
| 50 | + wallet |
| 51 | +} |
| 52 | + |
| 53 | +fn run(network: &Network, esplora_url: &str, xpriv: &str) { |
| 54 | + let xpriv = bip32::ExtendedPrivKey::from_str(xpriv).unwrap(); |
| 55 | + |
| 56 | + let blockchain = EsploraBlockchain::new(esplora_url, 20); |
| 57 | + |
| 58 | + let wallet = create_wallet(&network, &xpriv); |
| 59 | + |
| 60 | + wallet.sync(&blockchain, SyncOptions::default()).unwrap(); |
| 61 | + |
| 62 | + let address = wallet.get_address(AddressIndex::New).unwrap().address; |
| 63 | + |
| 64 | + println!("address: {}", address); |
| 65 | + |
| 66 | + let balance = wallet.get_balance().unwrap(); |
| 67 | + |
| 68 | + println!("balance: {}", balance); |
| 69 | + |
| 70 | + if balance > 10500 { |
| 71 | + // the wallet sends the amount to itself. |
| 72 | + let recipient_address = wallet |
| 73 | + .get_address(AddressIndex::New) |
| 74 | + .unwrap() |
| 75 | + .address |
| 76 | + .to_string(); |
| 77 | + |
| 78 | + let amount = 9359; |
| 79 | + |
| 80 | + let tx = build_signed_tx(&wallet, &recipient_address, amount); |
| 81 | + |
| 82 | + blockchain.broadcast(&tx).unwrap(); |
| 83 | + |
| 84 | + println!("tx id: {}", tx.txid().to_string()); |
| 85 | + } else { |
| 86 | + println!("Insufficient Funds. Fund the wallet with the address above"); |
| 87 | + } |
| 88 | + |
| 89 | + let export = FullyNodedExport::export_wallet(&wallet, "exported wallet", true) |
| 90 | + .map_err(ToString::to_string) |
| 91 | + .map_err(bdk::Error::Generic) |
| 92 | + .unwrap(); |
| 93 | + |
| 94 | + println!("------\nWallet Backup: {}", export.to_string()); |
| 95 | +} |
0 commit comments