Skip to content

Commit

Permalink
Removed *_with_* constructors from cigar and verfer
Browse files Browse the repository at this point in the history
  • Loading branch information
daidoji committed Jan 5, 2024
1 parent 0d54caa commit 05dc093
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 49 deletions.
1 change: 1 addition & 0 deletions wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
28 changes: 3 additions & 25 deletions wasm/src/primitives/cigar.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -30,30 +32,6 @@ impl CigarWrapper {
Ok(CigarWrapper(cigar))
}

pub fn new_with_raw(
raw: &[u8],
verfer: Option<VerferWrapper>,
code: Option<String>,
) -> Result<CigarWrapper> {
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<VerferWrapper>) -> Result<CigarWrapper> {
let cigar = Cigar::new_with_qb64b(qb64b, verfer.as_deref()).as_js()?;
Ok(CigarWrapper(cigar))
}

pub fn new_with_qb64(qb64: &str, verfer: Option<VerferWrapper>) -> Result<CigarWrapper> {
let cigar = Cigar::new_with_qb64(qb64, verfer.as_deref()).as_js()?;
Ok(CigarWrapper(cigar))
}

pub fn new_with_qb2(qb2: &[u8], verfer: Option<VerferWrapper>) -> Result<CigarWrapper> {
let cigar = Cigar::new_with_qb2(qb2, verfer.as_deref()).as_js()?;
Ok(CigarWrapper(cigar))
}

pub fn verfer(&self) -> VerferWrapper {
VerferWrapper(self.0.verfer())
}
Expand Down
35 changes: 12 additions & 23 deletions wasm/src/primitives/verfer.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -28,26 +30,6 @@ impl VerferWrapper {
Ok(VerferWrapper(verfer))
}

pub fn new_with_raw(raw: &[u8], code: Option<String>) -> Result<VerferWrapper> {
let verfer = Verfer::new_with_raw(raw, code.as_deref()).as_js()?;
Ok(VerferWrapper(verfer))
}

pub fn new_with_qb64b(qb64b: &[u8]) -> Result<VerferWrapper> {
let verfer = Verfer::new_with_qb64b(qb64b).as_js()?;
Ok(VerferWrapper(verfer))
}

pub fn new_with_qb64(qb64: &str) -> Result<VerferWrapper> {
let verfer = Verfer::new_with_qb64(qb64).as_js()?;
Ok(VerferWrapper(verfer))
}

pub fn new_with_qb2(qb2: &[u8]) -> Result<VerferWrapper> {
let verfer = Verfer::new_with_qb2(qb2).as_js()?;
Ok(VerferWrapper(verfer))
}

pub fn verify(&self, sig: &[u8], ser: &[u8]) -> Result<bool> {
self.0.verify(sig, ser).as_js().map_err(JsValue::from)
}
Expand Down Expand Up @@ -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;

Expand Down
38 changes: 37 additions & 1 deletion wasm/tests/test_wasm.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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());
}

0 comments on commit 05dc093

Please sign in to comment.