-
Notifications
You must be signed in to change notification settings - Fork 58
/
main.cairo
253 lines (234 loc) · 7.92 KB
/
main.cairo
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
use shinigami_compiler::compiler::CompilerImpl;
use shinigami_engine::engine::{EngineImpl, EngineInternalImpl};
use shinigami_engine::transaction::{EngineInternalTransactionImpl, EngineInternalTransactionTrait};
use shinigami_engine::flags;
use shinigami_engine::witness;
use shinigami_engine::hash_cache::HashCacheImpl;
use shinigami_utils::byte_array::felt252_to_byte_array;
use shinigami_utils::bytecode::hex_to_bytecode;
use shinigami_tests::utxo::UTXO;
use shinigami_tests::validate;
#[derive(Clone, Drop)]
struct InputData {
ScriptSig: ByteArray,
ScriptPubKey: ByteArray
}
#[derive(Clone, Drop)]
struct InputDataWithFlags {
ScriptSig: ByteArray,
ScriptPubKey: ByteArray,
Flags: ByteArray
}
#[derive(Clone, Drop)]
struct InputDataWithWitness {
ScriptSig: ByteArray,
ScriptPubKey: ByteArray,
Flags: ByteArray,
Witness: ByteArray
}
fn run_with_flags(input: InputDataWithFlags) -> Result<(), felt252> {
println!(
"Running Bitcoin Script with ScriptSig: '{}', ScriptPubKey: '{}' and Flags: '{}'",
input.ScriptSig,
input.ScriptPubKey,
input.Flags
);
let mut compiler = CompilerImpl::new();
let script_pubkey = compiler.compile(input.ScriptPubKey)?;
let compiler = CompilerImpl::new();
let script_sig = compiler.compile(input.ScriptSig)?;
let tx = EngineInternalTransactionImpl::new_signed(script_sig, script_pubkey.clone());
let flags = flags::parse_flags(input.Flags);
let hash_cache = HashCacheImpl::new(@tx);
let mut engine = EngineImpl::new(@script_pubkey, @tx, 0, flags, 0, @hash_cache)?;
let _ = engine.execute()?;
Result::Ok(())
}
fn run_with_witness(input: InputDataWithWitness) -> Result<(), felt252> {
println!(
"Running Bitcoin Script with ScriptSig: '{}', ScriptPubKey: '{}', Flags: '{}' and Witness: '{}'",
input.ScriptSig,
input.ScriptPubKey,
input.Flags,
input.Witness
);
let mut compiler = CompilerImpl::new();
let script_pubkey = compiler.compile(input.ScriptPubKey)?;
let compiler = CompilerImpl::new();
let script_sig = compiler.compile(input.ScriptSig)?;
let witness = witness::parse_witness_input(input.Witness);
let value = 1; // TODO
let tx = EngineInternalTransactionImpl::new_signed_witness(
script_sig, script_pubkey.clone(), witness, value
);
let flags = flags::parse_flags(input.Flags);
let hash_cache = HashCacheImpl::new(@tx);
let mut engine = EngineImpl::new(@script_pubkey, @tx, 0, flags, value, @hash_cache)?;
let _ = engine.execute()?;
Result::Ok(())
}
fn run(input: InputData) -> Result<(), felt252> {
println!(
"Running Bitcoin Script with ScriptSig: '{}' and ScriptPubKey: '{}'",
input.ScriptSig,
input.ScriptPubKey
);
let mut compiler = CompilerImpl::new();
let script_pubkey = compiler.compile(input.ScriptPubKey)?;
let compiler = CompilerImpl::new();
let script_sig = compiler.compile(input.ScriptSig)?;
let tx = EngineInternalTransactionImpl::new_signed(script_sig, script_pubkey.clone());
let hash_cache = HashCacheImpl::new(@tx);
let mut engine = EngineImpl::new(@script_pubkey, @tx, 0, 0, 0, @hash_cache)?;
let res = engine.execute();
match res {
Result::Ok(_) => {
println!("Execution successful");
Result::Ok(())
},
Result::Err(e) => {
println!("Execution failed: {}", felt252_to_byte_array(e));
Result::Err(e)
}
}
}
fn run_with_json(input: InputData) -> Result<(), felt252> {
println!(
"Running Bitcoin Script with ScriptSig: '{}' and ScriptPubKey: '{}'",
input.ScriptSig,
input.ScriptPubKey
);
let mut compiler = CompilerImpl::new();
let script_pubkey = compiler.compile(input.ScriptPubKey)?;
let compiler = CompilerImpl::new();
let script_sig = compiler.compile(input.ScriptSig)?;
let tx = EngineInternalTransactionImpl::new_signed(script_sig, script_pubkey.clone());
let hash_cache = HashCacheImpl::new(@tx);
let mut engine = EngineImpl::new(@script_pubkey, @tx, 0, 0, 0, @hash_cache)?;
let _ = engine.execute()?;
engine.json();
Result::Ok(())
}
fn debug(input: InputData) -> Result<bool, felt252> {
println!(
"Running Bitcoin Script with ScriptSig: '{}' and ScriptPubKey: '{}'",
input.ScriptSig,
input.ScriptPubKey
);
let mut compiler = CompilerImpl::new();
let script_pubkey = compiler.compile(input.ScriptPubKey)?;
let compiler = CompilerImpl::new();
let script_sig = compiler.compile(input.ScriptSig)?;
let tx = EngineInternalTransactionImpl::new_signed(script_sig, script_pubkey.clone());
let hash_cache = HashCacheImpl::new(@tx);
let mut engine = EngineImpl::new(@script_pubkey, @tx, 0, 0, 0, @hash_cache)?;
let mut res = Result::Ok(true);
while true {
res = engine.step();
if res.is_err() {
break;
}
if res.unwrap() == false {
break;
}
engine.json();
};
res
}
fn main(input: InputDataWithFlags) -> u8 {
let res = run_with_flags(input);
match res {
Result::Ok(_) => {
println!("Execution successful");
1
},
Result::Err(e) => {
println!("Execution failed: {}", felt252_to_byte_array(e));
0
}
}
}
fn main_with_witness(input: InputDataWithWitness) -> u8 {
let res = run_with_witness(input);
match res {
Result::Ok(_) => {
println!("Execution successful");
1
},
Result::Err(e) => {
println!("Execution failed: {}", felt252_to_byte_array(e));
0
}
}
}
fn backend_run(input: InputData) -> u8 {
let res = run_with_json(input);
match res {
Result::Ok(_) => {
println!("Execution successful");
1
},
Result::Err(e) => {
println!("Execution failed: {}", felt252_to_byte_array(e));
0
}
}
}
fn backend_debug(input: InputData) -> u8 {
let res = debug(input);
match res {
Result::Ok(_) => {
println!("Execution successful");
1
},
Result::Err(e) => {
println!("Execution failed: {}", felt252_to_byte_array(e));
0
}
}
}
#[derive(Drop)]
struct ValidateRawInput {
raw_transaction: ByteArray,
utxo_hints: Array<UTXO>,
flags: ByteArray,
}
fn run_raw_transaction(mut input: ValidateRawInput) -> u8 {
println!("Running Bitcoin Script with raw transaction: '{}'", input.raw_transaction);
let raw_transaction = hex_to_bytecode(@input.raw_transaction);
let transaction = EngineInternalTransactionTrait::deserialize(raw_transaction);
// Parse the flags
let script_flags = flags::parse_flags(input.flags);
println!("Script flags: {}", script_flags);
// For coinbase transactions, we expect no UTXO hints since it creates new coins
if input.utxo_hints.is_empty() {
println!("Potential coinbase transaction detected - skipping validation");
return 1;
}
let mut utxo_hints = array![];
for hint in input
.utxo_hints
.span() {
println!("UTXO hint: 'amount: {}, script_pubkey: {}'", hint.amount, hint.pubkey_script);
let pubkey_script = hex_to_bytecode(hint.pubkey_script);
utxo_hints
.append(
UTXO {
amount: *hint.amount,
pubkey_script: pubkey_script,
block_height: *hint.block_height,
}
);
};
let res = validate::validate_transaction(@transaction, script_flags, utxo_hints);
match res {
Result::Ok(_) => {
println!("Execution successful");
1
},
Result::Err(e) => {
println!("Execution failed: {}", felt252_to_byte_array(e));
0
}
}
}