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
use crate::types::{
BitcoinOutpoint, BitcoinTransaction, LegacySighashArgs, Script, ScriptPubkey, ScriptType,
Sighash, TxOut, WitnessSighashArgs,
};
use coins_core::hashes::{Digest, Hash160, MarkedDigest, MarkedDigestOutput, Sha256};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Hash)]
pub enum SpendScript {
None,
Missing,
Known(Script),
}
impl SpendScript {
pub fn from_script_pubkey(script: &ScriptPubkey) -> SpendScript {
match script.standard_type() {
ScriptType::Sh(_) | ScriptType::Wsh(_) => SpendScript::Missing,
_ => SpendScript::None,
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Utxo {
pub outpoint: BitcoinOutpoint,
pub value: u64,
pub script_pubkey: ScriptPubkey,
spend_script: SpendScript,
}
impl Utxo {
pub fn new(
outpoint: BitcoinOutpoint,
value: u64,
script_pubkey: ScriptPubkey,
spend_script: SpendScript,
) -> Utxo {
let spend_script = match SpendScript::from_script_pubkey(&script_pubkey) {
SpendScript::None => SpendScript::None,
SpendScript::Missing => spend_script,
SpendScript::Known(_) => panic!("unreachable"),
};
Utxo {
outpoint,
value,
script_pubkey,
spend_script,
}
}
pub fn from_tx_output<T>(tx: &T, idx: usize) -> Utxo
where
T: BitcoinTransaction,
{
let output = &tx.outputs()[idx];
Utxo {
outpoint: BitcoinOutpoint::new(tx.txid(), idx as u32),
value: output.value,
script_pubkey: output.script_pubkey.clone(),
spend_script: SpendScript::from_script_pubkey(&output.script_pubkey),
}
}
pub fn from_output_and_outpoint(output: &TxOut, outpoint: &BitcoinOutpoint) -> Utxo {
Utxo {
outpoint: *outpoint,
value: output.value,
script_pubkey: output.script_pubkey.clone(),
spend_script: SpendScript::from_script_pubkey(&output.script_pubkey),
}
}
pub fn script_pubkey(&self) -> &ScriptPubkey {
&self.script_pubkey
}
pub fn spend_script(&self) -> &SpendScript {
&self.spend_script
}
pub fn signing_script(&self) -> Option<Script> {
match self.spend_script() {
SpendScript::None => {
let spk = self.script_pubkey();
match spk.standard_type() {
ScriptType::Pkh(_) => Some(spk.into()),
ScriptType::Wpkh(payload) => {
let mut v = vec![0x76, 0xa9, 0x14];
v.extend(payload.as_slice());
v.extend(&[0x88, 0xac]);
Some(v.into())
}
_ => None,
}
}
SpendScript::Known(script) => Some(script.clone()),
SpendScript::Missing => None,
}
}
pub fn standard_type(&self) -> ScriptType {
self.script_pubkey.standard_type()
}
pub fn set_spend_script(&mut self, script: Script) -> bool {
match self.standard_type() {
ScriptType::Sh(data) => {
if data == Hash160::digest_marked(script.as_ref()) {
self.spend_script = SpendScript::Known(script);
return true;
}
}
ScriptType::Wsh(data) => {
if data.as_slice() == Sha256::digest(script.as_ref()).as_slice() {
self.spend_script = SpendScript::Known(script);
return true;
}
}
_ => return false,
}
false
}
pub fn sighash_args(&self, index: usize, flag: Sighash) -> Option<LegacySighashArgs> {
if let Some(prevout_script) = self.signing_script() {
Some(LegacySighashArgs {
index,
sighash_flag: flag,
prevout_script,
})
} else {
None
}
}
pub fn witness_sighash_args(&self, index: usize, flag: Sighash) -> Option<WitnessSighashArgs> {
if let Some(prevout_script) = self.signing_script() {
Some(WitnessSighashArgs {
index,
sighash_flag: flag,
prevout_script,
prevout_value: self.value,
})
} else {
None
}
}
}