1use std::collections::{BTreeSet, HashSet};
5
6use iota_protocol_config::ProtocolConfig;
7use iota_sdk_types::{
8 Address, MoveAuthenticator, MoveAuthenticatorV1, ObjectDigest, ObjectId, ObjectReference,
9 SharedObjectReference, TypeTag, Version, crypto::IntentMessage,
10};
11use serde::Serialize;
12
13use crate::{
14 error::{IotaError, IotaResult, UserInputError, UserInputResult},
15 signature::{AuthenticatorTrait, VerifyParams},
16 transaction::{CallArg, CallArgExt, InputObjectKind, add_type_tag_packages},
17};
18
19mod move_authenticator_ext {
20 pub trait Sealed {}
21 impl Sealed for super::MoveAuthenticator {}
22 impl Sealed for super::MoveAuthenticatorV1 {}
23}
24
25pub trait MoveAuthenticatorExt: Sized + move_authenticator_ext::Sealed {
26 fn address(&self) -> Address;
29
30 fn call_args(&self) -> &[CallArg];
33
34 fn type_args(&self) -> &[TypeTag];
36
37 fn object_to_authenticate(&self) -> &CallArg;
39
40 fn object_to_authenticate_components(
41 &self,
42 ) -> UserInputResult<(ObjectId, Option<Version>, Option<ObjectDigest>)>;
43
44 fn input_objects(&self, include_type_arg_packages: bool) -> Vec<InputObjectKind>;
48
49 fn type_arg_packages(&self) -> BTreeSet<ObjectId>;
52
53 fn receiving_objects(&self) -> Vec<ObjectReference>;
54
55 fn shared_objects(&self) -> Vec<SharedObjectReference>;
56
57 fn validity_check(&self, config: &ProtocolConfig) -> UserInputResult;
58}
59
60impl MoveAuthenticatorExt for MoveAuthenticator {
61 fn address(&self) -> Address {
62 match self {
63 Self::V1(v1) => v1.address(),
64 _ => unimplemented!(
65 "a new MoveAuthenticator enum variant was added and needs to be handled"
66 ),
67 }
68 }
69
70 fn call_args(&self) -> &[CallArg] {
71 match self {
72 Self::V1(v1) => v1.call_args(),
73 _ => unimplemented!(
74 "a new MoveAuthenticator enum variant was added and needs to be handled"
75 ),
76 }
77 }
78
79 fn type_args(&self) -> &[TypeTag] {
80 match self {
81 Self::V1(v1) => v1.type_args(),
82 _ => unimplemented!(
83 "a new MoveAuthenticator enum variant was added and needs to be handled"
84 ),
85 }
86 }
87
88 fn object_to_authenticate(&self) -> &CallArg {
89 match self {
90 Self::V1(v1) => v1.object_to_authenticate(),
91 _ => unimplemented!(
92 "a new MoveAuthenticator enum variant was added and needs to be handled"
93 ),
94 }
95 }
96
97 fn object_to_authenticate_components(
98 &self,
99 ) -> UserInputResult<(ObjectId, Option<Version>, Option<ObjectDigest>)> {
100 match self {
101 Self::V1(v1) => v1.object_to_authenticate_components(),
102 _ => unimplemented!(
103 "a new MoveAuthenticator enum variant was added and needs to be handled"
104 ),
105 }
106 }
107
108 fn input_objects(&self, include_type_arg_packages: bool) -> Vec<InputObjectKind> {
109 match self {
110 Self::V1(v1) => v1.input_objects(include_type_arg_packages),
111 _ => unimplemented!(
112 "a new MoveAuthenticator enum variant was added and needs to be handled"
113 ),
114 }
115 }
116
117 fn type_arg_packages(&self) -> BTreeSet<ObjectId> {
118 match self {
119 Self::V1(v1) => v1.type_arg_packages(),
120 _ => unimplemented!(
121 "a new MoveAuthenticator enum variant was added and needs to be handled"
122 ),
123 }
124 }
125
126 fn receiving_objects(&self) -> Vec<ObjectReference> {
127 match self {
128 Self::V1(v1) => v1.receiving_objects(),
129 _ => unimplemented!(
130 "a new MoveAuthenticator enum variant was added and needs to be handled"
131 ),
132 }
133 }
134
135 fn shared_objects(&self) -> Vec<SharedObjectReference> {
136 match self {
137 Self::V1(v1) => v1.shared_objects(),
138 _ => unimplemented!(
139 "a new MoveAuthenticator enum variant was added and needs to be handled"
140 ),
141 }
142 }
143
144 fn validity_check(&self, config: &ProtocolConfig) -> UserInputResult {
145 match self {
146 Self::V1(v1) => v1.validity_check(config),
147 _ => unimplemented!(
148 "a new MoveAuthenticator enum variant was added and needs to be handled"
149 ),
150 }
151 }
152}
153
154impl MoveAuthenticatorExt for MoveAuthenticatorV1 {
155 fn address(&self) -> Address {
158 self.address()
159 }
160
161 fn call_args(&self) -> &[CallArg] {
162 self.call_args()
163 }
164
165 fn type_args(&self) -> &[TypeTag] {
166 self.type_args()
167 }
168
169 fn object_to_authenticate(&self) -> &CallArg {
170 self.object_to_authenticate()
171 }
172
173 fn object_to_authenticate_components(
174 &self,
175 ) -> UserInputResult<(ObjectId, Option<Version>, Option<ObjectDigest>)> {
176 Ok(match self.object_to_authenticate() {
177 CallArg::Pure(_) => {
178 return Err(UserInputError::Unsupported(
179 "MoveAuthenticatorV1 cannot authenticate pure inputs".to_string(),
180 ));
181 }
182 CallArg::ImmutableOrOwned(object_ref) => (
183 object_ref.object_id,
184 Some(object_ref.version),
185 Some(object_ref.digest),
186 ),
187 CallArg::Shared(SharedObjectReference {
188 object_id, mutable, ..
189 }) => {
190 if *mutable {
191 return Err(UserInputError::Unsupported(
192 "MoveAuthenticatorV1 cannot authenticate mutable shared objects"
193 .to_string(),
194 ));
195 }
196
197 (*object_id, None, None)
198 }
199 CallArg::Receiving(_) => {
200 return Err(UserInputError::Unsupported(
201 "MoveAuthenticator cannot authenticate receiving objects".to_string(),
202 ));
203 }
204 _ => unimplemented!("a new CallArg enum variant was added and needs to be handled"),
205 })
206 }
207
208 fn input_objects(&self, include_type_arg_packages: bool) -> Vec<InputObjectKind> {
209 let mut inputs = self
210 .call_args()
211 .iter()
212 .filter_map(|arg| arg.input_object_kind())
213 .chain(self.object_to_authenticate().input_object_kind())
214 .collect::<Vec<_>>();
215 if include_type_arg_packages {
216 inputs.extend(
217 self.type_arg_packages()
218 .into_iter()
219 .map(InputObjectKind::MovePackage),
220 );
221 }
222 inputs
223 }
224
225 fn type_arg_packages(&self) -> BTreeSet<ObjectId> {
226 let mut packages = BTreeSet::new();
227 for type_arg in self.type_args() {
228 add_type_tag_packages(&mut packages, type_arg);
229 }
230 packages
231 }
232
233 fn receiving_objects(&self) -> Vec<ObjectReference> {
234 self.call_args()
235 .iter()
236 .filter_map(|arg| arg.as_opt_receiving().copied())
237 .collect()
238 }
239
240 fn shared_objects(&self) -> Vec<SharedObjectReference> {
243 self.call_args()
244 .iter()
245 .filter_map(|arg| arg.as_opt_shared())
246 .chain(self.object_to_authenticate().as_opt_shared())
247 .cloned()
248 .collect()
249 }
250
251 fn validity_check(&self, config: &ProtocolConfig) -> UserInputResult {
253 self.object_to_authenticate_components()?;
255
256 let max_args = (config.max_function_parameters() - 3) as usize;
265 fp_ensure!(
266 self.call_args().len() < max_args,
267 UserInputError::SizeLimitExceeded {
268 limit: "maximum arguments in MoveAuthenticatorV1".to_string(),
269 value: max_args.to_string()
270 }
271 );
272
273 fp_ensure!(
274 self.receiving_objects().is_empty(),
275 UserInputError::Unsupported(
276 "MoveAuthenticatorV1 cannot have receiving objects as input".to_string(),
277 )
278 );
279
280 let input_objects = self.input_objects(false);
285
286 if config.disallow_randomness_in_move_authenticator() {
292 fp_ensure!(
293 input_objects
294 .iter()
295 .all(|o| o.object_id() != ObjectId::RANDOMNESS_STATE),
296 UserInputError::RandomnessStateIsInMoveAuthenticatorInput {
297 object_id: ObjectId::RANDOMNESS_STATE,
298 }
299 );
300 }
301
302 if config.enable_pcool_flow() {
307 for kind in &input_objects {
308 match *kind {
309 InputObjectKind::MovePackage(package_id) => {
310 return Err(UserInputError::PackageIsInMoveAuthenticatorInput {
311 package_id,
312 });
313 }
314 InputObjectKind::SharedMoveObject {
315 id: ObjectId::AUTHENTICATOR_STATE,
316 ..
317 } => {
318 return Err(UserInputError::InaccessibleSystemObject {
319 object_id: ObjectId::AUTHENTICATOR_STATE,
320 });
321 }
322 InputObjectKind::SharedMoveObject {
323 id, mutable: true, ..
324 } => {
325 return Err(UserInputError::MutableSharedIsInMoveAuthenticatorInput {
326 object_id: id,
327 });
328 }
329 InputObjectKind::SharedMoveObject { .. }
330 | InputObjectKind::ImmOrOwnedMoveObject(_) => {}
331 }
332 }
333 }
334
335 let mut used = HashSet::new();
336 fp_ensure!(
337 input_objects.iter().all(|o| used.insert(o.object_id())),
338 UserInputError::DuplicateObjectRefInput
339 );
340
341 self.object_to_authenticate().validity_check(config)?;
345
346 self.call_args()
347 .iter()
348 .try_for_each(|obj| obj.validity_check(config))?;
349
350 let mut type_arguments_count = 0;
355 self.type_args().iter().try_for_each(|type_arg| {
356 crate::transaction::type_tag_validity_check(type_arg, config, &mut type_arguments_count)
357 })?;
358 if config.disallow_self_identifier_in_transactions() {
359 crate::transaction::move_authenticator_identifiers_validity_check(self)?;
360 }
361
362 Ok(())
363 }
364}
365
366impl AuthenticatorTrait for MoveAuthenticator {
367 fn verify_claims<T>(
370 &self,
371 value: &IntentMessage<T>,
372 author: Address,
373 aux_verify_data: &VerifyParams,
374 ) -> IotaResult
375 where
376 T: Serialize,
377 {
378 match self {
379 Self::V1(v1) => v1.verify_claims(value, author, aux_verify_data),
380 _ => unimplemented!(
381 "a new MoveAuthenticator enum variant was added and needs to be handled"
382 ),
383 }
384 }
385}
386
387impl AuthenticatorTrait for MoveAuthenticatorV1 {
388 fn verify_claims<T>(
391 &self,
392 _value: &IntentMessage<T>,
393 author: Address,
394 _aux_verify_data: &VerifyParams,
395 ) -> IotaResult
396 where
397 T: Serialize,
398 {
399 if author != self.address() {
400 return Err(IotaError::InvalidSignature {
401 error: "Invalid author".to_string(),
402 });
403 };
404
405 Ok(())
406 }
407}