Skip to content

Commit c46b315

Browse files
committed
bindings: optimize error handling + typos fix
1 parent c5d2854 commit c46b315

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

lwk_bindings/src/error.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,30 @@ impl From<elements::bitcoin::secp256k1::Error> for LwkError {
177177
}
178178
}
179179

180+
impl From<elements::bitcoin::key::FromSliceError> for LwkError {
181+
fn from(value: elements::bitcoin::key::FromSliceError) -> Self {
182+
LwkError::Generic {
183+
msg: format!("{value:?}"),
184+
}
185+
}
186+
}
187+
188+
impl From<elements::bitcoin::key::ParsePublicKeyError> for LwkError {
189+
fn from(value: elements::bitcoin::key::ParsePublicKeyError) -> Self {
190+
LwkError::Generic {
191+
msg: format!("{value:?}"),
192+
}
193+
}
194+
}
195+
196+
impl From<elements::secp256k1_zkp::Error> for LwkError {
197+
fn from(value: elements::secp256k1_zkp::Error) -> Self {
198+
LwkError::Generic {
199+
msg: format!("{value:?}"),
200+
}
201+
}
202+
}
203+
180204
impl From<elements::bitcoin::bip32::Error> for LwkError {
181205
fn from(value: elements::bitcoin::bip32::Error) -> Self {
182206
LwkError::Generic {

lwk_bindings/src/types/public_key.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ impl FromStr for PublicKey {
4848
type Err = LwkError;
4949

5050
fn from_str(s: &str) -> Result<Self, Self::Err> {
51-
let inner = elements::bitcoin::PublicKey::from_str(s).map_err(|e| LwkError::Generic {
52-
msg: format!("Invalid public key: {e}"),
53-
})?;
51+
let inner = elements::bitcoin::PublicKey::from_str(s)?;
5452
Ok(Self { inner })
5553
}
5654
}
@@ -66,19 +64,14 @@ impl PublicKey {
6664
/// See [`elements::bitcoin::PublicKey::from_slice`].
6765
#[uniffi::constructor]
6866
pub fn from_bytes(bytes: &[u8]) -> Result<Arc<Self>, LwkError> {
69-
let inner =
70-
elements::bitcoin::PublicKey::from_slice(bytes).map_err(|e| LwkError::Generic {
71-
msg: format!("Invalid public key: {e}"),
72-
})?;
67+
let inner = elements::bitcoin::PublicKey::from_slice(bytes)?;
7368
Ok(Arc::new(PublicKey { inner }))
7469
}
7570

7671
/// Creates a `PublicKey` from a hex string.
7772
#[uniffi::constructor]
7873
pub fn from_hex(hex: &str) -> Result<Arc<Self>, LwkError> {
79-
let bytes = Vec::<u8>::from_hex(hex).map_err(|e| LwkError::Generic {
80-
msg: format!("Invalid hex: {e}"),
81-
})?;
74+
let bytes = Vec::<u8>::from_hex(hex)?;
8275
Self::from_bytes(&bytes)
8376
}
8477

lwk_bindings/src/types/tx_sequence.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::LwkError;
22

33
use std::sync::Arc;
44

5-
/// Bitcoin transaction input sequence number.
5+
/// Transaction input sequence number.
66
///
77
/// See [`elements::Sequence`] for more details.
88
#[derive(uniffi::Object, PartialEq, Eq, Debug, Clone, Copy)]

0 commit comments

Comments
 (0)