diff --git a/build.rs b/build.rs deleted file mode 100644 index 2787def..0000000 --- a/build.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - uniffi::generate_scaffolding("./src/bitcoin.udl").unwrap(); -} diff --git a/src/bitcoin.udl b/src/bitcoin.udl deleted file mode 100644 index cee7d09..0000000 --- a/src/bitcoin.udl +++ /dev/null @@ -1,72 +0,0 @@ -namespace bitcoin {}; - -// ------------------------------------------------------------------------ -// Core types -// ------------------------------------------------------------------------ - -interface Script { - constructor(sequence raw_output_script); - - sequence to_bytes(); -}; - -interface Amount { - [Name=from_sat] - constructor(u64 from_sat); - - [Name=from_btc, Throws=ParseAmountError] - constructor(f64 from_btc); - - u64 to_sat(); - - f64 to_btc(); -}; - -interface FeeRate { - [Name=from_sat_per_vb, Throws=FeeRateError] - constructor(u64 sat_per_vb); - - [Name=from_sat_per_kwu] - constructor(u64 sat_per_kwu); - - u64 to_sat_per_vb_ceil(); - - u64 to_sat_per_vb_floor(); - - u64 to_sat_per_kwu(); -}; - -[Custom] -typedef string Txid; - -dictionary OutPoint { - Txid txid; - u32 vout; -}; - -[NonExhaustive] -enum Network { - "Bitcoin", - "Testnet", - "Signet", - "Regtest", -}; - -// ------------------------------------------------------------------------ -// Errors -// ------------------------------------------------------------------------ - -[Error] -interface ParseAmountError { - OutOfRange(); - TooPrecise(); - MissingDigits(); - InputTooLarge(); - InvalidCharacter(string error_message); - OtherParseAmountErr(); -}; - -[Error] -interface FeeRateError { - ArithmeticOverflow(); -}; diff --git a/src/error.rs b/src/error.rs index 0d3b7ab..5ac662d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,12 +1,12 @@ use bitcoin::amount::ParseAmountError as BitcoinParseAmountError; -#[derive(Debug, thiserror::Error)] +#[derive(Debug, thiserror::Error, uniffi::Error)] pub enum FeeRateError { #[error("arithmetic overflow on feerate")] ArithmeticOverflow, } -#[derive(Debug, thiserror::Error)] +#[derive(Debug, thiserror::Error, uniffi::Error)] pub enum ParseAmountError { #[error("amount out of range")] OutOfRange, diff --git a/src/lib.rs b/src/lib.rs index 0a54e58..0b6669e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,15 @@ +uniffi::setup_scaffolding!(); + use bitcoin::Amount as BitcoinAmount; use bitcoin::FeeRate as BitcoinFeeRate; use bitcoin::ScriptBuf as BitcoinScriptBuf; -pub use bitcoin::OutPoint; -pub use bitcoin::Txid; +use bitcoin::TxIn as BitcoinTxIn; +use bitcoin::Weight as BitcoinWeight; +use bitcoin::Sequence as BitcoinSequence; +use bitcoin::Witness as BitcoinWitness; +use bitcoin::Transaction as BitcoinTransaction; +use bitcoin::transaction::Version as BitcoinTxVersion; +use bitcoin::locktime::absolute::LockTime as BitcoinLockTime; use error::FeeRateError; use error::ParseAmountError; @@ -12,10 +19,12 @@ mod macros; pub mod error; pub use bitcoin::Network; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, uniffi::Object)] pub struct FeeRate(pub BitcoinFeeRate); +#[uniffi::export] impl FeeRate { + #[uniffi::constructor(name = "from_sat_per_vb")] pub fn from_sat_per_vb(sat_per_vb: u64) -> Result { let fee_rate: Option = BitcoinFeeRate::from_sat_per_vb(sat_per_vb); match fee_rate { @@ -24,6 +33,7 @@ impl FeeRate { } } + #[uniffi::constructor(name = "from_sat_per_kwu")] pub fn from_sat_per_kwu(sat_per_kwu: u64) -> Self { FeeRate(BitcoinFeeRate::from_sat_per_kwu(sat_per_kwu)) } @@ -44,30 +54,36 @@ impl FeeRate { impl_from_core_type!(FeeRate, BitcoinFeeRate); impl_from_ffi_type!(FeeRate, BitcoinFeeRate); -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct Script(pub BitcoinScriptBuf); +#[derive(Clone, Debug, PartialEq, Eq, uniffi::Record)] +pub struct Script { + buffer: Vec, +} -impl Script { - pub fn new(raw_output_script: Vec) -> Self { - let script: BitcoinScriptBuf = raw_output_script.into(); - Script(script) +impl From for Script { + fn from(script: BitcoinScriptBuf) -> Self { + Script { + buffer: script.to_bytes(), + } } +} - pub fn to_bytes(&self) -> Vec { - self.0.to_bytes() +impl From