7 unstable releases (3 breaking)

0.4.1 Mar 20, 2024
0.3.2 Aug 10, 2023
0.3.1 Jul 19, 2023
0.1.2 Jan 3, 2023
0.1.0 Nov 28, 2022

#909 in Magic Beans

Download history 50/week @ 2023-12-18 18/week @ 2023-12-25 53/week @ 2024-01-01 147/week @ 2024-01-08 124/week @ 2024-01-15 208/week @ 2024-01-22 190/week @ 2024-01-29 37/week @ 2024-02-05 176/week @ 2024-02-12 648/week @ 2024-02-19 589/week @ 2024-02-26 516/week @ 2024-03-04 420/week @ 2024-03-11 910/week @ 2024-03-18 480/week @ 2024-03-25 817/week @ 2024-04-01

2,633 downloads per month
Used in async-hwi

Apache-2.0

105KB
2.5K SLoC

Ledger Bitcoin application client

Client library in Rust for the Ledger Bitcoin application with minimal dependencies.

If you wish to contribute to this library, please read CONTRIBUTING.md.

Minimum Supported Rust Version

bitcoin_client_rs should always compile using Rust 1.60.

Getting started

The client::BitcoinClient struct implements the methods that call and interpret the commands between the Ledger device and your software.

pub struct BitcoinClient<T: Transport> {...}
impl<T: Transport> BitcoinClient<T> {
    pub fn get_extended_pubkey(
        &self,
        path: &bitcoin::util::bip32::DerivationPath,
        display: bool,
    ) -> Result<bitcoin::util::bip32::ExtendedPubKey, BitcoinClientError<T::Error>>;
}

It requires an internal connection implementing the client::Transport Trait.

pub trait Transport {
    type Error: Debug;
    fn exchange(&self, command: &APDUCommand) -> Result<(StatusWord, Vec<u8>), Self::Error>;
}

In order to satisfy this Trait, it is possible to import the ledger-transport-hid crate from https://github.com/Zondax/ledger-rs. Please, read the examples/ledger_hwi/src/transport.rs file to find an example.

The async feature

The optional feature async adds the async_client module to the crate and imports the async_trait library. The async_client::BitcoinClient struct is an asynchronous equivalent to the BitcoinClient struct. It requires an internal connection implementing the async_client::Transport Trait.

#[async_trait]
pub trait Transport {
    type Error: Debug;
    async fn exchange(&self, command: &APDUCommand) -> Result<(StatusWord, Vec<u8>), Self::Error>;
}

The no-std support

Work in progress.

Example

The code source for a simple tool to communicate with either a Ledger device or Speculos emulator can be found in the examples directory.

Example of a command to retrieve the extended pubkey with the given derivation path and display it on the device screen:

cargo run --package ledger_hwi -- \
get-extended-pubkey --derivation-path "m/44'/0'/0'/0/0" --display

Dependencies

~8.5MB
~100K SLoC