neo3/neo_builder/transaction/
witness_scope.rs1use num_enum::TryFromPrimitive;
2use serde_derive::{Deserialize, Serialize};
3use strum_macros::{Display, EnumString};
4
5#[derive(
6 Display, EnumString, TryFromPrimitive, Debug, Hash, PartialEq, Eq, Clone, Serialize, Deserialize,
7)]
8#[repr(u8)]
9pub enum WitnessScope {
10 #[strum(serialize = "None")]
11 None = 0x00,
12 #[strum(serialize = "CalledByEntry")]
13 CalledByEntry = 0x01,
14 #[strum(serialize = "CustomContracts")]
15 CustomContracts = 0x10,
16 #[strum(serialize = "CustomGroups")]
17 CustomGroups = 0x20,
18 #[strum(serialize = "WitnessRules")]
19 WitnessRules = 0x40,
20 #[strum(serialize = "Global")]
21 Global = 0x80,
22}
23
24impl WitnessScope {
25 pub fn from_str(s: &str) -> Result<Self, String> {
26 s.parse::<WitnessScope>().map_err(|_| format!("Invalid witness scope: {}", s))
27 }
28
29 pub fn byte_repr(&self) -> u8 {
30 match self {
31 WitnessScope::None => 0x00,
32 WitnessScope::CalledByEntry => 0x01,
33 WitnessScope::CustomContracts => 0x10,
34 WitnessScope::CustomGroups => 0x20,
35 WitnessScope::WitnessRules => 0x40,
36 WitnessScope::Global => 0x80,
37 }
38 }
39
40 pub fn combine(scopes: &[Self]) -> u8 {
41 let mut flags = 0;
42 for scope in scopes {
43 flags |= scope.byte_repr();
44 }
45 flags
46 }
47
48 pub fn split(flags: u8) -> Vec<Self> {
50 let mut scopes = Vec::new();
51
52 if flags & Self::None.byte_repr() != 0 {
53 scopes.push(Self::None);
54 }
55 if flags & Self::CalledByEntry.byte_repr() != 0 {
56 scopes.push(Self::CalledByEntry);
57 }
58 if flags & Self::CustomContracts.byte_repr() != 0 {
59 scopes.push(Self::CustomContracts);
60 }
61 if flags & Self::CustomGroups.byte_repr() != 0 {
62 scopes.push(Self::CustomGroups);
63 }
64 if flags & Self::WitnessRules.byte_repr() != 0 {
65 scopes.push(Self::WitnessRules);
66 }
67 if flags & Self::Global.byte_repr() != 0 {
68 scopes.push(Self::Global);
69 }
70
71 scopes
72 }
73}