diff --git a/crates/aleph-types/src/message/store.rs b/crates/aleph-types/src/message/store.rs index 53303ee..2d2e095 100644 --- a/crates/aleph-types/src/message/store.rs +++ b/crates/aleph-types/src/message/store.rs @@ -1,5 +1,5 @@ use crate::cid::Cid; -use crate::item_hash::ItemHash; +use crate::item_hash::{AlephItemHash, ItemHash}; use crate::storage_size::Bytes; use serde::{Deserialize, Serialize}; use std::collections::HashMap; @@ -8,13 +8,15 @@ use std::collections::HashMap; #[serde(tag = "item_type", rename_all = "lowercase")] pub enum StorageBackend { Ipfs { item_hash: Cid }, - Storage { item_hash: ItemHash }, + Storage { item_hash: AlephItemHash }, } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct StoreContent { #[serde(flatten)] - pub item_type: StorageBackend, + /// A combination of the `item_hash` and `item_type` fields, deserialized together to detect + /// inconsistencies. Use the `file_hash()` method to access the file hash. + file_hash: StorageBackend, #[serde(default)] /// Size of the file. Generated by CCNs upon processing. pub size: Option, @@ -26,11 +28,21 @@ pub struct StoreContent { pub metadata: Option>, } +impl StoreContent { + pub fn file_hash(&self) -> ItemHash { + match &self.file_hash { + StorageBackend::Ipfs { item_hash: cid } => ItemHash::Ipfs(cid.clone()), + StorageBackend::Storage { item_hash } => ItemHash::Native(*item_hash), + } + } +} + #[cfg(test)] mod tests { use super::*; use crate::chain::{Address, Chain, Signature}; use crate::channel::Channel; + use crate::item_hash; use crate::message::base_message::{Message, MessageContentEnum}; use crate::message::{ContentSource, MessageType}; use crate::timestamp::Timestamp; @@ -79,12 +91,16 @@ mod tests { match message.content() { MessageContentEnum::Store(store) => { assert_eq!( - store.item_type, + store.file_hash, StorageBackend::Ipfs { item_hash: Cid::try_from("QmYULJoNGPDmoRq4WNWTDTUvJGJv1hosox8H6vVd1kCsY8") .unwrap() } ); + assert_eq!( + store.file_hash(), + item_hash!("QmYULJoNGPDmoRq4WNWTDTUvJGJv1hosox8H6vVd1kCsY8") + ); assert!(store.size.is_none()); assert!(store.content_type.is_none());