Crates.io | jitash-bdk |
lib.rs | jitash-bdk |
version | 0.28.1 |
source | src |
created_at | 2023-07-14 06:23:43.961802 |
updated_at | 2023-07-26 03:28:58.37292 |
description | A modern, lightweight, descriptor-based wallet library |
homepage | https://bitcoindevkit.org |
repository | https://github.com/bitcoindevkit/bdk |
max_upload_size | |
id | 915877 |
size | 1,290,661 |
A modern, lightweight, descriptor-based wallet library written in Rust!
The bdk
library aims to be the core building block for Bitcoin wallets of any kind.
use jitash_bdk::Wallet;
use jitash_bdk::database::MemoryDatabase;
use jitash_bdk::blockchain::ElectrumBlockchain;
use jitash_bdk::SyncOptions;
use jitash_bdk::electrum_client::Client;
use jitash_bdk::bitcoin::Network;
fn main() -> Result<(), jitash_bdk::Error> {
let blockchain = ElectrumBlockchain::from(Client::new("ssl://electrum.blockstream.info:60002")?);
let wallet = Wallet::new(
"wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)",
Some("wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/1/*)"),
Network::Testnet,
MemoryDatabase::default(),
)?;
wallet.sync(&blockchain, SyncOptions::default())?;
println!("Descriptor balance: {} SAT", wallet.get_balance()?);
Ok(())
}
use jitash_bdk::{Wallet, database::MemoryDatabase};
use jitash_bdk::wallet::AddressIndex::New;
use jitash_bdk::bitcoin::Network;
fn main() -> Result<(), jitash_bdk::Error> {
let wallet = Wallet::new(
"wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)",
Some("wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/1/*)"),
Network::Testnet,
MemoryDatabase::default(),
)?;
println!("Address #0: {}", wallet.get_address(New)?);
println!("Address #1: {}", wallet.get_address(New)?);
println!("Address #2: {}", wallet.get_address(New)?);
Ok(())
}
use jitash_bdk::{FeeRate, Wallet, SyncOptions};
use jitash_bdk::database::MemoryDatabase;
use jitash_bdk::blockchain::ElectrumBlockchain;
use jitash_bdk::electrum_client::Client;
use jitash_bdk::wallet::AddressIndex::New;
use base64;
use jitash_bdk::bitcoin::consensus::serialize;
use jitash_bdk::bitcoin::Network;
fn main() -> Result<(), jitash_bdk::Error> {
let blockchain = ElectrumBlockchain::from(Client::new("ssl://electrum.blockstream.info:60002")?);
let wallet = Wallet::new(
"wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)",
Some("wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/1/*)"),
Network::Testnet,
MemoryDatabase::default(),
)?;
wallet.sync(&blockchain, SyncOptions::default())?;
let send_to = wallet.get_address(New)?;
let (psbt, details) = {
let mut builder = wallet.build_tx();
builder
.add_recipient(send_to.script_pubkey(), 50_000)
.enable_rbf()
.do_not_spend_change()
.fee_rate(FeeRate::from_sat_per_vb(5.0));
builder.finish()?
};
println!("Transaction details: {:#?}", details);
println!("Unsigned PSBT: {}", base64::encode(&serialize(&psbt)));
Ok(())
}
use jitash_bdk::{Wallet, SignOptions, database::MemoryDatabase};
use base64;
use jitash_bdk::bitcoin::consensus::deserialize;
use jitash_bdk::bitcoin::Network;
fn main() -> Result<(), jitash_bdk::Error> {
let wallet = Wallet::new(
"wpkh([c258d2e4/84h/1h/0h]tprv8griRPhA7342zfRyB6CqeKF8CJDXYu5pgnj1cjL1u2ngKcJha5jjTRimG82ABzJQ4MQe71CV54xfn25BbhCNfEGGJZnxvCDQCd6JkbvxW6h/0/*)",
Some("wpkh([c258d2e4/84h/1h/0h]tprv8griRPhA7342zfRyB6CqeKF8CJDXYu5pgnj1cjL1u2ngKcJha5jjTRimG82ABzJQ4MQe71CV54xfn25BbhCNfEGGJZnxvCDQCd6JkbvxW6h/1/*)"),
Network::Testnet,
MemoryDatabase::default(),
)?;
let psbt = "...";
let mut psbt = deserialize(&base64::decode(psbt).unwrap())?;
let _finalized = wallet.sign(&mut psbt, SignOptions::default())?;
Ok(())
}
cargo test
Integration testing require testing features, for example:
cargo test --features test-electrum
The other options are test-esplora
, test-rpc
or test-rpc-legacy
which runs against an older version of Bitcoin Core.
Note that electrs
and bitcoind
binaries are automatically downloaded (on mac and linux), to specify you already have installed binaries you must use --no-default-features
and provide BITCOIND_EXE
and ELECTRS_EXE
as environment variables.
If you want to run this library under WASM you will probably have to add the following lines to you Cargo.toml
:
[dependencies]
getrandom = { version = "0.2", features = ["js"] }
This enables the rand
crate to work in environments where JavaScript is available. See this link to learn more.
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.