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 13, 2024
1 parent 5c4d3fb commit 5f374ee
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/bitcoin.udl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,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
36 changes: 36 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use bitcoin::Address as BitcoinAddress;
use bitcoin::Amount as BitcoinAmount;
use bitcoin::FeeRate as BitcoinFeeRate;
use bitcoin::ScriptBuf as BitcoinScriptBuf;
use bitcoin::TxIn as BitcoinTxIn;
use bitcoin::TxOut as BitcoinTxOut;
use bitcoin::Sequence;

pub use bitcoin::BlockHash;
pub use bitcoin::OutPoint;
Expand Down Expand Up @@ -174,6 +176,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_string_custom_typedef!(BlockHash);
impl_string_custom_typedef!(Txid);

Expand Down

0 comments on commit 5f374ee

Please sign in to comment.