trezor_connect_rs/lib.rs
1//! # Trezor Connect
2//!
3//! A Rust library for communicating with Trezor hardware wallets.
4//! Bitcoin-only. Supports USB and Bluetooth connectivity.
5//!
6//! ## Supported Devices
7//!
8//! - **Trezor Safe 7** - Bluetooth (THP v2, Noise XX encrypted)
9//! - **Trezor Safe 5 / Safe 3 / Model T / Model One** - USB (Protocol v1)
10//!
11//! ## Feature Flags
12//!
13//! - `usb` - USB transport via libusb (enabled by default)
14//! - `bluetooth` - Bluetooth transport via btleplug (enabled by default)
15//! - `os-keychain` - OS-native credential storage for Bluetooth pairing
16//! (macOS Keychain, Windows Credential Manager, Linux Secret Service)
17//!
18//! ## Quick Start
19//!
20//! ```rust,no_run
21//! use std::sync::Arc;
22//! use trezor_connect_rs::{Trezor, GetAddressParams, TrezorUiCallback};
23//!
24//! /// Implement this trait to handle PIN/passphrase prompts from your UI.
25//! struct MyUiCallback;
26//! impl TrezorUiCallback for MyUiCallback {
27//! fn on_pin_request(&self) -> Option<String> {
28//! // Show PIN matrix UI, return the entered PIN or None to cancel
29//! Some("123456".to_string())
30//! }
31//! fn on_passphrase_request(&self, on_device: bool) -> Option<String> {
32//! if on_device { Some(String::new()) } else { Some(String::new()) }
33//! }
34//! }
35//!
36//! #[tokio::main]
37//! async fn main() -> trezor_connect_rs::Result<()> {
38//! let mut trezor = Trezor::new()
39//! .with_credential_store("~/.trezor-credentials.json")
40//! .with_ui_callback(Arc::new(MyUiCallback))
41//! .build()
42//! .await?;
43//!
44//! // Scan for USB and Bluetooth devices
45//! let devices = trezor.scan().await?;
46//! if devices.is_empty() {
47//! return Ok(());
48//! }
49//!
50//! let mut device = trezor.connect(&devices[0]).await?;
51//! device.initialize().await?;
52//!
53//! let addr = device.get_address(GetAddressParams {
54//! path: "m/84'/0'/0'/0/0".into(),
55//! show_on_trezor: true,
56//! ..Default::default()
57//! }).await?;
58//! println!("Address: {}", addr.address);
59//!
60//! device.disconnect().await?;
61//! Ok(())
62//! }
63//! ```
64//!
65//! ## Low-Level API
66//!
67//! For protocol-level access, use [`TrezorClient`] with a transport directly:
68//!
69//! ```rust,no_run
70//! use trezor_connect_rs::{TrezorClient, UsbTransport, Transport};
71//!
72//! #[tokio::main]
73//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
74//! let mut transport = UsbTransport::new()?;
75//! transport.init().await?;
76//!
77//! let devices = transport.enumerate().await?;
78//! let mut client = TrezorClient::new(transport);
79//! client.acquire(&devices[0].path).await?;
80//!
81//! let features = client.initialize().await?;
82//! println!("Connected to: {}", features.label.as_deref().unwrap_or_default());
83//!
84//! client.release().await?;
85//! Ok(())
86//! }
87//! ```
88
89// Core modules
90pub mod constants;
91pub mod error;
92pub mod protocol;
93pub mod protos;
94pub mod transport;
95pub mod device;
96pub mod api;
97pub mod types;
98pub mod compose;
99
100// High-level API modules
101pub mod trezor;
102pub mod connected_device;
103pub mod device_info;
104pub mod params;
105pub mod responses;
106pub mod credential_store;
107
108// UI callback for PIN/passphrase input
109pub mod ui_callback;
110
111// PSBT support (optional, requires `psbt` feature)
112#[cfg(feature = "psbt")]
113pub mod psbt;
114
115// Re-export error types
116pub use error::{TrezorError, Result};
117
118// Re-export high-level API (primary interface)
119pub use trezor::{Trezor, TrezorBuilder, PairingCallback};
120pub use connected_device::ConnectedDevice;
121pub use device_info::{DeviceInfo, TransportType};
122pub use params::*;
123pub use responses::*;
124pub use credential_store::{CredentialStore, StoredCredential};
125pub use ui_callback::TrezorUiCallback;
126
127// Re-export low-level API for advanced users
128pub use device::TrezorClient;
129pub use types::bitcoin::*;
130pub use types::network::*;
131pub use transport::Transport;
132
133// Re-export transport types based on features
134#[cfg(feature = "usb")]
135pub use transport::usb::UsbTransport;
136
137#[cfg(feature = "bluetooth")]
138pub use transport::bluetooth::BluetoothTransport;
139
140// Re-export callback transport (always available)
141pub use transport::callback::{
142 CallbackTransport, TransportCallback, CallbackDeviceInfo, CallbackResult, CallbackReadResult,
143 CallbackMessageResult,
144};