-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
182 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use anyhow::Result; | ||
use std::{ | ||
collections::HashMap, | ||
hash::{DefaultHasher, Hash, Hasher}, | ||
}; | ||
|
||
use super::metadata_service::MetadataService; | ||
|
||
pub struct FakeMetadataService { | ||
data: HashMap<String, Vec<u8>>, | ||
} | ||
|
||
impl FakeMetadataService { | ||
pub fn new() -> Self { | ||
Self { data: HashMap::new() } | ||
} | ||
} | ||
|
||
#[allow(async_fn_in_trait)] | ||
impl MetadataService for FakeMetadataService { | ||
async fn upload(&mut self, data: Vec<u8>) -> Result<String> { | ||
let mut hasher = DefaultHasher::new(); | ||
data.hash(&mut hasher); | ||
let hash = hasher.finish(); | ||
|
||
let uri = format!("ipfs://{:x}", hash); | ||
self.data.insert(uri.clone(), data); | ||
|
||
Ok(uri) | ||
} | ||
|
||
#[cfg(test)] | ||
async fn get(&self, uri: String) -> Result<Vec<u8>> { | ||
Ok(self.data.get(&uri).map(|x| x.clone()).unwrap_or(Vec::<u8>::new())) | ||
} | ||
} |
26 changes: 10 additions & 16 deletions
26
crates/dojo/world/src/metadata/ipfs.rs → ...s/dojo/world/src/metadata/ipfs_service.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use std::hash::{DefaultHasher, Hash, Hasher}; | ||
|
||
use super::metadata_service::MetadataService; | ||
use anyhow::Result; | ||
use serde_json::json; | ||
use starknet_crypto::Felt; | ||
|
||
use crate::config::metadata_config::{ResourceMetadata, WorldMetadata}; | ||
use crate::uri::Uri; | ||
|
||
/// Helper function to compute metadata hash using the Hash trait impl. | ||
fn compute_metadata_hash<T>(data: T) -> u64 | ||
where | ||
T: Hash, | ||
{ | ||
let mut hasher = DefaultHasher::new(); | ||
data.hash(&mut hasher); | ||
hasher.finish() | ||
} | ||
|
||
#[allow(async_fn_in_trait)] | ||
pub trait MetadataStorage { | ||
async fn upload(&self, service: &mut impl MetadataService) -> Result<String>; | ||
|
||
async fn upload_if_changed( | ||
&self, | ||
service: &mut impl MetadataService, | ||
current_hash: Felt, | ||
) -> Result<Option<(String, Felt)>> | ||
where | ||
Self: std::hash::Hash, | ||
{ | ||
let new_hash = compute_metadata_hash(self); | ||
let new_hash = Felt::from_raw([0, 0, 0, new_hash]); | ||
|
||
if new_hash != current_hash { | ||
let new_uri = self.upload(service).await?; | ||
return Ok(Some((new_uri, new_hash))); | ||
} | ||
|
||
Ok(None) | ||
} | ||
} | ||
|
||
#[allow(async_fn_in_trait)] | ||
impl MetadataStorage for WorldMetadata { | ||
async fn upload(&self, service: &mut impl MetadataService) -> Result<String> { | ||
let mut meta = self.clone(); | ||
|
||
if let Some(Uri::File(icon)) = &self.icon_uri { | ||
let icon_data = std::fs::read(icon)?; | ||
meta.icon_uri = Some(Uri::Ipfs(service.upload(icon_data).await?)); | ||
}; | ||
|
||
if let Some(Uri::File(cover)) = &self.cover_uri { | ||
let cover_data = std::fs::read(cover)?; | ||
meta.cover_uri = Some(Uri::Ipfs(service.upload(cover_data).await?)); | ||
}; | ||
|
||
let serialized = json!(meta).to_string(); | ||
service.upload(serialized.as_bytes().to_vec()).await | ||
} | ||
} | ||
|
||
#[allow(async_fn_in_trait)] | ||
impl MetadataStorage for ResourceMetadata { | ||
async fn upload(&self, service: &mut impl MetadataService) -> Result<String> { | ||
let mut meta = self.clone(); | ||
|
||
if let Some(Uri::File(icon)) = &self.icon_uri { | ||
let icon_data = std::fs::read(icon)?; | ||
meta.icon_uri = Some(Uri::Ipfs(service.upload(icon_data).await?)); | ||
}; | ||
|
||
let serialized = json!(meta).to_string(); | ||
service.upload(serialized.as_bytes().to_vec()).await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use anyhow::Result; | ||
|
||
#[allow(async_fn_in_trait)] | ||
pub trait MetadataService: std::marker::Send + std::marker::Sync + std::marker::Unpin { | ||
async fn upload(&mut self, data: Vec<u8>) -> Result<String>; | ||
|
||
#[cfg(test)] | ||
async fn get(&self, uri: String) -> Result<Vec<u8>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.