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
use super::*;
impl ExecutionContext {
pub(crate) fn handle_crypto_syscall(&mut self, name: &str) -> Result<bool, RuntimeError> {
match name {
"System.Crypto.CheckSig" => {
let sig_item = self.pop_stack()?;
let pub_item = self.pop_stack()?;
let pubkey = Self::stack_item_to_bytes(pub_item);
let sig = Self::stack_item_to_bytes(sig_item);
// Use the current transaction/message hash for verification
// In a real implementation, this would come from the transaction context
let msg_hash = self.get_current_message_hash();
let ok = Self::verify_secp256k1_with_message(&msg_hash, &pubkey, &sig);
self.push_stack(StackItem::Boolean(ok))?;
Ok(true)
}
"System.Crypto.CheckMultisig" => {
let sigs_item = self.pop_stack()?;
let pubs_item = self.pop_stack()?;
let msg_hash = self.get_current_message_hash();
// Extract individual items from arrays
let pub_items = match pubs_item {
StackItem::Array(items) => {
let items = items.borrow();
items.clone()
}
_ => {
// Fallback: treat as single pubkey
vec![pubs_item]
}
};
let sig_items = match sigs_item {
StackItem::Array(items) => {
let items = items.borrow();
items.clone()
}
_ => {
vec![sigs_item]
}
};
// M-of-N verification: each sig must match a pubkey in order
let mut pub_idx = 0;
let mut all_valid = !sig_items.is_empty() && !pub_items.is_empty();
for sig_item in &sig_items {
let sig = Self::stack_item_to_bytes(sig_item.clone());
let mut found = false;
while pub_idx < pub_items.len() {
let pubkey = Self::stack_item_to_bytes(pub_items[pub_idx].clone());
pub_idx += 1;
if Self::verify_secp256k1_with_message(&msg_hash, &pubkey, &sig) {
found = true;
break;
}
}
if !found {
all_valid = false;
break;
}
}
self.push_stack(StackItem::Boolean(all_valid))?;
Ok(true)
}
_ => Ok(false),
}
}
}