1#![warn(
4 future_incompatible,
5 nonstandard_style,
6 rust_2018_idioms,
7 rust_2021_compatibility
8)]
9
10use base_types::{SequenceNumber, SuiAddress};
11use move_binary_format::CompiledModule;
12use move_binary_format::file_format::{AbilitySet, SignatureToken};
13use move_bytecode_utils::resolve_struct;
14use move_core_types::language_storage::ModuleId;
15use move_core_types::{account_address::AccountAddress, language_storage::StructTag};
16pub use move_core_types::{identifier::Identifier, language_storage::TypeTag};
17use object::OBJECT_START_VERSION;
18
19use base_types::ObjectID;
20
21pub use mysten_network::multiaddr;
22
23use crate::base_types::{RESOLVED_ASCII_STR, RESOLVED_UTF8_STR};
24use crate::{base_types::RESOLVED_STD_OPTION, id::RESOLVED_SUI_ID};
25
26#[macro_use]
27pub mod error;
28pub mod accumulator_event;
29pub mod accumulator_metadata;
30pub mod accumulator_root;
31pub mod address_alias;
32pub mod allowance;
33pub mod authenticator_state;
34pub mod balance;
35pub mod balance_change;
36pub mod base_types;
37pub mod bridge;
38pub mod clock;
39pub mod coin;
40pub mod coin_registry;
41pub mod coin_regulation;
42pub mod coin_reservation;
43pub mod collection_types;
44pub mod committee;
45pub mod config;
46pub mod crypto;
47pub mod deny_list_v1;
48pub mod deny_list_v2;
49pub mod derived_object;
50pub mod digests;
51pub mod display;
52pub mod display_registry;
53pub mod dynamic_field;
54pub mod effects;
55pub mod epoch_data;
56pub mod event;
57pub mod executable_transaction;
58pub mod execution;
59pub mod execution_params;
60pub mod execution_status;
61pub mod full_checkpoint_content;
62pub mod funds_accumulator;
63pub mod gas;
64pub mod gas_coin;
65pub mod gas_model;
66pub mod global_state_hash;
67pub mod governance;
68pub mod id;
69pub mod in_memory_storage;
70pub mod inner_temporary_store;
71pub mod layout_resolver;
72pub mod message_envelope;
73pub mod messages_checkpoint;
74pub mod messages_consensus;
75pub mod messages_grpc;
76pub mod messages_safe_client;
77pub mod metrics;
78pub mod move_package;
79pub mod multisig;
80pub mod multisig_legacy;
81pub mod nitro_attestation;
82pub mod node_role;
83pub mod object;
84pub mod passkey_authenticator;
85pub mod programmable_transaction_builder;
86pub mod ptb_trace;
87pub mod randomness_state;
88pub mod rpc_proto_conversions;
89pub mod signature;
90pub mod signature_verification;
91pub mod storage;
92pub mod sui_sdk_types_conversions;
93pub mod sui_serde;
94pub mod sui_system_state;
95pub mod supported_protocol_versions;
96pub mod test_checkpoint_data_builder;
97pub mod traffic_control;
98pub mod transaction;
99pub mod transaction_deny_rules;
100pub mod transaction_driver_types;
101pub mod transaction_executor;
102pub mod transfer;
103pub mod type_input;
104pub mod versioned;
105pub mod zk_login_authenticator;
106pub mod zk_login_util;
107
108#[path = "./unit_tests/utils.rs"]
109pub mod utils;
110
111macro_rules! built_in_ids {
112 ($($addr:ident / $id:ident = $init:expr);* $(;)?) => {
113 $(
114 pub const $addr: AccountAddress = AccountAddress::from_suffix($init);
115 pub const $id: ObjectID = ObjectID::from_address($addr);
116 )*
117 }
118}
119
120macro_rules! built_in_pkgs {
121 ($($addr:ident / $id:ident = $init:expr);* $(;)?) => {
122 built_in_ids! { $($addr / $id = $init;)* }
123 pub const SYSTEM_PACKAGE_ADDRESSES: &[AccountAddress] = &[$($addr),*];
124 pub fn is_system_package(addr: impl Into<AccountAddress>) -> bool {
125 matches!(addr.into(), $($addr)|*)
126 }
127 }
128}
129
130built_in_pkgs! {
131 MOVE_STDLIB_ADDRESS / MOVE_STDLIB_PACKAGE_ID = 0x1;
132 SUI_FRAMEWORK_ADDRESS / SUI_FRAMEWORK_PACKAGE_ID = 0x2;
133 SUI_SYSTEM_ADDRESS / SUI_SYSTEM_PACKAGE_ID = 0x3;
134 BRIDGE_ADDRESS / BRIDGE_PACKAGE_ID = 0xb;
135 DEEPBOOK_ADDRESS / DEEPBOOK_PACKAGE_ID = 0xdee9;
136}
137
138built_in_ids! {
139 SUI_SYSTEM_STATE_ADDRESS / SUI_SYSTEM_STATE_OBJECT_ID = 0x5;
140 SUI_CLOCK_ADDRESS / SUI_CLOCK_OBJECT_ID = 0x6;
141 SUI_AUTHENTICATOR_STATE_ADDRESS / SUI_AUTHENTICATOR_STATE_OBJECT_ID = 0x7;
142 SUI_RANDOMNESS_STATE_ADDRESS / SUI_RANDOMNESS_STATE_OBJECT_ID = 0x8;
143 SUI_BRIDGE_ADDRESS / SUI_BRIDGE_OBJECT_ID = 0x9;
144 SUI_COIN_REGISTRY_ADDRESS / SUI_COIN_REGISTRY_OBJECT_ID = 0xc;
145 SUI_DISPLAY_REGISTRY_ADDRESS / SUI_DISPLAY_REGISTRY_OBJECT_ID = 0xd;
146 SUI_DENY_LIST_ADDRESS / SUI_DENY_LIST_OBJECT_ID = 0x403;
147 SUI_ACCUMULATOR_ROOT_ADDRESS / SUI_ACCUMULATOR_ROOT_OBJECT_ID = 0xacc;
148 SUI_ADDRESS_ALIAS_STATE_ADDRESS / SUI_ADDRESS_ALIAS_STATE_OBJECT_ID = 0xa;
149 SUI_FORWARDING_ADDRESS_REGISTRY_ADDRESS / SUI_FORWARDING_ADDRESS_REGISTRY_OBJECT_ID = 0xfa;
150 SUI_FREEZE_LIST_ADDRESS / SUI_FREEZE_LIST_OBJECT_ID = 0xcf;
151}
152
153pub const SUI_SYSTEM_STATE_OBJECT_SHARED_VERSION: SequenceNumber = OBJECT_START_VERSION;
154pub const SUI_CLOCK_OBJECT_SHARED_VERSION: SequenceNumber = OBJECT_START_VERSION;
155
156pub const IMPLICITLY_READ_SYSTEM_OBJECTS: &[ObjectID] = &[
169 SUI_ACCUMULATOR_ROOT_OBJECT_ID,
170 SUI_FORWARDING_ADDRESS_REGISTRY_OBJECT_ID,
171 SUI_CLOCK_OBJECT_ID,
172 SUI_FREEZE_LIST_OBJECT_ID,
173];
174
175pub fn sui_framework_address_concat_string(suffix: &str) -> String {
176 format!("{}{suffix}", SUI_FRAMEWORK_ADDRESS.to_hex_literal())
177}
178
179pub fn parse_sui_address(s: &str) -> anyhow::Result<SuiAddress> {
187 use move_core_types::parsing::address::ParsedAddress;
188 Ok(ParsedAddress::parse(s)?
189 .into_account_address(&resolve_address)?
190 .into())
191}
192
193pub fn parse_sui_module_id(s: &str) -> anyhow::Result<ModuleId> {
197 use move_core_types::parsing::types::ParsedModuleId;
198 ParsedModuleId::parse(s)?.into_module_id(&resolve_address)
199}
200
201pub fn parse_sui_fq_name(s: &str) -> anyhow::Result<(ModuleId, String)> {
206 use move_core_types::parsing::types::ParsedFqName;
207 ParsedFqName::parse(s)?.into_fq_name(&resolve_address)
208}
209
210pub fn parse_sui_struct_tag(s: &str) -> anyhow::Result<StructTag> {
215 use move_core_types::parsing::types::ParsedDatatype;
216 ParsedDatatype::parse(s)?.into_struct_tag(&resolve_address)
217}
218
219pub fn parse_sui_type_tag(s: &str) -> anyhow::Result<TypeTag> {
223 use move_core_types::parsing::types::ParsedType;
224 ParsedType::parse(s)?.into_type_tag(&resolve_address)
225}
226
227pub fn resolve_address(addr: &str) -> Option<AccountAddress> {
229 match addr {
230 "deepbook" => Some(DEEPBOOK_ADDRESS),
231 "std" => Some(MOVE_STDLIB_ADDRESS),
232 "sui" => Some(SUI_FRAMEWORK_ADDRESS),
233 "sui_system" => Some(SUI_SYSTEM_ADDRESS),
234 "bridge" => Some(BRIDGE_ADDRESS),
235 _ => None,
236 }
237}
238
239pub trait MoveTypeTagTrait {
240 fn get_type_tag() -> TypeTag;
241
242 fn get_instance_type_tag(&self) -> TypeTag {
243 Self::get_type_tag()
244 }
245}
246
247impl MoveTypeTagTrait for bool {
248 fn get_type_tag() -> TypeTag {
249 TypeTag::Bool
250 }
251}
252
253impl MoveTypeTagTrait for u8 {
254 fn get_type_tag() -> TypeTag {
255 TypeTag::U8
256 }
257}
258
259impl MoveTypeTagTrait for u64 {
260 fn get_type_tag() -> TypeTag {
261 TypeTag::U64
262 }
263}
264
265impl MoveTypeTagTrait for ObjectID {
266 fn get_type_tag() -> TypeTag {
267 TypeTag::Address
268 }
269}
270
271impl MoveTypeTagTrait for SuiAddress {
272 fn get_type_tag() -> TypeTag {
273 TypeTag::Address
274 }
275}
276
277impl<T: MoveTypeTagTrait> MoveTypeTagTrait for Vec<T> {
278 fn get_type_tag() -> TypeTag {
279 TypeTag::Vector(Box::new(T::get_type_tag()))
280 }
281}
282
283pub trait MoveTypeTagTraitGeneric {
284 fn get_type_tag(type_params: &[TypeTag]) -> TypeTag;
285}
286
287pub fn is_primitive(
288 view: &CompiledModule,
289 function_type_args: &[AbilitySet],
290 s: &SignatureToken,
291) -> bool {
292 use SignatureToken as S;
293 match s {
294 S::Bool | S::U8 | S::U16 | S::U32 | S::U64 | S::U128 | S::U256 | S::Address => true,
295 S::Signer => false,
296 S::TypeParameter(idx) => !function_type_args[*idx as usize].has_key(),
298
299 S::Datatype(idx) => [RESOLVED_SUI_ID, RESOLVED_ASCII_STR, RESOLVED_UTF8_STR]
300 .contains(&resolve_struct(view, *idx)),
301
302 S::DatatypeInstantiation(inst) => {
303 let (idx, targs) = &**inst;
304 let resolved_struct = resolve_struct(view, *idx);
305 resolved_struct == RESOLVED_STD_OPTION
307 && targs.len() == 1
308 && is_primitive(view, function_type_args, &targs[0])
309 }
310
311 S::Vector(inner) => is_primitive(view, function_type_args, inner),
312 S::Reference(_) | S::MutableReference(_) => false,
313 }
314}
315
316pub fn is_object(
317 view: &CompiledModule,
318 function_type_args: &[AbilitySet],
319 t: &SignatureToken,
320) -> Result<bool, String> {
321 use SignatureToken as S;
322 match t {
323 S::Reference(inner) | S::MutableReference(inner) => {
324 is_object(view, function_type_args, inner)
325 }
326 _ => is_object_struct(view, function_type_args, t),
327 }
328}
329
330pub fn is_object_vector(
331 view: &CompiledModule,
332 function_type_args: &[AbilitySet],
333 t: &SignatureToken,
334) -> Result<bool, String> {
335 use SignatureToken as S;
336 match t {
337 S::Vector(inner) => is_object_struct(view, function_type_args, inner),
338 _ => is_object_struct(view, function_type_args, t),
339 }
340}
341
342fn is_object_struct(
343 view: &CompiledModule,
344 function_type_args: &[AbilitySet],
345 s: &SignatureToken,
346) -> Result<bool, String> {
347 use SignatureToken as S;
348 match s {
349 S::Bool
350 | S::U8
351 | S::U16
352 | S::U32
353 | S::U64
354 | S::U128
355 | S::U256
356 | S::Address
357 | S::Signer
358 | S::Vector(_)
359 | S::Reference(_)
360 | S::MutableReference(_) => Ok(false),
361 S::TypeParameter(idx) => Ok(function_type_args
362 .get(*idx as usize)
363 .map(|abs| abs.has_key())
364 .unwrap_or(false)),
365 S::Datatype(_) | S::DatatypeInstantiation(_) => {
366 let abilities = view
367 .abilities(s, function_type_args)
368 .map_err(|vm_err| vm_err.to_string())?;
369 Ok(abilities.has_key())
370 }
371 }
372}
373
374#[cfg(test)]
375mod tests {
376 use super::*;
377 use expect_test::expect;
378 use sui_protocol_config::ProtocolConfig;
379
380 #[test]
381 fn test_parse_sui_numeric_address() {
382 let result = parse_sui_address("0x2").expect("should not error");
383
384 let expected =
385 expect!["0x0000000000000000000000000000000000000000000000000000000000000002"];
386 expected.assert_eq(&result.to_string());
387 }
388
389 #[test]
390 fn test_parse_sui_named_address() {
391 let result = parse_sui_address("sui").expect("should not error");
392
393 let expected =
394 expect!["0x0000000000000000000000000000000000000000000000000000000000000002"];
395 expected.assert_eq(&result.to_string());
396 }
397
398 #[test]
399 fn test_parse_sui_module_id() {
400 let result = parse_sui_module_id("0x2::sui").expect("should not error");
401 let expected =
402 expect!["0x0000000000000000000000000000000000000000000000000000000000000002::sui"];
403 expected.assert_eq(&result.to_canonical_string(true));
404 }
405
406 #[test]
407 fn test_parse_sui_fq_name() {
408 let (module, name) = parse_sui_fq_name("0x2::object::new").expect("should not error");
409 let expected = expect![
410 "0x0000000000000000000000000000000000000000000000000000000000000002::object::new"
411 ];
412 expected.assert_eq(&format!(
413 "{}::{name}",
414 module.to_canonical_display(true)
415 ));
416 }
417
418 #[test]
419 fn test_parse_sui_struct_tag_short_account_addr() {
420 let result = parse_sui_struct_tag("0x2::sui::SUI").expect("should not error");
421
422 let expected = expect!["0x2::sui::SUI"];
423 expected.assert_eq(&result.to_string());
424
425 let expected =
426 expect!["0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI"];
427 expected.assert_eq(&result.to_canonical_string(true));
428 }
429
430 #[test]
431 fn test_parse_sui_struct_tag_long_account_addr() {
432 let result = parse_sui_struct_tag(
433 "0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI",
434 )
435 .expect("should not error");
436
437 let expected = expect!["0x2::sui::SUI"];
438 expected.assert_eq(&result.to_string());
439
440 let expected =
441 expect!["0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI"];
442 expected.assert_eq(&result.to_canonical_string(true));
443 }
444
445 #[test]
446 fn test_parse_sui_struct_with_type_param_short_addr() {
447 let result =
448 parse_sui_struct_tag("0x2::coin::COIN<0x2::sui::SUI>").expect("should not error");
449
450 let expected = expect!["0x2::coin::COIN<0x2::sui::SUI>"];
451 expected.assert_eq(&result.to_string());
452
453 let expected = expect![
454 "0x0000000000000000000000000000000000000000000000000000000000000002::coin::COIN<0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI>"
455 ];
456 expected.assert_eq(&result.to_canonical_string(true));
457 }
458
459 #[test]
460 fn test_parse_sui_struct_with_type_param_long_addr() {
461 let result = parse_sui_struct_tag("0x0000000000000000000000000000000000000000000000000000000000000002::coin::COIN<0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI>")
462 .expect("should not error");
463
464 let expected = expect!["0x2::coin::COIN<0x2::sui::SUI>"];
465 expected.assert_eq(&result.to_string());
466
467 let expected = expect![
468 "0x0000000000000000000000000000000000000000000000000000000000000002::coin::COIN<0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI>"
469 ];
470 expected.assert_eq(&result.to_canonical_string(true));
471 }
472
473 #[test]
474 fn test_complex_struct_tag_with_short_addr() {
475 let result =
476 parse_sui_struct_tag("0xe7::vec_coin::VecCoin<vector<0x2::coin::Coin<0x2::sui::SUI>>>")
477 .expect("should not error");
478
479 let expected = expect!["0xe7::vec_coin::VecCoin<vector<0x2::coin::Coin<0x2::sui::SUI>>>"];
480 expected.assert_eq(&result.to_string());
481
482 let expected = expect![
483 "0x00000000000000000000000000000000000000000000000000000000000000e7::vec_coin::VecCoin<vector<0x0000000000000000000000000000000000000000000000000000000000000002::coin::Coin<0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI>>>"
484 ];
485 expected.assert_eq(&result.to_canonical_string(true));
486 }
487
488 #[test]
489 fn test_complex_struct_tag_with_long_addr() {
490 let result = parse_sui_struct_tag("0x00000000000000000000000000000000000000000000000000000000000000e7::vec_coin::VecCoin<vector<0x0000000000000000000000000000000000000000000000000000000000000002::coin::Coin<0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI>>>")
491 .expect("should not error");
492
493 let expected = expect!["0xe7::vec_coin::VecCoin<vector<0x2::coin::Coin<0x2::sui::SUI>>>"];
494 expected.assert_eq(&result.to_string());
495
496 let expected = expect![
497 "0x00000000000000000000000000000000000000000000000000000000000000e7::vec_coin::VecCoin<vector<0x0000000000000000000000000000000000000000000000000000000000000002::coin::Coin<0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI>>>"
498 ];
499 expected.assert_eq(&result.to_canonical_string(true));
500 }
501
502 #[test]
503 fn test_dynamic_field_short_addr() {
504 let result = parse_sui_struct_tag(
505 "0x2::dynamic_field::Field<address, 0xdee9::custodian_v2::Account<0x234::coin::COIN>>",
506 )
507 .expect("should not error");
508
509 let expected = expect![
510 "0x2::dynamic_field::Field<address, 0xdee9::custodian_v2::Account<0x234::coin::COIN>>"
511 ];
512 expected.assert_eq(&result.to_string());
513
514 let expected = expect![
515 "0x0000000000000000000000000000000000000000000000000000000000000002::dynamic_field::Field<address,0x000000000000000000000000000000000000000000000000000000000000dee9::custodian_v2::Account<0x0000000000000000000000000000000000000000000000000000000000000234::coin::COIN>>"
516 ];
517 expected.assert_eq(&result.to_canonical_string(true));
518 }
519
520 #[test]
521 fn test_dynamic_field_long_addr() {
522 let result = parse_sui_struct_tag(
523 "0x2::dynamic_field::Field<address, 0xdee9::custodian_v2::Account<0x234::coin::COIN>>",
524 )
525 .expect("should not error");
526
527 let expected = expect![
528 "0x2::dynamic_field::Field<address, 0xdee9::custodian_v2::Account<0x234::coin::COIN>>"
529 ];
530 expected.assert_eq(&result.to_string());
531
532 let expected = expect![
533 "0x0000000000000000000000000000000000000000000000000000000000000002::dynamic_field::Field<address,0x000000000000000000000000000000000000000000000000000000000000dee9::custodian_v2::Account<0x0000000000000000000000000000000000000000000000000000000000000234::coin::COIN>>"
534 ];
535 expected.assert_eq(&result.to_canonical_string(true));
536 }
537
538 const IMPLICITLY_READ_SYSTEM_OBJECTS_EXEMPT_FROM_INPUT_CHARGE: &[ObjectID] =
541 &[SUI_CLOCK_OBJECT_ID, SUI_ACCUMULATOR_ROOT_OBJECT_ID];
542
543 #[test]
544 fn implicitly_read_system_objects_are_classified_for_input_charge() {
545 let config = ProtocolConfig::get_for_max_version_UNSAFE();
546 let charge_ids = config
547 .mutable_system_object_input_charge_ids()
548 .expect("latest protocol version configures mutable system object input charges");
549 assert!(
550 !charge_ids.is_empty(),
551 "latest protocol version configures at least one mutable system object input charge"
552 );
553
554 for id in charge_ids {
555 let object_id = ObjectID::from(*id);
556 assert!(
557 IMPLICITLY_READ_SYSTEM_OBJECTS.contains(&object_id),
558 "configured mutable system object input charge ID {id} is not implicitly read",
559 );
560 }
561
562 for object_id in IMPLICITLY_READ_SYSTEM_OBJECTS {
563 let is_charged = charge_ids.contains(&(*object_id).into());
564 let is_exempt =
565 IMPLICITLY_READ_SYSTEM_OBJECTS_EXEMPT_FROM_INPUT_CHARGE.contains(object_id);
566 assert_ne!(
567 is_charged, is_exempt,
568 "implicitly read system object {object_id} must be added to \
569 mutable_system_object_input_charge_ids or \
570 IMPLICITLY_READ_SYSTEM_OBJECTS_EXEMPT_FROM_INPUT_CHARGE"
571 );
572 }
573 }
574}