Skip to main content

Crate trezor_connect_rs

Crate trezor_connect_rs 

Source
Expand description

§Trezor Connect

A Rust library for communicating with Trezor hardware wallets. Bitcoin-only. Supports USB and Bluetooth connectivity.

§Supported Devices

  • Trezor Safe 7 - Bluetooth (THP v2, Noise XX encrypted)
  • Trezor Safe 5 / Safe 3 / Model T / Model One - USB (Protocol v1)

§Feature Flags

  • usb - USB transport via libusb (enabled by default)
  • bluetooth - Bluetooth transport via btleplug (enabled by default)
  • os-keychain - OS-native credential storage for Bluetooth pairing (macOS Keychain, Windows Credential Manager, Linux Secret Service)

§Quick Start

use std::sync::Arc;
use trezor_connect_rs::{Trezor, GetAddressParams, TrezorUiCallback};

/// Implement this trait to handle PIN/passphrase prompts from your UI.
struct MyUiCallback;
impl TrezorUiCallback for MyUiCallback {
    fn on_pin_request(&self) -> Option<String> {
        // Show PIN matrix UI, return the entered PIN or None to cancel
        Some("123456".to_string())
    }
    fn on_passphrase_request(&self, on_device: bool) -> Option<String> {
        if on_device { Some(String::new()) } else { Some(String::new()) }
    }
}

#[tokio::main]
async fn main() -> trezor_connect_rs::Result<()> {
    let mut trezor = Trezor::new()
        .with_credential_store("~/.trezor-credentials.json")
        .with_ui_callback(Arc::new(MyUiCallback))
        .build()
        .await?;

    // Scan for USB and Bluetooth devices
    let devices = trezor.scan().await?;
    if devices.is_empty() {
        return Ok(());
    }

    let mut device = trezor.connect(&devices[0]).await?;
    device.initialize().await?;

    let addr = device.get_address(GetAddressParams {
        path: "m/84'/0'/0'/0/0".into(),
        show_on_trezor: true,
        ..Default::default()
    }).await?;
    println!("Address: {}", addr.address);

    device.disconnect().await?;
    Ok(())
}

§Low-Level API

For protocol-level access, use TrezorClient with a transport directly:

use trezor_connect_rs::{TrezorClient, UsbTransport, Transport};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut transport = UsbTransport::new()?;
    transport.init().await?;

    let devices = transport.enumerate().await?;
    let mut client = TrezorClient::new(transport);
    client.acquire(&devices[0].path).await?;

    let features = client.initialize().await?;
    println!("Connected to: {}", features.label.as_deref().unwrap_or_default());

    client.release().await?;
    Ok(())
}

Re-exports§

pub use error::TrezorError;
pub use error::Result;
pub use trezor::Trezor;
pub use trezor::TrezorBuilder;
pub use trezor::PairingCallback;
pub use connected_device::ConnectedDevice;
pub use device_info::DeviceInfo;
pub use device_info::TransportType;
pub use credential_store::CredentialStore;
pub use credential_store::StoredCredential;
pub use ui_callback::TrezorUiCallback;
pub use device::TrezorClient;
pub use transport::Transport;
pub use transport::usb::UsbTransport;
pub use transport::bluetooth::BluetoothTransport;
pub use transport::callback::CallbackTransport;
pub use transport::callback::TransportCallback;
pub use transport::callback::CallbackDeviceInfo;
pub use transport::callback::CallbackResult;
pub use transport::callback::CallbackReadResult;
pub use transport::callback::CallbackMessageResult;
pub use params::*;
pub use responses::*;
pub use types::bitcoin::*;
pub use types::network::*;

Modules§

api
High-level Bitcoin API.
compose
Transaction composition engine.
connected_device
Connected device wrapper with high-level Bitcoin API.
constants
Constants for Trezor device communication.
credential_store
Credential storage for Bluetooth pairing credentials.
device
Device abstraction layer.
device_info
Unified device information for USB and Bluetooth devices.
error
Error types for the Trezor Connect library.
params
Parameter types for Trezor API methods.
protocol
Protocol layer for Trezor communication.
protos
Auto-generated protobuf types from trezor-firmware.
responses
Response types for Trezor API methods.
transport
Transport layer for Trezor communication.
trezor
Main Trezor manager with unified device discovery and connection.
types
Type definitions for Trezor communication.
ui_callback
UI callback trait for PIN and passphrase input.