Skip to content

Commit 5a5cdca

Browse files
Small docs updates
- Makes the docs less verbose, removing useless arguments explanations - Add some code examples in the docs - only one test is run and the others are only compiled, as we can't run them in serial and if we run them in parallel, they fail - Improve expect message in test_enumerate, to explain what to do if the test fails
1 parent 4202320 commit 5a5cdca

File tree

2 files changed

+54
-39
lines changed

2 files changed

+54
-39
lines changed

src/interface.rs

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ impl Deref for HWIClient {
5959

6060
impl HWIClient {
6161
/// Lists all HW devices currently connected.
62+
/// ```no_run
63+
/// # use hwi::HWIClient;
64+
/// # use hwi::error::Error;
65+
/// # fn main() -> Result<(), Error> {
66+
/// let devices = HWIClient::enumerate()?;
67+
/// for device in devices {
68+
/// println!("I can see a {} here 😄", device.model);
69+
/// }
70+
/// # Ok(())
71+
/// # }
72+
/// ```
6273
pub fn enumerate() -> Result<Vec<HWIDevice>, Error> {
6374
let libs = HWILib::initialize()?;
6475
Python::with_gil(|py| {
@@ -68,11 +79,28 @@ impl HWIClient {
6879
})
6980
}
7081

71-
/// Finds the Python object of the device corresponding to Device
72-
/// # Arguements
73-
/// * `device` - The device for which the Python object will be passed
74-
/// * `expert` - Whether the device should be opened in expert mode (enables additional output for some actions)
75-
/// * `chain` - The Chain this client will be using
82+
/// Returns the HWIClient for a certain device. You can list all the available devices using
83+
/// [`enumerate`](HWIClient::enumerate).
84+
///
85+
/// Setting `expert` to `true` will enable additional output for some commands.
86+
/// ```
87+
/// # use hwi::HWIClient;
88+
/// # use hwi::types::*;
89+
/// # use hwi::error::Error;
90+
/// # fn main() -> Result<(), Error> {
91+
/// let devices = HWIClient::enumerate()?;
92+
/// for device in devices {
93+
/// let client = HWIClient::get_client(&device, false, HWIChain::Test)?;
94+
/// let xpub = client.get_master_xpub(HWIAddressType::Tap, 0)?;
95+
/// println!(
96+
/// "I can see a {} here, and its xpub is {}",
97+
/// device.model,
98+
/// xpub.to_string()
99+
/// );
100+
/// }
101+
/// # Ok(())
102+
/// # }
103+
/// ```
76104
pub fn get_client(
77105
device: &HWIDevice,
78106
expert: bool,
@@ -92,7 +120,7 @@ impl HWIClient {
92120
})
93121
}
94122

95-
/// Returns the master xpub of a device.
123+
/// Returns the master xpub of a device, given the address type and the account number.
96124
pub fn get_master_xpub(
97125
&self,
98126
addrtype: HWIAddressType,
@@ -109,9 +137,7 @@ impl HWIClient {
109137
})
110138
}
111139

112-
/// Returns a psbt signed.
113-
/// # Arguments
114-
/// * `psbt` - The PSBT to be signed.
140+
/// Signs a PSBT.
115141
pub fn sign_tx(
116142
&self,
117143
psbt: &PartiallySignedTransaction,
@@ -128,10 +154,7 @@ impl HWIClient {
128154
})
129155
}
130156

131-
/// Returns the xpub of a device.
132-
/// # Arguments
133-
/// * `path` - The derivation path to derive the key.
134-
/// * `expert` - Whether the device should be opened in expert mode (enables additional output for some actions)
157+
/// Returns the xpub of a device. If `expert` is set, additional output is returned.
135158
pub fn get_xpub(
136159
&self,
137160
path: &DerivationPath,
@@ -150,9 +173,6 @@ impl HWIClient {
150173
}
151174

152175
/// Signs a message.
153-
/// # Arguments
154-
/// * `message` - The message to sign.
155-
/// * `path` - The derivation path to derive the key.
156176
pub fn sign_message(
157177
&self,
158178
message: &str,
@@ -171,10 +191,10 @@ impl HWIClient {
171191
}
172192

173193
/// Returns an array of keys that can be imported in Bitcoin core using importmulti
174-
/// # Arguments
194+
///
175195
/// * `keypool` - `keypool` value in result. Check bitcoin core importmulti documentation for further information
176196
/// * `internal` - Whether to use internal (change) or external keys
177-
/// * `addr_type` - HWIAddressType to use
197+
/// * `addr_type` - Address type to use
178198
/// * `addr_all` - Whether to return a multiple descriptors for every address type
179199
/// * `account` - Optional BIP43 account to use
180200
/// * `path` - The derivation path to derive the keys.
@@ -218,9 +238,7 @@ impl HWIClient {
218238
})
219239
}
220240

221-
/// Returns device descriptors
222-
/// # Arguments
223-
/// * `account` - Optional BIP43 account to use.
241+
/// Returns device descriptors. You can optionally specify a BIP43 account to use.
224242
pub fn get_descriptors(&self, account: Option<u32>) -> Result<HWIDescriptor, Error> {
225243
Python::with_gil(|py| {
226244
let func_args = (&self.hw_client, account.unwrap_or(0));
@@ -234,9 +252,7 @@ impl HWIClient {
234252
})
235253
}
236254

237-
/// Returns an address given a descriptor.
238-
/// # Arguments
239-
/// * `descriptor` - The descriptor to use. HWI doesn't support descriptors checksums.
255+
/// Returns an address given a descriptor. Note that HWI doesn't support descriptors checksums.
240256
pub fn display_address_with_desc(&self, descriptor: &str) -> Result<HWIAddress, Error> {
241257
Python::with_gil(|py| {
242258
let path = py.None();
@@ -251,10 +267,7 @@ impl HWIClient {
251267
})
252268
}
253269

254-
/// Returns an address given pat and address type
255-
/// # Arguments
256-
/// * `path` - The derivation path to use.
257-
/// * `address_type` - Address type to use.
270+
/// Returns an address given path and address type.
258271
pub fn display_address_with_path(
259272
&self,
260273
path: &DerivationPath,
@@ -274,9 +287,9 @@ impl HWIClient {
274287
}
275288

276289
/// Install the udev rules to the local machine.
277-
/// The rules will be copied from the source to the location.
278-
/// The default source location is "./udev"
279-
/// The default destination location is "/lib/udev/rules.d"
290+
///
291+
/// The rules will be copied from the source to the location; the default source location is
292+
/// `./udev`, the default destination location is `/lib/udev/rules.d`
280293
pub fn install_udev_rules(source: Option<&str>, location: Option<&str>) -> Result<(), Error> {
281294
Python::with_gil(|py| {
282295
let libs = HWILib::initialize()?;

src/lib.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
//! Rust wrapper for [HWI](https://github.com/bitcoin-core/HWI/).
1+
//! Rust wrapper for the [Bitcoin Hardware Wallet Interface](https://github.com/bitcoin-core/HWI/).
22
//!
3-
//! # Example
4-
//! ```
3+
//! # Example - display address with path
4+
//! ```no_run
55
//! use bitcoin::util::bip32::{ChildNumber, DerivationPath};
66
//! use hwi::error::Error;
77
//! use hwi::interface::HWIClient;
@@ -13,11 +13,11 @@
1313
//! let devices = HWIClient::enumerate()?;
1414
//! let device = devices.first().expect("No devices");
1515
//! // Create a client for a device
16-
//! let client = HWIClient::get_client(&device, true, types::HWIChain::Test).unwrap();
16+
//! let client = HWIClient::get_client(&device, true, types::HWIChain::Test)?;
17+
//! // Display the address from path
1718
//! let derivation_path = DerivationPath::from_str("m/44'/1'/0'/0/0").unwrap();
18-
//! let hwi_address = client
19-
//! .display_address_with_path(&derivation_path, types::HWIAddressType::Legacy)
20-
//! .unwrap();
19+
//! let hwi_address =
20+
//! client.display_address_with_path(&derivation_path, types::HWIAddressType::Tap)?;
2121
//! println!("{}", hwi_address.address);
2222
//! Ok(())
2323
//! }
@@ -50,7 +50,9 @@ mod tests {
5050

5151
fn get_first_device() -> HWIClient {
5252
HWIClient::get_client(
53-
HWIClient::enumerate().unwrap().first().expect("No devices"),
53+
HWIClient::enumerate().unwrap().first().expect(
54+
"No devices found. Either plug in a hardware wallet, or start a simulator.",
55+
),
5456
true,
5557
types::HWIChain::Test,
5658
)

0 commit comments

Comments
 (0)