agent-ask 0.1.0

Federated public Q&A protocol for AI agents — signed Q/A/Rating, content-addressed, pull federation (Rust port of @p-vbordei/agent-ask)
Documentation
//! RFC 8785 JCS canonical encoding + CIDv1 raw+sha256.
//!
//! Mirrors `src/canonical.ts` in the TS reference.

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

use crate::error::Error;

const CID_VERSION: u8 = 0x01;
const CODEC_RAW: u8 = 0x55;
const MULTIHASH_SHA256: u8 = 0x12;
const DIGEST_LEN: u8 = 0x20;

pub fn jcs(value: &Value) -> Result<Vec<u8>, Error> {
    serde_jcs::to_string(value)
        .map(String::into_bytes)
        .map_err(|e| Error::Invalid(format!("jcs: {e}")))
}

/// JCS over the artifact with the top-level `sig` field removed.
pub fn artifact_bytes_for_sig(artifact: &Value) -> Result<Vec<u8>, Error> {
    let mut clone = artifact.clone();
    if let Some(obj) = clone.as_object_mut() {
        obj.remove("sig");
    }
    jcs(&clone)
}

pub fn compute_cid(data: &[u8]) -> String {
    let mut hasher = Sha256::new();
    hasher.update(data);
    let digest: [u8; 32] = hasher.finalize().into();
    let mut buf = Vec::with_capacity(4 + 32);
    buf.push(CID_VERSION);
    buf.push(CODEC_RAW);
    buf.push(MULTIHASH_SHA256);
    buf.push(DIGEST_LEN);
    buf.extend_from_slice(&digest);
    let b32 = BASE32_NOPAD.encode(&buf).to_lowercase();
    format!("b{b32}")
}