@@ -2,6 +2,7 @@ use super::types::{
2
2
BitcoinChainhookSpecification , BitcoinPredicateType , ExactMatchingRule , HookAction ,
3
3
InputPredicate , MatchingRule , OrdinalOperations , OutputPredicate , Protocols , StacksOperations ,
4
4
} ;
5
+ use crate :: utils:: Context ;
5
6
use base58:: FromBase58 ;
6
7
use bitcoincore_rpc:: bitcoin:: blockdata:: opcodes;
7
8
use bitcoincore_rpc:: bitcoin:: blockdata:: script:: Builder as BitcoinScriptBuilder ;
@@ -12,6 +13,7 @@ use chainhook_types::{
12
13
StacksBaseChainOperation , TransactionIdentifier ,
13
14
} ;
14
15
use clarity_repl:: clarity:: util:: hash:: to_hex;
16
+ use hiro_system_kit:: slog;
15
17
use reqwest:: { Client , Method } ;
16
18
use serde_json:: Value as JsonValue ;
17
19
use std:: collections:: HashMap ;
@@ -56,6 +58,7 @@ pub enum BitcoinChainhookOccurrence {
56
58
pub fn evaluate_bitcoin_chainhooks_on_chain_event < ' a > (
57
59
chain_event : & ' a BitcoinChainEvent ,
58
60
active_chainhooks : Vec < & ' a BitcoinChainhookSpecification > ,
61
+ ctx : & Context ,
59
62
) -> Vec < BitcoinTriggerChainhook < ' a > > {
60
63
let mut triggered_chainhooks = vec ! [ ] ;
61
64
match chain_event {
@@ -67,7 +70,7 @@ pub fn evaluate_bitcoin_chainhooks_on_chain_event<'a>(
67
70
for block in event. new_blocks . iter ( ) {
68
71
let mut hits = vec ! [ ] ;
69
72
for tx in block. transactions . iter ( ) {
70
- if chainhook. predicate . evaluate_transaction_predicate ( & tx) {
73
+ if chainhook. predicate . evaluate_transaction_predicate ( & tx, ctx ) {
71
74
hits. push ( tx) ;
72
75
}
73
76
}
@@ -93,7 +96,7 @@ pub fn evaluate_bitcoin_chainhooks_on_chain_event<'a>(
93
96
for block in event. blocks_to_apply . iter ( ) {
94
97
let mut hits = vec ! [ ] ;
95
98
for tx in block. transactions . iter ( ) {
96
- if chainhook. predicate . evaluate_transaction_predicate ( & tx) {
99
+ if chainhook. predicate . evaluate_transaction_predicate ( & tx, ctx ) {
97
100
hits. push ( tx) ;
98
101
}
99
102
}
@@ -104,7 +107,7 @@ pub fn evaluate_bitcoin_chainhooks_on_chain_event<'a>(
104
107
for block in event. blocks_to_rollback . iter ( ) {
105
108
let mut hits = vec ! [ ] ;
106
109
for tx in block. transactions . iter ( ) {
107
- if chainhook. predicate . evaluate_transaction_predicate ( & tx) {
110
+ if chainhook. predicate . evaluate_transaction_predicate ( & tx, ctx ) {
108
111
hits. push ( tx) ;
109
112
}
110
113
}
@@ -253,7 +256,11 @@ pub fn handle_bitcoin_hook_action<'a>(
253
256
}
254
257
255
258
impl BitcoinPredicateType {
256
- pub fn evaluate_transaction_predicate ( & self , tx : & BitcoinTransactionData ) -> bool {
259
+ pub fn evaluate_transaction_predicate (
260
+ & self ,
261
+ tx : & BitcoinTransactionData ,
262
+ ctx : & Context ,
263
+ ) -> bool {
257
264
// TODO(lgalabru): follow-up on this implementation
258
265
match & self {
259
266
BitcoinPredicateType :: Block => true ,
@@ -291,40 +298,18 @@ impl BitcoinPredicateType {
291
298
false
292
299
}
293
300
BitcoinPredicateType :: Outputs ( OutputPredicate :: P2pkh ( ExactMatchingRule :: Equals (
294
- address,
295
- ) ) ) => {
296
- let pubkey_hash = address
297
- . from_base58 ( )
298
- . expect ( "Unable to get bytes from btc address" ) ;
299
- let script = BitcoinScriptBuilder :: new ( )
300
- . push_opcode ( opcodes:: all:: OP_DUP )
301
- . push_opcode ( opcodes:: all:: OP_HASH160 )
302
- . push_slice ( & pubkey_hash[ 1 ..21 ] )
303
- . push_opcode ( opcodes:: all:: OP_EQUALVERIFY )
304
- . push_opcode ( opcodes:: all:: OP_CHECKSIG )
305
- . into_script ( ) ;
306
-
307
- for output in tx. metadata . outputs . iter ( ) {
308
- if output. script_pubkey == to_hex ( script. as_bytes ( ) ) {
309
- return true ;
310
- }
311
- }
312
- false
313
- }
314
- BitcoinPredicateType :: Outputs ( OutputPredicate :: P2sh ( ExactMatchingRule :: Equals (
315
- address,
301
+ encoded_address,
302
+ ) ) )
303
+ | BitcoinPredicateType :: Outputs ( OutputPredicate :: P2sh ( ExactMatchingRule :: Equals (
304
+ encoded_address,
316
305
) ) ) => {
317
- let script_hash = address
318
- . from_base58 ( )
319
- . expect ( "Unable to get bytes from btc address" ) ;
320
- let script = BitcoinScriptBuilder :: new ( )
321
- . push_opcode ( opcodes:: all:: OP_HASH160 )
322
- . push_slice ( & script_hash[ 1 ..21 ] )
323
- . push_opcode ( opcodes:: all:: OP_EQUAL )
324
- . into_script ( ) ;
325
-
306
+ let address = match Address :: from_str ( encoded_address) {
307
+ Ok ( address) => address,
308
+ Err ( _) => return false ,
309
+ } ;
310
+ let address_bytes = to_hex ( address. script_pubkey ( ) . as_bytes ( ) ) ;
326
311
for output in tx. metadata . outputs . iter ( ) {
327
- if output. script_pubkey == to_hex ( script . as_bytes ( ) ) {
312
+ if output. script_pubkey [ 2 .. ] == address_bytes {
328
313
return true ;
329
314
}
330
315
}
@@ -346,9 +331,9 @@ impl BitcoinPredicateType {
346
331
} ,
347
332
Err ( _) => return false ,
348
333
} ;
349
- let script_bytes = to_hex ( address. script_pubkey ( ) . as_bytes ( ) ) ;
334
+ let address_bytes = to_hex ( address. script_pubkey ( ) . as_bytes ( ) ) ;
350
335
for output in tx. metadata . outputs . iter ( ) {
351
- if output. script_pubkey == script_bytes {
336
+ if output. script_pubkey [ 2 .. ] == address_bytes {
352
337
return true ;
353
338
}
354
339
}
0 commit comments