|
| 1 | +use bdk_wallet::bitcoin::Network; |
| 2 | +use bdk_wallet::rusqlite; |
| 3 | +use bdk_wallet::KeychainKind; |
| 4 | +use bdk_wallet::Wallet; |
| 5 | + |
| 6 | +// --8<-- [start:descriptors] |
| 7 | +// const EXTERNAL_DESCRIPTOR: &str = "[your external descriptor here ...]"; |
| 8 | +// const INTERNAL_DESCRIPTOR: &str = "[your internal descriptor here ...]"; |
| 9 | +// Example private descriptors |
| 10 | +const EXTERNAL_DESCRIPTOR: &str = "tr(tprv8ZgxMBicQKsPdJuLWWArdBsWjqDA3W5WoREnfdgKEcCQB1FMKfSoaFz9JHZU71HwXAqTsjHripkLM62kUQar14SDD8brsmhFKqVUPXGrZLc/86'/1'/0'/0/*)#fv8tutn2"; |
| 11 | +const INTERNAL_DESCRIPTOR: &str = "tr(tprv8ZgxMBicQKsPdJuLWWArdBsWjqDA3W5WoREnfdgKEcCQB1FMKfSoaFz9JHZU71HwXAqTsjHripkLM62kUQar14SDD8brsmhFKqVUPXGrZLc/86'/1'/0'/1/*)#ccz2p7rj"; |
| 12 | +// --8<-- [end:descriptors] |
| 13 | + |
| 14 | +fn main() -> Result<(), anyhow::Error> { |
| 15 | + // --8<-- [start:load] |
| 16 | + let network = Network::Signet; |
| 17 | + let file_path = "test_wallet.sqlite3"; |
| 18 | + let mut conn = rusqlite::Connection::open(file_path)?; |
| 19 | + |
| 20 | + let wallet_opt = Wallet::load() |
| 21 | + .descriptor(KeychainKind::External, Some(EXTERNAL_DESCRIPTOR)) |
| 22 | + .descriptor(KeychainKind::Internal, Some(INTERNAL_DESCRIPTOR)) |
| 23 | + .extract_keys() // only needed if using private key descriptors |
| 24 | + .check_network(network) |
| 25 | + .load_wallet(&mut conn)?; |
| 26 | + // --8<-- [end:load] |
| 27 | + |
| 28 | + // --8<-- [start:create] |
| 29 | + let mut wallet = match wallet_opt { |
| 30 | + Some(wallet) => { |
| 31 | + println!("Loaded existing wallet database."); |
| 32 | + wallet |
| 33 | + } |
| 34 | + None => { |
| 35 | + println!("Creating new wallet database."); |
| 36 | + Wallet::create(EXTERNAL_DESCRIPTOR, INTERNAL_DESCRIPTOR) |
| 37 | + .network(network) |
| 38 | + .create_wallet(&mut conn)? |
| 39 | + } |
| 40 | + }; |
| 41 | + // --8<-- [end:create] |
| 42 | + |
| 43 | + // --8<-- [start:address] |
| 44 | + // Reveal a new address from your external keychain |
| 45 | + let address = wallet.reveal_next_address(KeychainKind::External); |
| 46 | + wallet.persist(&mut conn)?; |
| 47 | + // Only share new address with user after successfully persisting wallet |
| 48 | + println!("Wallet address[{}]: {}", address.index, address.address); |
| 49 | + // --8<-- [end:address] |
| 50 | + |
| 51 | + Ok(()) |
| 52 | +} |
0 commit comments