neo_solidity/runtime/bridge/
bridge_helpers.rs1fn extract_bytes(item: &StackItem) -> Result<Vec<u8>, VMBridgeError> {
2 Ok(match item {
3 StackItem::ByteArray(bytes) => bytes.borrow().clone(),
4 StackItem::UnsignedInteger(value) => value.to_be_bytes().to_vec(),
5 StackItem::Integer(value) => {
6 if *value < 0 {
7 return Err(VMBridgeError::StackOperationFailed {
8 message: "Negative integers not supported for byte extraction".to_string(),
9 });
10 }
11 (*value as u64).to_be_bytes().to_vec()
12 }
13 StackItem::Array(_) => {
14 return Err(VMBridgeError::StackOperationFailed {
15 message: "Cannot extract bytes from array stack item".to_string(),
16 })
17 }
18 StackItem::Map(_) => {
19 return Err(VMBridgeError::StackOperationFailed {
20 message: "Cannot extract bytes from map".to_string(),
21 })
22 }
23 StackItem::Boolean(flag) => vec![if *flag { 1 } else { 0 }],
24 StackItem::Null => Vec::new(),
25 })
26}
27
28fn extract_integer(item: &StackItem) -> Result<u128, VMBridgeError> {
29 Ok(match item {
30 StackItem::UnsignedInteger(value) => *value as u128,
31 StackItem::Integer(value) => {
32 if *value < 0 {
33 return Err(VMBridgeError::StackOperationFailed {
34 message: "Negative integers not supported here".to_string(),
35 });
36 }
37 *value as u128
38 }
39 StackItem::ByteArray(bytes) => {
40 let bytes = bytes.borrow();
41 if bytes.is_empty() {
42 0
43 } else {
44 let mut padded = [0u8; 16];
45 let copy_len = bytes.len().min(16);
46 padded[16 - copy_len..].copy_from_slice(&bytes[bytes.len() - copy_len..]);
47 u128::from_be_bytes(padded)
48 }
49 }
50 StackItem::Array(_) => {
51 return Err(VMBridgeError::StackOperationFailed {
52 message: "Arrays not supported for integer extraction".to_string(),
53 });
54 }
55 StackItem::Map(_) => {
56 return Err(VMBridgeError::StackOperationFailed {
57 message: "Maps not supported for integer extraction".to_string(),
58 });
59 }
60 StackItem::Boolean(flag) => {
61 if *flag {
62 1
63 } else {
64 0
65 }
66 }
67 StackItem::Null => 0,
68 })
69}