sv 0.2.2

A Rust library for working with Bitcoin SV
Documentation
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use crate::messages::message::Payload;
use crate::messages::{OutPoint, TxIn, TxOut, COINBASE_OUTPOINT_HASH, COINBASE_OUTPOINT_INDEX};
use crate::script::{op_codes, Script, TransactionChecker, NO_FLAGS, PREGENESIS_RULES};
use crate::transaction::sighash::SigHashCache;
use crate::util::{sha256d, var_int, Error, Hash256, Result, Serializable};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use linked_hash_map::LinkedHashMap;
use op_codes::{OP_EQUAL, OP_HASH160};
use std::collections::HashSet;
use std::fmt;
use std::io;
use std::io::{Read, Write};

/// Maximum number of satoshis possible
pub const MAX_SATOSHIS: i64 = 21_000_000 * 100_000_000;

/// Bitcoin transaction
#[derive(Default, PartialEq, Eq, Hash, Clone)]
pub struct Tx {
    /// Transaction version
    pub version: u32,
    /// Transaction inputs
    pub inputs: Vec<TxIn>,
    /// Transaction outputs
    pub outputs: Vec<TxOut>,
    /// The block number or timestamp at which this transaction is unlocked
    pub lock_time: u32,
}

impl Tx {
    /// Calculates the hash of the transaction also known as the txid
    pub fn hash(&self) -> Hash256 {
        let mut b = Vec::with_capacity(self.size());
        self.write(&mut b).unwrap();
        sha256d(&b)
    }

    /// Validates a non-coinbase transaction
    pub fn validate(
        &self,
        require_sighash_forkid: bool,
        use_genesis_rules: bool,
        utxos: &LinkedHashMap<OutPoint, TxOut>,
        pregenesis_outputs: &HashSet<OutPoint>,
    ) -> Result<()> {
        // Make sure neither in or out lists are empty
        if self.inputs.len() == 0 {
            return Err(Error::BadData("inputs empty".to_string()));
        }
        if self.outputs.len() == 0 {
            return Err(Error::BadData("outputs empty".to_string()));
        }

        // Each output value, as well as the total, must be in legal money range
        let mut total_out = 0;
        for tx_out in self.outputs.iter() {
            if tx_out.satoshis < 0 {
                return Err(Error::BadData("tx_out satoshis negative".to_string()));
            }
            total_out += tx_out.satoshis;
        }
        if total_out > MAX_SATOSHIS {
            return Err(Error::BadData("Total out exceeds max satoshis".to_string()));
        }

        // Make sure none of the inputs are coinbase transactions
        for tx_in in self.inputs.iter() {
            if tx_in.prev_output.hash == COINBASE_OUTPOINT_HASH
                && tx_in.prev_output.index == COINBASE_OUTPOINT_INDEX
            {
                return Err(Error::BadData("Unexpected coinbase".to_string()));
            }
        }

        // Check that lock_time <= INT_MAX because some clients interpret this differently
        if self.lock_time > 2_147_483_647 {
            return Err(Error::BadData("Lock time too large".to_string()));
        }

        // Check that all inputs are in the utxo set and are in legal money range
        let mut total_in = 0;
        for tx_in in self.inputs.iter() {
            let utxo = utxos.get(&tx_in.prev_output);
            if let Some(tx_out) = utxo {
                if tx_out.satoshis < 0 {
                    return Err(Error::BadData("tx_out satoshis negative".to_string()));
                }
                total_in += tx_out.satoshis;
            } else {
                return Err(Error::BadData("utxo not found".to_string()));
            }
        }
        if total_in > MAX_SATOSHIS {
            return Err(Error::BadData("Total in exceeds max satoshis".to_string()));
        }

        // Check inputs spent > outputs received
        if total_in < total_out {
            return Err(Error::BadData("Output total exceeds input".to_string()));
        }

        // Verify each script
        let mut sighash_cache = SigHashCache::new();
        for input in 0..self.inputs.len() {
            let tx_in = &self.inputs[input];
            let tx_out = utxos.get(&tx_in.prev_output).unwrap();

            let mut script = Script::new();
            script.append_slice(&tx_in.unlock_script.0);
            script.append(op_codes::OP_CODESEPARATOR);
            script.append_slice(&tx_out.lock_script.0);

            let mut tx_checker = TransactionChecker {
                tx: self,
                sig_hash_cache: &mut sighash_cache,
                input: input,
                satoshis: tx_out.satoshis,
                require_sighash_forkid,
            };

            let is_pregenesis_input = pregenesis_outputs.contains(&tx_in.prev_output);
            let flags = if !use_genesis_rules || is_pregenesis_input {
                PREGENESIS_RULES
            } else {
                NO_FLAGS
            };

            script.eval(&mut tx_checker, flags)?;
        }

        if use_genesis_rules {
            for tx_out in self.outputs.iter() {
                if tx_out.lock_script.0.len() == 22
                    && tx_out.lock_script.0[0] == OP_HASH160
                    && tx_out.lock_script.0[21] == OP_EQUAL
                {
                    return Err(Error::BadData("P2SH sunsetted".to_string()));
                }
            }
        }

        Ok(())
    }

    /// Returns whether the transaction is the block reward
    pub fn coinbase(&self) -> bool {
        return self.inputs.len() == 1
            && self.inputs[0].prev_output.hash == COINBASE_OUTPOINT_HASH
            && self.inputs[0].prev_output.index == COINBASE_OUTPOINT_INDEX;
    }
}

impl Serializable<Tx> for Tx {
    fn read(reader: &mut dyn Read) -> Result<Tx> {
        let version = reader.read_i32::<LittleEndian>()?;
        let version = version as u32;
        let n_inputs = var_int::read(reader)?;
        let mut inputs = Vec::with_capacity(n_inputs as usize);
        for _i in 0..n_inputs {
            inputs.push(TxIn::read(reader)?);
        }
        let n_outputs = var_int::read(reader)?;
        let mut outputs = Vec::with_capacity(n_outputs as usize);
        for _i in 0..n_outputs {
            outputs.push(TxOut::read(reader)?);
        }
        let lock_time = reader.read_u32::<LittleEndian>()?;
        Ok(Tx {
            version,
            inputs,
            outputs,
            lock_time,
        })
    }

    fn write(&self, writer: &mut dyn Write) -> io::Result<()> {
        writer.write_u32::<LittleEndian>(self.version)?;
        var_int::write(self.inputs.len() as u64, writer)?;
        for tx_in in self.inputs.iter() {
            tx_in.write(writer)?;
        }
        var_int::write(self.outputs.len() as u64, writer)?;
        for tx_out in self.outputs.iter() {
            tx_out.write(writer)?;
        }
        writer.write_u32::<LittleEndian>(self.lock_time)?;
        Ok(())
    }
}

impl Payload<Tx> for Tx {
    fn size(&self) -> usize {
        let mut size = 8;
        size += var_int::size(self.inputs.len() as u64);
        for tx_in in self.inputs.iter() {
            size += tx_in.size();
        }
        size += var_int::size(self.outputs.len() as u64);
        for tx_out in self.outputs.iter() {
            size += tx_out.size();
        }
        size
    }
}

impl fmt::Debug for Tx {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let inputs_str = format!("[<{} inputs>]", self.inputs.len());
        let outputs_str = format!("[<{} outputs>]", self.outputs.len());

        f.debug_struct("Tx")
            .field("version", &self.version)
            .field(
                "inputs",
                if self.inputs.len() <= 3 {
                    &self.inputs
                } else {
                    &inputs_str
                },
            )
            .field(
                "outputs",
                if self.outputs.len() <= 3 {
                    &self.outputs
                } else {
                    &outputs_str
                },
            )
            .field("lock_time", &self.lock_time)
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::messages::OutPoint;
    use crate::util::Hash256;
    use std::io::Cursor;

    #[test]
    fn write_read() {
        let mut v = Vec::new();
        let t = Tx {
            version: 1,
            inputs: vec![
                TxIn {
                    prev_output: OutPoint {
                        hash: Hash256([9; 32]),
                        index: 9,
                    },
                    unlock_script: Script(vec![1, 3, 5, 7, 9]),
                    sequence: 100,
                },
                TxIn {
                    prev_output: OutPoint {
                        hash: Hash256([0; 32]),
                        index: 8,
                    },
                    unlock_script: Script(vec![3; 333]),
                    sequence: 22,
                },
            ],
            outputs: vec![
                TxOut {
                    satoshis: 99,
                    lock_script: Script(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 100, 99, 98, 97, 96]),
                },
                TxOut {
                    satoshis: 199,
                    lock_script: Script(vec![56, 78, 90, 90, 78, 56]),
                },
            ],
            lock_time: 1000,
        };
        t.write(&mut v).unwrap();
        assert!(v.len() == t.size());
        assert!(Tx::read(&mut Cursor::new(&v)).unwrap() == t);
    }

    #[test]
    fn hash() {
        // The coinbase from block 2
        let tx = Tx {
            version: 1,
            inputs: vec![TxIn {
                prev_output: OutPoint {
                    hash: Hash256([0; 32]),
                    index: 4294967295,
                },
                unlock_script: Script(vec![4, 255, 255, 0, 29, 1, 11]),
                sequence: 4294967295,
            }],
            outputs: vec![TxOut {
                satoshis: 5000000000,
                lock_script: Script(vec![
                    65, 4, 114, 17, 168, 36, 245, 91, 80, 82, 40, 228, 195, 213, 25, 76, 31, 207,
                    170, 21, 164, 86, 171, 223, 55, 249, 185, 217, 122, 64, 64, 175, 192, 115, 222,
                    230, 200, 144, 100, 152, 79, 3, 56, 82, 55, 217, 33, 103, 193, 62, 35, 100, 70,
                    180, 23, 171, 121, 160, 252, 174, 65, 42, 227, 49, 107, 119, 172,
                ]),
            }],
            lock_time: 0,
        };
        let h = "9b0fc92260312ce44e74ef369f5c66bbb85848f2eddd5a7a1cde251e54ccfdd5";
        assert!(tx.hash() == Hash256::decode(h).unwrap());
        assert!(tx.coinbase());
    }

    #[test]
    fn validate() {
        let utxo = (
            OutPoint {
                hash: Hash256([5; 32]),
                index: 3,
            },
            TxOut {
                satoshis: 100,
                lock_script: Script(vec![]),
            },
        );
        let mut utxos = LinkedHashMap::new();
        utxos.insert(utxo.0.clone(), utxo.1.clone());

        let tx = Tx {
            version: 2,
            inputs: vec![TxIn {
                prev_output: utxo.0.clone(),
                unlock_script: Script(vec![op_codes::OP_1]),
                sequence: 0,
            }],
            outputs: vec![
                TxOut {
                    satoshis: 10,
                    lock_script: Script(vec![]),
                },
                TxOut {
                    satoshis: 20,
                    lock_script: Script(vec![]),
                },
            ],
            lock_time: 0,
        };
        assert!(tx.validate(true, true, &utxos, &HashSet::new()).is_ok());

        let mut tx_test = tx.clone();
        tx_test.inputs = vec![];
        assert!(tx_test
            .validate(true, true, &utxos, &HashSet::new())
            .is_err());

        let mut tx_test = tx.clone();
        tx_test.outputs = vec![];
        assert!(tx_test
            .validate(true, true, &utxos, &HashSet::new())
            .is_err());

        let mut tx_test = tx.clone();
        tx_test.outputs[0].satoshis = -1;
        assert!(tx_test
            .validate(true, true, &utxos, &HashSet::new())
            .is_err());

        let mut tx_test = tx.clone();
        tx_test.outputs[0].satoshis = 0;
        tx_test.outputs[0].satoshis = 0;
        assert!(tx_test
            .validate(true, true, &utxos, &HashSet::new())
            .is_ok());

        let mut tx_test = tx.clone();
        tx_test.outputs[0].satoshis = MAX_SATOSHIS;
        tx_test.outputs[1].satoshis = MAX_SATOSHIS;
        assert!(tx_test
            .validate(true, true, &utxos, &HashSet::new())
            .is_err());

        let mut tx_test = tx.clone();
        tx_test.outputs[1].satoshis = MAX_SATOSHIS + 1;
        assert!(tx_test
            .validate(true, true, &utxos, &HashSet::new())
            .is_err());

        let mut tx_test = tx.clone();
        tx_test.inputs[0].prev_output.hash = COINBASE_OUTPOINT_HASH;
        tx_test.inputs[0].prev_output.index = COINBASE_OUTPOINT_INDEX;
        assert!(tx_test
            .validate(true, true, &utxos, &HashSet::new())
            .is_err());

        let mut tx_test = tx.clone();
        tx_test.lock_time = 4294967295;
        assert!(tx_test
            .validate(true, true, &utxos, &HashSet::new())
            .is_err());

        let mut tx_test = tx.clone();
        tx_test.inputs[0].prev_output.hash = Hash256([8; 32]);
        assert!(tx_test
            .validate(true, true, &utxos, &HashSet::new())
            .is_err());

        let mut utxos_clone = utxos.clone();
        let prev_output = &tx.inputs[0].prev_output;
        utxos_clone.get_mut(prev_output).unwrap().satoshis = -1;
        assert!(tx
            .validate(true, true, &utxos_clone, &HashSet::new())
            .is_err());

        let mut utxos_clone = utxos.clone();
        let prev_output = &tx.inputs[0].prev_output;
        utxos_clone.get_mut(prev_output).unwrap().satoshis = MAX_SATOSHIS + 1;
        assert!(tx
            .validate(true, true, &utxos_clone, &HashSet::new())
            .is_err());

        let mut tx_test = tx.clone();
        tx_test.outputs[0].satoshis = 100;
        assert!(tx_test
            .validate(true, true, &utxos, &HashSet::new())
            .is_err());

        let mut utxos_clone = utxos.clone();
        let prev_output = &tx.inputs[0].prev_output;
        utxos_clone.get_mut(prev_output).unwrap().lock_script = Script(vec![op_codes::OP_0]);
        assert!(tx
            .validate(true, true, &utxos_clone, &HashSet::new())
            .is_err());

        let mut tx_test = tx.clone();
        tx_test.outputs[0].lock_script = Script(vec![
            OP_HASH160, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, OP_EQUAL,
        ]);
        assert!(tx_test
            .validate(true, false, &utxos, &HashSet::new())
            .is_ok());
        assert!(tx_test
            .validate(true, true, &utxos, &HashSet::new())
            .is_err());
    }
}