This commit is contained in:
Jon Staab
2026-04-21 06:18:49 -07:00
parent b96ad44d3a
commit c8f6bc1652
5 changed files with 888 additions and 30 deletions
+29 -30
View File
@@ -8,7 +8,7 @@
use wasm_bindgen::prelude::*;
use coracle_lib::encryption as ce;
use coracle_lib::event as cev;
use coracle_lib::events as cev;
use coracle_lib::keys as ck;
fn js_err<E: std::fmt::Display>(e: E) -> JsError {
@@ -181,16 +181,24 @@ impl Event {
created_at: u64,
sk: &SecretKey,
) -> Result<Event, JsError> {
let tags: Vec<Vec<String>> =
use coracle_lib::tags::{Tag, Tags};
let raw_tags: Vec<Vec<String>> =
serde_wasm_bindgen::from_value(tags).map_err(|e| JsError::new(&e.to_string()))?;
// coracle-lib's SecretKey hides the inner secp type behind a
// crate-private accessor, so round-trip through hex to hand
// Event::new the low-level key it wants.
let bytes = hex::decode(sk.0.to_hex()).map_err(js_err)?;
let secp_sk = secp256k1::SecretKey::from_slice(&bytes).map_err(js_err)?;
Ok(Event(cev::Event::new(
kind, content, tags, created_at, &secp_sk,
)))
let tags = Tags::from(
raw_tags
.into_iter()
.map(|v| Tag::from(v))
.collect::<Vec<_>>(),
);
let hashed = cev::EventContent::new(content, tags)
.kind(kind)
.stamp(created_at)
.own(sk.0.public_key())
.hash();
let sig = sk.0.sign(&hashed.id);
Ok(Event(hashed.sign(sig)))
}
/// Parse an event from its JSON representation.
@@ -209,12 +217,12 @@ impl Event {
#[wasm_bindgen(getter)]
pub fn id(&self) -> String {
self.0.id.clone()
hex::encode(self.0.id)
}
#[wasm_bindgen(getter)]
pub fn pubkey(&self) -> String {
self.0.pubkey.clone()
self.0.pubkey.to_hex()
}
#[wasm_bindgen(getter, js_name = createdAt)]
@@ -234,7 +242,7 @@ impl Event {
#[wasm_bindgen(getter)]
pub fn sig(&self) -> String {
self.0.sig.clone()
hex::encode(self.0.sig)
}
#[wasm_bindgen(getter)]
@@ -242,24 +250,15 @@ impl Event {
serde_wasm_bindgen::to_value(&self.0.tags).map_err(|e| JsError::new(&e.to_string()))
}
/// Canonical `[0, pubkey, created_at, kind, tags, content]` serialization.
#[wasm_bindgen]
pub fn serialize(&self) -> String {
self.0.serialize()
}
#[wasm_bindgen(js_name = computeId)]
pub fn compute_id(&self) -> String {
self.0.compute_id()
}
#[wasm_bindgen(js_name = idIsValid)]
pub fn id_is_valid(&self) -> bool {
self.0.id_is_valid()
}
/// Verify both the id and the Schnorr signature.
#[wasm_bindgen]
pub fn verify(&self) -> bool {
self.0.verify()
self.0.verify().is_ok()
}
/// Check whether the recomputed id matches the stored id.
#[wasm_bindgen(js_name = verifyId)]
pub fn verify_id(&self) -> bool {
self.0.verify_id()
}
}