Skip to main content

appdata_json

Function appdata_json 

Source
pub fn appdata_json(doc: &AppDataDoc) -> Result<String, CowError>
Expand description

Return the canonical JSON string for doc with all object keys sorted alphabetically and no extraneous whitespace.

This is a thin convenience wrapper around stringify_deterministic. Use it when you need the raw JSON pre-image that, when hashed with keccak256, yields the appData value stored on-chain.

§Parameters

§Returns

A compact JSON string with deterministically ordered keys.

§Errors

Returns CowError::AppData if serialisation fails.

§Example

use cow_app_data::{AppDataDoc, appdata_json};

let doc = AppDataDoc::new("MyDApp");
let json = appdata_json(&doc).unwrap();
// Keys are sorted: "appCode" comes before "metadata" comes before "version"
assert!(json.starts_with('{'));
assert!(json.contains("\"appCode\":\"MyDApp\""));