-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: full wallet persisted #80
Merged
thunderbiscuit
merged 2 commits into
bitcoindevkit:master
from
riverKanies:feat/persistence-in-fullwallet
Oct 30, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ dist/ | |
drafts/ | ||
target/ | ||
*Cargo.lock | ||
*.sqlite3 | ||
|
||
# MacOS | ||
*.DS_Store | ||
|
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
4 changes: 2 additions & 2 deletions
4
examples/rust/transaction/Cargo.toml → examples/rust/full-wallet/Cargo.toml
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,9 +1,9 @@ | ||
[package] | ||
name = "transaction" | ||
name = "full-wallet" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
bdk_wallet = { version = "=1.0.0-beta.5", features = ["keys-bip39"] } | ||
bdk_wallet = { version = "=1.0.0-beta.5", features = ["keys-bip39", "rusqlite"] } | ||
bdk_esplora = { version = "=0.19.0", features = ["blocking"] } | ||
anyhow = "1" |
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,100 @@ | ||
use bdk_wallet::bitcoin::Address; | ||
use bdk_wallet::AddressInfo; | ||
use bdk_wallet::KeychainKind; | ||
use bdk_wallet::bitcoin::{Network, Amount}; | ||
use bdk_wallet::SignOptions; | ||
use bdk_wallet::Wallet; | ||
use bdk_esplora::EsploraExt; | ||
use bdk_esplora::esplora_client::Builder; | ||
use bdk_esplora::esplora_client; | ||
use bdk_wallet::chain::spk_client::{FullScanRequestBuilder, FullScanResult, SyncRequestBuilder, SyncResult}; | ||
use std::str::FromStr; | ||
use bdk_wallet::rusqlite::Connection; | ||
|
||
const DB_PATH: &str = "full-wallet.sqlite3"; | ||
const STOP_GAP: usize = 50; | ||
const PARALLEL_REQUESTS: usize = 1; | ||
|
||
// --8<-- [start:descriptors] | ||
const DESCRIPTOR_PRIVATE_EXTERNAL: &str = "[your private external descriptor here ...]"; | ||
const DESCRIPTOR_PRIVATE_INTERNAL: &str = "[your private internal descriptor here ...]"; | ||
// Example private descriptors | ||
// const DESCRIPTOR_PRIVATE_EXTERNAL: &str = "tr(tprv8ZgxMBicQKsPdJuLWWArdBsWjqDA3W5WoREnfdgKEcCQB1FMKfSoaFz9JHZU71HwXAqTsjHripkLM62kUQar14SDD8brsmhFKqVUPXGrZLc/86'/1'/0'/0/*)#fv8tutn2"; | ||
// const DESCRIPTOR_PRIVATE_INTERNAL: &str = "tr(tprv8ZgxMBicQKsPdJuLWWArdBsWjqDA3W5WoREnfdgKEcCQB1FMKfSoaFz9JHZU71HwXAqTsjHripkLM62kUQar14SDD8brsmhFKqVUPXGrZLc/86'/1'/0'/1/*)#ccz2p7rj"; | ||
// --8<-- [end:descriptors] | ||
|
||
fn main() -> Result<(), anyhow::Error> { | ||
// --8<-- [start:persist] | ||
let mut conn = Connection::open(DB_PATH)?; | ||
|
||
let wallet_opt = Wallet::load() | ||
.descriptor(KeychainKind::External, Some(DESCRIPTOR_PRIVATE_EXTERNAL)) | ||
.descriptor(KeychainKind::Internal, Some(DESCRIPTOR_PRIVATE_INTERNAL)) | ||
.extract_keys() | ||
.check_network(Network::Signet) | ||
.load_wallet(&mut conn)?; | ||
|
||
let (mut wallet, is_new_wallet) = if let Some(loaded_wallet) = wallet_opt { | ||
(loaded_wallet, false) | ||
} else { | ||
(Wallet::create(DESCRIPTOR_PRIVATE_EXTERNAL, DESCRIPTOR_PRIVATE_INTERNAL) | ||
.network(Network::Signet) | ||
.create_wallet(&mut conn)?, true) | ||
}; | ||
// --8<-- [end:persist] | ||
|
||
// --8<-- [start:scan] | ||
let client: esplora_client::BlockingClient = Builder::new("https://mutinynet.com/api").build_blocking(); | ||
// Sync the wallet | ||
if is_new_wallet { | ||
// Perform a full scan | ||
println!("Performing full scan..."); | ||
let full_scan_request: FullScanRequestBuilder<KeychainKind> = wallet.start_full_scan(); | ||
let update: FullScanResult<KeychainKind> = client.full_scan(full_scan_request, STOP_GAP, PARALLEL_REQUESTS)?; | ||
wallet.apply_update(update).unwrap(); | ||
} else { | ||
// Perform a regular sync | ||
println!("Performing regular sync..."); | ||
let sync_request: SyncRequestBuilder<(KeychainKind, u32)> = wallet.start_sync_with_revealed_spks(); | ||
let update: SyncResult = client.sync(sync_request, PARALLEL_REQUESTS)?; | ||
wallet.apply_update(update).unwrap(); | ||
}; | ||
wallet.persist(&mut conn)?; | ||
// --8<-- [end:scan] | ||
|
||
// --8<-- [start:address] | ||
// Reveal a new address from your external keychain | ||
let address: AddressInfo = wallet.reveal_next_address(KeychainKind::External); | ||
println!("Generated address {} at index {}", address.address, address.index); | ||
wallet.persist(&mut conn)?; | ||
// --8<-- [end:address] | ||
|
||
let balance = wallet.balance(); | ||
println!("Wallet balance: {} sat", balance.total().to_sat()); | ||
|
||
// --8<-- [start:faucet] | ||
// Use the Mutinynet faucet return address | ||
let address = Address::from_str("tb1qd28npep0s8frcm3y7dxqajkcy2m40eysplyr9v") | ||
.unwrap() | ||
.require_network(Network::Signet) | ||
.unwrap(); | ||
|
||
let send_amount: Amount = Amount::from_sat(5000); | ||
// --8<-- [end:faucet] | ||
|
||
// --8<-- [start:transaction] | ||
// Transaction Logic | ||
let mut tx_builder = wallet.build_tx(); | ||
tx_builder.add_recipient(address.script_pubkey(), send_amount); | ||
|
||
let mut psbt = tx_builder.finish()?; | ||
let finalized = wallet.sign(&mut psbt, SignOptions::default())?; | ||
assert!(finalized); | ||
|
||
let tx = psbt.extract_tx()?; | ||
client.broadcast(&tx)?; | ||
println!("Tx broadcasted! Txid: {}", tx.compute_txid()); | ||
// --8<-- [end:transaction] | ||
|
||
Ok(()) | ||
} |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you feel about adding a check on the balance here instead of letting it error out?
Something like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the error it throws already says "Error: Insufficient funds: 0 sat available of 5042 sat needed", it seems quite good, so I saw no reason to add extra code to the example