1use 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#[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#[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#[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#[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#[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#[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#[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#[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#[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#[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}