1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use bitcoin::consensus::Encodable;
use bitcoin::locktime::absolute;
use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_core::H256;
use sp_std::vec::Vec;

/// An absolute lock time value, representing either a block height or a UNIX timestamp (seconds
/// since epoch).
#[derive(Clone, Debug, Encode, Decode, TypeInfo, PartialEq)]
pub enum LockTime {
    /// A block height lock time value.
    Height(u32),
    /// A UNIX timestamp lock time value.
    ///
    /// A UNIX timestamp, seconds since epoch, guaranteed to always contain a valid time value.
    Time(u32),
}

impl From<absolute::LockTime> for LockTime {
    fn from(lock_time: absolute::LockTime) -> Self {
        match lock_time {
            absolute::LockTime::Blocks(n) => Self::Height(n.to_consensus_u32()),
            absolute::LockTime::Seconds(n) => Self::Time(n.to_consensus_u32()),
        }
    }
}

impl From<LockTime> for absolute::LockTime {
    fn from(val: LockTime) -> Self {
        match val {
            LockTime::Height(n) => Self::Blocks(
                absolute::Height::from_consensus(n).expect("Invalid height in LockTime"),
            ),
            LockTime::Time(n) => {
                Self::Seconds(absolute::Time::from_consensus(n).expect("Invalid time in LockTime"))
            }
        }
    }
}

/// Wrapper type for representing [`bitcoin::Txid`] in runtime, stored in reversed byte order.
#[derive(Clone, Copy, TypeInfo, Encode, Decode, MaxEncodedLen, PartialEq)]
pub struct Txid(H256);

impl Txid {
    /// Converts a [`bitcoin::Txid`]` to a [`Txid`].
    pub fn from_bitcoin_txid(txid: bitcoin::Txid) -> Self {
        let mut bytes = Vec::with_capacity(32);
        txid.consensus_encode(&mut bytes)
            .expect("txid must be encoded correctly; qed");

        bytes.reverse();

        let bytes: [u8; 32] = bytes
            .try_into()
            .expect("Bitcoin txid is sha256 hash which must fit into [u8; 32]; qed");

        Self(H256::from(bytes))
    }

    /// Converts a [`Txid`] to a [`bitcoin::Txid`].
    pub fn into_bitcoin_txid(self) -> bitcoin::Txid {
        let mut bytes = self.encode();
        bytes.reverse();
        bitcoin::consensus::Decodable::consensus_decode(&mut bytes.as_slice())
            .expect("Decode must succeed as txid was guaranteed to be encoded correctly; qed")
    }
}

impl core::fmt::Debug for Txid {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        for byte in self.0.as_bytes().iter() {
            write!(f, "{:02x}", byte)?;
        }
        Ok(())
    }
}

/// A reference to a transaction output.
#[derive(Clone, Debug, Encode, Decode, TypeInfo, PartialEq, MaxEncodedLen)]
pub struct OutPoint {
    /// The transaction ID of the referenced output.
    pub txid: Txid,
    /// The index of the output within the referenced transaction.
    pub output_index: u32,
}

impl From<bitcoin::OutPoint> for OutPoint {
    fn from(out_point: bitcoin::OutPoint) -> Self {
        Self {
            txid: Txid::from_bitcoin_txid(out_point.txid),
            output_index: out_point.vout,
        }
    }
}

impl From<OutPoint> for bitcoin::OutPoint {
    fn from(val: OutPoint) -> Self {
        bitcoin::OutPoint {
            txid: val.txid.into_bitcoin_txid(),
            vout: val.output_index,
        }
    }
}

/// The Witness is the data used to unlock bitcoin since the [segwit upgrade].
///
/// Can be logically seen as an array of bytestrings, i.e. `Vec<Vec<u8>>`, and it is serialized on the wire
/// in that format.
///
/// [segwit upgrade]: <https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki>
#[derive(Clone, Debug, Encode, Decode, TypeInfo, PartialEq)]
pub struct Witness {
    /// Contains the witness `Vec<Vec<u8>>` serialization without the initial varint indicating the
    /// number of elements (which is stored in `witness_elements`).
    content: Vec<u8>,
    /// The number of elements in the witness.
    ///
    /// Stored separately (instead of as a VarInt in the initial part of content) so that methods
    /// like [`Witness::push`] don't have to shift the entire array.
    // usize
    witness_elements: u64,
    /// This is the valid index pointing to the beginning of the index area. This area is 4 *
    /// stack_size bytes at the end of the content vector which stores the indices of each item.
    indices_start: u64,
}

/// Regular bitcoin transaction input.
///
/// Structurely same with [`bitcoin::TxIn`].
#[derive(Clone, Debug, Encode, Decode, TypeInfo, PartialEq)]
pub struct RegularTxIn {
    /// The reference to the previous output that is being used as an input.
    pub previous_output: OutPoint,
    /// The unlocking script (scriptSig) that pushes values onto the stack,
    /// enabling the referenced output's script to be satisfied.
    pub unlocking_script: Vec<u8>,
    /// The sequence number, which suggests to miners which of two
    /// conflicting transactions should be preferred, or 0xFFFFFFFF
    /// to ignore this feature. This is generally never used since
    /// the miner behavior cannot be enforced.
    pub sequence: u32,
    /// Witness data: an array of byte-arrays.
    /// Note that this field is *not* (de)serialized with the rest of the TxIn in
    /// Encodable/Decodable, as it is (de)serialized at the end of the full
    /// Transaction. It *is* (de)serialized with the rest of the TxIn in other
    /// (de)serialization routines.
    pub witness: Vec<Vec<u8>>,
}

/// Bitcoin transaction input.
///
/// This type is a wrapper around [`bitcoin::TxIn`], designed to provide a user-friendly representation
/// of transaction inputs (`TxIn`) in polkadot.js.org. It handles both coinbase (block reward)
/// transactions and standard transactions.
#[derive(Clone, Debug, Encode, Decode, TypeInfo, PartialEq)]
pub enum TxIn {
    /// Input from a coinbase transaction, which does not reference any previous output.
    Coinbase {
        /// Arbitrary data used in the coinbase transaction, such as extra nonce or miner-specific information.
        data: Vec<u8>,
        /// Sequence.
        ///
        /// Note that the value of sequence is not always Sequence::MAX, see https://www.blockchain.com/explorer/transactions/btc/0961c660358478829505e16a1f028757e54b5bbf9758341a7546573738f31429
        sequence: u32,
    },
    /// Input from a regular transaction, which references a previous output (`OutPoint`).
    Regular(RegularTxIn),
}

impl From<bitcoin::TxIn> for TxIn {
    fn from(txin: bitcoin::TxIn) -> Self {
        if txin.previous_output.is_null() {
            Self::Coinbase {
                data: txin.script_sig.into_bytes(),
                sequence: txin.sequence.0,
            }
        } else {
            Self::Regular(RegularTxIn {
                previous_output: txin.previous_output.into(),
                unlocking_script: txin.script_sig.into_bytes(),
                sequence: txin.sequence.0,
                witness: txin.witness.to_vec(),
            })
        }
    }
}

impl From<TxIn> for bitcoin::TxIn {
    fn from(val: TxIn) -> Self {
        match val {
            TxIn::Coinbase { data, sequence } => bitcoin::TxIn {
                previous_output: bitcoin::OutPoint::null(),
                script_sig: bitcoin::ScriptBuf::from_bytes(data),
                sequence: bitcoin::Sequence(sequence),
                witness: bitcoin::Witness::new(),
            },
            TxIn::Regular(RegularTxIn {
                previous_output,
                unlocking_script,
                sequence,
                witness,
            }) => bitcoin::TxIn {
                previous_output: previous_output.into(),
                script_sig: bitcoin::ScriptBuf::from_bytes(unlocking_script),
                sequence: bitcoin::Sequence(sequence),
                witness: witness.into(),
            },
        }
    }
}

/// Bitcoin transaction output.
#[derive(Clone, Debug, Encode, Decode, TypeInfo, PartialEq)]
pub struct TxOut {
    /// The value of the output, in satoshis.
    pub value: u64,
    /// The script which must be satisfied for the output to be spent.
    pub script_pubkey: Vec<u8>,
}

/// Bitcoin transaction.
#[derive(Clone, Debug, Encode, Decode, TypeInfo, PartialEq)]
pub struct Transaction {
    /// The protocol version, is currently expected to be 1 or 2 (BIP 68).
    pub version: i32,
    /// Block height or timestamp. Transaction cannot be included in a block until this height/time.
    pub lock_time: LockTime,
    /// List of transaction inputs.
    pub input: Vec<TxIn>,
    /// List of transaction outputs.
    pub output: Vec<TxOut>,
}

impl From<Transaction> for bitcoin::Transaction {
    fn from(val: Transaction) -> Self {
        let Transaction {
            version,
            lock_time,
            input,
            output,
        } = val;

        bitcoin::Transaction {
            version: bitcoin::transaction::Version(version),
            lock_time: lock_time.into(),
            input: input.into_iter().map(Into::into).collect(),
            output: output
                .into_iter()
                .map(|txout| bitcoin::TxOut {
                    value: bitcoin::Amount::from_sat(txout.value),
                    script_pubkey: bitcoin::ScriptBuf::from_bytes(txout.script_pubkey),
                })
                .collect(),
        }
    }
}

impl From<bitcoin::Transaction> for Transaction {
    fn from(btc_tx: bitcoin::Transaction) -> Self {
        Self {
            version: btc_tx.version.0,
            lock_time: btc_tx.lock_time.into(),
            input: btc_tx.input.into_iter().map(Into::into).collect(),
            output: btc_tx
                .output
                .into_iter()
                .map(|txout| TxOut {
                    value: txout.value.to_sat(),
                    script_pubkey: txout.script_pubkey.into_bytes(),
                })
                .collect(),
        }
    }
}