Skip to content

Commit

Permalink
lib: add TxIn
Browse files Browse the repository at this point in the history
  • Loading branch information
rustaceanrob committed Sep 12, 2024
1 parent 537532a commit b2e23ec
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/bitcoin.udl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ dictionary OutPoint {
u32 vout;
};

dictionary TxIn {
OutPoint previous_output;
Script script_sig;
u32 sequence;
sequence<sequence<u8>> witness;
};

[NonExhaustive]
enum Network {
"Bitcoin",
Expand Down
41 changes: 39 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ use bitcoin::address::{NetworkChecked, NetworkUnchecked};
use bitcoin::Address as BitcoinAddress;
use bitcoin::Amount as BitcoinAmount;
use bitcoin::FeeRate as BitcoinFeeRate;
pub use bitcoin::OutPoint;
use bitcoin::ScriptBuf as BitcoinScriptBuf;
use bitcoin::TxIn as BitcoinTxIn;
use bitcoin::TxOut as BitcoinTxOut;
use bitcoin::Sequence;

pub use bitcoin::OutPoint;
pub use bitcoin::Txid;
use std::sync::Arc;

use error::AddressParseError;
use error::FeeRateError;
Expand All @@ -15,6 +17,7 @@ use error::ParseAmountError;

use std::fmt::Display;
use std::str::FromStr;
use std::sync::Arc;

#[macro_use]
mod macros;
Expand Down Expand Up @@ -172,6 +175,40 @@ impl Amount {
impl_from_core_type!(Amount, BitcoinAmount);
impl_from_ffi_type!(Amount, BitcoinAmount);

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TxIn {
pub previous_output: OutPoint,
pub script_sig: Arc<Script>,
pub sequence: u32,
pub witness: Vec<Vec<u8>>,
}

impl From<BitcoinTxIn> for TxIn {
fn from(value: BitcoinTxIn) -> Self {
Self {
previous_output: value.previous_output,
script_sig: Arc::new(value.script_sig.into()),
sequence: value.sequence.0,
witness: value.witness.to_vec(),
}
}
}

impl From<TxIn> for BitcoinTxIn {
fn from(value: TxIn) -> Self {
let script_sig = match Arc::try_unwrap(value.script_sig) {
Ok(val) => val.0,
Err(arc) => arc.0.clone()
};
Self {
previous_output: value.previous_output,
script_sig,
sequence: Sequence(value.sequence),
witness: value.witness.into(),
}
}
}

impl UniffiCustomTypeConverter for Txid {
type Builtin = String;
fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
Expand Down

0 comments on commit b2e23ec

Please sign in to comment.