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
use bitcoin::consensus::encode::Encodable;
use bitcoin::hashes::sha256;
use bitcoin::hashes::Hash;
use bitcoin::util::amount::Amount;
pub trait CTVHash {
fn get_ctv_hash(&self, input_index: u32) -> sha256::Hash;
fn total_amount(&self) -> Amount;
}
impl CTVHash for bitcoin::Transaction {
fn get_ctv_hash(&self, input_index: u32) -> sha256::Hash {
let mut ctv_hash = sha256::Hash::engine();
self.version.consensus_encode(&mut ctv_hash).unwrap();
self.lock_time.consensus_encode(&mut ctv_hash).unwrap();
(self.input.len() as u32)
.consensus_encode(&mut ctv_hash)
.unwrap();
{
let mut enc = sha256::Hash::engine();
for seq in self.input.iter().map(|i| i.sequence) {
seq.consensus_encode(&mut enc).unwrap();
}
sha256::Hash::from_engine(enc)
.into_inner()
.consensus_encode(&mut ctv_hash)
.unwrap();
}
(self.output.len() as u32)
.consensus_encode(&mut ctv_hash)
.unwrap();
{
let mut enc = sha256::Hash::engine();
for out in self.output.iter() {
out.consensus_encode(&mut enc).unwrap();
}
sha256::Hash::from_engine(enc)
.into_inner()
.consensus_encode(&mut ctv_hash)
.unwrap();
}
input_index.consensus_encode(&mut ctv_hash).unwrap();
sha256::Hash::from_engine(ctv_hash)
}
fn total_amount(&self) -> Amount {
Amount::from_sat(self.output.iter().fold(0, |a, b| a + b.value))
}
}