neo3/neo_protocol/responses/
response_transaction_signer.rs

1use 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	// #[serde(serialize_with = "serialize_vec_public_key_option")]
31	// #[serde(deserialize_with = "deserialize_vec_public_key_option")]
32	// #[serde(skip_serializing_if = "Option::is_none")]
33	// #[serde(default)]
34	pub allowed_groups: Vec<String>,
35
36	#[serde(rename = "rules", default)]
37	// #[serde(default)]
38	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// impl NeoSerializable for TransactionSigner {
198// 	type Error = TransactionError;
199
200// 	fn size(&self) -> usize {
201// 		let mut size = (NeoConstants::HASH160_SIZE + 1) as usize;
202// 		if self.scopes.contains(&WitnessScope::CustomContracts) {
203// 			size += &self.allowed_contracts.clone().unwrap().var_size();
204// 		}
205// 		if self.scopes.contains(&WitnessScope::CustomGroups) {
206// 			size += &self.allowed_groups.clone().unwrap().var_size();
207// 		}
208
209// 		if self.scopes.contains(&WitnessScope::WitnessRules) {
210// 			size += &self.rules.clone().unwrap().var_size();
211// 		}
212
213// 		size
214// 	}
215
216// 	fn encode(&self, writer: &mut Encoder) {
217// 		writer.write_serializable_fixed(self.get_signer_hash());
218// 		writer.write_u8(WitnessScope::combine(self.scopes.as_slice()));
219// 		if self.scopes.contains(&WitnessScope::CustomContracts) {
220// 			writer.write_serializable_variable_list(self.allowed_contracts.as_ref().unwrap());
221// 		}
222// 		if self.scopes.contains(&WitnessScope::CustomGroups) {
223// 			writer.write_serializable_variable_list(self.allowed_groups.as_ref().unwrap());
224// 		}
225// 		if self.scopes.contains(&WitnessScope::WitnessRules) {
226// 			writer.write_serializable_variable_list(self.rules.as_ref().unwrap());
227// 		}
228// 	}
229
230// 	fn decode(reader: &mut Decoder) -> Result<Self, Self::Error>
231// 	where
232// 		Self: Sized,
233// 	{
234// 		let mut signer = TransactionSigner::default();
235// 		signer.set_signer_hash(reader.read_serializable().unwrap());
236// 		let scopes = WitnessScope::split(reader.read_u8());
237// 		signer.set_scopes(scopes);
238// 		if signer.get_scopes().contains(&WitnessScope::CustomContracts) {
239// 			signer.allowed_contracts = Some(reader.read_serializable_list().unwrap());
240// 		}
241// 		if signer.get_scopes().contains(&WitnessScope::CustomGroups) {
242// 			signer.allowed_groups = Some(reader.read_serializable_list().unwrap());
243// 		}
244// 		if signer.get_scopes().contains(&WitnessScope::WitnessRules) {
245// 			signer.rules = Some(reader.read_serializable_list().unwrap());
246// 		}
247// 		Ok(signer)
248// 	}
249
250// 	fn to_array(&self) -> Vec<u8> {
251// 		let writer = &mut Encoder::new();
252// 		self.encode(writer);
253// 		writer.to_bytes()
254// 	}
255// }