|
| 1 | +use bdk::bitcoin::secp256k1::Secp256k1; |
| 2 | +use bdk::bitcoin::util::bip32::ExtendedPrivKey; |
| 3 | +use bdk::bitcoin::Network; |
| 4 | +use bdk::blockchain::{ConfigurableBlockchain, LogProgress}; |
| 5 | + |
| 6 | +use bdk::template::Bip84; |
| 7 | +use bdk::wallet::export::WalletExport; |
| 8 | +use bdk::KeychainKind; |
| 9 | + |
| 10 | +use bdk::blockchain::rpc::{Auth, RpcBlockchain, RpcConfig}; |
| 11 | + |
| 12 | +use bdk::wallet::{wallet_name_from_descriptor, AddressIndex}; |
| 13 | +use bdk::Wallet; |
| 14 | + |
| 15 | +use bdk::sled::{self, Tree}; |
| 16 | + |
| 17 | +use crate::wallet_common::{build_signed_tx, mnemonic_to_xprv}; |
| 18 | + |
| 19 | +fn load_or_create_wallet( |
| 20 | + network: &Network, |
| 21 | + rpc_url: &str, |
| 22 | + username: &str, |
| 23 | + password: &str, |
| 24 | + xpriv: &ExtendedPrivKey, |
| 25 | +) -> Wallet<RpcBlockchain, Tree> { |
| 26 | + let auth = Auth::UserPass { |
| 27 | + username: username.to_string(), |
| 28 | + password: password.to_string(), |
| 29 | + }; |
| 30 | + |
| 31 | + // Use deterministic wallet name derived from descriptor |
| 32 | + let wallet_name = wallet_name_from_descriptor( |
| 33 | + Bip84(*xpriv, KeychainKind::External), |
| 34 | + Some(Bip84(*xpriv, KeychainKind::Internal)), |
| 35 | + *network, |
| 36 | + &Secp256k1::new(), |
| 37 | + ) |
| 38 | + .unwrap(); |
| 39 | + |
| 40 | + println!("wallet name: {:?}", wallet_name); |
| 41 | + |
| 42 | + // Setup the RPC configuration |
| 43 | + let rpc_config = RpcConfig { |
| 44 | + url: rpc_url.to_string(), |
| 45 | + auth, |
| 46 | + network: *network, |
| 47 | + wallet_name: wallet_name.clone(), |
| 48 | + skip_blocks: Some(70_000), // Some(block_count) |
| 49 | + }; |
| 50 | + |
| 51 | + // Use the above configuration to create a RPC blockchain backend |
| 52 | + let blockchain = RpcBlockchain::from_config(&rpc_config).unwrap(); |
| 53 | + |
| 54 | + // Create the datadir to store wallet data |
| 55 | + let mut datadir = dirs_next::home_dir().unwrap(); |
| 56 | + datadir.push(".bdk-wallets"); |
| 57 | + datadir.push(wallet_name.clone()); |
| 58 | + let database = sled::open(datadir).unwrap(); |
| 59 | + let db_tree = database.open_tree(wallet_name).unwrap(); |
| 60 | + |
| 61 | + // Combine everything and finally create the BDK wallet structure |
| 62 | + let wallet = Wallet::new( |
| 63 | + Bip84(*xpriv, KeychainKind::External), |
| 64 | + Some(Bip84(*xpriv, KeychainKind::Internal)), |
| 65 | + *network, |
| 66 | + db_tree, |
| 67 | + blockchain, |
| 68 | + ) |
| 69 | + .unwrap(); |
| 70 | + |
| 71 | + // Sync the wallet |
| 72 | + //let sync_result = wallet.sync(LogProgress, None); |
| 73 | + wallet.sync(LogProgress, None).unwrap(); |
| 74 | + |
| 75 | + wallet |
| 76 | +} |
| 77 | + |
| 78 | +pub fn run(network: Network, rpc_url: &str, username: &str, password: &str, mnemonic_words: &str) { |
| 79 | + let xpriv = mnemonic_to_xprv(&network, mnemonic_words); |
| 80 | + |
| 81 | + let wallet = load_or_create_wallet(&network, rpc_url, username, password, &xpriv); |
| 82 | + |
| 83 | + println!( |
| 84 | + "mnemonic: {}\n\nrecv desc (pub key): {:#?}\n\nchng desc (pub key): {:#?}", |
| 85 | + mnemonic_words, |
| 86 | + wallet |
| 87 | + .get_descriptor_for_keychain(KeychainKind::External) |
| 88 | + .to_string(), |
| 89 | + wallet |
| 90 | + .get_descriptor_for_keychain(KeychainKind::Internal) |
| 91 | + .to_string() |
| 92 | + ); |
| 93 | + |
| 94 | + // Fetch a fresh address to receive coins |
| 95 | + let address = wallet.get_address(AddressIndex::New).unwrap().address; |
| 96 | + |
| 97 | + println!("new address: {}", address); |
| 98 | + |
| 99 | + let balance = wallet.get_balance().unwrap(); |
| 100 | + |
| 101 | + println!("balance: {}", balance); |
| 102 | + |
| 103 | + if balance > 5500 { |
| 104 | + let recipient_address = "tb1q766f5v4h9ml8dh99ev5ertg2ysrjz2kkuzq8up"; |
| 105 | + |
| 106 | + let tx = build_signed_tx(&wallet, recipient_address, 5000); |
| 107 | + |
| 108 | + // Broadcast the transaction |
| 109 | + let tx_id = wallet.broadcast(&tx).unwrap(); |
| 110 | + |
| 111 | + println!("tx id: {}", tx_id.to_string()); |
| 112 | + } else { |
| 113 | + println!("Insufficient Funds. Fund the wallet with the address above"); |
| 114 | + } |
| 115 | + |
| 116 | + let export = WalletExport::export_wallet(&wallet, "exported wallet", true) |
| 117 | + .map_err(ToString::to_string) |
| 118 | + .map_err(bdk::Error::Generic) |
| 119 | + .unwrap(); |
| 120 | + |
| 121 | + println!("------\nWallet Backup: {}", export.to_string()); |
| 122 | +} |
0 commit comments