-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfixed_lock_script.rs
More file actions
166 lines (143 loc) · 5.18 KB
/
fixed_lock_script.rs
File metadata and controls
166 lines (143 loc) · 5.18 KB
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
use std::marker::PhantomData;
use ark_ff::PrimeField;
use ark_r1cs_std::{
eq::EqGadget,
prelude::{AllocVar, Boolean},
};
use ark_relations::r1cs::{ConstraintSystemRef, SynthesisError};
use chain_gang::script::Script;
use crate::bitcoin_predicates::data_structures::unit::{BitcoinUnit, BitcoinUnitVar};
use crate::constraints::{
script::ScriptVar,
tx::{TxVar, TxVarConfig},
};
use crate::traits::BitcoinPredicate;
/// Bitcoin Predicate to enforce that the output of the transaction at `index`
/// has locking script equal to `lock_script`
pub struct FixedLockScript<F: PrimeField, P: TxVarConfig + Clone> {
pub lock_script: Script,
pub index: usize,
_phantom_field: PhantomData<F>,
_phantom_config: PhantomData<P>,
}
impl<F: PrimeField, P: TxVarConfig + Clone> FixedLockScript<F, P> {
pub fn new(lock_script: Script, index: usize) -> Self {
Self {
lock_script,
index,
_phantom_field: PhantomData,
_phantom_config: PhantomData,
}
}
}
impl<F: PrimeField, P: TxVarConfig + Clone> BitcoinPredicate<F, P> for FixedLockScript<F, P> {
type LockingData = BitcoinUnit<F, P>;
type UnlockingData = BitcoinUnit<F, P>;
type Witness = BitcoinUnit<F, P>;
type LockingDataVar = BitcoinUnitVar<F, P>;
type UnlockingDataVar = BitcoinUnitVar<F, P>;
type WitnessVar = BitcoinUnitVar<F, P>;
fn generate_constraints(
&self,
cs: ConstraintSystemRef<F>,
_locking_data: &Self::LockingDataVar,
_unlocking_data: &Self::UnlockingDataVar,
spending_data: &TxVar<F, P>,
_witness: &Self::WitnessVar,
) -> Result<Boolean<F>, SynthesisError> {
// Validate input
assert!(
self.index <= spending_data.outputs.len(),
"Index: {} is larger the the number of outputs: {}",
self.index,
spending_data.outputs.len()
);
// Enforce that output at index `self.index` has the correct locking script
spending_data.outputs[self.index]
.lock_script
.is_eq(&ScriptVar::<F>::new_constant(
cs.clone(),
self.lock_script.clone(),
)?)
}
}
#[cfg(test)]
mod test {
use ark_bls12_381::Fr as F;
use ark_r1cs_std::alloc::AllocVar;
use ark_relations::r1cs::ConstraintSystem;
use chain_gang::address::addr_decode;
use chain_gang::script::Script;
use chain_gang::messages::{OutPoint, Tx, TxIn, TxOut};
use chain_gang::network::Network;
use chain_gang::transaction::p2pkh;
use chain_gang::util::Hash256;
use crate::bitcoin_predicates::data_structures::unit::BitcoinUnitVar;
use crate::constraints::tx::TxVar;
use crate::{constraints::tx::TxVarConfig, traits::BitcoinPredicate};
use super::FixedLockScript;
#[derive(Clone)]
struct Config;
impl TxVarConfig for Config {
const N_INPUTS: usize = 1;
const N_OUTPUTS: usize = 2;
const LEN_UNLOCK_SCRIPTS: &[usize] = &[0];
const LEN_LOCK_SCRIPTS: &[usize] = &[0x19, 0x19];
}
fn test_predicate(addr: &str, lock_script: Script, index: usize, expected: bool) {
let hash160 = addr_decode(addr, Network::BSV_Testnet).unwrap().0;
let tx = Tx {
version: 2,
inputs: vec![TxIn {
prev_output: OutPoint {
hash: Hash256::decode(
"f671dc000ad12795e86b59b27e0c367d9b026bbd4141c227b9285867a53bb6f7",
)
.unwrap(),
index: 0,
},
unlock_script: Script(vec![]),
sequence: 0,
}],
outputs: vec![
TxOut {
satoshis: 100,
lock_script: lock_script.clone(),
},
TxOut {
satoshis: 259899900,
lock_script: p2pkh::create_lock_script(&hash160),
},
],
lock_time: 0,
};
let predicate = FixedLockScript::<F, Config>::new(lock_script, index);
let cs = ConstraintSystem::<F>::new_ref();
let tx_var = TxVar::<F, Config>::new_input(cs.clone(), || Ok(tx)).unwrap();
predicate
.enforce_constraints(
cs.clone(),
&BitcoinUnitVar::default(),
&BitcoinUnitVar::default(),
&tx_var,
&BitcoinUnitVar::default(),
)
.unwrap();
assert_eq!(cs.is_satisfied().unwrap(), expected);
}
#[test]
fn test_predicate_is_ok() {
let addr = "mfmKD4cP6Na7T8D87XRSiR7shA1HNGSaec";
let hash160 = addr_decode(addr, Network::BSV_Testnet).unwrap().0;
let lock_script = p2pkh::create_lock_script(&hash160);
test_predicate(addr, lock_script, 0, true);
}
#[test]
fn test_predicate_fails() {
let addr = "mfmKD4cP6Na7T8D87XRSiR7shA1HNGSaec";
let wrong_addr = "mzXd2pQG2dbgK9trYAZcpKycWDEfjVbeMz";
let hash160 = addr_decode(addr, Network::BSV_Testnet).unwrap().0;
let lock_script = p2pkh::create_lock_script(&hash160);
test_predicate(wrong_addr, lock_script, 1, false);
}
}