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
use crate::parser::errors::{OpError, OpResult};
use bitcoin::blockdata::opcodes::{all, All};
use bitcoin::blockdata::script::Instruction;
use bitcoin::util::address::Payload;
use bitcoin::{Address, Network, PubkeyHash, PublicKey, Script};
use bitcoin_hashes::{hash160, Hash};
use serde::{Deserialize, Serialize};
use std::fmt;
use Instruction::{Op, PushBytes};
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub enum Type {
OpReturn,
Pay2MultiSig,
Pay2PublicKey,
Pay2PublicKeyHash,
Pay2ScriptHash,
Pay2WitnessPublicKeyHash,
Pay2WitnessScriptHash,
WitnessProgram,
Unspendable,
NotRecognised,
}
impl fmt::Display for Type {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Type::OpReturn => write!(f, "OpReturn"),
Type::Pay2MultiSig => write!(f, "Pay2MultiSig"),
Type::Pay2PublicKey => write!(f, "Pay2PublicKey"),
Type::Pay2PublicKeyHash => write!(f, "Pay2PublicKeyHash"),
Type::Pay2ScriptHash => write!(f, "Pay2ScriptHash"),
Type::Pay2WitnessPublicKeyHash => write!(f, "Pay2WitnessPublicKeyHash"),
Type::Pay2WitnessScriptHash => write!(f, "Pay2WitnessScriptHash"),
Type::WitnessProgram => write!(f, "WitnessProgram"),
Type::Unspendable => write!(f, "Unspendable"),
Type::NotRecognised => write!(f, "NotRecognised"),
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ScriptInfo {
pub addresses: Vec<Address>,
pub pattern: Type,
}
impl ScriptInfo {
pub fn new(address: Option<Address>, pattern: Type) -> Self {
if let Some(address) = address {
Self::new_from_vec(vec![address], pattern)
} else {
Self::new_from_vec(Vec::new(), pattern)
}
}
pub fn new_from_vec(addresses: Vec<Address>, pattern: Type) -> Self {
Self { addresses, pattern }
}
}
fn p2pk_to_address(script: &Script) -> Option<Address> {
assert!(script.is_p2pk());
let pk = match script.instructions().next() {
Some(Ok(Instruction::PushBytes(bytes))) => bytes,
_ => {
unreachable!()
}
};
let pkh = hash160::Hash::hash(pk);
let address = Address {
payload: Payload::PubkeyHash(PubkeyHash::from_slice(&pkh).ok()?),
network: Network::Bitcoin,
};
Some(address)
}
trait Cmp {
fn ge(&self, other: &Self) -> bool;
}
impl Cmp for bitcoin::blockdata::opcodes::All {
fn ge(&self, other: &Self) -> bool {
self.into_u8() >= other.into_u8()
}
}
fn decode_from_op_n(op: &All) -> i32 {
if op.eq(&all::OP_PUSHBYTES_0) {
0
} else if op.eq(&all::OP_PUSHNUM_NEG1) {
-1
} else {
op.into_u8() as i32 + 1 - all::OP_PUSHNUM_1.into_u8() as i32
}
}
fn is_multisig(script: &Script) -> bool {
let mut chunks: Vec<Instruction> = Vec::new();
for i in script.instructions() {
if let Ok(i) = i {
chunks.push(i);
} else {
return false;
}
}
if chunks.len() < 4 {
return false;
}
let last_chunk = chunks.get(chunks.len() - 1).unwrap();
match last_chunk {
PushBytes(_) => {
return false;
}
Op(op) => {
if !(op.eq(&all::OP_CHECKMULTISIG) || op.eq(&all::OP_CHECKMULTISIGVERIFY)) {
return false;
}
}
}
let second_last_chunk = chunks.get(chunks.len() - 2).unwrap();
match second_last_chunk {
PushBytes(_) => {
return false;
}
Op(op) => {
if !(op.eq(&all::OP_PUSHNUM_NEG1)
|| op.eq(&all::OP_PUSHBYTES_0)
|| (op.ge(&all::OP_PUSHNUM_1) && all::OP_PUSHNUM_16.ge(op)))
{
return false;
} else {
let num_keys = decode_from_op_n(op);
if num_keys < 1 || (num_keys + 3) as usize != chunks.len() {
return false;
}
}
}
}
for i in 1..(chunks.len() - 2) {
match chunks.get(i).unwrap() {
PushBytes(_) => {}
Op(_) => {
return false;
}
}
}
let first_chunk = chunks.get(0).unwrap();
match first_chunk {
PushBytes(_) => {
return false;
}
Op(op) => {
if !(op.eq(&all::OP_PUSHNUM_NEG1)
|| op.eq(&all::OP_PUSHBYTES_0)
|| (op.ge(&all::OP_PUSHNUM_1) && all::OP_PUSHNUM_16.ge(op)))
{
return false;
} else {
if decode_from_op_n(op) < 1 {
return false;
}
}
}
}
return true;
}
fn get_pub_keys(script: &Script) -> OpResult<Vec<PublicKey>> {
assert!(is_multisig(script));
let ops: Vec<Instruction> = script.instructions().map(|o| o.unwrap()).collect();
if let Op(op) = ops.get(ops.len() - 2).unwrap() {
let num_keys = decode_from_op_n(op);
let mut public_keys = Vec::with_capacity(num_keys as usize);
for i in 0..num_keys {
if let Some(PushBytes(data)) = ops.get(i as usize + 1) {
match PublicKey::from_slice(data) {
Ok(pk) => public_keys.push(pk),
Err(_) => return Err(OpError::from("failed to parse public key")),
}
} else {
unreachable!()
}
}
Ok(public_keys)
} else {
unreachable!()
}
}
fn pub_keys_to_addresses(script: &Script) -> Vec<Address> {
assert!(is_multisig(script));
if let Ok(pub_keys) = get_pub_keys(&script) {
pub_keys
.iter()
.map(|k| Address {
payload: Payload::PubkeyHash(k.pubkey_hash()),
network: Network::Bitcoin,
})
.collect()
} else {
Vec::new()
}
}
pub fn evaluate_script(script: &Script, net: Network) -> ScriptInfo {
let address = Address::from_script(&script, net);
if script.is_p2pk() {
ScriptInfo::new(p2pk_to_address(&script), Type::Pay2PublicKey)
} else if script.is_p2pkh() {
ScriptInfo::new(address, Type::Pay2PublicKeyHash)
} else if script.is_p2sh() {
ScriptInfo::new(address, Type::Pay2ScriptHash)
} else if script.is_v0_p2wpkh() {
ScriptInfo::new(address, Type::Pay2WitnessPublicKeyHash)
} else if script.is_v0_p2wsh() {
ScriptInfo::new(address, Type::Pay2WitnessScriptHash)
} else if script.is_witness_program() {
ScriptInfo::new(address, Type::WitnessProgram)
} else if script.is_op_return() {
ScriptInfo::new(address, Type::OpReturn)
} else if script.is_provably_unspendable() {
ScriptInfo::new(address, Type::Unspendable)
} else if is_multisig(&script) {
ScriptInfo::new_from_vec(pub_keys_to_addresses(&script), Type::Pay2MultiSig)
} else {
ScriptInfo::new(address, Type::NotRecognised)
}
}
#[cfg(test)]
mod tests {
use super::{evaluate_script, Type};
use bitcoin::hashes::hex::{FromHex, ToHex};
use bitcoin::{Network, Script};
#[test]
fn test_bitcoin_script_p2pkh() {
let bytes = [
0x76 as u8, 0xa9, 0x14, 0x12, 0xab, 0x8d, 0xc5, 0x88, 0xca, 0x9d, 0x57, 0x87, 0xdd,
0xe7, 0xeb, 0x29, 0x56, 0x9d, 0xa6, 0x3c, 0x3a, 0x23, 0x8c, 0x88, 0xac,
];
let result = evaluate_script(
&Script::from_hex(&bytes.to_hex()).unwrap(),
Network::Bitcoin,
);
assert_eq!(
result.addresses.get(0).unwrap().to_string(),
String::from("12higDjoCCNXSA95xZMWUdPvXNmkAduhWv")
);
assert_eq!(result.pattern, Type::Pay2PublicKeyHash);
}
#[test]
fn test_bitcoin_script_p2pk() {
let bytes = [
0x41 as u8,
0x04, 0x4b, 0xca, 0x63, 0x3a, 0x91, 0xde, 0x10, 0xdf, 0x85, 0xa6, 0x3d, 0x0a, 0x24,
0xcb, 0x09, 0x78, 0x31, 0x48, 0xfe, 0x0e, 0x16, 0xc9, 0x2e, 0x93, 0x7f, 0xc4, 0x49,
0x15, 0x80, 0xc8, 0x60, 0x75, 0x71, 0x48, 0xef, 0xfa, 0x05, 0x95, 0xa9, 0x55, 0xf4,
0x40, 0x78, 0xb4, 0x8b, 0xa6, 0x7f, 0xa1, 0x98, 0x78, 0x2e, 0x8b, 0xb6, 0x81, 0x15,
0xda, 0x0d, 0xaa, 0x8f, 0xde, 0x53, 0x01, 0xf7, 0xf9, 0xac,
];
let result = evaluate_script(
&Script::from_hex(&bytes.to_hex()).unwrap(),
Network::Bitcoin,
);
assert_eq!(
result.addresses.get(0).unwrap().to_string(),
String::from("1LEWwJkDj8xriE87ALzQYcHjTmD8aqDj1f")
);
assert_eq!(result.pattern, Type::Pay2PublicKey);
}
#[test]
fn test_bitcoin_script_p2ms() {
let bytes = [
0x52 as u8, 0x21, 0x02, 0x2d, 0xf8, 0x75, 0x04, 0x80, 0xad, 0x5b, 0x26, 0x95, 0x0b,
0x25, 0xc7, 0xba, 0x79, 0xd3, 0xe3, 0x7d, 0x75, 0xf6, 0x40, 0xf8, 0xe5, 0xd9, 0xbc,
0xd5, 0xb1, 0x50, 0xa0, 0xf8, 0x50, 0x14, 0xda, 0x21, 0x03, 0xe3, 0x81, 0x8b, 0x65,
0xbc, 0xc7, 0x3a, 0x7d, 0x64, 0x06, 0x41, 0x06, 0xa8, 0x59, 0xcc, 0x1a, 0x5a, 0x72,
0x8c, 0x43, 0x45, 0xff, 0x0b, 0x64, 0x12, 0x09, 0xfb, 0xa0, 0xd9, 0x0d, 0xe6, 0xe9,
0x21, 0x02, 0x1f, 0x2f, 0x6e, 0x1e, 0x50, 0xcb, 0x6a, 0x95, 0x39, 0x35, 0xc3, 0x60,
0x12, 0x84, 0x92, 0x5d, 0xec, 0xd3, 0xfd, 0x21, 0xbc, 0x44, 0x57, 0x12, 0x57, 0x68,
0x73, 0xfb, 0x8c, 0x6e, 0xbc, 0x18, 0x53, 0xae,
];
let result = evaluate_script(
&Script::from_hex(&bytes.to_hex()).unwrap(),
Network::Bitcoin,
);
assert_eq!(result.pattern, Type::Pay2MultiSig);
}
#[test]
fn test_bitcoin_script_p2sh() {
let bytes = [
0xa9 as u8, 0x14,
0xe9, 0xc3, 0xdd, 0x0c, 0x07, 0xaa, 0xc7, 0x61, 0x79, 0xeb, 0xc7, 0x6a, 0x6c, 0x78,
0xd4, 0xd6, 0x7c, 0x6c, 0x16, 0x0a, 0x87,
];
let result = evaluate_script(
&Script::from_hex(&bytes.to_hex()).unwrap(),
Network::Bitcoin,
);
assert_eq!(
result.addresses.get(0).unwrap().to_string(),
String::from("3P14159f73E4gFr7JterCCQh9QjiTjiZrG")
);
assert_eq!(result.pattern, Type::Pay2ScriptHash);
}
#[test]
fn test_bitcoin_script_non_standard() {
let bytes = [0x73 as u8, 0x63, 0x72, 0x69, 0x70, 0x74];
let result = evaluate_script(
&Script::from_hex(&bytes.to_hex()).unwrap(),
Network::Bitcoin,
);
assert_eq!(result.addresses.get(0), None);
assert_eq!(result.pattern, Type::NotRecognised);
}
#[test]
fn test_bitcoin_bogus_script() {
let bytes = [0x4c as u8, 0xFF, 0x00];
let result = evaluate_script(
&Script::from_hex(&bytes.to_hex()).unwrap(),
Network::Bitcoin,
);
assert_eq!(result.addresses.get(0), None);
assert_eq!(result.pattern, Type::NotRecognised);
}
}