iota_types/
id.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use move_core_types::{
6    account_address::AccountAddress,
7    annotated_value::{MoveFieldLayout, MoveStructLayout, MoveTypeLayout},
8    ident_str,
9    identifier::IdentStr,
10    language_storage::{StructTag, TypeTag},
11};
12use schemars::JsonSchema;
13use serde::{Deserialize, Serialize};
14
15use crate::{IOTA_FRAMEWORK_ADDRESS, MoveTypeTagTrait, base_types::ObjectID};
16
17pub const OBJECT_MODULE_NAME_STR: &str = "object";
18pub const OBJECT_MODULE_NAME: &IdentStr = ident_str!(OBJECT_MODULE_NAME_STR);
19pub const UID_STRUCT_NAME: &IdentStr = ident_str!("UID");
20pub const ID_STRUCT_NAME: &IdentStr = ident_str!("ID");
21pub const RESOLVED_IOTA_ID: (&AccountAddress, &IdentStr, &IdentStr) =
22    (&IOTA_FRAMEWORK_ADDRESS, OBJECT_MODULE_NAME, ID_STRUCT_NAME);
23
24/// Rust version of the Move iota::object::Info type
25#[derive(Debug, Serialize, Deserialize, JsonSchema, Clone, Eq, PartialEq)]
26pub struct UID {
27    pub id: ID,
28}
29
30/// Rust version of the Move iota::object::ID type
31#[derive(Debug, Serialize, Deserialize, JsonSchema, Clone, Eq, PartialEq)]
32#[serde(transparent)]
33pub struct ID {
34    pub bytes: ObjectID,
35}
36
37impl UID {
38    pub fn new(bytes: ObjectID) -> Self {
39        Self {
40            id: { ID::new(bytes) },
41        }
42    }
43
44    pub fn type_() -> StructTag {
45        StructTag {
46            address: IOTA_FRAMEWORK_ADDRESS,
47            module: OBJECT_MODULE_NAME.to_owned(),
48            name: UID_STRUCT_NAME.to_owned(),
49            type_params: Vec::new(),
50        }
51    }
52
53    pub fn object_id(&self) -> &ObjectID {
54        &self.id.bytes
55    }
56
57    pub fn to_bcs_bytes(&self) -> Vec<u8> {
58        bcs::to_bytes(&self).unwrap()
59    }
60
61    pub fn layout() -> MoveStructLayout {
62        MoveStructLayout {
63            type_: Self::type_(),
64            fields: vec![MoveFieldLayout::new(
65                ident_str!("id").to_owned(),
66                MoveTypeLayout::Struct(Box::new(ID::layout())),
67            )],
68        }
69    }
70}
71
72impl ID {
73    pub fn new(object_id: ObjectID) -> Self {
74        Self { bytes: object_id }
75    }
76
77    pub fn type_() -> StructTag {
78        StructTag {
79            address: IOTA_FRAMEWORK_ADDRESS,
80            module: OBJECT_MODULE_NAME.to_owned(),
81            name: ID_STRUCT_NAME.to_owned(),
82            type_params: Vec::new(),
83        }
84    }
85
86    pub fn layout() -> MoveStructLayout {
87        MoveStructLayout {
88            type_: Self::type_(),
89            fields: vec![MoveFieldLayout::new(
90                ident_str!("bytes").to_owned(),
91                MoveTypeLayout::Address,
92            )],
93        }
94    }
95}
96
97impl MoveTypeTagTrait for ID {
98    fn get_type_tag() -> TypeTag {
99        TypeTag::Struct(Box::new(Self::type_()))
100    }
101}