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 std::fmt;
6
7use iota_sdk_types::{StructTag, TypeTag};
8use move_core_types::{
9    account_address::AccountAddress,
10    annotated_value::{MoveFieldLayout, MoveStructLayout, MoveTypeLayout},
11    ident_str,
12    identifier::IdentStr,
13};
14use serde::{Deserialize, Serialize};
15
16use crate::{
17    IOTA_FRAMEWORK_ADDRESS, MoveTypeTagTrait, base_types::ObjectID,
18    iota_sdk_types_conversions::struct_tag_sdk_to_core,
19};
20
21pub const RESOLVED_IOTA_ID: (&AccountAddress, &IdentStr, &IdentStr) = (
22    &IOTA_FRAMEWORK_ADDRESS,
23    ident_str!("object"),
24    ident_str!("ID"),
25);
26
27/// Rust version of the Move iota::object::Info type
28#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
29pub struct UID {
30    pub id: ID,
31}
32
33/// Rust version of the Move iota::object::ID type
34#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
35#[serde(transparent)]
36pub struct ID {
37    pub bytes: ObjectID,
38}
39
40impl UID {
41    pub fn new(bytes: ObjectID) -> Self {
42        Self {
43            id: { ID::new(bytes) },
44        }
45    }
46
47    pub fn object_id(&self) -> &ObjectID {
48        &self.id.bytes
49    }
50
51    pub fn to_bcs_bytes(&self) -> Vec<u8> {
52        bcs::to_bytes(&self).unwrap()
53    }
54
55    pub fn layout() -> MoveStructLayout {
56        MoveStructLayout {
57            type_: struct_tag_sdk_to_core(&StructTag::new_uid()),
58            fields: vec![MoveFieldLayout::new(
59                ident_str!("id").to_owned(),
60                MoveTypeLayout::Struct(Box::new(ID::layout())),
61            )],
62        }
63    }
64}
65
66impl fmt::Display for UID {
67    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
68        self.id.fmt(f)
69    }
70}
71
72impl ID {
73    pub fn new(object_id: ObjectID) -> Self {
74        Self { bytes: object_id }
75    }
76
77    pub fn layout() -> MoveStructLayout {
78        MoveStructLayout {
79            type_: struct_tag_sdk_to_core(&StructTag::new_id()),
80            fields: vec![MoveFieldLayout::new(
81                ident_str!("bytes").to_owned(),
82                MoveTypeLayout::Address,
83            )],
84        }
85    }
86}
87
88impl fmt::Display for ID {
89    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
90        self.bytes.fmt(f)
91    }
92}
93
94impl MoveTypeTagTrait for ID {
95    fn get_type_tag() -> TypeTag {
96        TypeTag::Struct(Box::new(StructTag::new_id()))
97    }
98}