From: Esraa Jbara Date: Sun, 17 Oct 2021 04:50:01 +0000 (+0300) Subject: update hello world tutorial X-Git-Url: http://internal-gitweb-vhost/script/%22https:/struct.EncoderStringWriter.html?a=commitdiff_plain;h=32c7b5a1ec9d2db5119014d43dfd3846df8a0cd4;p=bitcoindevkit.org update hello world tutorial --- diff --git a/docs/tutorials/hello-world.md b/docs/tutorials/hello-world.md index 0b9146c371..30facc72a7 100644 --- a/docs/tutorials/hello-world.md +++ b/docs/tutorials/hello-world.md @@ -3,8 +3,7 @@ ## Introduction This article should serve as a "getting started" guide for developers who are considering integrating BDK in their projects: it tries to introduce the reader to the basic concepts behind the library and some of its -modules and components that can be used to build a very simple functioning Bitcoin wallet. All the information written in this article are valid for the current `master` git branch, and should remain valid for the upcoming [`v0.2.0` release](https://github.com/bitcoindevkit/bdk/projects/1) -which is planned to be tagged pretty soon. +modules and components that can be used to build a very simple functioning Bitcoin wallet. All the information written in this article are valid for the current `master` git branch and latest version `bdk = 0.12.0`. ## Design Goals @@ -39,15 +38,18 @@ sign [PSBTs][PSBT], but with a greatly reduced attack surface because a sizable This is how an `OfflineWallet` can be created. Notice that we are using [`MemoryDatabase`][MemoryDatabase] as our `Database`. We'll get to that in a second. ```rust -use bdk::{Wallet, OfflineWallet}; -use bdk::database::MemoryDatabase; -use bdk::bitcoin::Network; +use bdk::{ + bitcoin::Network, + database::MemoryDatabase, + Wallet, + wallet::AddressIndex, +}; fn main() -> Result<(), Box> { let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/0'/0'/0/*)"; let internal_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/0'/0'/1/*)"; - let wallet: OfflineWallet<_> = Wallet::new_offline( + let wallet: Wallet<(), MemoryDatabase> = Wallet::new_offline( external_descriptor, Some(internal_descriptor), Network::Testnet, @@ -63,7 +65,8 @@ Once we have our `Wallet` instance we can generate a new address and print it ou ```rust // ... -println!("Generated Address: {}", wallet.get_new_address()?); +let address = wallet.get_address(AddressIndex::New)?; +println!("Generated Address: {}", address); ``` Building and running this code will print out: @@ -81,12 +84,17 @@ since that's available out of the box in BDK and is pretty fast. We can change our `Wallet` construction to look something like this: ```rust -use bdk::blockchain::ElectrumBlockchain; -use bdk::electrum_client::Client; +use bdk::{ + blockchain::ElectrumBlockchain, + bitcoin::Network, + database::MemoryDatabase, + electrum_client::Client, + Wallet, +}; // ... -let wallet = Wallet::new( +let wallet: Wallet = Wallet::new( external_descriptor, Some(internal_descriptor), Network::Testnet, @@ -123,21 +131,24 @@ Right now we will not get into details of all the available options in `TxBuilde how to build transactions. We'll come back to this in a future article. ```rust +use bdk::{bitcoin::Address, FeeRate}; use std::str::FromStr; -use bdk::bitcoin::Address; -use bdk::TxBuilder; - // ... let balance = wallet.get_balance()?; println!("Wallet balance in SAT: {}", balance); let faucet_address = Address::from_str("mkHS9ne12qx9pS9VojpwU5xtRd4T7X7ZUt")?; -let (unsigned_psbt, tx_details) = wallet.create_tx( - TxBuilder::with_recipients(vec![(faucet_address.script_pubkey(), balance / 2)]) - .enable_rbf(), -)?; + +let mut tx_builder = wallet.build_tx(); +tx_builder + .add_recipient(faucet_address.script_pubkey(), 0_900) + .fee_rate(FeeRate::from_sat_per_vb(5.0)) + .do_not_spend_change() + .enable_rbf(); +let (mut psbt, tx_details) = tx_builder.finish()? + println!("Transaction details: {:#?}", tx_details); ``` @@ -149,8 +160,11 @@ All that's left to do once we have our unsigned PSBT is to sign it: ```rust // ... -let (signed_psbt, tx_finalized) = wallet.sign(unsigned_psbt, None)?; -assert!(tx_finalized, "Tx has not been finalized"); +use bdk::SignOptions; + +let finalized = wallet.sign(&mut psbt, SignOptions::default())?; +assert!(finalized, "Tx has not been finalized"); +println!("Transaction Signed: {}", finalized); ``` And then broadcast it: @@ -158,7 +172,7 @@ And then broadcast it: ```rust // ... -let raw_transaction = signed_psbt.extract_tx(); +let raw_transaction = psbt.extract_tx(); let txid = wallet.broadcast(raw_transaction)?; println!( "Transaction sent! TXID: {txid}.\nExplorer URL: https://blockstream.info/testnet/tx/{txid}",