Skip to main content

vcp_wasm/
lib.rs

1//! # vcp-wasm
2//!
3//! WebAssembly bindings for the VCP SDK, exposing core parsing
4//! and encoding functions to JavaScript/TypeScript via `wasm-bindgen`.
5//!
6//! ## Usage from JS
7//!
8//! ```js
9//! import init, { parse_csm1, encode_csm1, parse_context_wire, validate_token } from 'vcp-wasm';
10//!
11//! await init();
12//!
13//! const code = parse_csm1("N5+F+E");
14//! console.log(code.persona); // "Nanny"
15//!
16//! const wire = parse_context_wire("⏰🌅|📍🏡‖🧠focused:4|💭calm:3");
17//! console.log(wire.personal.cognitive.value); // "focused"
18//! ```
19
20use wasm_bindgen::prelude::*;
21
22use vcp_core::context::FullContext;
23use vcp_core::csm1::{Csm1Code, Csm1Token};
24use vcp_core::identity::VcpToken;
25use vcp_core::transport;
26
27/// Parse a CSM-1 compact code (e.g. `"N5+F+E"`) and return it as a JS object.
28#[wasm_bindgen]
29pub fn parse_csm1(code: &str) -> Result<JsValue, JsValue> {
30    let parsed = Csm1Code::parse(code).map_err(|e| JsValue::from_str(&e.to_string()))?;
31    serde_wasm_bindgen::to_value(&parsed).map_err(|e| JsValue::from_str(&e.to_string()))
32}
33
34/// Encode a CSM-1 compact code from a JS object back to a string.
35///
36/// Accepts the same shape returned by `parse_csm1`.
37#[wasm_bindgen]
38pub fn encode_csm1(obj: JsValue) -> Result<String, JsValue> {
39    let code: Csm1Code =
40        serde_wasm_bindgen::from_value(obj).map_err(|e| JsValue::from_str(&e.to_string()))?;
41    Ok(code.encode())
42}
43
44/// Parse a CSM-1 8-line token string and return it as a JS object.
45#[wasm_bindgen]
46pub fn parse_csm1_token(token: &str) -> Result<JsValue, JsValue> {
47    let parsed = Csm1Token::parse(token).map_err(|e| JsValue::from_str(&e.to_string()))?;
48    serde_wasm_bindgen::to_value(&parsed).map_err(|e| JsValue::from_str(&e.to_string()))
49}
50
51/// Encode a CSM-1 8-line token from a JS object back to a string.
52#[wasm_bindgen]
53pub fn encode_csm1_token(obj: JsValue) -> Result<String, JsValue> {
54    let token: Csm1Token =
55        serde_wasm_bindgen::from_value(obj).map_err(|e| JsValue::from_str(&e.to_string()))?;
56    Ok(token.encode())
57}
58
59/// Parse the full context wire format (situational + personal, separated by `‖`).
60///
61/// Returns a JS object with `situational` and `personal` fields.
62#[wasm_bindgen]
63pub fn parse_context_wire(wire: &str) -> Result<JsValue, JsValue> {
64    let parsed = FullContext::from_wire(wire).map_err(|e| JsValue::from_str(&e.to_string()))?;
65    serde_wasm_bindgen::to_value(&parsed).map_err(|e| JsValue::from_str(&e.to_string()))
66}
67
68/// Encode a full context object to wire format.
69#[wasm_bindgen]
70pub fn encode_context_wire(obj: JsValue) -> Result<String, JsValue> {
71    let ctx: FullContext =
72        serde_wasm_bindgen::from_value(obj).map_err(|e| JsValue::from_str(&e.to_string()))?;
73    Ok(ctx.to_wire())
74}
75
76/// Validate a VCP/I identity token (e.g. `"family.safe.guide@1.2.0"`).
77///
78/// Returns the parsed token as a JS object on success.
79#[wasm_bindgen]
80pub fn validate_token(token: &str) -> Result<JsValue, JsValue> {
81    let parsed = VcpToken::parse(token).map_err(|e| JsValue::from_str(&e.to_string()))?;
82    serde_wasm_bindgen::to_value(&parsed).map_err(|e| JsValue::from_str(&e.to_string()))
83}
84
85/// Compute the SHA-256 content hash of constitution text.
86///
87/// Returns a string in the format `"sha256:<hex>"`.
88#[wasm_bindgen]
89pub fn hash_content(content: &str) -> Result<String, JsValue> {
90    transport::compute_content_hash(content).map_err(|e| JsValue::from_str(&e.to_string()))
91}
92
93/// Verify that content matches an expected hash.
94///
95/// Returns `true` if the hash matches.
96#[wasm_bindgen]
97pub fn verify_hash(content: &str, expected_hash: &str) -> Result<bool, JsValue> {
98    transport::verify_content_hash(content, expected_hash)
99        .map_err(|e| JsValue::from_str(&e.to_string()))
100}
101
102/// Verify a bundle (manifest JSON + content).
103///
104/// Returns a JS object with `code` and `message` fields.
105#[wasm_bindgen]
106pub fn verify_bundle(manifest_json: &str, content: &str) -> Result<JsValue, JsValue> {
107    let result = transport::verify_bundle(manifest_json, content)
108        .map_err(|e| JsValue::from_str(&e.to_string()))?;
109    serde_wasm_bindgen::to_value(&result).map_err(|e| JsValue::from_str(&e.to_string()))
110}