chainhook_types/bitcoin.rs
1use crate::TransactionIdentifier;
2
3/// A transaction input, which defines old coins to be consumed
4#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Serialize, Deserialize)]
5pub struct TxIn {
6 /// The reference to the previous output that is being used an an input.
7 pub previous_output: OutPoint,
8 /// The script which pushes values on the stack which will cause
9 /// the referenced output's script to be accepted.
10 pub script_sig: String,
11 /// The sequence number, which suggests to miners which of two
12 /// conflicting transactions should be preferred, or 0xFFFFFFFF
13 /// to ignore this feature. This is generally never used since
14 /// the miner behaviour cannot be enforced.
15 pub sequence: u32,
16 /// Witness data: an array of byte-arrays.
17 /// Note that this field is *not* (de)serialized with the rest of the TxIn in
18 /// Encodable/Decodable, as it is (de)serialized at the end of the full
19 /// Transaction. It *is* (de)serialized with the rest of the TxIn in other
20 /// (de)serialization routines.
21 pub witness: Vec<String>,
22}
23
24/// A transaction output, which defines new coins to be created from old ones.
25#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Serialize, Deserialize)]
26pub struct TxOut {
27 /// The value of the output, in satoshis.
28 pub value: u64,
29 /// The script which must be satisfied for the output to be spent.
30 pub script_pubkey: String,
31}
32
33/// A reference to a transaction output.
34#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
35pub struct OutPoint {
36 /// The referenced transaction's txid.
37 pub txid: TransactionIdentifier,
38 /// The index of the referenced output in its transaction's vout.
39 pub vout: u32,
40 /// The value of the referenced.
41 pub value: u64,
42 /// The script which must be satisfied for the output to be spent.
43 pub block_height: u64,
44}
45
46impl TxOut {
47 pub fn get_script_pubkey_bytes(&self) -> Vec<u8> {
48 hex::decode(&self.get_script_pubkey_hex()).expect("not provided for coinbase txs")
49 }
50
51 pub fn get_script_pubkey_hex(&self) -> &str {
52 &self.script_pubkey[2..]
53 }
54}
55
56/// The Witness is the data used to unlock bitcoins since the [segwit upgrade](https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki)
57///
58/// Can be logically seen as an array of byte-arrays `Vec<Vec<u8>>` and indeed you can convert from
59/// it [`Witness::from_vec`] and convert into it [`Witness::to_vec`].
60///
61/// For serialization and deserialization performance it is stored internally as a single `Vec`,
62/// saving some allocations.
63///
64#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Serialize, Deserialize)]
65pub struct Witness {
66 /// contains the witness `Vec<Vec<u8>>` serialization without the initial varint indicating the
67 /// number of elements (which is stored in `witness_elements`)
68 content: Vec<u8>,
69
70 /// Number of elements in the witness.
71 /// It is stored separately (instead of as VarInt in the initial part of content) so that method
72 /// like [`Witness::push`] doesn't have case requiring to shift the entire array
73 witness_elements: usize,
74
75 /// If `witness_elements > 0` it's a valid index pointing to the last witness element in `content`
76 /// (Including the varint specifying the length of the element)
77 last: usize,
78
79 /// If `witness_elements > 1` it's a valid index pointing to the second-to-last witness element in `content`
80 /// (Including the varint specifying the length of the element)
81 second_to_last: usize,
82}