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
use std::borrow::ToOwned;
use bitcoin::blockdata::script::Script;
use bitcoin::blockdata::transaction::SigHashType;
use bitcoin::util::bip143::SighashComponents;
use bitcoin::PublicKey;
use secp256k1::{self, Message, Secp256k1, SecretKey, Signature, Signing, Verification};
use crate::{Sha256dHash, TxInRef, UnspentTxOutValue};
#[derive(Debug, Clone, PartialEq)]
pub struct InputSignature(Vec<u8>);
impl InputSignature {
pub fn new(mut inner: Vec<u8>, sighash_type: SigHashType) -> InputSignature {
inner.push(sighash_type as u8);
InputSignature(inner)
}
pub fn from_bytes(bytes: Vec<u8>) -> Result<InputSignature, secp256k1::Error> {
InputSignatureRef::from_bytes(bytes.as_ref())?;
Ok(InputSignature(bytes))
}
pub fn content(&self) -> &[u8] {
self.0.split_last().unwrap().1
}
pub fn sighash_type(&self) -> SigHashType {
let byte = *self.0.last().unwrap();
SigHashType::from_u32(u32::from(byte))
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct InputSignatureRef<'a>(&'a [u8]);
impl<'a> InputSignatureRef<'a> {
pub fn from_bytes(bytes: &'a [u8]) -> Result<InputSignatureRef<'a>, secp256k1::Error> {
let (_sighash_type, content) = bytes
.split_last()
.ok_or_else(|| secp256k1::Error::InvalidMessage)?;
Signature::from_der(content)?;
Ok(InputSignatureRef(bytes))
}
pub fn content(&self) -> &[u8] {
self.0.split_last().unwrap().1
}
pub fn sighash_type(&self) -> SigHashType {
let byte = *self.0.last().unwrap();
SigHashType::from_u32(u32::from(byte))
}
}
impl From<InputSignature> for Vec<u8> {
fn from(s: InputSignature) -> Self {
s.0
}
}
impl AsRef<[u8]> for InputSignature {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}
impl<'a> AsRef<[u8]> for InputSignatureRef<'a> {
fn as_ref(&self) -> &[u8] {
self.0
}
}
impl<'a> From<&'a InputSignature> for InputSignatureRef<'a> {
fn from(s: &'a InputSignature) -> InputSignatureRef {
InputSignatureRef(s.0.as_ref())
}
}
impl<'a> From<InputSignatureRef<'a>> for Vec<u8> {
fn from(s: InputSignatureRef<'a>) -> Vec<u8> {
s.0.to_owned()
}
}
impl<'a> From<InputSignatureRef<'a>> for InputSignature {
fn from(s: InputSignatureRef<'a>) -> InputSignature {
InputSignature(s.0.to_owned())
}
}
pub fn signature_hash<'a, 'b, V: Into<UnspentTxOutValue<'b>>>(
txin: TxInRef<'a>,
script: &Script,
value: V,
) -> Sha256dHash {
let value = value.into().balance(txin);
SighashComponents::new(txin.transaction()).sighash_all(txin.as_ref(), script, value)
}
pub fn sign_input<'a, 'b, C, V>(
context: &mut Secp256k1<C>,
txin: TxInRef<'a>,
script: &Script,
value: V,
secret_key: &SecretKey,
) -> Result<InputSignature, secp256k1::Error>
where
C: Signing,
V: Into<UnspentTxOutValue<'b>>,
{
let sighash = signature_hash(txin, script, value);
let msg = Message::from_slice(&sighash[..])?;
let signature = context.sign(&msg, secret_key).serialize_der();
Ok(InputSignature::new(signature, SigHashType::All))
}
pub fn verify_input_signature<'a, 'b, C, V>(
context: &Secp256k1<C>,
txin: TxInRef<'a>,
script: &Script,
value: V,
public_key: &PublicKey,
signature: &[u8],
) -> Result<(), secp256k1::Error>
where
C: Verification,
V: Into<UnspentTxOutValue<'b>>,
{
let sighash = signature_hash(txin, script, value);
let msg = Message::from_slice(&sighash[..])?;
let sign = Signature::from_der(signature)?;
context.verify(&msg, &sign, &public_key.key)
}
#[test]
fn test_input_signature_ref_incorrect() {
let bytes = b"abacaba";
InputSignatureRef::from_bytes(bytes).expect_err("Signature should be incorrect");
InputSignature::from_bytes(bytes.to_vec()).expect_err("Signature should be incorrect");
}
#[test]
fn test_input_signature_ref_correct() {
let bytes = ::hex::decode(
"304402201538279618a4626653775069b43d4315c7d2ff30008d339d0ed31ff41e628e71022028f3182fc39df\
28201ca4d7d489aece7bc5bc6bfe05b09b6a9d3b70bf5f3743101",
)
.unwrap();
InputSignatureRef::from_bytes(&bytes).expect("Signature should be correct");
InputSignature::from_bytes(bytes).expect("Signature should be correct");
}