Skip to content

Commit

Permalink
lib: add Txid and Outpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
rustaceanrob committed Aug 29, 2024
1 parent 142f5ab commit d72f189
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/bitcoin.udl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ interface FeeRate {
u64 to_sat_per_kwu();
};

[Custom]
typedef string Txid;

dictionary OutPoint {
Txid txid;
u32 vout;
};

[NonExhaustive]
enum Network {
"Bitcoin",
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub use bitcoin::amount::ParseAmountError as BitcoinParseAmountError;
use bitcoin::amount::ParseAmountError as BitcoinParseAmountError;

#[derive(Debug, thiserror::Error)]
pub enum FeeRateError {
Expand Down
17 changes: 15 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use bitcoin::Amount as BitcoinAmount;
use bitcoin::FeeRate as BitcoinFeeRate;
use bitcoin::ScriptBuf as BitcoinScriptBuf;
pub use bitcoin::OutPoint;
pub use bitcoin::Txid;

use error::FeeRateError;
use error::ParseAmountError;
Expand Down Expand Up @@ -67,8 +69,8 @@ impl Amount {
}

pub fn from_btc(btc: f64) -> Result<Self, ParseAmountError> {
let bdk_amount = BitcoinAmount::from_btc(btc).map_err(ParseAmountError::from)?;
Ok(Amount(bdk_amount))
let bitcoin_amount = BitcoinAmount::from_btc(btc).map_err(ParseAmountError::from)?;
Ok(Amount(bitcoin_amount))
}

pub fn to_sat(&self) -> u64 {
Expand All @@ -83,4 +85,15 @@ impl Amount {
impl_from_core_type!(Amount, BitcoinAmount);
impl_from_ffi_type!(Amount, BitcoinAmount);

impl UniffiCustomTypeConverter for Txid {
type Builtin = String;
fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
Ok(val.parse::<Txid>()?)
}

fn from_custom(obj: Self) -> Self::Builtin {
obj.to_string()
}
}

uniffi::include_scaffolding!("bitcoin");

0 comments on commit d72f189

Please sign in to comment.