Compare commits
21 Commits
dependabot
...
v1.0.0-alp
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9efaead8f1 | ||
|
|
1ff9d5ce8f | ||
|
|
8694624bd5 | ||
|
|
003271117c | ||
|
|
f6418ba911 | ||
|
|
028caa9f8c | ||
|
|
d71829914a | ||
|
|
a1d34afa24 | ||
|
|
9cc03324f4 | ||
|
|
de54e710ed | ||
|
|
95d34854f4 | ||
|
|
7eff024213 | ||
|
|
1def76f1f1 | ||
|
|
c9467dcbb2 | ||
|
|
bc796f412a | ||
|
|
4fd539b647 | ||
|
|
01698ae5ec | ||
|
|
f4863c6314 | ||
|
|
b5612f269a | ||
|
|
e7fbc8bcf3 | ||
|
|
2251b8d416 |
2
.github/workflows/code_coverage.yml
vendored
2
.github/workflows/code_coverage.yml
vendored
@@ -51,7 +51,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
- name: Upload artifact
|
- name: Upload artifact
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v2
|
||||||
with:
|
with:
|
||||||
name: coverage-report
|
name: coverage-report
|
||||||
path: coverage-report.html
|
path: coverage-report.html
|
||||||
|
|||||||
6
.github/workflows/cont_integration.yml
vendored
6
.github/workflows/cont_integration.yml
vendored
@@ -33,7 +33,7 @@ jobs:
|
|||||||
cargo update -p zip --precise "0.6.2"
|
cargo update -p zip --precise "0.6.2"
|
||||||
cargo update -p time --precise "0.3.20"
|
cargo update -p time --precise "0.3.20"
|
||||||
cargo update -p jobserver --precise "0.1.26"
|
cargo update -p jobserver --precise "0.1.26"
|
||||||
cargo update -p reqwest --precise "0.11.19"
|
cargo update -p home --precise "0.5.5"
|
||||||
- name: Build
|
- name: Build
|
||||||
run: cargo build ${{ matrix.features }}
|
run: cargo build ${{ matrix.features }}
|
||||||
- name: Test
|
- name: Test
|
||||||
@@ -118,9 +118,7 @@ jobs:
|
|||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v1
|
||||||
- uses: actions-rs/toolchain@v1
|
- uses: actions-rs/toolchain@v1
|
||||||
with:
|
with:
|
||||||
# we pin clippy instead of using "stable" so that our CI doesn't break
|
toolchain: stable
|
||||||
# at each new cargo release
|
|
||||||
toolchain: "1.67.0"
|
|
||||||
components: clippy
|
components: clippy
|
||||||
override: true
|
override: true
|
||||||
- name: Rust Cache
|
- name: Rust Cache
|
||||||
|
|||||||
2
.github/workflows/nightly_docs.yml
vendored
2
.github/workflows/nightly_docs.yml
vendored
@@ -22,7 +22,7 @@ jobs:
|
|||||||
env:
|
env:
|
||||||
RUSTDOCFLAGS: '--cfg docsrs -Dwarnings'
|
RUSTDOCFLAGS: '--cfg docsrs -Dwarnings'
|
||||||
- name: Upload artifact
|
- name: Upload artifact
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v2
|
||||||
with:
|
with:
|
||||||
name: built-docs
|
name: built-docs
|
||||||
path: ./target/doc/*
|
path: ./target/doc/*
|
||||||
|
|||||||
@@ -65,14 +65,14 @@ This library should compile with any combination of features with Rust 1.63.0.
|
|||||||
To build with the MSRV you will need to pin dependencies as follows:
|
To build with the MSRV you will need to pin dependencies as follows:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
# zip 0.6.3 has MSRV 1.64.0+
|
# zip 0.6.3 has MSRV 1.64.0
|
||||||
cargo update -p zip --precise "0.6.2"
|
cargo update -p zip --precise "0.6.2"
|
||||||
# time 0.3.21 has MSRV 1.65.0+
|
# time 0.3.21 has MSRV 1.65.0
|
||||||
cargo update -p time --precise "0.3.20"
|
cargo update -p time --precise "0.3.20"
|
||||||
# jobserver 0.1.27 has MSRV 1.66.0
|
# jobserver 0.1.27 has MSRV 1.66.0
|
||||||
cargo update -p jobserver --precise "0.1.26"
|
cargo update -p jobserver --precise "0.1.26"
|
||||||
# reqwest 0.11.20 has MSRV > 1.63.0+
|
# home 0.5.9 has MSRV 1.70.0
|
||||||
cargo update -p reqwest --precise "0.11.19"
|
cargo update -p home --precise "0.5.5"
|
||||||
```
|
```
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
msrv="1.57.0"
|
msrv="1.63.0"
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "bdk"
|
name = "bdk"
|
||||||
homepage = "https://bitcoindevkit.org"
|
homepage = "https://bitcoindevkit.org"
|
||||||
version = "1.0.0-alpha.2"
|
version = "1.0.0-alpha.3"
|
||||||
repository = "https://github.com/bitcoindevkit/bdk"
|
repository = "https://github.com/bitcoindevkit/bdk"
|
||||||
documentation = "https://docs.rs/bdk"
|
documentation = "https://docs.rs/bdk"
|
||||||
description = "A modern, lightweight, descriptor-based wallet library"
|
description = "A modern, lightweight, descriptor-based wallet library"
|
||||||
@@ -18,11 +18,11 @@ miniscript = { version = "10.0.0", features = ["serde"], default-features = fals
|
|||||||
bitcoin = { version = "0.30.0", features = ["serde", "base64", "rand-std"], default-features = false }
|
bitcoin = { version = "0.30.0", features = ["serde", "base64", "rand-std"], default-features = false }
|
||||||
serde = { version = "^1.0", features = ["derive"] }
|
serde = { version = "^1.0", features = ["derive"] }
|
||||||
serde_json = { version = "^1.0" }
|
serde_json = { version = "^1.0" }
|
||||||
bdk_chain = { path = "../chain", version = "0.6.0", features = ["miniscript", "serde"], default-features = false }
|
bdk_chain = { path = "../chain", version = "0.7.0", features = ["miniscript", "serde"], default-features = false }
|
||||||
|
|
||||||
# Optional dependencies
|
# Optional dependencies
|
||||||
hwi = { version = "0.7.0", optional = true, features = [ "miniscript"] }
|
hwi = { version = "0.7.0", optional = true, features = [ "miniscript"] }
|
||||||
bip39 = { version = "1.0.1", optional = true }
|
bip39 = { version = "2.0", optional = true }
|
||||||
|
|
||||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||||
getrandom = "0.2"
|
getrandom = "0.2"
|
||||||
|
|||||||
@@ -575,7 +575,7 @@ mod test {
|
|||||||
|
|
||||||
if let ExtendedDescriptor::Pkh(pkh) = xdesc.0 {
|
if let ExtendedDescriptor::Pkh(pkh) = xdesc.0 {
|
||||||
let path: Vec<ChildNumber> = pkh.into_inner().full_derivation_path().unwrap().into();
|
let path: Vec<ChildNumber> = pkh.into_inner().full_derivation_path().unwrap().into();
|
||||||
let purpose = path.get(0).unwrap();
|
let purpose = path.first().unwrap();
|
||||||
assert_matches!(purpose, Hardened { index: 44 });
|
assert_matches!(purpose, Hardened { index: 44 });
|
||||||
let coin_type = path.get(1).unwrap();
|
let coin_type = path.get(1).unwrap();
|
||||||
assert_matches!(coin_type, Hardened { index: 0 });
|
assert_matches!(coin_type, Hardened { index: 0 });
|
||||||
@@ -589,7 +589,7 @@ mod test {
|
|||||||
|
|
||||||
if let ExtendedDescriptor::Pkh(pkh) = tdesc.0 {
|
if let ExtendedDescriptor::Pkh(pkh) = tdesc.0 {
|
||||||
let path: Vec<ChildNumber> = pkh.into_inner().full_derivation_path().unwrap().into();
|
let path: Vec<ChildNumber> = pkh.into_inner().full_derivation_path().unwrap().into();
|
||||||
let purpose = path.get(0).unwrap();
|
let purpose = path.first().unwrap();
|
||||||
assert_matches!(purpose, Hardened { index: 44 });
|
assert_matches!(purpose, Hardened { index: 44 });
|
||||||
let coin_type = path.get(1).unwrap();
|
let coin_type = path.get(1).unwrap();
|
||||||
assert_matches!(coin_type, Hardened { index: 1 });
|
assert_matches!(coin_type, Hardened { index: 1 });
|
||||||
|
|||||||
@@ -237,6 +237,7 @@ impl Wallet {
|
|||||||
network: Network,
|
network: Network,
|
||||||
) -> Result<Self, DescriptorError> {
|
) -> Result<Self, DescriptorError> {
|
||||||
Self::new(descriptor, change_descriptor, (), network).map_err(|e| match e {
|
Self::new(descriptor, change_descriptor, (), network).map_err(|e| match e {
|
||||||
|
NewError::NonEmptyDatabase => unreachable!("mock-database cannot have data"),
|
||||||
NewError::Descriptor(e) => e,
|
NewError::Descriptor(e) => e,
|
||||||
NewError::Write(_) => unreachable!("mock-write must always succeed"),
|
NewError::Write(_) => unreachable!("mock-write must always succeed"),
|
||||||
})
|
})
|
||||||
@@ -251,6 +252,7 @@ impl Wallet {
|
|||||||
) -> Result<Self, crate::descriptor::DescriptorError> {
|
) -> Result<Self, crate::descriptor::DescriptorError> {
|
||||||
Self::new_with_genesis_hash(descriptor, change_descriptor, (), network, genesis_hash)
|
Self::new_with_genesis_hash(descriptor, change_descriptor, (), network, genesis_hash)
|
||||||
.map_err(|e| match e {
|
.map_err(|e| match e {
|
||||||
|
NewError::NonEmptyDatabase => unreachable!("mock-database cannot have data"),
|
||||||
NewError::Descriptor(e) => e,
|
NewError::Descriptor(e) => e,
|
||||||
NewError::Write(_) => unreachable!("mock-write must always succeed"),
|
NewError::Write(_) => unreachable!("mock-write must always succeed"),
|
||||||
})
|
})
|
||||||
@@ -288,6 +290,8 @@ where
|
|||||||
/// [`new_with_genesis_hash`]: Wallet::new_with_genesis_hash
|
/// [`new_with_genesis_hash`]: Wallet::new_with_genesis_hash
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum NewError<W> {
|
pub enum NewError<W> {
|
||||||
|
/// Database already has data.
|
||||||
|
NonEmptyDatabase,
|
||||||
/// There was problem with the passed-in descriptor(s).
|
/// There was problem with the passed-in descriptor(s).
|
||||||
Descriptor(crate::descriptor::DescriptorError),
|
Descriptor(crate::descriptor::DescriptorError),
|
||||||
/// We were unable to write the wallet's data to the persistence backend.
|
/// We were unable to write the wallet's data to the persistence backend.
|
||||||
@@ -300,6 +304,10 @@ where
|
|||||||
{
|
{
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
|
NewError::NonEmptyDatabase => write!(
|
||||||
|
f,
|
||||||
|
"database already has data - use `load` or `new_or_load` methods instead"
|
||||||
|
),
|
||||||
NewError::Descriptor(e) => e.fmt(f),
|
NewError::Descriptor(e) => e.fmt(f),
|
||||||
NewError::Write(e) => e.fmt(f),
|
NewError::Write(e) => e.fmt(f),
|
||||||
}
|
}
|
||||||
@@ -348,7 +356,7 @@ where
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl<L> std::error::Error for LoadError<L> where L: core::fmt::Display + core::fmt::Debug {}
|
impl<L> std::error::Error for LoadError<L> where L: core::fmt::Display + core::fmt::Debug {}
|
||||||
|
|
||||||
/// Error type for when we try load a [`Wallet`] from persistence and creating it if non-existant.
|
/// Error type for when we try load a [`Wallet`] from persistence and creating it if non-existent.
|
||||||
///
|
///
|
||||||
/// Methods [`new_or_load`] and [`new_or_load_with_genesis_hash`] may return this error.
|
/// Methods [`new_or_load`] and [`new_or_load_with_genesis_hash`] may return this error.
|
||||||
///
|
///
|
||||||
@@ -446,13 +454,18 @@ impl<D> Wallet<D> {
|
|||||||
pub fn new_with_genesis_hash<E: IntoWalletDescriptor>(
|
pub fn new_with_genesis_hash<E: IntoWalletDescriptor>(
|
||||||
descriptor: E,
|
descriptor: E,
|
||||||
change_descriptor: Option<E>,
|
change_descriptor: Option<E>,
|
||||||
db: D,
|
mut db: D,
|
||||||
network: Network,
|
network: Network,
|
||||||
genesis_hash: BlockHash,
|
genesis_hash: BlockHash,
|
||||||
) -> Result<Self, NewError<D::WriteError>>
|
) -> Result<Self, NewError<D::WriteError>>
|
||||||
where
|
where
|
||||||
D: PersistBackend<ChangeSet>,
|
D: PersistBackend<ChangeSet>,
|
||||||
{
|
{
|
||||||
|
if let Ok(changeset) = db.load_from_persistence() {
|
||||||
|
if changeset.is_some() {
|
||||||
|
return Err(NewError::NonEmptyDatabase);
|
||||||
|
}
|
||||||
|
}
|
||||||
let secp = Secp256k1::new();
|
let secp = Secp256k1::new();
|
||||||
let (chain, chain_changeset) = LocalChain::from_genesis_hash(genesis_hash);
|
let (chain, chain_changeset) = LocalChain::from_genesis_hash(genesis_hash);
|
||||||
let mut index = KeychainTxOutIndex::<KeychainKind>::default();
|
let mut index = KeychainTxOutIndex::<KeychainKind>::default();
|
||||||
@@ -517,7 +530,9 @@ impl<D> Wallet<D> {
|
|||||||
create_signers(&mut index, &secp, descriptor, change_descriptor, network)
|
create_signers(&mut index, &secp, descriptor, change_descriptor, network)
|
||||||
.map_err(LoadError::Descriptor)?;
|
.map_err(LoadError::Descriptor)?;
|
||||||
|
|
||||||
let indexed_graph = IndexedTxGraph::new(index);
|
let mut indexed_graph = IndexedTxGraph::new(index);
|
||||||
|
indexed_graph.apply_changeset(changeset.indexed_tx_graph);
|
||||||
|
|
||||||
let persist = Persist::new(db);
|
let persist = Persist::new(db);
|
||||||
|
|
||||||
Ok(Wallet {
|
Ok(Wallet {
|
||||||
@@ -613,6 +628,9 @@ impl<D> Wallet<D> {
|
|||||||
genesis_hash,
|
genesis_hash,
|
||||||
)
|
)
|
||||||
.map_err(|e| match e {
|
.map_err(|e| match e {
|
||||||
|
NewError::NonEmptyDatabase => {
|
||||||
|
unreachable!("database is already checked to have no data")
|
||||||
|
}
|
||||||
NewError::Descriptor(e) => NewOrLoadError::Descriptor(e),
|
NewError::Descriptor(e) => NewOrLoadError::Descriptor(e),
|
||||||
NewError::Write(e) => NewOrLoadError::Write(e),
|
NewError::Write(e) => NewOrLoadError::Write(e),
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -812,9 +812,10 @@ pub struct SignOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Customize which taproot script-path leaves the signer should sign.
|
/// Customize which taproot script-path leaves the signer should sign.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Default, Debug, Clone, PartialEq, Eq)]
|
||||||
pub enum TapLeavesOptions {
|
pub enum TapLeavesOptions {
|
||||||
/// The signer will sign all the leaves it has a key for.
|
/// The signer will sign all the leaves it has a key for.
|
||||||
|
#[default]
|
||||||
All,
|
All,
|
||||||
/// The signer won't sign leaves other than the ones specified. Note that it could still ignore
|
/// The signer won't sign leaves other than the ones specified. Note that it could still ignore
|
||||||
/// some of the specified leaves, if it doesn't have the right key to sign them.
|
/// some of the specified leaves, if it doesn't have the right key to sign them.
|
||||||
@@ -825,12 +826,6 @@ pub enum TapLeavesOptions {
|
|||||||
None,
|
None,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for TapLeavesOptions {
|
|
||||||
fn default() -> Self {
|
|
||||||
TapLeavesOptions::All
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::derivable_impls)]
|
#[allow(clippy::derivable_impls)]
|
||||||
impl Default for SignOptions {
|
impl Default for SignOptions {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
|
|||||||
@@ -811,9 +811,10 @@ impl<'a, D> TxBuilder<'a, D, DefaultCoinSelectionAlgorithm, BumpFee> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Ordering of the transaction's inputs and outputs
|
/// Ordering of the transaction's inputs and outputs
|
||||||
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Clone, Copy)]
|
#[derive(Default, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Clone, Copy)]
|
||||||
pub enum TxOrdering {
|
pub enum TxOrdering {
|
||||||
/// Randomized (default)
|
/// Randomized (default)
|
||||||
|
#[default]
|
||||||
Shuffle,
|
Shuffle,
|
||||||
/// Unchanged
|
/// Unchanged
|
||||||
Untouched,
|
Untouched,
|
||||||
@@ -821,12 +822,6 @@ pub enum TxOrdering {
|
|||||||
Bip69Lexicographic,
|
Bip69Lexicographic,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for TxOrdering {
|
|
||||||
fn default() -> Self {
|
|
||||||
TxOrdering::Shuffle
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TxOrdering {
|
impl TxOrdering {
|
||||||
/// Sort transaction inputs and outputs by [`TxOrdering`] variant
|
/// Sort transaction inputs and outputs by [`TxOrdering`] variant
|
||||||
pub fn sort_tx(&self, tx: &mut Transaction) {
|
pub fn sort_tx(&self, tx: &mut Transaction) {
|
||||||
@@ -880,9 +875,10 @@ impl RbfValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Policy regarding the use of change outputs when creating a transaction
|
/// Policy regarding the use of change outputs when creating a transaction
|
||||||
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Clone, Copy)]
|
#[derive(Default, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Clone, Copy)]
|
||||||
pub enum ChangeSpendPolicy {
|
pub enum ChangeSpendPolicy {
|
||||||
/// Use both change and non-change outputs (default)
|
/// Use both change and non-change outputs (default)
|
||||||
|
#[default]
|
||||||
ChangeAllowed,
|
ChangeAllowed,
|
||||||
/// Only use change outputs (see [`TxBuilder::only_spend_change`])
|
/// Only use change outputs (see [`TxBuilder::only_spend_change`])
|
||||||
OnlyChange,
|
OnlyChange,
|
||||||
@@ -890,12 +886,6 @@ pub enum ChangeSpendPolicy {
|
|||||||
ChangeForbidden,
|
ChangeForbidden,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ChangeSpendPolicy {
|
|
||||||
fn default() -> Self {
|
|
||||||
ChangeSpendPolicy::ChangeAllowed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ChangeSpendPolicy {
|
impl ChangeSpendPolicy {
|
||||||
pub(crate) fn is_satisfied_by(&self, utxo: &LocalOutput) -> bool {
|
pub(crate) fn is_satisfied_by(&self, utxo: &LocalOutput) -> bool {
|
||||||
match self {
|
match self {
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ use bdk::signer::{SignOptions, SignerError};
|
|||||||
use bdk::wallet::coin_selection::{self, LargestFirstCoinSelection};
|
use bdk::wallet::coin_selection::{self, LargestFirstCoinSelection};
|
||||||
use bdk::wallet::error::CreateTxError;
|
use bdk::wallet::error::CreateTxError;
|
||||||
use bdk::wallet::tx_builder::AddForeignUtxoError;
|
use bdk::wallet::tx_builder::AddForeignUtxoError;
|
||||||
use bdk::wallet::AddressIndex::*;
|
|
||||||
use bdk::wallet::{AddressIndex, AddressInfo, Balance, Wallet};
|
use bdk::wallet::{AddressIndex, AddressInfo, Balance, Wallet};
|
||||||
|
use bdk::wallet::{AddressIndex::*, NewError};
|
||||||
use bdk::{FeeRate, KeychainKind};
|
use bdk::{FeeRate, KeychainKind};
|
||||||
use bdk_chain::COINBASE_MATURITY;
|
use bdk_chain::COINBASE_MATURITY;
|
||||||
use bdk_chain::{BlockId, ConfirmationTime};
|
use bdk_chain::{BlockId, ConfirmationTime};
|
||||||
@@ -71,19 +71,33 @@ fn load_recovers_wallet() {
|
|||||||
let file_path = temp_dir.path().join("store.db");
|
let file_path = temp_dir.path().join("store.db");
|
||||||
|
|
||||||
// create new wallet
|
// create new wallet
|
||||||
let wallet_keychains = {
|
let wallet_spk_index = {
|
||||||
let db = bdk_file_store::Store::create_new(DB_MAGIC, &file_path).expect("must create db");
|
let db = bdk_file_store::Store::create_new(DB_MAGIC, &file_path).expect("must create db");
|
||||||
let wallet =
|
let mut wallet = Wallet::new(get_test_tr_single_sig_xprv(), None, db, Network::Testnet)
|
||||||
Wallet::new(get_test_wpkh(), None, db, Network::Testnet).expect("must init wallet");
|
.expect("must init wallet");
|
||||||
wallet.keychains().clone()
|
|
||||||
|
wallet.try_get_address(New).unwrap();
|
||||||
|
wallet.spk_index().clone()
|
||||||
};
|
};
|
||||||
|
|
||||||
// recover wallet
|
// recover wallet
|
||||||
{
|
{
|
||||||
let db = bdk_file_store::Store::open(DB_MAGIC, &file_path).expect("must recover db");
|
let db = bdk_file_store::Store::open(DB_MAGIC, &file_path).expect("must recover db");
|
||||||
let wallet = Wallet::load(get_test_wpkh(), None, db).expect("must recover wallet");
|
let wallet =
|
||||||
|
Wallet::load(get_test_tr_single_sig_xprv(), None, db).expect("must recover wallet");
|
||||||
assert_eq!(wallet.network(), Network::Testnet);
|
assert_eq!(wallet.network(), Network::Testnet);
|
||||||
assert_eq!(wallet.spk_index().keychains(), &wallet_keychains);
|
assert_eq!(wallet.spk_index().keychains(), wallet_spk_index.keychains());
|
||||||
|
assert_eq!(
|
||||||
|
wallet.spk_index().last_revealed_indices(),
|
||||||
|
wallet_spk_index.last_revealed_indices()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// `new` can only be called on empty db
|
||||||
|
{
|
||||||
|
let db = bdk_file_store::Store::open(DB_MAGIC, &file_path).expect("must recover db");
|
||||||
|
let result = Wallet::new(get_test_tr_single_sig_xprv(), None, db, Network::Testnet);
|
||||||
|
assert!(matches!(result, Err(NewError::NonEmptyDatabase)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,7 +106,7 @@ fn new_or_load() {
|
|||||||
let temp_dir = tempfile::tempdir().expect("must create tempdir");
|
let temp_dir = tempfile::tempdir().expect("must create tempdir");
|
||||||
let file_path = temp_dir.path().join("store.db");
|
let file_path = temp_dir.path().join("store.db");
|
||||||
|
|
||||||
// init wallet when non-existant
|
// init wallet when non-existent
|
||||||
let wallet_keychains = {
|
let wallet_keychains = {
|
||||||
let db = bdk_file_store::Store::open_or_create_new(DB_MAGIC, &file_path)
|
let db = bdk_file_store::Store::open_or_create_new(DB_MAGIC, &file_path)
|
||||||
.expect("must create db");
|
.expect("must create db");
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "bdk_bitcoind_rpc"
|
name = "bdk_bitcoind_rpc"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.63"
|
rust-version = "1.63"
|
||||||
homepage = "https://bitcoindevkit.org"
|
homepage = "https://bitcoindevkit.org"
|
||||||
@@ -16,7 +16,7 @@ readme = "README.md"
|
|||||||
# For no-std, remember to enable the bitcoin/no-std feature
|
# For no-std, remember to enable the bitcoin/no-std feature
|
||||||
bitcoin = { version = "0.30", default-features = false }
|
bitcoin = { version = "0.30", default-features = false }
|
||||||
bitcoincore-rpc = { version = "0.17" }
|
bitcoincore-rpc = { version = "0.17" }
|
||||||
bdk_chain = { path = "../chain", version = "0.6", default-features = false }
|
bdk_chain = { path = "../chain", version = "0.7", default-features = false }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
bitcoind = { version = "0.33", features = ["25_0"] }
|
bitcoind = { version = "0.33", features = ["25_0"] }
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "bdk_chain"
|
name = "bdk_chain"
|
||||||
version = "0.6.0"
|
version = "0.7.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.63"
|
rust-version = "1.63"
|
||||||
homepage = "https://bitcoindevkit.org"
|
homepage = "https://bitcoindevkit.org"
|
||||||
|
|||||||
@@ -5,12 +5,13 @@ use crate::{
|
|||||||
spk_iter::BIP32_MAX_INDEX,
|
spk_iter::BIP32_MAX_INDEX,
|
||||||
SpkIterator, SpkTxOutIndex,
|
SpkIterator, SpkTxOutIndex,
|
||||||
};
|
};
|
||||||
use alloc::vec::Vec;
|
|
||||||
use bitcoin::{OutPoint, Script, TxOut};
|
use bitcoin::{OutPoint, Script, TxOut};
|
||||||
use core::{fmt::Debug, ops::Deref};
|
use core::{fmt::Debug, ops::Deref};
|
||||||
|
|
||||||
use crate::Append;
|
use crate::Append;
|
||||||
|
|
||||||
|
const DEFAULT_LOOKAHEAD: u32 = 1_000;
|
||||||
|
|
||||||
/// A convenient wrapper around [`SpkTxOutIndex`] that relates script pubkeys to miniscript public
|
/// A convenient wrapper around [`SpkTxOutIndex`] that relates script pubkeys to miniscript public
|
||||||
/// [`Descriptor`]s.
|
/// [`Descriptor`]s.
|
||||||
///
|
///
|
||||||
@@ -46,7 +47,7 @@ use crate::Append;
|
|||||||
/// # let secp = bdk_chain::bitcoin::secp256k1::Secp256k1::signing_only();
|
/// # let secp = bdk_chain::bitcoin::secp256k1::Secp256k1::signing_only();
|
||||||
/// # let (external_descriptor,_) = Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/0/*)").unwrap();
|
/// # let (external_descriptor,_) = Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/0/*)").unwrap();
|
||||||
/// # let (internal_descriptor,_) = Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/*)").unwrap();
|
/// # let (internal_descriptor,_) = Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/*)").unwrap();
|
||||||
/// # let descriptor_for_user_42 = external_descriptor.clone();
|
/// # let (descriptor_for_user_42, _) = Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/2/*)").unwrap();
|
||||||
/// txout_index.add_keychain(MyKeychain::External, external_descriptor);
|
/// txout_index.add_keychain(MyKeychain::External, external_descriptor);
|
||||||
/// txout_index.add_keychain(MyKeychain::Internal, internal_descriptor);
|
/// txout_index.add_keychain(MyKeychain::Internal, internal_descriptor);
|
||||||
/// txout_index.add_keychain(MyKeychain::MyAppUser { user_id: 42 }, descriptor_for_user_42);
|
/// txout_index.add_keychain(MyKeychain::MyAppUser { user_id: 42 }, descriptor_for_user_42);
|
||||||
@@ -65,17 +66,12 @@ pub struct KeychainTxOutIndex<K> {
|
|||||||
// last revealed indexes
|
// last revealed indexes
|
||||||
last_revealed: BTreeMap<K, u32>,
|
last_revealed: BTreeMap<K, u32>,
|
||||||
// lookahead settings for each keychain
|
// lookahead settings for each keychain
|
||||||
lookahead: BTreeMap<K, u32>,
|
lookahead: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K> Default for KeychainTxOutIndex<K> {
|
impl<K> Default for KeychainTxOutIndex<K> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self::new(DEFAULT_LOOKAHEAD)
|
||||||
inner: SpkTxOutIndex::default(),
|
|
||||||
keychains: BTreeMap::default(),
|
|
||||||
last_revealed: BTreeMap::default(),
|
|
||||||
lookahead: BTreeMap::default(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,6 +114,25 @@ impl<K: Clone + Ord + Debug> Indexer for KeychainTxOutIndex<K> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<K> KeychainTxOutIndex<K> {
|
||||||
|
/// Construct a [`KeychainTxOutIndex`] with the given `lookahead`.
|
||||||
|
///
|
||||||
|
/// The `lookahead` is the number of script pubkeys to derive and cache from the internal
|
||||||
|
/// descriptors over and above the last revealed script index. Without a lookahead the index
|
||||||
|
/// will miss outputs you own when processing transactions whose output script pubkeys lie
|
||||||
|
/// beyond the last revealed index. In certain situations, such as when performing an initial
|
||||||
|
/// scan of the blockchain during wallet import, it may be uncertain or unknown what the index
|
||||||
|
/// of the last revealed script pubkey actually is.
|
||||||
|
pub fn new(lookahead: u32) -> Self {
|
||||||
|
Self {
|
||||||
|
inner: SpkTxOutIndex::default(),
|
||||||
|
keychains: BTreeMap::new(),
|
||||||
|
last_revealed: BTreeMap::new(),
|
||||||
|
lookahead,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
|
impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
|
||||||
/// Return a reference to the internal [`SpkTxOutIndex`].
|
/// Return a reference to the internal [`SpkTxOutIndex`].
|
||||||
pub fn inner(&self) -> &SpkTxOutIndex<(K, u32)> {
|
pub fn inner(&self) -> &SpkTxOutIndex<(K, u32)> {
|
||||||
@@ -145,54 +160,22 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
|
|||||||
pub fn add_keychain(&mut self, keychain: K, descriptor: Descriptor<DescriptorPublicKey>) {
|
pub fn add_keychain(&mut self, keychain: K, descriptor: Descriptor<DescriptorPublicKey>) {
|
||||||
let old_descriptor = &*self
|
let old_descriptor = &*self
|
||||||
.keychains
|
.keychains
|
||||||
.entry(keychain)
|
.entry(keychain.clone())
|
||||||
.or_insert_with(|| descriptor.clone());
|
.or_insert_with(|| descriptor.clone());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
&descriptor, old_descriptor,
|
&descriptor, old_descriptor,
|
||||||
"keychain already contains a different descriptor"
|
"keychain already contains a different descriptor"
|
||||||
);
|
);
|
||||||
|
self.replenish_lookahead(&keychain, self.lookahead);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the lookahead setting for each keychain.
|
/// Get the lookahead setting.
|
||||||
///
|
///
|
||||||
/// Refer to [`set_lookahead`] for a deeper explanation of the `lookahead`.
|
/// Refer to [`new`] for more information on the `lookahead`.
|
||||||
///
|
///
|
||||||
/// [`set_lookahead`]: Self::set_lookahead
|
/// [`new`]: Self::new
|
||||||
pub fn lookaheads(&self) -> &BTreeMap<K, u32> {
|
pub fn lookahead(&self) -> u32 {
|
||||||
&self.lookahead
|
self.lookahead
|
||||||
}
|
|
||||||
|
|
||||||
/// Convenience method to call [`set_lookahead`] for all keychains.
|
|
||||||
///
|
|
||||||
/// [`set_lookahead`]: Self::set_lookahead
|
|
||||||
pub fn set_lookahead_for_all(&mut self, lookahead: u32) {
|
|
||||||
for keychain in &self.keychains.keys().cloned().collect::<Vec<_>>() {
|
|
||||||
self.set_lookahead(keychain, lookahead);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set the lookahead count for `keychain`.
|
|
||||||
///
|
|
||||||
/// The lookahead is the number of scripts to cache ahead of the last revealed script index. This
|
|
||||||
/// is useful to find outputs you own when processing block data that lie beyond the last revealed
|
|
||||||
/// index. In certain situations, such as when performing an initial scan of the blockchain during
|
|
||||||
/// wallet import, it may be uncertain or unknown what the last revealed index is.
|
|
||||||
///
|
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// This will panic if the `keychain` does not exist.
|
|
||||||
pub fn set_lookahead(&mut self, keychain: &K, lookahead: u32) {
|
|
||||||
self.lookahead.insert(keychain.clone(), lookahead);
|
|
||||||
self.replenish_lookahead(keychain);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Convenience method to call [`lookahead_to_target`] for multiple keychains.
|
|
||||||
///
|
|
||||||
/// [`lookahead_to_target`]: Self::lookahead_to_target
|
|
||||||
pub fn lookahead_to_target_multi(&mut self, target_indexes: BTreeMap<K, u32>) {
|
|
||||||
for (keychain, target_index) in target_indexes {
|
|
||||||
self.lookahead_to_target(&keychain, target_index)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Store lookahead scripts until `target_index`.
|
/// Store lookahead scripts until `target_index`.
|
||||||
@@ -201,22 +184,14 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
|
|||||||
pub fn lookahead_to_target(&mut self, keychain: &K, target_index: u32) {
|
pub fn lookahead_to_target(&mut self, keychain: &K, target_index: u32) {
|
||||||
let next_index = self.next_store_index(keychain);
|
let next_index = self.next_store_index(keychain);
|
||||||
if let Some(temp_lookahead) = target_index.checked_sub(next_index).filter(|&v| v > 0) {
|
if let Some(temp_lookahead) = target_index.checked_sub(next_index).filter(|&v| v > 0) {
|
||||||
let old_lookahead = self.lookahead.insert(keychain.clone(), temp_lookahead);
|
self.replenish_lookahead(keychain, temp_lookahead);
|
||||||
self.replenish_lookahead(keychain);
|
|
||||||
|
|
||||||
// revert
|
|
||||||
match old_lookahead {
|
|
||||||
Some(lookahead) => self.lookahead.insert(keychain.clone(), lookahead),
|
|
||||||
None => self.lookahead.remove(keychain),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn replenish_lookahead(&mut self, keychain: &K) {
|
fn replenish_lookahead(&mut self, keychain: &K, lookahead: u32) {
|
||||||
let descriptor = self.keychains.get(keychain).expect("keychain must exist");
|
let descriptor = self.keychains.get(keychain).expect("keychain must exist");
|
||||||
let next_store_index = self.next_store_index(keychain);
|
let next_store_index = self.next_store_index(keychain);
|
||||||
let next_reveal_index = self.last_revealed.get(keychain).map_or(0, |v| *v + 1);
|
let next_reveal_index = self.last_revealed.get(keychain).map_or(0, |v| *v + 1);
|
||||||
let lookahead = self.lookahead.get(keychain).map_or(0, |v| *v);
|
|
||||||
|
|
||||||
for (new_index, new_spk) in
|
for (new_index, new_spk) in
|
||||||
SpkIterator::new_with_range(descriptor, next_store_index..next_reveal_index + lookahead)
|
SpkIterator::new_with_range(descriptor, next_store_index..next_reveal_index + lookahead)
|
||||||
@@ -388,12 +363,8 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
|
|||||||
|
|
||||||
let target_index = if has_wildcard { target_index } else { 0 };
|
let target_index = if has_wildcard { target_index } else { 0 };
|
||||||
let next_reveal_index = self.last_revealed.get(keychain).map_or(0, |v| *v + 1);
|
let next_reveal_index = self.last_revealed.get(keychain).map_or(0, |v| *v + 1);
|
||||||
let lookahead = self.lookahead.get(keychain).map_or(0, |v| *v);
|
|
||||||
|
|
||||||
debug_assert_eq!(
|
debug_assert!(next_reveal_index + self.lookahead >= self.next_store_index(keychain));
|
||||||
next_reveal_index + lookahead,
|
|
||||||
self.next_store_index(keychain)
|
|
||||||
);
|
|
||||||
|
|
||||||
// if we need to reveal new indices, the latest revealed index goes here
|
// if we need to reveal new indices, the latest revealed index goes here
|
||||||
let mut reveal_to_index = None;
|
let mut reveal_to_index = None;
|
||||||
@@ -401,12 +372,12 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
|
|||||||
// if the target is not yet revealed, but is already stored (due to lookahead), we need to
|
// if the target is not yet revealed, but is already stored (due to lookahead), we need to
|
||||||
// set the `reveal_to_index` as target here (as the `for` loop below only updates
|
// set the `reveal_to_index` as target here (as the `for` loop below only updates
|
||||||
// `reveal_to_index` for indexes that are NOT stored)
|
// `reveal_to_index` for indexes that are NOT stored)
|
||||||
if next_reveal_index <= target_index && target_index < next_reveal_index + lookahead {
|
if next_reveal_index <= target_index && target_index < next_reveal_index + self.lookahead {
|
||||||
reveal_to_index = Some(target_index);
|
reveal_to_index = Some(target_index);
|
||||||
}
|
}
|
||||||
|
|
||||||
// we range over indexes that are not stored
|
// we range over indexes that are not stored
|
||||||
let range = next_reveal_index + lookahead..=target_index + lookahead;
|
let range = next_reveal_index + self.lookahead..=target_index + self.lookahead;
|
||||||
for (new_index, new_spk) in SpkIterator::new_with_range(descriptor, range) {
|
for (new_index, new_spk) in SpkIterator::new_with_range(descriptor, range) {
|
||||||
let _inserted = self
|
let _inserted = self
|
||||||
.inner
|
.inner
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
//! This crate is a collection of core structures for [Bitcoin Dev Kit] (alpha release).
|
//! This crate is a collection of core structures for [Bitcoin Dev Kit].
|
||||||
//!
|
//!
|
||||||
//! The goal of this crate is to give wallets the mechanisms needed to:
|
//! The goal of this crate is to give wallets the mechanisms needed to:
|
||||||
//!
|
//!
|
||||||
|
|||||||
@@ -148,7 +148,7 @@ mod test {
|
|||||||
Descriptor<DescriptorPublicKey>,
|
Descriptor<DescriptorPublicKey>,
|
||||||
Descriptor<DescriptorPublicKey>,
|
Descriptor<DescriptorPublicKey>,
|
||||||
) {
|
) {
|
||||||
let mut txout_index = KeychainTxOutIndex::<TestKeychain>::default();
|
let mut txout_index = KeychainTxOutIndex::<TestKeychain>::new(0);
|
||||||
|
|
||||||
let secp = Secp256k1::signing_only();
|
let secp = Secp256k1::signing_only();
|
||||||
let (external_descriptor,_) = Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/0/*)").unwrap();
|
let (external_descriptor,_) = Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/0/*)").unwrap();
|
||||||
|
|||||||
@@ -168,9 +168,7 @@ impl<I: Clone + Ord> SpkTxOutIndex<I> {
|
|||||||
///
|
///
|
||||||
/// Returns `None` if the `TxOut` hasn't been scanned or if nothing matching was found there.
|
/// Returns `None` if the `TxOut` hasn't been scanned or if nothing matching was found there.
|
||||||
pub fn txout(&self, outpoint: OutPoint) -> Option<(&I, &TxOut)> {
|
pub fn txout(&self, outpoint: OutPoint) -> Option<(&I, &TxOut)> {
|
||||||
self.txouts
|
self.txouts.get(&outpoint).map(|v| (&v.0, &v.1))
|
||||||
.get(&outpoint)
|
|
||||||
.map(|(spk_i, txout)| (spk_i, txout))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the script that has been inserted at the `index`.
|
/// Returns the script that has been inserted at the `index`.
|
||||||
|
|||||||
@@ -581,10 +581,7 @@ impl<A: Clone + Ord> TxGraph<A> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (outpoint, txout) in changeset.txouts {
|
for (outpoint, txout) in changeset.txouts {
|
||||||
let tx_entry = self
|
let tx_entry = self.txs.entry(outpoint.txid).or_default();
|
||||||
.txs
|
|
||||||
.entry(outpoint.txid)
|
|
||||||
.or_insert_with(Default::default);
|
|
||||||
|
|
||||||
match tx_entry {
|
match tx_entry {
|
||||||
(TxNodeInternal::Whole(_), _, _) => { /* do nothing since we already have full tx */
|
(TxNodeInternal::Whole(_), _, _) => { /* do nothing since we already have full tx */
|
||||||
@@ -597,13 +594,13 @@ impl<A: Clone + Ord> TxGraph<A> {
|
|||||||
|
|
||||||
for (anchor, txid) in changeset.anchors {
|
for (anchor, txid) in changeset.anchors {
|
||||||
if self.anchors.insert((anchor.clone(), txid)) {
|
if self.anchors.insert((anchor.clone(), txid)) {
|
||||||
let (_, anchors, _) = self.txs.entry(txid).or_insert_with(Default::default);
|
let (_, anchors, _) = self.txs.entry(txid).or_default();
|
||||||
anchors.insert(anchor);
|
anchors.insert(anchor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (txid, new_last_seen) in changeset.last_seen {
|
for (txid, new_last_seen) in changeset.last_seen {
|
||||||
let (_, _, last_seen) = self.txs.entry(txid).or_insert_with(Default::default);
|
let (_, _, last_seen) = self.txs.entry(txid).or_default();
|
||||||
if new_last_seen > *last_seen {
|
if new_last_seen > *last_seen {
|
||||||
*last_seen = new_last_seen;
|
*last_seen = new_last_seen;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
mod tx_template;
|
mod tx_template;
|
||||||
|
#[allow(unused_imports)]
|
||||||
pub use tx_template::*;
|
pub use tx_template::*;
|
||||||
|
|
||||||
#[allow(unused_macros)]
|
#[allow(unused_macros)]
|
||||||
|
|||||||
@@ -27,9 +27,10 @@ fn insert_relevant_txs() {
|
|||||||
let spk_0 = descriptor.at_derivation_index(0).unwrap().script_pubkey();
|
let spk_0 = descriptor.at_derivation_index(0).unwrap().script_pubkey();
|
||||||
let spk_1 = descriptor.at_derivation_index(9).unwrap().script_pubkey();
|
let spk_1 = descriptor.at_derivation_index(9).unwrap().script_pubkey();
|
||||||
|
|
||||||
let mut graph = IndexedTxGraph::<ConfirmationHeightAnchor, KeychainTxOutIndex<()>>::default();
|
let mut graph = IndexedTxGraph::<ConfirmationHeightAnchor, KeychainTxOutIndex<()>>::new(
|
||||||
|
KeychainTxOutIndex::new(10),
|
||||||
|
);
|
||||||
graph.index.add_keychain((), descriptor);
|
graph.index.add_keychain((), descriptor);
|
||||||
graph.index.set_lookahead(&(), 10);
|
|
||||||
|
|
||||||
let tx_a = Transaction {
|
let tx_a = Transaction {
|
||||||
output: vec![
|
output: vec![
|
||||||
@@ -118,12 +119,12 @@ fn test_list_owned_txouts() {
|
|||||||
let (desc_1, _) = Descriptor::parse_descriptor(&Secp256k1::signing_only(), "tr(tprv8ZgxMBicQKsPd3krDUsBAmtnRsK3rb8u5yi1zhQgMhF1tR8MW7xfE4rnrbbsrbPR52e7rKapu6ztw1jXveJSCGHEriUGZV7mCe88duLp5pj/86'/1'/0'/0/*)").unwrap();
|
let (desc_1, _) = Descriptor::parse_descriptor(&Secp256k1::signing_only(), "tr(tprv8ZgxMBicQKsPd3krDUsBAmtnRsK3rb8u5yi1zhQgMhF1tR8MW7xfE4rnrbbsrbPR52e7rKapu6ztw1jXveJSCGHEriUGZV7mCe88duLp5pj/86'/1'/0'/0/*)").unwrap();
|
||||||
let (desc_2, _) = Descriptor::parse_descriptor(&Secp256k1::signing_only(), "tr(tprv8ZgxMBicQKsPd3krDUsBAmtnRsK3rb8u5yi1zhQgMhF1tR8MW7xfE4rnrbbsrbPR52e7rKapu6ztw1jXveJSCGHEriUGZV7mCe88duLp5pj/86'/1'/0'/1/*)").unwrap();
|
let (desc_2, _) = Descriptor::parse_descriptor(&Secp256k1::signing_only(), "tr(tprv8ZgxMBicQKsPd3krDUsBAmtnRsK3rb8u5yi1zhQgMhF1tR8MW7xfE4rnrbbsrbPR52e7rKapu6ztw1jXveJSCGHEriUGZV7mCe88duLp5pj/86'/1'/0'/1/*)").unwrap();
|
||||||
|
|
||||||
let mut graph =
|
let mut graph = IndexedTxGraph::<ConfirmationHeightAnchor, KeychainTxOutIndex<String>>::new(
|
||||||
IndexedTxGraph::<ConfirmationHeightAnchor, KeychainTxOutIndex<String>>::default();
|
KeychainTxOutIndex::new(10),
|
||||||
|
);
|
||||||
|
|
||||||
graph.index.add_keychain("keychain_1".into(), desc_1);
|
graph.index.add_keychain("keychain_1".into(), desc_1);
|
||||||
graph.index.add_keychain("keychain_2".into(), desc_2);
|
graph.index.add_keychain("keychain_2".into(), desc_2);
|
||||||
graph.index.set_lookahead_for_all(10);
|
|
||||||
|
|
||||||
// Get trusted and untrusted addresses
|
// Get trusted and untrusted addresses
|
||||||
|
|
||||||
|
|||||||
@@ -18,12 +18,14 @@ enum TestKeychain {
|
|||||||
Internal,
|
Internal,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_txout_index() -> (
|
fn init_txout_index(
|
||||||
|
lookahead: u32,
|
||||||
|
) -> (
|
||||||
bdk_chain::keychain::KeychainTxOutIndex<TestKeychain>,
|
bdk_chain::keychain::KeychainTxOutIndex<TestKeychain>,
|
||||||
Descriptor<DescriptorPublicKey>,
|
Descriptor<DescriptorPublicKey>,
|
||||||
Descriptor<DescriptorPublicKey>,
|
Descriptor<DescriptorPublicKey>,
|
||||||
) {
|
) {
|
||||||
let mut txout_index = bdk_chain::keychain::KeychainTxOutIndex::<TestKeychain>::default();
|
let mut txout_index = bdk_chain::keychain::KeychainTxOutIndex::<TestKeychain>::new(lookahead);
|
||||||
|
|
||||||
let secp = bdk_chain::bitcoin::secp256k1::Secp256k1::signing_only();
|
let secp = bdk_chain::bitcoin::secp256k1::Secp256k1::signing_only();
|
||||||
let (external_descriptor,_) = Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/0/*)").unwrap();
|
let (external_descriptor,_) = Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/0/*)").unwrap();
|
||||||
@@ -46,7 +48,7 @@ fn spk_at_index(descriptor: &Descriptor<DescriptorPublicKey>, index: u32) -> Scr
|
|||||||
fn test_set_all_derivation_indices() {
|
fn test_set_all_derivation_indices() {
|
||||||
use bdk_chain::indexed_tx_graph::Indexer;
|
use bdk_chain::indexed_tx_graph::Indexer;
|
||||||
|
|
||||||
let (mut txout_index, _, _) = init_txout_index();
|
let (mut txout_index, _, _) = init_txout_index(0);
|
||||||
let derive_to: BTreeMap<_, _> =
|
let derive_to: BTreeMap<_, _> =
|
||||||
[(TestKeychain::External, 12), (TestKeychain::Internal, 24)].into();
|
[(TestKeychain::External, 12), (TestKeychain::Internal, 24)].into();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@@ -64,19 +66,10 @@ fn test_set_all_derivation_indices() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_lookahead() {
|
fn test_lookahead() {
|
||||||
let (mut txout_index, external_desc, internal_desc) = init_txout_index();
|
let (mut txout_index, external_desc, internal_desc) = init_txout_index(10);
|
||||||
|
|
||||||
// ensure it does not break anything if lookahead is set multiple times
|
|
||||||
(0..=10).for_each(|lookahead| txout_index.set_lookahead(&TestKeychain::External, lookahead));
|
|
||||||
(0..=20)
|
|
||||||
.filter(|v| v % 2 == 0)
|
|
||||||
.for_each(|lookahead| txout_index.set_lookahead(&TestKeychain::Internal, lookahead));
|
|
||||||
|
|
||||||
assert_eq!(txout_index.inner().all_spks().len(), 30);
|
|
||||||
|
|
||||||
// given:
|
// given:
|
||||||
// - external lookahead set to 10
|
// - external lookahead set to 10
|
||||||
// - internal lookahead set to 20
|
|
||||||
// when:
|
// when:
|
||||||
// - set external derivation index to value higher than last, but within the lookahead value
|
// - set external derivation index to value higher than last, but within the lookahead value
|
||||||
// expect:
|
// expect:
|
||||||
@@ -97,7 +90,7 @@ fn test_lookahead() {
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
txout_index.inner().all_spks().len(),
|
txout_index.inner().all_spks().len(),
|
||||||
10 /* external lookahead */ +
|
10 /* external lookahead */ +
|
||||||
20 /* internal lookahead */ +
|
10 /* internal lookahead */ +
|
||||||
index as usize + 1 /* `derived` count */
|
index as usize + 1 /* `derived` count */
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@@ -127,7 +120,7 @@ fn test_lookahead() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// given:
|
// given:
|
||||||
// - internal lookahead is 20
|
// - internal lookahead is 10
|
||||||
// - internal derivation index is `None`
|
// - internal derivation index is `None`
|
||||||
// when:
|
// when:
|
||||||
// - derivation index is set ahead of current derivation index + lookahead
|
// - derivation index is set ahead of current derivation index + lookahead
|
||||||
@@ -148,7 +141,7 @@ fn test_lookahead() {
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
txout_index.inner().all_spks().len(),
|
txout_index.inner().all_spks().len(),
|
||||||
10 /* external lookahead */ +
|
10 /* external lookahead */ +
|
||||||
20 /* internal lookahead */ +
|
10 /* internal lookahead */ +
|
||||||
20 /* external stored index count */ +
|
20 /* external stored index count */ +
|
||||||
25 /* internal stored index count */
|
25 /* internal stored index count */
|
||||||
);
|
);
|
||||||
@@ -226,8 +219,7 @@ fn test_lookahead() {
|
|||||||
// - last used index should change as expected
|
// - last used index should change as expected
|
||||||
#[test]
|
#[test]
|
||||||
fn test_scan_with_lookahead() {
|
fn test_scan_with_lookahead() {
|
||||||
let (mut txout_index, external_desc, _) = init_txout_index();
|
let (mut txout_index, external_desc, _) = init_txout_index(10);
|
||||||
txout_index.set_lookahead_for_all(10);
|
|
||||||
|
|
||||||
let spks: BTreeMap<u32, ScriptBuf> = [0, 10, 20, 30]
|
let spks: BTreeMap<u32, ScriptBuf> = [0, 10, 20, 30]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@@ -281,7 +273,7 @@ fn test_scan_with_lookahead() {
|
|||||||
#[test]
|
#[test]
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
fn test_wildcard_derivations() {
|
fn test_wildcard_derivations() {
|
||||||
let (mut txout_index, external_desc, _) = init_txout_index();
|
let (mut txout_index, external_desc, _) = init_txout_index(0);
|
||||||
let external_spk_0 = external_desc.at_derivation_index(0).unwrap().script_pubkey();
|
let external_spk_0 = external_desc.at_derivation_index(0).unwrap().script_pubkey();
|
||||||
let external_spk_16 = external_desc.at_derivation_index(16).unwrap().script_pubkey();
|
let external_spk_16 = external_desc.at_derivation_index(16).unwrap().script_pubkey();
|
||||||
let external_spk_26 = external_desc.at_derivation_index(26).unwrap().script_pubkey();
|
let external_spk_26 = external_desc.at_derivation_index(26).unwrap().script_pubkey();
|
||||||
@@ -339,7 +331,7 @@ fn test_wildcard_derivations() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_non_wildcard_derivations() {
|
fn test_non_wildcard_derivations() {
|
||||||
let mut txout_index = KeychainTxOutIndex::<TestKeychain>::default();
|
let mut txout_index = KeychainTxOutIndex::<TestKeychain>::new(0);
|
||||||
|
|
||||||
let secp = bitcoin::secp256k1::Secp256k1::signing_only();
|
let secp = bitcoin::secp256k1::Secp256k1::signing_only();
|
||||||
let (no_wildcard_descriptor, _) = Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, "wpkh([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/0)").unwrap();
|
let (no_wildcard_descriptor, _) = Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, "wpkh([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/0)").unwrap();
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "bdk_electrum"
|
name = "bdk_electrum"
|
||||||
version = "0.4.0"
|
version = "0.5.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
homepage = "https://bitcoindevkit.org"
|
homepage = "https://bitcoindevkit.org"
|
||||||
repository = "https://github.com/bitcoindevkit/bdk"
|
repository = "https://github.com/bitcoindevkit/bdk"
|
||||||
@@ -12,6 +12,6 @@ readme = "README.md"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bdk_chain = { path = "../chain", version = "0.6.0", default-features = false }
|
bdk_chain = { path = "../chain", version = "0.7.0", default-features = false }
|
||||||
electrum-client = { version = "0.18" }
|
electrum-client = { version = "0.18" }
|
||||||
#rustls = { version = "=0.21.1", optional = true, features = ["dangerous_configuration"] }
|
#rustls = { version = "=0.21.1", optional = true, features = ["dangerous_configuration"] }
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
# BDK Electrum
|
# BDK Electrum
|
||||||
|
|
||||||
BDK Electrum client library for updating the keychain tracker.
|
BDK Electrum extends [`electrum-client`] to update [`bdk_chain`] structures
|
||||||
|
from an Electrum server.
|
||||||
|
|
||||||
|
[`electrum-client`]: https://docs.rs/electrum-client/
|
||||||
|
[`bdk_chain`]: https://docs.rs/bdk-chain/
|
||||||
|
|||||||
@@ -134,64 +134,54 @@ pub struct ElectrumUpdate {
|
|||||||
|
|
||||||
/// Trait to extend [`Client`] functionality.
|
/// Trait to extend [`Client`] functionality.
|
||||||
pub trait ElectrumExt {
|
pub trait ElectrumExt {
|
||||||
/// Scan the blockchain (via electrum) for the data specified and returns updates for
|
/// Full scan the keychain scripts specified with the blockchain (via an Electrum client) and
|
||||||
/// [`bdk_chain`] data structures.
|
/// returns updates for [`bdk_chain`] data structures.
|
||||||
///
|
///
|
||||||
/// - `prev_tip`: the most recent blockchain tip present locally
|
/// - `prev_tip`: the most recent blockchain tip present locally
|
||||||
/// - `keychain_spks`: keychains that we want to scan transactions for
|
/// - `keychain_spks`: keychains that we want to scan transactions for
|
||||||
/// - `txids`: transactions for which we want updated [`Anchor`]s
|
|
||||||
/// - `outpoints`: transactions associated with these outpoints (residing, spending) that we
|
|
||||||
/// want to included in the update
|
|
||||||
///
|
///
|
||||||
/// The scan for each keychain stops after a gap of `stop_gap` script pubkeys with no associated
|
/// The full scan for each keychain stops after a gap of `stop_gap` script pubkeys with no associated
|
||||||
/// transactions. `batch_size` specifies the max number of script pubkeys to request for in a
|
/// transactions. `batch_size` specifies the max number of script pubkeys to request for in a
|
||||||
/// single batch request.
|
/// single batch request.
|
||||||
fn scan<K: Ord + Clone>(
|
fn full_scan<K: Ord + Clone>(
|
||||||
&self,
|
&self,
|
||||||
prev_tip: CheckPoint,
|
prev_tip: CheckPoint,
|
||||||
keychain_spks: BTreeMap<K, impl IntoIterator<Item = (u32, ScriptBuf)>>,
|
keychain_spks: BTreeMap<K, impl IntoIterator<Item = (u32, ScriptBuf)>>,
|
||||||
txids: impl IntoIterator<Item = Txid>,
|
|
||||||
outpoints: impl IntoIterator<Item = OutPoint>,
|
|
||||||
stop_gap: usize,
|
stop_gap: usize,
|
||||||
batch_size: usize,
|
batch_size: usize,
|
||||||
) -> Result<(ElectrumUpdate, BTreeMap<K, u32>), Error>;
|
) -> Result<(ElectrumUpdate, BTreeMap<K, u32>), Error>;
|
||||||
|
|
||||||
/// Convenience method to call [`scan`] without requiring a keychain.
|
/// Sync a set of scripts with the blockchain (via an Electrum client) for the data specified
|
||||||
|
/// and returns updates for [`bdk_chain`] data structures.
|
||||||
///
|
///
|
||||||
/// [`scan`]: ElectrumExt::scan
|
/// - `prev_tip`: the most recent blockchain tip present locally
|
||||||
fn scan_without_keychain(
|
/// - `misc_spks`: an iterator of scripts we want to sync transactions for
|
||||||
|
/// - `txids`: transactions for which we want updated [`Anchor`]s
|
||||||
|
/// - `outpoints`: transactions associated with these outpoints (residing, spending) that we
|
||||||
|
/// want to include in the update
|
||||||
|
///
|
||||||
|
/// `batch_size` specifies the max number of script pubkeys to request for in a single batch
|
||||||
|
/// request.
|
||||||
|
///
|
||||||
|
/// If the scripts to sync are unknown, such as when restoring or importing a keychain that
|
||||||
|
/// may include scripts that have been used, use [`full_scan`] with the keychain.
|
||||||
|
///
|
||||||
|
/// [`full_scan`]: ElectrumExt::full_scan
|
||||||
|
fn sync(
|
||||||
&self,
|
&self,
|
||||||
prev_tip: CheckPoint,
|
prev_tip: CheckPoint,
|
||||||
misc_spks: impl IntoIterator<Item = ScriptBuf>,
|
misc_spks: impl IntoIterator<Item = ScriptBuf>,
|
||||||
txids: impl IntoIterator<Item = Txid>,
|
txids: impl IntoIterator<Item = Txid>,
|
||||||
outpoints: impl IntoIterator<Item = OutPoint>,
|
outpoints: impl IntoIterator<Item = OutPoint>,
|
||||||
batch_size: usize,
|
batch_size: usize,
|
||||||
) -> Result<ElectrumUpdate, Error> {
|
) -> Result<ElectrumUpdate, Error>;
|
||||||
let spk_iter = misc_spks
|
|
||||||
.into_iter()
|
|
||||||
.enumerate()
|
|
||||||
.map(|(i, spk)| (i as u32, spk));
|
|
||||||
|
|
||||||
let (electrum_update, _) = self.scan(
|
|
||||||
prev_tip,
|
|
||||||
[((), spk_iter)].into(),
|
|
||||||
txids,
|
|
||||||
outpoints,
|
|
||||||
usize::MAX,
|
|
||||||
batch_size,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(electrum_update)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ElectrumExt for Client {
|
impl ElectrumExt for Client {
|
||||||
fn scan<K: Ord + Clone>(
|
fn full_scan<K: Ord + Clone>(
|
||||||
&self,
|
&self,
|
||||||
prev_tip: CheckPoint,
|
prev_tip: CheckPoint,
|
||||||
keychain_spks: BTreeMap<K, impl IntoIterator<Item = (u32, ScriptBuf)>>,
|
keychain_spks: BTreeMap<K, impl IntoIterator<Item = (u32, ScriptBuf)>>,
|
||||||
txids: impl IntoIterator<Item = Txid>,
|
|
||||||
outpoints: impl IntoIterator<Item = OutPoint>,
|
|
||||||
stop_gap: usize,
|
stop_gap: usize,
|
||||||
batch_size: usize,
|
batch_size: usize,
|
||||||
) -> Result<(ElectrumUpdate, BTreeMap<K, u32>), Error> {
|
) -> Result<(ElectrumUpdate, BTreeMap<K, u32>), Error> {
|
||||||
@@ -201,9 +191,6 @@ impl ElectrumExt for Client {
|
|||||||
.collect::<BTreeMap<K, _>>();
|
.collect::<BTreeMap<K, _>>();
|
||||||
let mut scanned_spks = BTreeMap::<(K, u32), (ScriptBuf, bool)>::new();
|
let mut scanned_spks = BTreeMap::<(K, u32), (ScriptBuf, bool)>::new();
|
||||||
|
|
||||||
let txids = txids.into_iter().collect::<Vec<_>>();
|
|
||||||
let outpoints = outpoints.into_iter().collect::<Vec<_>>();
|
|
||||||
|
|
||||||
let (electrum_update, keychain_update) = loop {
|
let (electrum_update, keychain_update) = loop {
|
||||||
let (tip, _) = construct_update_tip(self, prev_tip.clone())?;
|
let (tip, _) = construct_update_tip(self, prev_tip.clone())?;
|
||||||
let mut relevant_txids = RelevantTxids::default();
|
let mut relevant_txids = RelevantTxids::default();
|
||||||
@@ -242,15 +229,6 @@ impl ElectrumExt for Client {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
populate_with_txids(self, &cps, &mut relevant_txids, &mut txids.iter().cloned())?;
|
|
||||||
|
|
||||||
let _txs = populate_with_outpoints(
|
|
||||||
self,
|
|
||||||
&cps,
|
|
||||||
&mut relevant_txids,
|
|
||||||
&mut outpoints.iter().cloned(),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
// check for reorgs during scan process
|
// check for reorgs during scan process
|
||||||
let server_blockhash = self.block_header(tip.height() as usize)?.block_hash();
|
let server_blockhash = self.block_header(tip.height() as usize)?.block_hash();
|
||||||
if tip.hash() != server_blockhash {
|
if tip.hash() != server_blockhash {
|
||||||
@@ -284,6 +262,41 @@ impl ElectrumExt for Client {
|
|||||||
|
|
||||||
Ok((electrum_update, keychain_update))
|
Ok((electrum_update, keychain_update))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn sync(
|
||||||
|
&self,
|
||||||
|
prev_tip: CheckPoint,
|
||||||
|
misc_spks: impl IntoIterator<Item = ScriptBuf>,
|
||||||
|
txids: impl IntoIterator<Item = Txid>,
|
||||||
|
outpoints: impl IntoIterator<Item = OutPoint>,
|
||||||
|
batch_size: usize,
|
||||||
|
) -> Result<ElectrumUpdate, Error> {
|
||||||
|
let spk_iter = misc_spks
|
||||||
|
.into_iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, spk)| (i as u32, spk));
|
||||||
|
|
||||||
|
let (mut electrum_update, _) = self.full_scan(
|
||||||
|
prev_tip.clone(),
|
||||||
|
[((), spk_iter)].into(),
|
||||||
|
usize::MAX,
|
||||||
|
batch_size,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let (tip, _) = construct_update_tip(self, prev_tip)?;
|
||||||
|
let cps = tip
|
||||||
|
.iter()
|
||||||
|
.take(10)
|
||||||
|
.map(|cp| (cp.height(), cp))
|
||||||
|
.collect::<BTreeMap<u32, CheckPoint>>();
|
||||||
|
|
||||||
|
populate_with_txids(self, &cps, &mut electrum_update.relevant_txids, txids)?;
|
||||||
|
|
||||||
|
let _txs =
|
||||||
|
populate_with_outpoints(self, &cps, &mut electrum_update.relevant_txids, outpoints)?;
|
||||||
|
|
||||||
|
Ok(electrum_update)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return a [`CheckPoint`] of the latest tip, that connects with `prev_tip`.
|
/// Return a [`CheckPoint`] of the latest tip, that connects with `prev_tip`.
|
||||||
@@ -405,7 +418,7 @@ fn populate_with_outpoints(
|
|||||||
client: &Client,
|
client: &Client,
|
||||||
cps: &BTreeMap<u32, CheckPoint>,
|
cps: &BTreeMap<u32, CheckPoint>,
|
||||||
relevant_txids: &mut RelevantTxids,
|
relevant_txids: &mut RelevantTxids,
|
||||||
outpoints: &mut impl Iterator<Item = OutPoint>,
|
outpoints: impl IntoIterator<Item = OutPoint>,
|
||||||
) -> Result<HashMap<Txid, Transaction>, Error> {
|
) -> Result<HashMap<Txid, Transaction>, Error> {
|
||||||
let mut full_txs = HashMap::new();
|
let mut full_txs = HashMap::new();
|
||||||
for outpoint in outpoints {
|
for outpoint in outpoints {
|
||||||
@@ -466,7 +479,7 @@ fn populate_with_txids(
|
|||||||
client: &Client,
|
client: &Client,
|
||||||
cps: &BTreeMap<u32, CheckPoint>,
|
cps: &BTreeMap<u32, CheckPoint>,
|
||||||
relevant_txids: &mut RelevantTxids,
|
relevant_txids: &mut RelevantTxids,
|
||||||
txids: &mut impl Iterator<Item = Txid>,
|
txids: impl IntoIterator<Item = Txid>,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
for txid in txids {
|
for txid in txids {
|
||||||
let tx = match client.transaction_get(&txid) {
|
let tx = match client.transaction_get(&txid) {
|
||||||
@@ -477,7 +490,7 @@ fn populate_with_txids(
|
|||||||
|
|
||||||
let spk = tx
|
let spk = tx
|
||||||
.output
|
.output
|
||||||
.get(0)
|
.first()
|
||||||
.map(|txo| &txo.script_pubkey)
|
.map(|txo| &txo.script_pubkey)
|
||||||
.expect("tx must have an output");
|
.expect("tx must have an output");
|
||||||
|
|
||||||
|
|||||||
@@ -1,26 +1,26 @@
|
|||||||
//! This crate is used for updating structures of the [`bdk_chain`] crate with data from electrum.
|
//! This crate is used for updating structures of [`bdk_chain`] with data from an Electrum server.
|
||||||
//!
|
//!
|
||||||
//! The star of the show is the [`ElectrumExt::scan`] method, which scans for relevant blockchain
|
//! The two primary methods are [`ElectrumExt::sync`] and [`ElectrumExt::full_scan`]. In most cases
|
||||||
//! data (via electrum) and outputs updates for [`bdk_chain`] structures as a tuple of form:
|
//! [`ElectrumExt::sync`] is used to sync the transaction histories of scripts that the application
|
||||||
|
//! cares about, for example the scripts for all the receive addresses of a Wallet's keychain that it
|
||||||
|
//! has shown a user. [`ElectrumExt::full_scan`] is meant to be used when importing or restoring a
|
||||||
|
//! keychain where the range of possibly used scripts is not known. In this case it is necessary to
|
||||||
|
//! scan all keychain scripts until a number (the "stop gap") of unused scripts is discovered. For a
|
||||||
|
//! sync or full scan the user receives relevant blockchain data and output updates for
|
||||||
|
//! [`bdk_chain`] including [`RelevantTxids`].
|
||||||
//!
|
//!
|
||||||
//! ([`bdk_chain::local_chain::Update`], [`RelevantTxids`], `keychain_update`)
|
//! The [`RelevantTxids`] only includes `txid`s and not full transactions. The caller is responsible
|
||||||
|
//! for obtaining full transactions before applying new data to their [`bdk_chain`]. This can be
|
||||||
|
//! done with these steps:
|
||||||
//!
|
//!
|
||||||
//! An [`RelevantTxids`] only includes `txid`s and no full transactions. The caller is
|
//! 1. Determine which full transactions are missing. Use [`RelevantTxids::missing_full_txs`].
|
||||||
//! responsible for obtaining full transactions before applying. This can be done with
|
|
||||||
//! these steps:
|
|
||||||
//!
|
//!
|
||||||
//! 1. Determine which full transactions are missing. The method [`missing_full_txs`] of
|
//! 2. Obtaining the full transactions. To do this via electrum use [`ElectrumApi::batch_transaction_get`].
|
||||||
//! [`RelevantTxids`] can be used.
|
|
||||||
//!
|
//!
|
||||||
//! 2. Obtaining the full transactions. To do this via electrum, the method
|
//! Refer to [`example_electrum`] for a complete example.
|
||||||
//! [`batch_transaction_get`] can be used.
|
|
||||||
//!
|
//!
|
||||||
//! Refer to [`bdk_electrum_example`] for a complete example.
|
//! [`ElectrumApi::batch_transaction_get`]: electrum_client::ElectrumApi::batch_transaction_get
|
||||||
//!
|
//! [`example_electrum`]: https://github.com/bitcoindevkit/bdk/tree/master/example-crates/example_electrum
|
||||||
//! [`ElectrumClient::scan`]: electrum_client::ElectrumClient::scan
|
|
||||||
//! [`missing_full_txs`]: RelevantTxids::missing_full_txs
|
|
||||||
//! [`batch_transaction_get`]: electrum_client::ElectrumApi::batch_transaction_get
|
|
||||||
//! [`bdk_electrum_example`]: https://github.com/LLFourn/bdk_core_staging/tree/master/bdk_electrum_example
|
|
||||||
|
|
||||||
#![warn(missing_docs)]
|
#![warn(missing_docs)]
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ readme = "README.md"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bdk_chain = { path = "../chain", version = "0.6.0", default-features = false }
|
bdk_chain = { path = "../chain", version = "0.7.0", default-features = false }
|
||||||
esplora-client = { version = "0.6.0", default-features = false }
|
esplora-client = { version = "0.6.0", default-features = false }
|
||||||
async-trait = { version = "0.1.66", optional = true }
|
async-trait = { version = "0.1.66", optional = true }
|
||||||
futures = { version = "0.3.26", optional = true }
|
futures = { version = "0.3.26", optional = true }
|
||||||
|
|||||||
@@ -36,58 +36,45 @@ pub trait EsploraAsyncExt {
|
|||||||
request_heights: impl IntoIterator<IntoIter = impl Iterator<Item = u32> + Send> + Send,
|
request_heights: impl IntoIterator<IntoIter = impl Iterator<Item = u32> + Send> + Send,
|
||||||
) -> Result<local_chain::Update, Error>;
|
) -> Result<local_chain::Update, Error>;
|
||||||
|
|
||||||
/// Scan Esplora for the data specified and return a [`TxGraph`] and a map of last active
|
/// Full scan the keychain scripts specified with the blockchain (via an Esplora client) and
|
||||||
/// indices.
|
/// returns a [`TxGraph`] and a map of last active indices.
|
||||||
///
|
///
|
||||||
/// * `keychain_spks`: keychains that we want to scan transactions for
|
/// * `keychain_spks`: keychains that we want to scan transactions for
|
||||||
/// * `txids`: transactions for which we want updated [`ConfirmationTimeHeightAnchor`]s
|
|
||||||
/// * `outpoints`: transactions associated with these outpoints (residing, spending) that we
|
|
||||||
/// want to include in the update
|
|
||||||
///
|
///
|
||||||
/// The scan for each keychain stops after a gap of `stop_gap` script pubkeys with no associated
|
/// The full scan for each keychain stops after a gap of `stop_gap` script pubkeys with no associated
|
||||||
/// transactions. `parallel_requests` specifies the max number of HTTP requests to make in
|
/// transactions. `parallel_requests` specifies the max number of HTTP requests to make in
|
||||||
/// parallel.
|
/// parallel.
|
||||||
#[allow(clippy::result_large_err)]
|
#[allow(clippy::result_large_err)]
|
||||||
async fn scan_txs_with_keychains<K: Ord + Clone + Send>(
|
async fn full_scan<K: Ord + Clone + Send>(
|
||||||
&self,
|
&self,
|
||||||
keychain_spks: BTreeMap<
|
keychain_spks: BTreeMap<
|
||||||
K,
|
K,
|
||||||
impl IntoIterator<IntoIter = impl Iterator<Item = (u32, ScriptBuf)> + Send> + Send,
|
impl IntoIterator<IntoIter = impl Iterator<Item = (u32, ScriptBuf)> + Send> + Send,
|
||||||
>,
|
>,
|
||||||
txids: impl IntoIterator<IntoIter = impl Iterator<Item = Txid> + Send> + Send,
|
|
||||||
outpoints: impl IntoIterator<IntoIter = impl Iterator<Item = OutPoint> + Send> + Send,
|
|
||||||
stop_gap: usize,
|
stop_gap: usize,
|
||||||
parallel_requests: usize,
|
parallel_requests: usize,
|
||||||
) -> Result<(TxGraph<ConfirmationTimeHeightAnchor>, BTreeMap<K, u32>), Error>;
|
) -> Result<(TxGraph<ConfirmationTimeHeightAnchor>, BTreeMap<K, u32>), Error>;
|
||||||
|
|
||||||
/// Convenience method to call [`scan_txs_with_keychains`] without requiring a keychain.
|
/// Sync a set of scripts with the blockchain (via an Esplora client) for the data
|
||||||
|
/// specified and return a [`TxGraph`].
|
||||||
///
|
///
|
||||||
/// [`scan_txs_with_keychains`]: EsploraAsyncExt::scan_txs_with_keychains
|
/// * `misc_spks`: scripts that we want to sync transactions for
|
||||||
|
/// * `txids`: transactions for which we want updated [`ConfirmationTimeHeightAnchor`]s
|
||||||
|
/// * `outpoints`: transactions associated with these outpoints (residing, spending) that we
|
||||||
|
/// want to include in the update
|
||||||
|
///
|
||||||
|
/// If the scripts to sync are unknown, such as when restoring or importing a keychain that
|
||||||
|
/// may include scripts that have been used, use [`full_scan`] with the keychain.
|
||||||
|
///
|
||||||
|
/// [`full_scan`]: EsploraAsyncExt::full_scan
|
||||||
#[allow(clippy::result_large_err)]
|
#[allow(clippy::result_large_err)]
|
||||||
async fn scan_txs(
|
async fn sync(
|
||||||
&self,
|
&self,
|
||||||
misc_spks: impl IntoIterator<IntoIter = impl Iterator<Item = ScriptBuf> + Send> + Send,
|
misc_spks: impl IntoIterator<IntoIter = impl Iterator<Item = ScriptBuf> + Send> + Send,
|
||||||
txids: impl IntoIterator<IntoIter = impl Iterator<Item = Txid> + Send> + Send,
|
txids: impl IntoIterator<IntoIter = impl Iterator<Item = Txid> + Send> + Send,
|
||||||
outpoints: impl IntoIterator<IntoIter = impl Iterator<Item = OutPoint> + Send> + Send,
|
outpoints: impl IntoIterator<IntoIter = impl Iterator<Item = OutPoint> + Send> + Send,
|
||||||
parallel_requests: usize,
|
parallel_requests: usize,
|
||||||
) -> Result<TxGraph<ConfirmationTimeHeightAnchor>, Error> {
|
) -> Result<TxGraph<ConfirmationTimeHeightAnchor>, Error>;
|
||||||
self.scan_txs_with_keychains(
|
|
||||||
[(
|
|
||||||
(),
|
|
||||||
misc_spks
|
|
||||||
.into_iter()
|
|
||||||
.enumerate()
|
|
||||||
.map(|(i, spk)| (i as u32, spk)),
|
|
||||||
)]
|
|
||||||
.into(),
|
|
||||||
txids,
|
|
||||||
outpoints,
|
|
||||||
usize::MAX,
|
|
||||||
parallel_requests,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map(|(g, _)| g)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
|
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
|
||||||
@@ -199,14 +186,12 @@ impl EsploraAsyncExt for esplora_client::AsyncClient {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn scan_txs_with_keychains<K: Ord + Clone + Send>(
|
async fn full_scan<K: Ord + Clone + Send>(
|
||||||
&self,
|
&self,
|
||||||
keychain_spks: BTreeMap<
|
keychain_spks: BTreeMap<
|
||||||
K,
|
K,
|
||||||
impl IntoIterator<IntoIter = impl Iterator<Item = (u32, ScriptBuf)> + Send> + Send,
|
impl IntoIterator<IntoIter = impl Iterator<Item = (u32, ScriptBuf)> + Send> + Send,
|
||||||
>,
|
>,
|
||||||
txids: impl IntoIterator<IntoIter = impl Iterator<Item = Txid> + Send> + Send,
|
|
||||||
outpoints: impl IntoIterator<IntoIter = impl Iterator<Item = OutPoint> + Send> + Send,
|
|
||||||
stop_gap: usize,
|
stop_gap: usize,
|
||||||
parallel_requests: usize,
|
parallel_requests: usize,
|
||||||
) -> Result<(TxGraph<ConfirmationTimeHeightAnchor>, BTreeMap<K, u32>), Error> {
|
) -> Result<(TxGraph<ConfirmationTimeHeightAnchor>, BTreeMap<K, u32>), Error> {
|
||||||
@@ -275,6 +260,32 @@ impl EsploraAsyncExt for esplora_client::AsyncClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok((graph, last_active_indexes))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn sync(
|
||||||
|
&self,
|
||||||
|
misc_spks: impl IntoIterator<IntoIter = impl Iterator<Item = ScriptBuf> + Send> + Send,
|
||||||
|
txids: impl IntoIterator<IntoIter = impl Iterator<Item = Txid> + Send> + Send,
|
||||||
|
outpoints: impl IntoIterator<IntoIter = impl Iterator<Item = OutPoint> + Send> + Send,
|
||||||
|
parallel_requests: usize,
|
||||||
|
) -> Result<TxGraph<ConfirmationTimeHeightAnchor>, Error> {
|
||||||
|
let mut graph = self
|
||||||
|
.full_scan(
|
||||||
|
[(
|
||||||
|
(),
|
||||||
|
misc_spks
|
||||||
|
.into_iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, spk)| (i as u32, spk)),
|
||||||
|
)]
|
||||||
|
.into(),
|
||||||
|
usize::MAX,
|
||||||
|
parallel_requests,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.map(|(g, _)| g)?;
|
||||||
|
|
||||||
let mut txids = txids.into_iter();
|
let mut txids = txids.into_iter();
|
||||||
loop {
|
loop {
|
||||||
let handles = txids
|
let handles = txids
|
||||||
@@ -323,7 +334,6 @@ impl EsploraAsyncExt for esplora_client::AsyncClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(graph)
|
||||||
Ok((graph, last_active_indexes))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,8 +19,8 @@ use crate::{anchor_from_status, ASSUME_FINAL_DEPTH};
|
|||||||
pub trait EsploraExt {
|
pub trait EsploraExt {
|
||||||
/// Prepare an [`LocalChain`] update with blocks fetched from Esplora.
|
/// Prepare an [`LocalChain`] update with blocks fetched from Esplora.
|
||||||
///
|
///
|
||||||
/// * `prev_tip` is the previous tip of [`LocalChain::tip`].
|
/// * `local_tip` is the previous tip of [`LocalChain::tip`].
|
||||||
/// * `get_heights` is the block heights that we are interested in fetching from Esplora.
|
/// * `request_heights` is the block heights that we are interested in fetching from Esplora.
|
||||||
///
|
///
|
||||||
/// The result of this method can be applied to [`LocalChain::apply_update`].
|
/// The result of this method can be applied to [`LocalChain::apply_update`].
|
||||||
///
|
///
|
||||||
@@ -34,54 +34,42 @@ pub trait EsploraExt {
|
|||||||
request_heights: impl IntoIterator<Item = u32>,
|
request_heights: impl IntoIterator<Item = u32>,
|
||||||
) -> Result<local_chain::Update, Error>;
|
) -> Result<local_chain::Update, Error>;
|
||||||
|
|
||||||
/// Scan Esplora for the data specified and return a [`TxGraph`] and a map of last active
|
/// Full scan the keychain scripts specified with the blockchain (via an Esplora client) and
|
||||||
/// indices.
|
/// returns a [`TxGraph`] and a map of last active indices.
|
||||||
///
|
///
|
||||||
/// * `keychain_spks`: keychains that we want to scan transactions for
|
/// * `keychain_spks`: keychains that we want to scan transactions for
|
||||||
/// * `txids`: transactions for which we want updated [`ConfirmationTimeHeightAnchor`]s
|
|
||||||
/// * `outpoints`: transactions associated with these outpoints (residing, spending) that we
|
|
||||||
/// want to include in the update
|
|
||||||
///
|
///
|
||||||
/// The scan for each keychain stops after a gap of `stop_gap` script pubkeys with no associated
|
/// The full scan for each keychain stops after a gap of `stop_gap` script pubkeys with no associated
|
||||||
/// transactions. `parallel_requests` specifies the max number of HTTP requests to make in
|
/// transactions. `parallel_requests` specifies the max number of HTTP requests to make in
|
||||||
/// parallel.
|
/// parallel.
|
||||||
#[allow(clippy::result_large_err)]
|
#[allow(clippy::result_large_err)]
|
||||||
fn scan_txs_with_keychains<K: Ord + Clone>(
|
fn full_scan<K: Ord + Clone>(
|
||||||
&self,
|
&self,
|
||||||
keychain_spks: BTreeMap<K, impl IntoIterator<Item = (u32, ScriptBuf)>>,
|
keychain_spks: BTreeMap<K, impl IntoIterator<Item = (u32, ScriptBuf)>>,
|
||||||
txids: impl IntoIterator<Item = Txid>,
|
|
||||||
outpoints: impl IntoIterator<Item = OutPoint>,
|
|
||||||
stop_gap: usize,
|
stop_gap: usize,
|
||||||
parallel_requests: usize,
|
parallel_requests: usize,
|
||||||
) -> Result<(TxGraph<ConfirmationTimeHeightAnchor>, BTreeMap<K, u32>), Error>;
|
) -> Result<(TxGraph<ConfirmationTimeHeightAnchor>, BTreeMap<K, u32>), Error>;
|
||||||
|
|
||||||
/// Convenience method to call [`scan_txs_with_keychains`] without requiring a keychain.
|
/// Sync a set of scripts with the blockchain (via an Esplora client) for the data
|
||||||
|
/// specified and return a [`TxGraph`].
|
||||||
///
|
///
|
||||||
/// [`scan_txs_with_keychains`]: EsploraExt::scan_txs_with_keychains
|
/// * `misc_spks`: scripts that we want to sync transactions for
|
||||||
|
/// * `txids`: transactions for which we want updated [`ConfirmationTimeHeightAnchor`]s
|
||||||
|
/// * `outpoints`: transactions associated with these outpoints (residing, spending) that we
|
||||||
|
/// want to include in the update
|
||||||
|
///
|
||||||
|
/// If the scripts to sync are unknown, such as when restoring or importing a keychain that
|
||||||
|
/// may include scripts that have been used, use [`full_scan`] with the keychain.
|
||||||
|
///
|
||||||
|
/// [`full_scan`]: EsploraExt::full_scan
|
||||||
#[allow(clippy::result_large_err)]
|
#[allow(clippy::result_large_err)]
|
||||||
fn scan_txs(
|
fn sync(
|
||||||
&self,
|
&self,
|
||||||
misc_spks: impl IntoIterator<Item = ScriptBuf>,
|
misc_spks: impl IntoIterator<Item = ScriptBuf>,
|
||||||
txids: impl IntoIterator<Item = Txid>,
|
txids: impl IntoIterator<Item = Txid>,
|
||||||
outpoints: impl IntoIterator<Item = OutPoint>,
|
outpoints: impl IntoIterator<Item = OutPoint>,
|
||||||
parallel_requests: usize,
|
parallel_requests: usize,
|
||||||
) -> Result<TxGraph<ConfirmationTimeHeightAnchor>, Error> {
|
) -> Result<TxGraph<ConfirmationTimeHeightAnchor>, Error>;
|
||||||
self.scan_txs_with_keychains(
|
|
||||||
[(
|
|
||||||
(),
|
|
||||||
misc_spks
|
|
||||||
.into_iter()
|
|
||||||
.enumerate()
|
|
||||||
.map(|(i, spk)| (i as u32, spk)),
|
|
||||||
)]
|
|
||||||
.into(),
|
|
||||||
txids,
|
|
||||||
outpoints,
|
|
||||||
usize::MAX,
|
|
||||||
parallel_requests,
|
|
||||||
)
|
|
||||||
.map(|(g, _)| g)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EsploraExt for esplora_client::BlockingClient {
|
impl EsploraExt for esplora_client::BlockingClient {
|
||||||
@@ -190,11 +178,9 @@ impl EsploraExt for esplora_client::BlockingClient {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scan_txs_with_keychains<K: Ord + Clone>(
|
fn full_scan<K: Ord + Clone>(
|
||||||
&self,
|
&self,
|
||||||
keychain_spks: BTreeMap<K, impl IntoIterator<Item = (u32, ScriptBuf)>>,
|
keychain_spks: BTreeMap<K, impl IntoIterator<Item = (u32, ScriptBuf)>>,
|
||||||
txids: impl IntoIterator<Item = Txid>,
|
|
||||||
outpoints: impl IntoIterator<Item = OutPoint>,
|
|
||||||
stop_gap: usize,
|
stop_gap: usize,
|
||||||
parallel_requests: usize,
|
parallel_requests: usize,
|
||||||
) -> Result<(TxGraph<ConfirmationTimeHeightAnchor>, BTreeMap<K, u32>), Error> {
|
) -> Result<(TxGraph<ConfirmationTimeHeightAnchor>, BTreeMap<K, u32>), Error> {
|
||||||
@@ -266,6 +252,31 @@ impl EsploraExt for esplora_client::BlockingClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok((graph, last_active_indexes))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sync(
|
||||||
|
&self,
|
||||||
|
misc_spks: impl IntoIterator<Item = ScriptBuf>,
|
||||||
|
txids: impl IntoIterator<Item = Txid>,
|
||||||
|
outpoints: impl IntoIterator<Item = OutPoint>,
|
||||||
|
parallel_requests: usize,
|
||||||
|
) -> Result<TxGraph<ConfirmationTimeHeightAnchor>, Error> {
|
||||||
|
let mut graph = self
|
||||||
|
.full_scan(
|
||||||
|
[(
|
||||||
|
(),
|
||||||
|
misc_spks
|
||||||
|
.into_iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, spk)| (i as u32, spk)),
|
||||||
|
)]
|
||||||
|
.into(),
|
||||||
|
usize::MAX,
|
||||||
|
parallel_requests,
|
||||||
|
)
|
||||||
|
.map(|(g, _)| g)?;
|
||||||
|
|
||||||
let mut txids = txids.into_iter();
|
let mut txids = txids.into_iter();
|
||||||
loop {
|
loop {
|
||||||
let handles = txids
|
let handles = txids
|
||||||
@@ -292,7 +303,7 @@ impl EsploraExt for esplora_client::BlockingClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for op in outpoints.into_iter() {
|
for op in outpoints {
|
||||||
if graph.get_tx(op.txid).is_none() {
|
if graph.get_tx(op.txid).is_none() {
|
||||||
if let Some(tx) = self.get_tx(&op.txid)? {
|
if let Some(tx) = self.get_tx(&op.txid)? {
|
||||||
let _ = graph.insert_tx(tx);
|
let _ = graph.insert_tx(tx);
|
||||||
@@ -317,7 +328,6 @@ impl EsploraExt for esplora_client::BlockingClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(graph)
|
||||||
Ok((graph, last_active_indexes))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,21 @@
|
|||||||
#![doc = include_str!("../README.md")]
|
#![doc = include_str!("../README.md")]
|
||||||
|
|
||||||
|
//! This crate is used for updating structures of [`bdk_chain`] with data from an Esplora server.
|
||||||
|
//!
|
||||||
|
//! The two primary methods are [`EsploraExt::sync`] and [`EsploraExt::full_scan`]. In most cases
|
||||||
|
//! [`EsploraExt::sync`] is used to sync the transaction histories of scripts that the application
|
||||||
|
//! cares about, for example the scripts for all the receive addresses of a Wallet's keychain that it
|
||||||
|
//! has shown a user. [`EsploraExt::full_scan`] is meant to be used when importing or restoring a
|
||||||
|
//! keychain where the range of possibly used scripts is not known. In this case it is necessary to
|
||||||
|
//! scan all keychain scripts until a number (the "stop gap") of unused scripts is discovered. For a
|
||||||
|
//! sync or full scan the user receives relevant blockchain data and output updates for [`bdk_chain`]
|
||||||
|
//! via a new [`TxGraph`] to be appended to any existing [`TxGraph`] data.
|
||||||
|
//!
|
||||||
|
//! Refer to [`example_esplora`] for a complete example.
|
||||||
|
//!
|
||||||
|
//! [`TxGraph`]: bdk_chain::tx_graph::TxGraph
|
||||||
|
//! [`example_esplora`]: https://github.com/bitcoindevkit/bdk/tree/master/example-crates/example_esplora
|
||||||
|
|
||||||
use bdk_chain::{BlockId, ConfirmationTimeHeightAnchor};
|
use bdk_chain::{BlockId, ConfirmationTimeHeightAnchor};
|
||||||
use esplora_client::TxStatus;
|
use esplora_client::TxStatus;
|
||||||
|
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ pub async fn test_update_tx_graph_without_keychain() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
let graph_update = env
|
let graph_update = env
|
||||||
.client
|
.client
|
||||||
.scan_txs(
|
.sync(
|
||||||
misc_spks.into_iter(),
|
misc_spks.into_iter(),
|
||||||
vec![].into_iter(),
|
vec![].into_iter(),
|
||||||
vec![].into_iter(),
|
vec![].into_iter(),
|
||||||
@@ -166,28 +166,10 @@ pub async fn test_async_update_tx_graph_gap_limit() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
// A scan with a gap limit of 2 won't find the transaction, but a scan with a gap limit of 3
|
// A scan with a gap limit of 2 won't find the transaction, but a scan with a gap limit of 3
|
||||||
// will.
|
// will.
|
||||||
let (graph_update, active_indices) = env
|
let (graph_update, active_indices) = env.client.full_scan(keychains.clone(), 2, 1).await?;
|
||||||
.client
|
|
||||||
.scan_txs_with_keychains(
|
|
||||||
keychains.clone(),
|
|
||||||
vec![].into_iter(),
|
|
||||||
vec![].into_iter(),
|
|
||||||
2,
|
|
||||||
1,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
assert!(graph_update.full_txs().next().is_none());
|
assert!(graph_update.full_txs().next().is_none());
|
||||||
assert!(active_indices.is_empty());
|
assert!(active_indices.is_empty());
|
||||||
let (graph_update, active_indices) = env
|
let (graph_update, active_indices) = env.client.full_scan(keychains.clone(), 3, 1).await?;
|
||||||
.client
|
|
||||||
.scan_txs_with_keychains(
|
|
||||||
keychains.clone(),
|
|
||||||
vec![].into_iter(),
|
|
||||||
vec![].into_iter(),
|
|
||||||
3,
|
|
||||||
1,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
assert_eq!(graph_update.full_txs().next().unwrap().txid, txid_4th_addr);
|
assert_eq!(graph_update.full_txs().next().unwrap().txid, txid_4th_addr);
|
||||||
assert_eq!(active_indices[&0], 3);
|
assert_eq!(active_indices[&0], 3);
|
||||||
|
|
||||||
@@ -209,24 +191,12 @@ pub async fn test_async_update_tx_graph_gap_limit() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
// A scan with gap limit 4 won't find the second transaction, but a scan with gap limit 5 will.
|
// A scan with gap limit 4 won't find the second transaction, but a scan with gap limit 5 will.
|
||||||
// The last active indice won't be updated in the first case but will in the second one.
|
// The last active indice won't be updated in the first case but will in the second one.
|
||||||
let (graph_update, active_indices) = env
|
let (graph_update, active_indices) = env.client.full_scan(keychains.clone(), 4, 1).await?;
|
||||||
.client
|
|
||||||
.scan_txs_with_keychains(
|
|
||||||
keychains.clone(),
|
|
||||||
vec![].into_iter(),
|
|
||||||
vec![].into_iter(),
|
|
||||||
4,
|
|
||||||
1,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
let txs: HashSet<_> = graph_update.full_txs().map(|tx| tx.txid).collect();
|
let txs: HashSet<_> = graph_update.full_txs().map(|tx| tx.txid).collect();
|
||||||
assert_eq!(txs.len(), 1);
|
assert_eq!(txs.len(), 1);
|
||||||
assert!(txs.contains(&txid_4th_addr));
|
assert!(txs.contains(&txid_4th_addr));
|
||||||
assert_eq!(active_indices[&0], 3);
|
assert_eq!(active_indices[&0], 3);
|
||||||
let (graph_update, active_indices) = env
|
let (graph_update, active_indices) = env.client.full_scan(keychains, 5, 1).await?;
|
||||||
.client
|
|
||||||
.scan_txs_with_keychains(keychains, vec![].into_iter(), vec![].into_iter(), 5, 1)
|
|
||||||
.await?;
|
|
||||||
let txs: HashSet<_> = graph_update.full_txs().map(|tx| tx.txid).collect();
|
let txs: HashSet<_> = graph_update.full_txs().map(|tx| tx.txid).collect();
|
||||||
assert_eq!(txs.len(), 2);
|
assert_eq!(txs.len(), 2);
|
||||||
assert!(txs.contains(&txid_4th_addr) && txs.contains(&txid_last_addr));
|
assert!(txs.contains(&txid_4th_addr) && txs.contains(&txid_last_addr));
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ pub fn test_update_tx_graph_without_keychain() -> anyhow::Result<()> {
|
|||||||
sleep(Duration::from_millis(10))
|
sleep(Duration::from_millis(10))
|
||||||
}
|
}
|
||||||
|
|
||||||
let graph_update = env.client.scan_txs(
|
let graph_update = env.client.sync(
|
||||||
misc_spks.into_iter(),
|
misc_spks.into_iter(),
|
||||||
vec![].into_iter(),
|
vec![].into_iter(),
|
||||||
vec![].into_iter(),
|
vec![].into_iter(),
|
||||||
@@ -164,22 +164,10 @@ pub fn test_update_tx_graph_gap_limit() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
// A scan with a gap limit of 2 won't find the transaction, but a scan with a gap limit of 3
|
// A scan with a gap limit of 2 won't find the transaction, but a scan with a gap limit of 3
|
||||||
// will.
|
// will.
|
||||||
let (graph_update, active_indices) = env.client.scan_txs_with_keychains(
|
let (graph_update, active_indices) = env.client.full_scan(keychains.clone(), 2, 1)?;
|
||||||
keychains.clone(),
|
|
||||||
vec![].into_iter(),
|
|
||||||
vec![].into_iter(),
|
|
||||||
2,
|
|
||||||
1,
|
|
||||||
)?;
|
|
||||||
assert!(graph_update.full_txs().next().is_none());
|
assert!(graph_update.full_txs().next().is_none());
|
||||||
assert!(active_indices.is_empty());
|
assert!(active_indices.is_empty());
|
||||||
let (graph_update, active_indices) = env.client.scan_txs_with_keychains(
|
let (graph_update, active_indices) = env.client.full_scan(keychains.clone(), 3, 1)?;
|
||||||
keychains.clone(),
|
|
||||||
vec![].into_iter(),
|
|
||||||
vec![].into_iter(),
|
|
||||||
3,
|
|
||||||
1,
|
|
||||||
)?;
|
|
||||||
assert_eq!(graph_update.full_txs().next().unwrap().txid, txid_4th_addr);
|
assert_eq!(graph_update.full_txs().next().unwrap().txid, txid_4th_addr);
|
||||||
assert_eq!(active_indices[&0], 3);
|
assert_eq!(active_indices[&0], 3);
|
||||||
|
|
||||||
@@ -201,24 +189,12 @@ pub fn test_update_tx_graph_gap_limit() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
// A scan with gap limit 4 won't find the second transaction, but a scan with gap limit 5 will.
|
// A scan with gap limit 4 won't find the second transaction, but a scan with gap limit 5 will.
|
||||||
// The last active indice won't be updated in the first case but will in the second one.
|
// The last active indice won't be updated in the first case but will in the second one.
|
||||||
let (graph_update, active_indices) = env.client.scan_txs_with_keychains(
|
let (graph_update, active_indices) = env.client.full_scan(keychains.clone(), 4, 1)?;
|
||||||
keychains.clone(),
|
|
||||||
vec![].into_iter(),
|
|
||||||
vec![].into_iter(),
|
|
||||||
4,
|
|
||||||
1,
|
|
||||||
)?;
|
|
||||||
let txs: HashSet<_> = graph_update.full_txs().map(|tx| tx.txid).collect();
|
let txs: HashSet<_> = graph_update.full_txs().map(|tx| tx.txid).collect();
|
||||||
assert_eq!(txs.len(), 1);
|
assert_eq!(txs.len(), 1);
|
||||||
assert!(txs.contains(&txid_4th_addr));
|
assert!(txs.contains(&txid_4th_addr));
|
||||||
assert_eq!(active_indices[&0], 3);
|
assert_eq!(active_indices[&0], 3);
|
||||||
let (graph_update, active_indices) = env.client.scan_txs_with_keychains(
|
let (graph_update, active_indices) = env.client.full_scan(keychains, 5, 1)?;
|
||||||
keychains,
|
|
||||||
vec![].into_iter(),
|
|
||||||
vec![].into_iter(),
|
|
||||||
5,
|
|
||||||
1,
|
|
||||||
)?;
|
|
||||||
let txs: HashSet<_> = graph_update.full_txs().map(|tx| tx.txid).collect();
|
let txs: HashSet<_> = graph_update.full_txs().map(|tx| tx.txid).collect();
|
||||||
assert_eq!(txs.len(), 2);
|
assert_eq!(txs.len(), 2);
|
||||||
assert!(txs.contains(&txid_4th_addr) && txs.contains(&txid_last_addr));
|
assert!(txs.contains(&txid_4th_addr) && txs.contains(&txid_last_addr));
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ authors = ["Bitcoin Dev Kit Developers"]
|
|||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bdk_chain = { path = "../chain", version = "0.6.0", features = [ "serde", "miniscript" ] }
|
bdk_chain = { path = "../chain", version = "0.7.0", features = [ "serde", "miniscript" ] }
|
||||||
bincode = { version = "1" }
|
bincode = { version = "1" }
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
|
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ where
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attempt to open existing [`Store`] file; create it if the file is non-existant.
|
/// Attempt to open existing [`Store`] file; create it if the file is non-existent.
|
||||||
///
|
///
|
||||||
/// Internally, this calls either [`open`] or [`create_new`].
|
/// Internally, this calls either [`open`] or [`create_new`].
|
||||||
///
|
///
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ use bdk_bitcoind_rpc::{
|
|||||||
Emitter,
|
Emitter,
|
||||||
};
|
};
|
||||||
use bdk_chain::{
|
use bdk_chain::{
|
||||||
bitcoin::{Block, Transaction},
|
bitcoin::{constants::genesis_block, Block, Transaction},
|
||||||
indexed_tx_graph, keychain,
|
indexed_tx_graph, keychain,
|
||||||
local_chain::{self, CheckPoint, LocalChain},
|
local_chain::{self, CheckPoint, LocalChain},
|
||||||
ConfirmationTimeHeightAnchor, IndexedTxGraph,
|
ConfirmationTimeHeightAnchor, IndexedTxGraph,
|
||||||
@@ -64,9 +64,6 @@ struct RpcArgs {
|
|||||||
/// Starting block height to fallback to if no point of agreement if found
|
/// Starting block height to fallback to if no point of agreement if found
|
||||||
#[clap(env = "FALLBACK_HEIGHT", long, default_value = "0")]
|
#[clap(env = "FALLBACK_HEIGHT", long, default_value = "0")]
|
||||||
fallback_height: u32,
|
fallback_height: u32,
|
||||||
/// The unused-scripts lookahead will be kept at this size
|
|
||||||
#[clap(long, default_value = "10")]
|
|
||||||
lookahead: u32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<RpcArgs> for Auth {
|
impl From<RpcArgs> for Auth {
|
||||||
@@ -120,10 +117,11 @@ fn main() -> anyhow::Result<()> {
|
|||||||
"[{:>10}s] loaded initial changeset from db",
|
"[{:>10}s] loaded initial changeset from db",
|
||||||
start.elapsed().as_secs_f32()
|
start.elapsed().as_secs_f32()
|
||||||
);
|
);
|
||||||
|
let (init_chain_changeset, init_graph_changeset) = init_changeset;
|
||||||
|
|
||||||
let graph = Mutex::new({
|
let graph = Mutex::new({
|
||||||
let mut graph = IndexedTxGraph::new(index);
|
let mut graph = IndexedTxGraph::new(index);
|
||||||
graph.apply_changeset(init_changeset.1);
|
graph.apply_changeset(init_graph_changeset);
|
||||||
graph
|
graph
|
||||||
});
|
});
|
||||||
println!(
|
println!(
|
||||||
@@ -131,7 +129,16 @@ fn main() -> anyhow::Result<()> {
|
|||||||
start.elapsed().as_secs_f32()
|
start.elapsed().as_secs_f32()
|
||||||
);
|
);
|
||||||
|
|
||||||
let chain = Mutex::new(LocalChain::from_changeset(init_changeset.0)?);
|
let chain = Mutex::new(if init_chain_changeset.is_empty() {
|
||||||
|
let genesis_hash = genesis_block(args.network).block_hash();
|
||||||
|
let (chain, chain_changeset) = LocalChain::from_genesis_hash(genesis_hash);
|
||||||
|
let mut db = db.lock().unwrap();
|
||||||
|
db.stage((chain_changeset, Default::default()));
|
||||||
|
db.commit()?;
|
||||||
|
chain
|
||||||
|
} else {
|
||||||
|
LocalChain::from_changeset(init_chain_changeset)?
|
||||||
|
});
|
||||||
println!(
|
println!(
|
||||||
"[{:>10}s] loaded local chain from changeset",
|
"[{:>10}s] loaded local chain from changeset",
|
||||||
start.elapsed().as_secs_f32()
|
start.elapsed().as_secs_f32()
|
||||||
@@ -161,13 +168,9 @@ fn main() -> anyhow::Result<()> {
|
|||||||
match rpc_cmd {
|
match rpc_cmd {
|
||||||
RpcCommands::Sync { rpc_args } => {
|
RpcCommands::Sync { rpc_args } => {
|
||||||
let RpcArgs {
|
let RpcArgs {
|
||||||
fallback_height,
|
fallback_height, ..
|
||||||
lookahead,
|
|
||||||
..
|
|
||||||
} = rpc_args;
|
} = rpc_args;
|
||||||
|
|
||||||
graph.lock().unwrap().index.set_lookahead_for_all(lookahead);
|
|
||||||
|
|
||||||
let chain_tip = chain.lock().unwrap().tip();
|
let chain_tip = chain.lock().unwrap().tip();
|
||||||
let rpc_client = rpc_args.new_client()?;
|
let rpc_client = rpc_args.new_client()?;
|
||||||
let mut emitter = Emitter::new(&rpc_client, chain_tip, fallback_height);
|
let mut emitter = Emitter::new(&rpc_client, chain_tip, fallback_height);
|
||||||
@@ -233,13 +236,10 @@ fn main() -> anyhow::Result<()> {
|
|||||||
}
|
}
|
||||||
RpcCommands::Live { rpc_args } => {
|
RpcCommands::Live { rpc_args } => {
|
||||||
let RpcArgs {
|
let RpcArgs {
|
||||||
fallback_height,
|
fallback_height, ..
|
||||||
lookahead,
|
|
||||||
..
|
|
||||||
} = rpc_args;
|
} = rpc_args;
|
||||||
let sigterm_flag = start_ctrlc_handler();
|
let sigterm_flag = start_ctrlc_handler();
|
||||||
|
|
||||||
graph.lock().unwrap().index.set_lookahead_for_all(lookahead);
|
|
||||||
let last_cp = chain.lock().unwrap().tip();
|
let last_cp = chain.lock().unwrap().tip();
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
|
|||||||
@@ -172,14 +172,7 @@ fn main() -> anyhow::Result<()> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
client
|
client
|
||||||
.scan(
|
.full_scan(tip, keychain_spks, stop_gap, scan_options.batch_size)
|
||||||
tip,
|
|
||||||
keychain_spks,
|
|
||||||
core::iter::empty(),
|
|
||||||
core::iter::empty(),
|
|
||||||
stop_gap,
|
|
||||||
scan_options.batch_size,
|
|
||||||
)
|
|
||||||
.context("scanning the blockchain")?
|
.context("scanning the blockchain")?
|
||||||
}
|
}
|
||||||
ElectrumCommands::Sync {
|
ElectrumCommands::Sync {
|
||||||
@@ -279,7 +272,7 @@ fn main() -> anyhow::Result<()> {
|
|||||||
drop((graph, chain));
|
drop((graph, chain));
|
||||||
|
|
||||||
let electrum_update = client
|
let electrum_update = client
|
||||||
.scan_without_keychain(tip, spks, txids, outpoints, scan_options.batch_size)
|
.sync(tip, spks, txids, outpoints, scan_options.batch_size)
|
||||||
.context("scanning the blockchain")?;
|
.context("scanning the blockchain")?;
|
||||||
(electrum_update, BTreeMap::new())
|
(electrum_update, BTreeMap::new())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -188,13 +188,7 @@ fn main() -> anyhow::Result<()> {
|
|||||||
// represents the last active spk derivation indices of keychains
|
// represents the last active spk derivation indices of keychains
|
||||||
// (`keychain_indices_update`).
|
// (`keychain_indices_update`).
|
||||||
let (graph_update, last_active_indices) = client
|
let (graph_update, last_active_indices) = client
|
||||||
.scan_txs_with_keychains(
|
.full_scan(keychain_spks, *stop_gap, scan_options.parallel_requests)
|
||||||
keychain_spks,
|
|
||||||
core::iter::empty(),
|
|
||||||
core::iter::empty(),
|
|
||||||
*stop_gap,
|
|
||||||
scan_options.parallel_requests,
|
|
||||||
)
|
|
||||||
.context("scanning for transactions")?;
|
.context("scanning for transactions")?;
|
||||||
|
|
||||||
let mut graph = graph.lock().expect("mutex must not be poisoned");
|
let mut graph = graph.lock().expect("mutex must not be poisoned");
|
||||||
@@ -312,7 +306,7 @@ fn main() -> anyhow::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let graph_update =
|
let graph_update =
|
||||||
client.scan_txs(spks, txids, outpoints, scan_options.parallel_requests)?;
|
client.sync(spks, txids, outpoints, scan_options.parallel_requests)?;
|
||||||
|
|
||||||
graph.lock().unwrap().apply_update(graph_update)
|
graph.lock().unwrap().apply_update(graph_update)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ fn main() -> Result<(), anyhow::Error> {
|
|||||||
relevant_txids,
|
relevant_txids,
|
||||||
},
|
},
|
||||||
keychain_update,
|
keychain_update,
|
||||||
) = client.scan(prev_tip, keychain_spks, None, None, STOP_GAP, BATCH_SIZE)?;
|
) = client.full_scan(prev_tip, keychain_spks, STOP_GAP, BATCH_SIZE)?;
|
||||||
|
|
||||||
println!();
|
println!();
|
||||||
|
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ async fn main() -> Result<(), anyhow::Error> {
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
let (update_graph, last_active_indices) = client
|
let (update_graph, last_active_indices) = client
|
||||||
.scan_txs_with_keychains(keychain_spks, None, None, STOP_GAP, PARALLEL_REQUESTS)
|
.full_scan(keychain_spks, STOP_GAP, PARALLEL_REQUESTS)
|
||||||
.await?;
|
.await?;
|
||||||
let missing_heights = update_graph.missing_heights(wallet.local_chain());
|
let missing_heights = update_graph.missing_heights(wallet.local_chain());
|
||||||
let chain_update = client.update_local_chain(prev_tip, missing_heights).await?;
|
let chain_update = client.update_local_chain(prev_tip, missing_heights).await?;
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ fn main() -> Result<(), anyhow::Error> {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let (update_graph, last_active_indices) =
|
let (update_graph, last_active_indices) =
|
||||||
client.scan_txs_with_keychains(keychain_spks, None, None, STOP_GAP, PARALLEL_REQUESTS)?;
|
client.full_scan(keychain_spks, STOP_GAP, PARALLEL_REQUESTS)?;
|
||||||
let missing_heights = update_graph.missing_heights(wallet.local_chain());
|
let missing_heights = update_graph.missing_heights(wallet.local_chain());
|
||||||
let chain_update = client.update_local_chain(prev_tip, missing_heights)?;
|
let chain_update = client.update_local_chain(prev_tip, missing_heights)?;
|
||||||
let update = Update {
|
let update = Update {
|
||||||
|
|||||||
Reference in New Issue
Block a user