use std::str::FromStr;
use minicbor::Decode;
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
use crate::{
signing::{PrivateKey, PublicKey},
tbs_cert::TbsCert,
};
#[wasm_bindgen]
#[allow(clippy::needless_pass_by_value)]
pub fn generate(tbs_cert: JsValue, private_key: Option<PrivateKey>) -> Result<JsValue, JsValue> {
let tbs_cert: TbsCert = serde_wasm_bindgen::from_value(tbs_cert)?;
let c509 = crate::generate(&tbs_cert, private_key.as_ref())
.map_err(|e| JsValue::from(e.to_string()))?;
Ok(serde_wasm_bindgen::to_value(&c509)?)
}
#[wasm_bindgen]
pub fn verify(c509: &[u8], public_key: &PublicKey) -> Result<JsValue, JsValue> {
match crate::verify(c509, public_key) {
Ok(()) => Ok(JsValue::from("Signature verified")),
Err(e) => Err(JsValue::from(e.to_string())),
}
}
#[wasm_bindgen]
pub fn decode(c509: &[u8]) -> Result<JsValue, JsValue> {
let mut d = minicbor::Decoder::new(c509);
let c509 = crate::C509::decode(&mut d, &mut ()).map_err(|e| JsValue::from(e.to_string()))?;
Ok(serde_wasm_bindgen::to_value(&c509)?)
}
#[wasm_bindgen]
impl PrivateKey {
#[wasm_bindgen]
pub fn str_to_sk(str: &str) -> Result<PrivateKey, JsValue> {
FromStr::from_str(str).map_err(|_| {
JsValue::from("Cannot decode private key from string. Invalid PEM format.")
})
}
}
#[wasm_bindgen]
impl PublicKey {
#[wasm_bindgen]
pub fn str_to_pk(str: &str) -> Result<PublicKey, JsValue> {
FromStr::from_str(str)
.map_err(|_| JsValue::from("Cannot decode public key from string. Invalid PEM format."))
}
}