diff --git a/wasm/Cargo.toml b/wasm/Cargo.toml index cb1c810..3155a85 100644 --- a/wasm/Cargo.toml +++ b/wasm/Cargo.toml @@ -28,6 +28,7 @@ package = "cesride" [dev-dependencies] wasm-bindgen-test = "0.3.39" +hex-literal = "~0.4" # wasm-opt is used instead #[profile.release] diff --git a/wasm/src/primitives/cigar.rs b/wasm/src/primitives/cigar.rs index 115af9b..97bbce1 100644 --- a/wasm/src/primitives/cigar.rs +++ b/wasm/src/primitives/cigar.rs @@ -1,10 +1,12 @@ use std::ops::Deref; +use wasm_bindgen::prelude::*; + use crate::{error::*, VerferWrapper, Wrap}; use cesride_core::{Cigar, Matter}; -use wasm_bindgen::prelude::*; #[wasm_bindgen(js_name = Cigar)] +#[derive(Debug)] pub struct CigarWrapper(pub(crate) Cigar); #[wasm_bindgen(js_class = Cigar)] @@ -30,30 +32,6 @@ impl CigarWrapper { Ok(CigarWrapper(cigar)) } - pub fn new_with_raw( - raw: &[u8], - verfer: Option, - code: Option, - ) -> Result { - let cigar = Cigar::new_with_raw(raw, verfer.as_deref(), code.as_deref()).as_js()?; - Ok(CigarWrapper(cigar)) - } - - pub fn new_with_qb64b(qb64b: &[u8], verfer: Option) -> Result { - let cigar = Cigar::new_with_qb64b(qb64b, verfer.as_deref()).as_js()?; - Ok(CigarWrapper(cigar)) - } - - pub fn new_with_qb64(qb64: &str, verfer: Option) -> Result { - let cigar = Cigar::new_with_qb64(qb64, verfer.as_deref()).as_js()?; - Ok(CigarWrapper(cigar)) - } - - pub fn new_with_qb2(qb2: &[u8], verfer: Option) -> Result { - let cigar = Cigar::new_with_qb2(qb2, verfer.as_deref()).as_js()?; - Ok(CigarWrapper(cigar)) - } - pub fn verfer(&self) -> VerferWrapper { VerferWrapper(self.0.verfer()) } diff --git a/wasm/src/primitives/verfer.rs b/wasm/src/primitives/verfer.rs index 76f3600..65af8e2 100644 --- a/wasm/src/primitives/verfer.rs +++ b/wasm/src/primitives/verfer.rs @@ -1,10 +1,12 @@ -use crate::{error::*, Wrap}; -use cesride_core::{Matter, Verfer}; use std::ops::Deref; + use wasm_bindgen::prelude::*; +use crate::{error::*, Wrap}; +use cesride_core::{Matter, Verfer}; + #[wasm_bindgen(js_name = Verfer)] -#[derive(Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct VerferWrapper(pub(crate) Verfer); #[wasm_bindgen(js_class = Verfer)] @@ -28,26 +30,6 @@ impl VerferWrapper { Ok(VerferWrapper(verfer)) } - pub fn new_with_raw(raw: &[u8], code: Option) -> Result { - let verfer = Verfer::new_with_raw(raw, code.as_deref()).as_js()?; - Ok(VerferWrapper(verfer)) - } - - pub fn new_with_qb64b(qb64b: &[u8]) -> Result { - let verfer = Verfer::new_with_qb64b(qb64b).as_js()?; - Ok(VerferWrapper(verfer)) - } - - pub fn new_with_qb64(qb64: &str) -> Result { - let verfer = Verfer::new_with_qb64(qb64).as_js()?; - Ok(VerferWrapper(verfer)) - } - - pub fn new_with_qb2(qb2: &[u8]) -> Result { - let verfer = Verfer::new_with_qb2(qb2).as_js()?; - Ok(VerferWrapper(verfer)) - } - pub fn verify(&self, sig: &[u8], ser: &[u8]) -> Result { self.0.verify(sig, ser).as_js().map_err(JsValue::from) } @@ -77,6 +59,13 @@ impl VerferWrapper { } } +/// A default Verfer for our VerferWrapper +impl Default for VerferWrapper { + fn default() -> Self { + VerferWrapper{ 0: Verfer::default() } + } +} + impl Deref for VerferWrapper { type Target = Verfer; diff --git a/wasm/tests/test_wasm.rs b/wasm/tests/test_wasm.rs index d285cdf..b3d656c 100644 --- a/wasm/tests/test_wasm.rs +++ b/wasm/tests/test_wasm.rs @@ -1,8 +1,13 @@ +use hex_literal::hex; use wasm_bindgen_test::*; +use cesride_core::matter::Codex; + use cesride_wasm::BexterWrapper; -use cesride_wasm::DaterWrapper; use cesride_wasm::CesrideMatterCodex; +use cesride_wasm::CigarWrapper; +use cesride_wasm::DaterWrapper; +use cesride_wasm::VerferWrapper; /* These dater tests are transcriptions from the first two test_dater tests in @@ -52,3 +57,34 @@ fn test_bexter_bext_string_simple_arg() { assert_eq!(first_bexter.qb64b(), second_bexter.qb64b()); assert_eq!(first_bexter.qb2(), second_bexter.qb2()); } + +#[wasm_bindgen_test] +fn test_cigar_convenience() { + let verf_default = VerferWrapper::default(); + let code = Codex::Ed25519_Sig; + let raw = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ[]"; + + let first_cigar = CigarWrapper::new(Some(verf_default.clone()), Some(code.to_string()), Some(raw.to_vec()), None, None, None).unwrap(); + let second_cigar = CigarWrapper::new(Some(verf_default), Some(code.to_string()), Some(raw.to_vec()), None, None, None).unwrap(); + assert_eq!(first_cigar.verfer(), second_cigar.verfer()); + assert_eq!(first_cigar.code(), second_cigar.code()); + assert_eq!(first_cigar.size(), second_cigar.size()); + assert_eq!(first_cigar.raw(), second_cigar.raw()); + assert_eq!(first_cigar.qb64(), second_cigar.qb64()); + assert_eq!(first_cigar.qb64b(), second_cigar.qb64b()); + assert_eq!(first_cigar.qb2(), second_cigar.qb2()); +} + +#[wasm_bindgen_test] +fn test_verfer_convenience() { + let raw = &hex!("0123456789abcdef00001111222233334444555566667777888899990000aaaa"); + let code = Codex::Ed25519N; + let verf_wrapper = VerferWrapper::new(Some(code.to_string()), Some(raw.to_vec()), None, None, None).unwrap(); + let verf_wrapper_2 = verf_wrapper.clone(); + assert_eq!(verf_wrapper.code(), verf_wrapper_2.code()); + assert_eq!(verf_wrapper.size(), verf_wrapper_2.size()); + assert_eq!(verf_wrapper.raw(), verf_wrapper_2.raw()); + assert_eq!(verf_wrapper.qb64(), verf_wrapper_2.qb64()); + assert_eq!(verf_wrapper.qb64b(), verf_wrapper_2.qb64b()); + assert_eq!(verf_wrapper.qb2(), verf_wrapper_2.qb2()); +}