neo3/neo_protocol/responses/
response_transaction_signer.rs1use crate::{
2 builder::{SignerTrait, SignerType, WitnessRule, WitnessScope},
3 crypto::Secp256r1PublicKey,
4 deserialize_scopes, deserialize_script_hash, deserialize_vec_script_hash, serialize_scopes,
5 serialize_script_hash, serialize_vec_script_hash,
6};
7use neo3::TypeError;
8use primitive_types::H160;
9use serde::{Deserialize, Serialize};
10use std::hash::{Hash, Hasher};
11
12#[derive(Default, Serialize, Deserialize, Clone, Debug, PartialEq)]
13pub struct RTransactionSigner {
14 #[serde(rename = "account")]
15 #[serde(serialize_with = "serialize_script_hash")]
16 #[serde(deserialize_with = "deserialize_script_hash")]
17 pub account: H160,
18
19 #[serde(rename = "scopes", default)]
20 #[serde(serialize_with = "serialize_scopes")]
21 #[serde(deserialize_with = "deserialize_scopes")]
22 pub scopes: Vec<WitnessScope>,
23
24 #[serde(rename = "allowedcontracts", default)]
25 #[serde(serialize_with = "serialize_vec_script_hash")]
26 #[serde(deserialize_with = "deserialize_vec_script_hash")]
27 pub allowed_contracts: Vec<H160>,
28
29 #[serde(rename = "allowedgroups", default)]
30 pub allowed_groups: Vec<String>,
35
36 #[serde(rename = "rules", default)]
37 pub rules: Vec<WitnessRule>,
39}
40
41impl Hash for RTransactionSigner {
42 fn hash<H: Hasher>(&self, state: &mut H) {
43 self.account.hash(state);
44 self.scopes.hash(state);
45 self.allowed_contracts.hash(state);
46 self.allowed_groups.hash(state);
47 self.rules.hash(state);
48 }
49}
50
51impl RTransactionSigner {
52 pub fn new(account: H160, scopes: Vec<WitnessScope>) -> Self {
53 Self { account, scopes, allowed_contracts: vec![], allowed_groups: vec![], rules: vec![] }
54 }
55
56 pub fn new_full(
57 account: H160,
58 scopes: Vec<WitnessScope>,
59 allowed_contracts: Vec<H160>,
60 allowed_groups: Vec<String>,
61 rules: Vec<WitnessRule>,
62 ) -> Self {
63 Self { account, scopes, allowed_contracts, allowed_groups, rules }
64 }
65
66 pub fn get_first_scope(&self) -> Result<&WitnessScope, TypeError> {
67 if self.scopes.is_empty() {
68 return Err(TypeError::IndexOutOfBounds(
69 "This transaction signer does not have any witness scopes. It might be malformed, since every transaction signer needs to have a witness scope specified.".to_string(),
70 ));
71 }
72 self.get_scope(0)
73 }
74
75 pub fn get_scope(&self, index: usize) -> Result<&WitnessScope, TypeError> {
76 if index >= self.scopes.len() {
77 return Err(TypeError::IndexOutOfBounds(format!(
78 "This transaction signer only has {} witness scopes. Tried to access index {}.",
79 self.scopes.len(),
80 index
81 )));
82 }
83 Ok(&self.scopes[index])
84 }
85
86 pub fn get_first_allowed_contract(&self) -> Result<&H160, TypeError> {
87 if self.allowed_contracts.is_empty() {
88 return Err(TypeError::IndexOutOfBounds(
89 "This transaction signer does not allow any specific contract.".to_string(),
90 ));
91 }
92 self.get_allowed_contract(0)
93 }
94
95 pub fn get_allowed_contract(&self, index: usize) -> Result<&H160, TypeError> {
96 if index >= self.allowed_contracts.len() {
97 return Err(TypeError::IndexOutOfBounds(format!(
98 "This transaction signer only allows {} contracts. Tried to access index {}.",
99 self.allowed_contracts.len(),
100 index
101 )));
102 }
103 Ok(&self.allowed_contracts[index])
104 }
105
106 pub fn get_first_allowed_group(&self) -> Result<&String, TypeError> {
107 if self.allowed_groups.is_empty() {
108 return Err(TypeError::IndexOutOfBounds(
109 "This transaction signer does not allow any specific group.".to_string(),
110 ));
111 }
112 self.get_allowed_group(0)
113 }
114
115 pub fn get_allowed_group(&self, index: usize) -> Result<&String, TypeError> {
116 if index >= self.allowed_groups.len() {
117 return Err(TypeError::IndexOutOfBounds(format!(
118 "This transaction signer only allows {} groups. Tried to access index {}.",
119 self.allowed_groups.len(),
120 index
121 )));
122 }
123 Ok(&self.allowed_groups[index])
124 }
125
126 pub fn get_first_rule(&self) -> Result<&WitnessRule, TypeError> {
127 if self.rules.is_empty() {
128 return Err(TypeError::IndexOutOfBounds(
129 "This transaction signer does not have any witness rules.".to_string(),
130 ));
131 }
132 self.get_rule(0)
133 }
134
135 pub fn get_rule(&self, index: usize) -> Result<&WitnessRule, TypeError> {
136 if index >= self.rules.len() {
137 return Err(TypeError::IndexOutOfBounds(format!(
138 "This transaction signer only has {} witness rules. Tried to access index {}.",
139 self.rules.len(),
140 index
141 )));
142 }
143 Ok(&self.rules[index])
144 }
145}
146
147impl SignerTrait for RTransactionSigner {
148 fn get_type(&self) -> SignerType {
149 SignerType::TransactionSigner
150 }
151
152 fn get_signer_hash(&self) -> &H160 {
153 &self.account
154 }
155
156 fn set_signer_hash(&mut self, signer_hash: H160) {
157 self.account = signer_hash;
158 }
159
160 fn get_scopes(&self) -> &Vec<WitnessScope> {
161 &self.scopes
162 }
163
164 fn get_scopes_mut(&mut self) -> &mut Vec<WitnessScope> {
165 &mut self.scopes
166 }
167
168 fn set_scopes(&mut self, scopes: Vec<WitnessScope>) {
169 self.scopes = scopes;
170 }
171
172 fn get_allowed_contracts(&self) -> &Vec<H160> {
173 &self.allowed_contracts
174 }
175
176 fn get_allowed_contracts_mut(&mut self) -> &mut Vec<H160> {
177 &mut self.allowed_contracts
178 }
179
180 fn get_allowed_groups(&self) -> &Vec<Secp256r1PublicKey> {
181 unimplemented!("RTransactionSigner uses String for groups, conversion needed")
182 }
183
184 fn get_allowed_groups_mut(&mut self) -> &mut Vec<Secp256r1PublicKey> {
185 unimplemented!("RTransactionSigner uses String for groups, conversion needed")
186 }
187
188 fn get_rules(&self) -> &Vec<WitnessRule> {
189 &self.rules
190 }
191
192 fn get_rules_mut(&mut self) -> &mut Vec<WitnessRule> {
193 &mut self.rules
194 }
195}
196
197