agent-pay 0.1.0

L402 + DID-signed invoices: agent-to-agent Lightning payments (Rust port of @p-vbordei/agent-pay)
Documentation
//! RFC 8785 JSON Canonicalization Scheme + SHA-256 hash.
//!
//! NOTE: serde_jcs preserves u64 integers; the TS reference uses ECMAScript JSON
//! semantics (all numbers are f64). agent-pay's envelope payloads use `price_msat`
//! as a STRING and other large numbers are strings too, so we do not need the
//! `normalize_numbers` walker here.

use serde_json::Value;
use sha2::{Digest, Sha256};

use crate::error::Error;

pub fn canonical_json(value: &Value) -> Result<Vec<u8>, Error> {
    let s = serde_jcs::to_string(value).map_err(|e| Error::Other(format!("jcs: {e}")))?;
    Ok(s.into_bytes())
}

pub fn jcs_hash(value: &Value) -> Result<[u8; 32], Error> {
    let canonical = canonical_json(value)?;
    let mut hasher = Sha256::new();
    hasher.update(&canonical);
    Ok(hasher.finalize().into())
}