Skip to main content

MetadataApi

Struct MetadataApi 

Source
pub struct MetadataApi {
    pub ipfs: Ipfs,
}
Expand description

High-level facade mirroring MetadataApi from the TypeScript SDK.

All operations are available as free functions in this module; MetadataApi groups them under a single type that carries an optional Ipfs configuration, so callers do not have to thread IPFS settings through every call.

§Typical workflow

use cow_app_data::{Ipfs, MetadataApi};

// 1. Create the API (with optional IPFS config).
let api = MetadataApi::new();

// 2. Build an app-data document.
let doc = api.generate_app_data_doc("MyDApp");

// 3. Validate it.
let result = api.validate_app_data_doc(&doc);
assert!(result.is_valid());

// 4. Derive the hash + CID.
let info = api.get_app_data_info(&doc).unwrap();
assert!(info.app_data_hex.starts_with("0x"));

Fields§

§ipfs: Ipfs

Optional IPFS configuration.

Implementations§

Source§

impl MetadataApi

Source

pub fn new() -> Self

Create a new MetadataApi with default IPFS settings.

Uses DEFAULT_IPFS_READ_URI for fetching and DEFAULT_IPFS_WRITE_URI for uploads. No Pinata credentials are configured — set them via with_ipfs if you need upload capability.

§Returns

A new MetadataApi with default Ipfs configuration.

Source

pub const fn with_ipfs(ipfs: Ipfs) -> Self

Create a MetadataApi with custom IPFS configuration.

§Parameters
  • ipfs — the Ipfs settings (gateway URIs, Pinata credentials).
§Returns

A new MetadataApi using the given configuration.

§Example
use cow_app_data::{Ipfs, MetadataApi};

let api = MetadataApi::with_ipfs(
    Ipfs::default().with_read_uri("https://my-gateway.io/ipfs").with_pinata("key", "secret"),
);
Source

pub fn generate_app_data_doc(&self, app_code: impl Into<String>) -> AppDataDoc

Generate a minimal AppDataDoc for app_code.

§Example
use cow_app_data::MetadataApi;

let api = MetadataApi::new();
let doc = api.generate_app_data_doc("CoW Swap");
assert_eq!(doc.app_code.as_deref(), Some("CoW Swap"));
Source

pub fn validate_app_data_doc(&self, doc: &AppDataDoc) -> ValidationResult

Validate an AppDataDoc.

§Example
use cow_app_data::{AppDataDoc, MetadataApi};

let api = MetadataApi::new();
let doc = AppDataDoc::new("CoW Swap");
let result = api.validate_app_data_doc(&doc);
assert!(result.is_valid());
Source

pub fn appdata_hex(&self, doc: &AppDataDoc) -> Result<B256, CowError>

Compute the keccak256 hash of doc as a B256.

Delegates to appdata_hex. The document is serialised to deterministic JSON before hashing.

§Parameters
§Returns

A 32-byte B256 digest.

§Errors

Propagates CowError::AppData on serialisation failure.

Source

pub fn get_app_data_info( &self, doc: &AppDataDoc, ) -> Result<AppDataInfo, CowError>

Derive the full AppDataInfo (JSON content, hex hash, CID) from doc.

Delegates to get_app_data_info. This is the most common method for obtaining everything needed to submit an order and pin data on IPFS.

§Parameters
§Returns

An AppDataInfo with canonical JSON, hex hash, and CID.

§Errors

Propagates CowError::AppData.

§Example
use cow_app_data::{AppDataDoc, MetadataApi};

let api = MetadataApi::new();
let doc = AppDataDoc::new("CoW Swap");
let info = api.get_app_data_info(&doc)?;
assert!(info.app_data_hex.starts_with("0x"));
Source

pub fn get_app_data_info_from_str( &self, json: &str, ) -> Result<AppDataInfo, CowError>

Derive AppDataInfo from a pre-serialised JSON string.

Hashes json directly without re-serialising, preserving the exact byte sequence as the canonical keccak256 pre-image.

§Parameters
  • json — the canonical JSON string.
§Returns

An AppDataInfo where app_data_content is json verbatim.

§Errors

Propagates CowError::AppData.

Source

pub fn app_data_hex_to_cid( &self, app_data_hex: &str, ) -> Result<String, CowError>

Convert app_data_hex to a CIDv1 base16 string.

Delegates to appdata_hex_to_cid.

§Parameters
  • app_data_hex — the appData hex value, with or without 0x.
§Returns

A base16 CIDv1 string (prefix f).

§Errors

Propagates CowError::AppData.

Source

pub fn cid_to_app_data_hex(&self, cid: &str) -> Result<String, CowError>

Extract the appData hex digest from a CIDv1 string.

Delegates to cid_to_appdata_hex.

§Parameters
  • cid — a base16 multibase CID string.
§Returns

A 0x-prefixed hex string of the 32-byte digest.

§Errors

Propagates CowError::AppData.

Source

pub async fn fetch_doc_from_cid( &self, cid: &str, ) -> Result<AppDataDoc, CowError>

Fetch an AppDataDoc from IPFS by CIDv1.

Uses the configured ipfs.read_uri or DEFAULT_IPFS_READ_URI when no custom gateway is set.

§Parameters
  • cid — the CIDv1 base16 string identifying the document on IPFS.
§Returns

The deserialised AppDataDoc.

§Errors

Propagates CowError::Http on network failure or CowError::Parse if the response is not valid JSON.

Source

pub async fn fetch_doc_from_app_data_hex( &self, app_data_hex: &str, ) -> Result<AppDataDoc, CowError>

Fetch an AppDataDoc from IPFS by appData hex value.

Converts app_data_hex to a CIDv1, then fetches the document from the configured IPFS gateway.

§Parameters
  • app_data_hex — the 0x-prefixed 32-byte hex value.
§Returns

The deserialised AppDataDoc.

§Errors

Propagates CowError::AppData, CowError::Http, or CowError::Parse.

Source

pub async fn upload_app_data( &self, doc: &AppDataDoc, ) -> Result<String, CowError>

Upload doc to IPFS via the Pinata pinning service.

The document is serialised to deterministic JSON, hashed, and pinned to Pinata. Requires Ipfs::pinata_api_key and Ipfs::pinata_api_secret to be set on the configured Ipfs instance.

§Parameters
§Returns

The IPFS CIDv1 hash string of the pinned content.

§Errors

Returns CowError::AppData when credentials are missing, CowError::Http on transport failure, or CowError::Api on a non-2xx Pinata response.

Source

pub fn get_app_data_info_legacy( &self, doc: &AppDataDoc, ) -> Result<AppDataInfo, CowError>

👎Deprecated:

Use get_app_data_info instead

Derive AppDataInfo using the legacy CID encoding method.

§Errors

Propagates CowError::AppData.

Source

pub async fn fetch_doc_from_app_data_hex_legacy( &self, app_data_hex: &str, ) -> Result<AppDataDoc, CowError>

👎Deprecated:

Use fetch_doc_from_app_data_hex instead

Fetch an AppDataDoc from IPFS using the legacy CID derivation.

§Errors

Propagates CowError::AppData, CowError::Http, or CowError::Parse.

Source

pub async fn upload_metadata_doc_to_ipfs_legacy( &self, doc: &AppDataDoc, ) -> Result<IpfsUploadResult, CowError>

👎Deprecated:

Use upload_app_data instead

Upload doc to IPFS via Pinata using the legacy method.

§Errors

Returns CowError::AppData, CowError::Http, or CowError::Api.

Source

pub fn get_app_data_schema(&self, version: &str) -> Result<AppDataDoc, CowError>

Get the app-data schema for a given version.

Delegates to get_app_data_schema. Currently known versions: "0.7.0", "1.3.0".

§Parameters
  • version — semver string (e.g. "1.3.0").
§Returns

An AppDataDoc placeholder with the requested version.

§Errors

Returns CowError::AppData when the version doesn’t exist.

Source

pub fn import_schema(&self, version: &str) -> Result<AppDataDoc, CowError>

Import a schema by version string.

Delegates to import_schema.

§Parameters
  • version — semver string.
§Returns

An AppDataDoc placeholder with the requested version.

§Errors

Returns CowError::AppData if the version is invalid or unknown.

Source

pub fn app_data_hex_to_cid_legacy( &self, app_data_hex: &str, ) -> Result<String, CowError>

👎Deprecated:

Use app_data_hex_to_cid instead

Convert app_data_hex to a CID using the legacy method.

§Errors

Propagates CowError::AppData.

Source

pub fn parse_cid(&self, ipfs_hash: &str) -> Result<CidComponents, CowError>

Parse a CID string into its constituent CidComponents.

Delegates to parse_cid. Only base16 CIDs (prefix f or F) are supported.

§Parameters
  • ipfs_hash — a multibase-encoded CID string.
§Returns

A CidComponents with version, codec, hash function, hash length, and raw digest.

§Errors

Propagates CowError::AppData.

Source

pub fn decode_cid(&self, bytes: &[u8]) -> Result<CidComponents, CowError>

Decode raw CID bytes into their constituent CidComponents.

Delegates to decode_cid.

§Parameters
  • bytes — raw CID bytes ([version, codec, hash_fn, hash_len, ...digest]).
§Returns

A CidComponents with the parsed fields.

§Errors

Propagates CowError::AppData if the slice is too short.

Source

pub fn extract_digest(&self, cid: &str) -> Result<String, CowError>

Extract the multihash digest from a CID string as 0x-prefixed hex.

Delegates to extract_digest.

§Parameters
  • cid — a base16 multibase CID string.
§Returns

A 0x-prefixed hex string of the raw digest bytes.

§Errors

Propagates CowError::AppData.

Trait Implementations§

Source§

impl Clone for MetadataApi

Source§

fn clone(&self) -> MetadataApi

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MetadataApi

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for MetadataApi

Source§

fn default() -> MetadataApi

Returns the “default value” for a type. Read more
Source§

impl Display for MetadataApi

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T> ToStringFallible for T
where T: Display,

Source§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more