Skip to content

Commit 75e4fcc

Browse files
authored
feat: add from_string constructor for hash-like types
1 parent c04f610 commit 75e4fcc

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

bdk-ffi/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,6 +1516,9 @@ impl From<BdkEncodeError> for TransactionError {
15161516
pub enum HashParseError {
15171517
#[error("invalid hash: expected length 32 bytes, got {len} bytes")]
15181518
InvalidHash { len: u32 },
1519+
1520+
#[error("invalid hex string: {hex}")]
1521+
InvalidHexString { hex: String },
15191522
}
15201523

15211524
impl From<bdk_kyoto::builder::SqlInitializationError> for CbfBuilderError {

bdk-ffi/src/macros.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ macro_rules! impl_hash_like {
3535
Ok(Self(hash_like))
3636
}
3737

38+
/// Construct a hash-like type from a hex string.
39+
#[uniffi::constructor]
40+
pub fn from_string(hex: String) -> Result<Self, HashParseError> {
41+
hex.parse::<$core_type>()
42+
.map(Self)
43+
.map_err(|_| HashParseError::InvalidHexString { hex })
44+
}
45+
3846
/// Serialize this type into a 32 byte array.
3947
pub fn serialize(&self) -> Vec<u8> {
4048
serialize(&self.0)

0 commit comments

Comments
 (0)