Skip to content

Commit

Permalink
Merge pull request #228 from xch-dev/hash-mismatch
Browse files Browse the repository at this point in the history
Handle hash mismatches and mime types better
  • Loading branch information
Rigidity authored Jan 4, 2025
2 parents 8100139 + c44578e commit 51b1b0a
Show file tree
Hide file tree
Showing 30 changed files with 507 additions and 385 deletions.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 31 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ sage-config = { path = "./crates/sage-config" }
sage-database = { path = "./crates/sage-database" }
sage-keychain = { path = "./crates/sage-keychain" }
sage-wallet = { path = "./crates/sage-wallet" }
sage-assets = { path = "./crates/sage-assets" }

# Serialization
serde = "1.0.204"
Expand Down Expand Up @@ -104,6 +105,9 @@ num-traits = "0.2.19"
paste = "1.0.15"
chrono = "0.4.38"
glob = "0.3.1"
num-bigint = "0.4.6"
uuid = "1.11.0"
mime-sniffer = "0.1.3"

# Tracing
tracing = "0.1.40"
Expand Down
2 changes: 2 additions & 0 deletions crates/sage-api/src/records/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ pub struct NftRecord {
pub struct NftData {
pub blob: Option<String>,
pub mime_type: Option<String>,
pub hash_matches: bool,
pub metadata_json: Option<String>,
pub metadata_hash_matches: bool,
}
29 changes: 29 additions & 0 deletions crates/sage-assets/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "sage-assets"
version = "0.8.6"
edition = "2021"
license = "Apache-2.0"
description = "Fetches non-critical data from various APIs for use in Sage wallet."
authors = ["Rigidity <[email protected]>"]
homepage = "https://github.com/rigidity/sage"
repository = "https://github.com/rigidity/sage"
readme = { workspace = true }
keywords = { workspace = true }
categories = { workspace = true }

[lints]
workspace = true

[dependencies]
chia = { workspace = true }
clvmr = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
num-bigint = { workspace = true, features = ["serde"] }
uuid = { workspace = true, features = ["serde"] }
reqwest = { workspace = true }
futures-lite = { workspace = true }
futures-util = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true }
mime-sniffer = { workspace = true }
3 changes: 3 additions & 0 deletions crates/sage-assets/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod nfts;

pub use nfts::*;
5 changes: 5 additions & 0 deletions crates/sage-assets/src/nfts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod chip0007_metadata;
mod fetch_nft_uri;

pub use chip0007_metadata::*;
pub use fetch_nft_uri::*;
94 changes: 94 additions & 0 deletions crates/sage-assets/src/nfts/chip0007_metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use std::{num::NonZeroUsize, str::FromStr};

use num_bigint::BigInt;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// Pertains to [CHIP-0007](https://github.com/Chia-Network/chips/blob/main/CHIPs/chip-0007.md) off-chain metadata for NFTs.
/// The `data` field in the spec is ommitted as it's not useful for wallet implementations at this time.
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct Chip0007Metadata {
pub format: String,
pub name: String,
pub description: String,
pub minting_tool: Option<String>,
pub sensitive_content: Option<SensitiveContent>,
pub series_number: Option<NonZeroUsize>,
pub series_total: Option<NonZeroUsize>,
pub attributes: Option<Vec<NftAttribute>>,
pub collection: Option<Collection>,
}

impl Chip0007Metadata {
pub fn is_sensitive(&self) -> bool {
match &self.sensitive_content {
Some(SensitiveContent::Flag(flag)) => *flag,
Some(SensitiveContent::Items(items)) => !items.is_empty(),
None => false,
}
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SensitiveContent {
Flag(bool),
Items(Vec<String>),
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct NftAttribute {
pub trait_type: AttributeValue,
pub value: AttributeValue,
pub min_value: Option<BigInt>,
pub max_value: Option<BigInt>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum AttributeValue {
Integer(BigInt),
String(String),
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Collection {
pub id: Uuid,
pub name: String,
pub attributes: Option<Vec<CollectionAttribute>>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CollectionAttribute {
#[serde(rename = "type")]
pub kind: AttributeValue,
pub value: AttributeValue,
}

impl Chip0007Metadata {
pub fn parse(json: &str) -> Result<Self, serde_json::Error> {
serde_json::from_str(json)
}

pub fn from_bytes(bytes: &[u8]) -> Result<Self, serde_json::Error> {
serde_json::from_slice(bytes)
}
}

impl FromStr for Chip0007Metadata {
type Err = serde_json::Error;

fn from_str(json: &str) -> Result<Self, Self::Err> {
Self::parse(json)
}
}

impl AttributeValue {
pub fn as_str(&self) -> Option<&str> {
match self {
AttributeValue::String(value) => Some(value),
AttributeValue::Integer(..) => None,
}
}
}
Loading

0 comments on commit 51b1b0a

Please sign in to comment.