志宇 eabeb6ccb1
Implement linked-list LocalChain and update chain-src crates/examples
This commit changes the `LocalChain` implementation to have blocks
stored as a linked-list. This allows the data-src thread to hold a
shared ref to a single checkpoint and have access to the whole history
of checkpoints without cloning or keeping a lock on `LocalChain`.

The APIs of `bdk::Wallet`, `esplora` and `electrum` are also updated to
reflect these changes. Note that the `esplora` crate is rewritten to
anchor txs in the confirmation block (using the esplora API's tx status
block_hash). This guarantees 100% consistency between anchor blocks and
their transactions (instead of anchoring txs to the latest tip).
`ExploraExt` now has separate methods for updating the `TxGraph` and
`LocalChain`.

A new method `TxGraph::missing_blocks` is introduced for finding
"floating anchors" of a `TxGraph` update (given a chain).

Additional changes:

* `test_local_chain.rs` is refactored to make test cases easier to
  write. Additional tests are also added.
* Examples are updated.
* Fix `tempfile` dev dependency of `bdk_file_store` to work with MSRV

Co-authored-by: LLFourn <lloyd.fourn@gmail.com>
2023-07-28 11:30:16 +08:00
..
2023-03-02 10:56:22 +01:00
2023-07-04 12:28:06 -05:00
2023-03-02 10:56:22 +01:00
2023-03-02 10:56:22 +01:00
2023-03-02 10:56:22 +01:00

BDK

A modern, lightweight, descriptor-based wallet library written in Rust!

Crate Info MIT or Apache-2.0 Licensed CI Status API Docs Rustc Version 1.57.0+ Chat on Discord

Project Homepage | Documentation

bdk

The bdk crate provides the Wallet type which is a simple, high-level interface built from the low-level components of bdk_chain. Wallet is a good starting point for many simple applications as well as a good demonstration of how to use the other mechanisms to construct a wallet. It has two keychains (external and internal) which are defined by miniscript descriptors and uses them to generate addresses. When you give it chain data it also uses the descriptors to find transaction outputs owned by them. From there, you can create and sign transactions.

For more information, see the Wallet's documentation.

Blockchain data

In order to get blockchain data for Wallet to consume, you have to put it into particular form. Right now this is KeychainScan which is defined in bdk_chain.

This can be created manually or from blockchain-scanning crates.

Blockchain Data Sources

  • bdk_esplora: Grabs blockchain data from Esplora for updating BDK structures.
  • bdk_electrum: Grabs blockchain data from Electrum for updating BDK structures.

Examples

Persistence

To persist the Wallet on disk, Wallet needs to be constructed with a Persist implementation.

Implementations

Example

use bdk::{bitcoin::Network, wallet::{AddressIndex, Wallet}};

fn main() {
    // a type that implements `Persist`
    let db = ();

    let descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/0'/0'/0/*)";
    let mut wallet = Wallet::new(descriptor, None, db, Network::Testnet).expect("should create");

    // get a new address (this increments revealed derivation index)
    println!("revealed address: {}", wallet.get_address(AddressIndex::New));
    println!("staged changes: {:?}", wallet.staged());
    // persist changes
    wallet.commit().expect("must save");
}

Testing

Unit testing

cargo test

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.