Crate ledger_lib

Crate ledger_lib 

Source
Expand description

A Ledger hardware wallet communication library

Device provides a high-level API for exchanging APDUs with Ledger devices using the [ledger_proto] traits. This is suitable for extension with application-specific interface traits, and automatically implemented over Exchange for low-level byte exchange with devices.

LedgerProvider and LedgerHandle provide a high-level tokio-compatible Transport for application integration, supporting connecting to and interacting with ledger devices.

Low-level Transport implementations are provided for USB/HID, BLE and TCP, with a Generic implementation providing a common interface over all enabled transports.

Note that futures produced by async methods of Transport and Device are not Send, i.e. they can’t be used with multi-threaded async executors. The reason is that UsbTransport and UsbDevice are not Send. This is a (probably redundant) precaution against potential quirks that might occur when the underlying hidapi objects change threads.
In a multi-threaded async environment use LedgerProvider and LedgerHandle instead.

§Examples

use ledger_lib::{LedgerProvider, Filters, Transport, Device, DEFAULT_TIMEOUT};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Fetch provider handle
    let mut provider = LedgerProvider::init().await;

    // List available devices
    let devices = provider.list(Filters::Any).await?;

    // Check we have -a- device to connect to
    if devices.is_empty() {
        return Err(anyhow::anyhow!("No devices found"));
    }

    // Connect to the first device
    let mut ledger = provider.connect(devices[0].clone()).await?;

    // Request device information
    let info = ledger.app_info(DEFAULT_TIMEOUT).await?;
    println!("info: {info:?}");

    Ok(())
}

Re-exports§

pub use info::LedgerInfo;
pub use transport::Transport;

Modules§

info
Device information types and connection filters
transport
Low-level transport implementations for communication with ledger devices and nano apps

Structs§

LaunchAppOpts
LedgerHandle
Ledger device handle for interacting with LedgerProvider backed devices
LedgerProvider
Ledger provider manages device discovery and connection

Enums§

Error
Ledger interface error type
Filters
Device discovery filter

Constants§

DEFAULT_TIMEOUT
Default timeout helper for use with Device and Exchange/NonSendExchange

Traits§

Device
Device provides a high-level interface exchanging APDU objects with implementers of Exchange.
Exchange
Exchange trait provides a low-level interface for byte-wise exchange of APDU commands with a ledger devices.
NonSendExchange
NonSendExchange trait provides a low-level interface for byte-wise exchange of APDU commands with a ledger devices.

Functions§

launch_app
Launch an application by name and return a device handle.