Skip to content

Commit e1a7464

Browse files
committed
feat(iota-sdk-types)!: make MoveStruct contents checked (#1065)
* feat(iota-sdk-types)!: make MoveStruct contents checked * use thiserror * remove contents_mut_unchecked() * make remaining fields private, move serialization
1 parent 5cef1c9 commit e1a7464

8 files changed

Lines changed: 180 additions & 83 deletions

File tree

crates/iota-sdk-ffi/src/types/object.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -561,22 +561,24 @@ pub struct MoveStruct {
561561

562562
impl From<iota_sdk::types::MoveStruct> for MoveStruct {
563563
fn from(value: iota_sdk::types::MoveStruct) -> Self {
564-
let struct_tag: iota_sdk::types::StructTag = value.object_type.into();
564+
let (object_type, version, contents) = value.into_parts();
565+
let struct_tag: iota_sdk::types::StructTag = object_type.into();
565566
Self {
566567
struct_type: Arc::new(struct_tag.into()),
567-
version: Arc::new(value.version.into()),
568-
contents: value.contents,
568+
version: Arc::new(version.into()),
569+
contents,
569570
}
570571
}
571572
}
572573

573574
impl From<MoveStruct> for iota_sdk::types::MoveStruct {
574575
fn from(value: MoveStruct) -> Self {
575-
Self {
576-
object_type: value.struct_type.0.clone().into(),
577-
version: **value.version,
578-
contents: value.contents,
579-
}
576+
iota_sdk::types::MoveStruct::new(
577+
value.struct_type.0.clone().into(),
578+
**value.version,
579+
value.contents,
580+
)
581+
.expect("FFI MoveStruct should always have valid contents")
580582
}
581583
}
582584

crates/iota-sdk-types/src/framework.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,21 @@ impl Coin {
3030
match &object.data {
3131
super::ObjectData::Struct(move_struct) => {
3232
let coin_type = move_struct
33-
.object_type
33+
.object_type()
3434
.coin_type_opt()
3535
.ok_or(CoinFromObjectError::NotACoin)?;
3636

37-
let contents = &move_struct.contents;
37+
let contents = move_struct.contents();
3838
if contents.len() != ObjectId::LENGTH + std::mem::size_of::<u64>() {
3939
return Err(CoinFromObjectError::InvalidContentLength);
4040
}
4141

42-
let id = ObjectId::new((&contents[..ObjectId::LENGTH]).try_into().unwrap());
4342
let balance =
4443
u64::from_le_bytes((&contents[ObjectId::LENGTH..]).try_into().unwrap());
4544

4645
Ok(Self {
4746
coin_type: coin_type.clone(),
48-
id,
47+
id: move_struct.id(),
4948
balance,
5049
})
5150
}

crates/iota-sdk-types/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ pub use gas::GasCostSummary;
160160
pub use move_core::{Identifier, StructTag, TypeParseError, TypeTag};
161161
pub use move_package::{MovePackage, MovePackageData, TypeOrigin, UpgradeInfo, UpgradePolicy};
162162
pub use object::{
163-
GenesisObject, MoveObjectType, MoveStruct, Object, ObjectData, ObjectReference, ObjectType,
164-
Owner,
163+
GenesisObject, MoveObjectType, MoveStruct, MoveStructContentsError, Object, ObjectData,
164+
ObjectReference, ObjectType, Owner,
165165
};
166166
pub use object_id::ObjectId;
167167
#[cfg(feature = "serde")]

0 commit comments

Comments
 (0)