From 390f004e3e91f1257da4bbcd6bc2d72f67b62c0f Mon Sep 17 00:00:00 2001 From: blu3beri Date: Thu, 16 Mar 2023 10:17:23 +0100 Subject: [PATCH] fix(wasm): compilable and updated some bindings Signed-off-by: blu3beri --- .gitignore | 1 + Cargo.lock | 111 +++++++++++++++ libursa/.gitignore | 1 - libursa/Cargo.lock | 3 + libursa/Cargo.toml | 3 +- libursa/src/lib.rs | 12 +- libursa/src/signatures/secp256k1.rs | 89 ++++++++---- libursa/src/wasm/bls.rs | 35 ++++- libursa/src/wasm/cl.rs | 205 ++++++++++++++++------------ libursa/src/wasm/ed25519.rs | 72 ++++++---- libursa/src/wasm/encryption.rs | 1 + libursa/src/wasm/mod.rs | 26 ++-- libursa/src/wasm/secp256k1.rs | 35 +++-- libursa/src/wasm/x25519.rs | 11 +- 14 files changed, 419 insertions(+), 186 deletions(-) delete mode 100644 libursa/.gitignore diff --git a/.gitignore b/.gitignore index df6086d3..2b6a254b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ cargo-registry target +pkg build .idea *.iml diff --git a/Cargo.lock b/Cargo.lock index 55052f6c..9153e8a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "anyhow" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224afbd727c3d6e4b90103ece64b8d1b67fbb1973b1046c2281eed3f3803f800" + [[package]] name = "autocfg" version = "1.0.1" @@ -76,6 +82,15 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2d9162b7289a46e86208d6af2c686ca5bfde445878c41a458a9fac706252d0b" +[[package]] +name = "core2" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b49ba7ef1ad6107f8824dbe97de947cbaac53c44e7f9756a1fba0d37c1eec505" +dependencies = [ + "memchr", +] + [[package]] name = "curve25519-dalek" version = "3.2.0" @@ -233,6 +248,18 @@ dependencies = [ "subtle", ] +[[package]] +name = "half" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7" + +[[package]] +name = "itoa" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" + [[package]] name = "k256" version = "0.5.10" @@ -249,6 +276,12 @@ version = "0.2.112" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125" +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + [[package]] name = "num-bigint" version = "0.2.6" @@ -465,6 +498,48 @@ dependencies = [ "rand_core 0.3.1", ] +[[package]] +name = "ryu" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" + +[[package]] +name = "serde" +version = "1.0.156" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "314b5b092c0ade17c00142951e50ced110ec27cea304b1037c6969246c2469a4" + +[[package]] +name = "serde_bare" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51c55386eed0f1ae957b091dc2ca8122f287b60c79c774cbe3d5f2b69fded660" +dependencies = [ + "serde", +] + +[[package]] +name = "serde_cbor" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bef2ebfde456fb76bbcf9f59315333decc4fda0b2b44b420243c11e0f5ec1f5" +dependencies = [ + "half", + "serde", +] + +[[package]] +name = "serde_json" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c533a59c9d8a93a09c6ab31f0fd5e5f4dd1b8fc9434804029839884765d04ea" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "sha2" version = "0.8.2" @@ -477,6 +552,12 @@ dependencies = [ "opaque-debug", ] +[[package]] +name = "string-error" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439558b73299d7afdf52c36399c3c812cca10fe5ca08429f38815df5bacf3ef7" + [[package]] name = "subtle" version = "2.4.1" @@ -506,6 +587,26 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "thiserror" +version = "1.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5ab016db510546d856297882807df8da66a16fb8c4101cb8b30054b0d5b2d9c" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5420d42e90af0c38c3290abcca25b9b3bdf379fc9f55c528f53a269d9c9a267e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "typenum" version = "1.15.0" @@ -532,6 +633,16 @@ version = "0.5.0" [[package]] name = "ursa_core" version = "0.5.0" +dependencies = [ + "anyhow", + "core2", + "serde", + "serde_bare", + "serde_cbor", + "serde_json", + "string-error", + "thiserror", +] [[package]] name = "ursa_encryption" diff --git a/libursa/.gitignore b/libursa/.gitignore deleted file mode 100644 index cace5e84..00000000 --- a/libursa/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/pkg/ diff --git a/libursa/Cargo.lock b/libursa/Cargo.lock index 51ac7871..1543df95 100644 --- a/libursa/Cargo.lock +++ b/libursa/Cargo.lock @@ -725,8 +725,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" dependencies = [ "cfg-if", + "js-sys", "libc", "wasi 0.11.0+wasi-snapshot-preview1", + "wasm-bindgen", ] [[package]] @@ -1638,6 +1640,7 @@ dependencies = [ "env_logger", "failure", "ffi-support", + "getrandom 0.2.7", "glass_pumpkin", "hex", "hkdf", diff --git a/libursa/Cargo.toml b/libursa/Cargo.toml index b35b5f93..38aaccb7 100644 --- a/libursa/Cargo.toml +++ b/libursa/Cargo.toml @@ -102,7 +102,7 @@ sharing_native = ["failure", "int_traits", "lazy_static", "log", "openssl", "ran signatures = ["cl", "ed25519", "ecdsa_secp256k1", "bls_bls12381", "bls_bn254"] signatures_native = ["cl_native", "ed25519", "ecdsa_secp256k1_native", "bls_bls12381", "bls_bn254"] signatures_asm = ["cl_native", "ed25519_asm", "ecdsa_secp256k1_asm", "bls_bls12381", "bls_bn254_asm"] -wasm = ["console_error_panic_hook", "failure", "hex", "js-sys", "log", "rand/wasm-bindgen", "serde", "serde_json", "wasm-bindgen", "zeroize"] +wasm = ["console_error_panic_hook", "failure", "hex", "js-sys", "log", "rand/wasm-bindgen", "serde", "serde_json", "wasm-bindgen", "zeroize", "getrandom"] x25519 = ["arrayref", "curve25519-dalek/std", "curve25519-dalek/u64_backend", "hex", "rand", "rand_chacha", "sha2/std", "x25519-dalek/std", "x25519-dalek/u64_backend", "zeroize"] x25519_asm = ["arrayref", "curve25519-dalek/nightly", "curve25519-dalek/avx2_backend", "hex", "rand", "rand_chacha", "sha2/asm", "x25519-dalek/nightly", "x25519-dalek/u64_backend", "zeroize"] @@ -137,6 +137,7 @@ num-traits = { version = "=0.2.11", optional = true } openssl = { version = "0.10", optional = true } # TODO: Find out if the wasm-bindgen feature can be made dependent on our own wasm feature rand = { version = "0.7", features = ["wasm-bindgen"], optional = true } +getrandom = { version = "0.2", features = ["js"], optional = true } rand_chacha = { version = "=0.2.1", optional = true } rustchacha20poly1305 = { version = "0.7", package = "chacha20poly1305", optional = true } k256 = { version = "0.9.6", optional = true, features = ["ecdh", "ecdsa", "sha256"]} diff --git a/libursa/src/lib.rs b/libursa/src/lib.rs index 4e1bcf02..0af9c467 100644 --- a/libursa/src/lib.rs +++ b/libursa/src/lib.rs @@ -129,12 +129,14 @@ pub mod utils; #[cfg(any(feature = "bls_bn254", feature = "bls_bn254_asm"))] pub mod bls; -#[cfg(any(feature = "cl_native", feature = "sharing_native"))] -#[path = "bn/openssl.rs"] -pub mod bn; -#[cfg(any(feature = "cl", feature = "sharing"))] -#[path = "bn/rust.rs"] + +#[cfg_attr( + any(feature = "cl_native", feature = "sharing_native"), + path = "bn/openssl.rs" +)] +#[cfg_attr(any(feature = "cl", feature = "sharing"), path = "bn/rust.rs")] pub mod bn; + #[cfg(any(feature = "cl", feature = "cl_native"))] pub mod cl; #[cfg(any( diff --git a/libursa/src/signatures/secp256k1.rs b/libursa/src/signatures/secp256k1.rs index e68d5ec6..4fb870ac 100644 --- a/libursa/src/signatures/secp256k1.rs +++ b/libursa/src/signatures/secp256k1.rs @@ -1,9 +1,8 @@ use super::*; + use sha2::digest::generic_array::typenum::U32; use CryptoError; -use rand::rngs::OsRng; - #[cfg(feature = "serde")] use serde::{de::Visitor, Deserialize, Deserializer, Serialize, Serializer}; @@ -116,6 +115,7 @@ mod ecdsa_secp256k1 { use sha2::Digest; use zeroize::Zeroize; + use rand::rngs::OsRng; use rand::{RngCore, SeedableRng}; use rand_chacha::ChaChaRng; @@ -217,13 +217,12 @@ mod ecdsa_secp256k1 { #[cfg(feature = "ecdsa_secp256k1")] mod ecdsa_secp256k1 { use super::*; - use k256; - - use rand::{RngCore, SeedableRng}; - use rand_chacha::ChaChaRng; - use zeroize::Zeroize; - use amcl::secp256k1::{ecdh, ecp}; + use amcl_wrapper::amcl::secp256k1::ecp::ECP; + use k256::ecdsa::signature::{Signature, Signer, Verifier}; + use k256::elliptic_curve::rand_core::OsRng; + use k256::EncodedPoint; + use sha2::Digest; #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct EcdsaSecp256k1Impl; @@ -231,60 +230,92 @@ mod ecdsa_secp256k1 { impl EcdsaSecp256k1Impl { pub fn public_key_compressed(&self, pk: &PublicKey) -> Vec { let mut compressed = [0u8; PUBLIC_KEY_SIZE]; - ecp::ECP::frombytes(&pk[..]).tobytes(&mut compressed, true); + ECP::frombytes(&pk[..]).tobytes(&mut compressed, true); compressed.to_vec() } + pub fn public_key_uncompressed(&self, pk: &PublicKey) -> Vec { let mut uncompressed = [0u8; PUBLIC_UNCOMPRESSED_KEY_SIZE]; - ecp::ECP::frombytes(&pk[..]).tobytes(&mut uncompressed, false); + ECP::frombytes(&pk[..]).tobytes(&mut uncompressed, false); uncompressed.to_vec() } + pub fn parse(&self, data: &[u8]) -> Result { match data.len() { PUBLIC_KEY_SIZE => Ok(PublicKey(data.to_vec())), PUBLIC_UNCOMPRESSED_KEY_SIZE => { let mut compressed = [0u8; PUBLIC_KEY_SIZE]; - ecp::ECP::frombytes(data).tobytes(&mut compressed, true); + ECP::frombytes(data).tobytes(&mut compressed, true); Ok(PublicKey(compressed.to_vec())) } _ => Err(CryptoError::ParseError("Invalid key length".to_string())), } } + pub fn new() -> Self { Self {} } - pub fn keypair( + + pub fn keypair( &self, option: Option, - ) -> Result<(PublicKey, PrivateKey), CryptoError> { - let signing_key = k256::SigningKey::random(&mut OsRng); - let sk = signing_key::to_bytes(); - let verify_key = signing_key.verify_key(); - let compressed = verify_key::to_bytes(); //serialized as compressed point + ) -> Result<(PublicKey, PrivateKey), CryptoError> + where + D: Digest, + { + let signing_key = match option { + Some(ref o) => match o { + KeyGenOption::FromSecretKey(s) => k256::ecdsa::SigningKey::from_bytes(&s.0), + _ => unreachable!(), + }, + None => Ok(k256::ecdsa::SigningKey::random(&mut OsRng)), + } + .map_err(|e| CryptoError::KeyGenError(format!("{:?}", e)))?; + let sk = signing_key.to_bytes(); + let verify_key = signing_key.verifying_key(); + let compressed = verify_key.to_bytes(); //serialized as compressed point Ok((PublicKey(compressed.to_vec()), PrivateKey(sk.to_vec()))) } - pub fn sign(&self, message: &[u8], sk: &PrivateKey) -> Result, CryptoError> { - let signing_key = k256::SigningKey::new(&sk) + + pub fn sign(&self, message: &[u8], sk: &PrivateKey) -> Result, CryptoError> + where + D: Digest, + { + let signing_key = k256::ecdsa::SigningKey::from_bytes(&sk.0) .map_err(|e| CryptoError::SigningError(format!("{:?}", e)))?; - let (sig, _) = k256::signing_key.sign(&message); - Ok(sig.serialize().to_vec()) + let sig: k256::ecdsa::Signature = signing_key.sign(message); + Ok(k256::ecdsa::signature::Signature::as_bytes(&sig).to_vec()) } - pub fn verify( + + pub fn verify( &self, message: &[u8], signature: &[u8], pk: &PublicKey, - ) -> Result { + ) -> Result + where + D: Digest, + { + let digest = D::digest(message); + let message = digest.as_slice(); let compressed_pk = self.public_key_compressed(pk); - let pk = k256::VerifyKey::from_encoded_point(compressed_pk) + let encoded_point = EncodedPoint::from_bytes(compressed_pk) + .map_err(|e| CryptoError::SigningError(format!("{:?}", e)))?; + let pk = k256::ecdsa::VerifyingKey::from_encoded_point(&encoded_point) .map_err(|e| CryptoError::SigningError(format!("{:?}", e)))?; - let sig: Signature = k256::Signature::from_bytes(signature); - Ok(k256::verify(&msg, &sig, &pk)) + let sig = ::from_bytes( + signature, + ) + .map_err(|e| CryptoError::SigningError(format!("{:?}", e)))?; + Ok(pk.verify(&message, &sig).is_ok()) } + pub fn normalize_s(&self, signature: &mut [u8]) -> Result<(), CryptoError> { - let mut sig = k256::Signature::parse(array_ref!(signature, 0, SIGNATURE_SIZE)); - sig.normalize_s(); - array_copy!(sig.serialize(), signature, 0, SIGNATURE_SIZE); + let mut sig = k256::ecdsa::Signature::from_bytes(signature) + .map_err(|e| CryptoError::ParseError(format!("{:?}", e)))?; + sig.normalize_s() + .map_err(|e| CryptoError::ParseError(format!("{:?}", e)))?; + array_copy!(sig.as_bytes(), signature, 0, SIGNATURE_SIZE); Ok(()) } } diff --git a/libursa/src/wasm/bls.rs b/libursa/src/wasm/bls.rs index deadd913..42888c1c 100644 --- a/libursa/src/wasm/bls.rs +++ b/libursa/src/wasm/bls.rs @@ -8,14 +8,17 @@ pub struct Generator(bls::Generator); #[wasm_bindgen] impl Generator { + #[wasm_bindgen(constructor)] pub fn new() -> Result { Ok(Generator(maperr!(bls::Generator::new()))) } - pub fn as_bytes(&self) -> Vec { + #[wasm_bindgen(js_name = toBytes)] + pub fn to_bytes(&self) -> Vec { self.0.as_bytes().to_vec() } + #[wasm_bindgen(js_name = fromBytes)] pub fn from_bytes(bytes: &[u8]) -> Result { Ok(Generator(maperr!(bls::Generator::from_bytes(bytes)))) } @@ -26,18 +29,22 @@ pub struct SignKey(bls::SignKey); #[wasm_bindgen] impl SignKey { + #[wasm_bindgen(constructor)] pub fn new() -> Result { Ok(SignKey(maperr!(bls::SignKey::new(None)))) } + #[wasm_bindgen(js_name = fromSeed)] pub fn from_seed(seed: &[u8]) -> Result { Ok(SignKey(maperr!(bls::SignKey::new(Some(seed))))) } + #[wasm_bindgen(js_name = fromBytes)] pub fn from_bytes(bytes: &[u8]) -> Result { Ok(SignKey(maperr!(bls::SignKey::from_bytes(bytes)))) } + #[wasm_bindgen(js_name = toBytes)] pub fn to_bytes(&self) -> Result, JsValue> { Ok(self.0.as_bytes().to_vec()) } @@ -48,14 +55,17 @@ pub struct VerKey(bls::VerKey); #[wasm_bindgen] impl VerKey { + #[wasm_bindgen(constructor)] pub fn new(generator: Generator, sign_key: SignKey) -> Result { Ok(VerKey(maperr!(bls::VerKey::new(&generator.0, &sign_key.0)))) } + #[wasm_bindgen(js_name = fromBytes)] pub fn from_bytes(bytes: &[u8]) -> Result { Ok(VerKey(maperr!(bls::VerKey::from_bytes(bytes)))) } + #[wasm_bindgen(js_name = toBytes)] pub fn to_bytes(&self) -> Result, JsValue> { Ok(self.0.as_bytes().to_vec()) } @@ -66,6 +76,7 @@ pub struct ProofOfPossession(bls::ProofOfPossession); #[wasm_bindgen] impl ProofOfPossession { + #[wasm_bindgen(constructor)] pub fn new(ver_key: &VerKey, sign_key: &SignKey) -> Result { Ok(ProofOfPossession(maperr!(bls::ProofOfPossession::new( &ver_key.0, @@ -73,13 +84,15 @@ impl ProofOfPossession { )))) } + #[wasm_bindgen(js_name = fromBytes)] pub fn from_bytes(bytes: &[u8]) -> Result { Ok(ProofOfPossession(maperr!( bls::ProofOfPossession::from_bytes(bytes) ))) } - pub fn as_bytes(&self) -> Result, JsValue> { + #[wasm_bindgen(js_name = toBytes)] + pub fn to_bytes(&self) -> Result, JsValue> { Ok(self.0.as_bytes().to_vec()) } } @@ -89,10 +102,13 @@ pub struct Signature(bls::Signature); #[wasm_bindgen] impl Signature { + #[wasm_bindgen(js_name = fromBytes)] pub fn from_bytes(bytes: &[u8]) -> Result { Ok(Signature(maperr!(bls::Signature::from_bytes(bytes)))) } - pub fn as_bytes(&self) -> Result, JsValue> { + + #[wasm_bindgen(js_name = toBytes)] + pub fn to_bytes(&self) -> Result, JsValue> { Ok(self.0.as_bytes().to_vec()) } } @@ -102,6 +118,7 @@ pub struct MultiSignature(bls::MultiSignature); #[wasm_bindgen] impl MultiSignature { + #[wasm_bindgen(constructor)] pub fn new(signatures: Vec) -> Result { let sigs: Vec = signatures.iter().map(|x| x.into_serde().unwrap()).collect(); @@ -110,13 +127,15 @@ impl MultiSignature { )?)) } + #[wasm_bindgen(js_name = fromBytes)] pub fn from_bytes(bytes: &[u8]) -> Result { Ok(MultiSignature(maperr!(bls::MultiSignature::from_bytes( bytes )))) } - pub fn as_bytes(&self) -> Result, JsValue> { + #[wasm_bindgen(js_name = toBytes)] + pub fn to_bytes(&self) -> Result, JsValue> { Ok(self.0.as_bytes().to_vec()) } } @@ -125,8 +144,8 @@ impl MultiSignature { pub struct Bls(bls::Generator); #[wasm_bindgen] -#[allow(non_snake_case)] impl Bls { + #[wasm_bindgen(constructor)] pub fn new() -> Bls { Bls(bls::Generator::new().unwrap()) } @@ -151,7 +170,8 @@ impl Bls { ))) } - pub fn verifyProofOfPosession( + #[wasm_bindgen(js_name = verifyProofOfPosession)] + pub fn verify_proof_of_posession( pop: &ProofOfPossession, ver_key: &VerKey, generator: &Generator, @@ -163,7 +183,8 @@ impl Bls { ))) } - pub fn verifyMultiSignature( + #[wasm_bindgen(js_name = verifyMultiSignature)] + pub fn verify_multi_signature( multi_sig: &MultiSignature, message: &[u8], ver_keys: Vec, diff --git a/libursa/src/wasm/cl.rs b/libursa/src/wasm/cl.rs index 1fdef836..4b458f07 100644 --- a/libursa/src/wasm/cl.rs +++ b/libursa/src/wasm/cl.rs @@ -1,10 +1,12 @@ +use cl; use js_sys::Function; -use wasm_bindgen::prelude::*; - use std::collections::HashSet; -use cl; -use cl::RevocationTailsAccessor; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; +use wasm_bindgen::prelude::*; + +use crate::cl::RevocationTailsAccessor; use super::convert_from_js; @@ -13,10 +15,12 @@ pub struct CredentialSchema(cl::CredentialSchemaBuilder); #[wasm_bindgen] impl CredentialSchema { + #[wasm_bindgen(constructor)] pub fn new() -> CredentialSchema { CredentialSchema(cl::CredentialSchemaBuilder::new().unwrap()) } + #[wasm_bindgen(js_name = addAttr)] pub fn add_attr(&mut self, attribute: &str) { self.0.add_attr(attribute).unwrap(); } @@ -27,10 +31,12 @@ pub struct NonCredentialSchema(cl::NonCredentialSchemaBuilder); #[wasm_bindgen] impl NonCredentialSchema { + #[wasm_bindgen(constructor)] pub fn new() -> NonCredentialSchema { NonCredentialSchema(cl::NonCredentialSchemaBuilder::new().unwrap()) } + #[wasm_bindgen(js_name = addAttr)] pub fn add_attr(&mut self, attribute: &str) { self.0.add_attr(attribute).unwrap(); } @@ -39,38 +45,43 @@ impl NonCredentialSchema { #[wasm_bindgen] pub struct CredentialValues(cl::CredentialValuesBuilder); -//#[wasm_bindgen] -//impl CredentialValues { -// pub fn new() -> CredentialValues -// CredentialValues(cl::CredentialValuesBuilder::new().unwrap()) -// } -// -// pub fn add_master_secret(&mut self, value: &MasterSecret) -> Result<(), JsValue> { -// let ms = maperr!(value.0.value()); -// maperr!(self.0.add_value_hidden("master_secret", &ms)); -// Ok(()) -// } -// -// pub fn add_known(&mut self, attr: &str, value: &str) -> Result<(), JsValue> { -// maperr!(self.0.add_dec_known(attr, value)); -// Ok(()) -// } -// -// pub fn add_hidden(&mut self, attr: &str, value: &str) -> Result<(), JsValue> { -// maperr!(self.0.add_dec_hidden(attr, value)); -// Ok(()) -// } -// -// pub fn add_commitment( -// &mut self, -// attr: &str, -// value: &str, -// blinding_factor: &str, -// ) -> Result<(), JsValue> { -// maperr!(self.0.add_dec_commitment(attr, value, blinding_factor)); -// Ok(()) -// } -//} +#[wasm_bindgen] +impl CredentialValues { + #[wasm_bindgen(constructor)] + pub fn new() -> CredentialValues { + CredentialValues(cl::CredentialValuesBuilder::new().unwrap()) + } + + #[wasm_bindgen(js_name = addMasterSecret)] + pub fn add_master_secret(&mut self, value: &MasterSecret) -> Result<(), JsValue> { + let ms = maperr!(value.0.value()); + maperr!(self.0.add_value_hidden("master_secret", &ms)); + Ok(()) + } + + #[wasm_bindgen(js_name = addKnown)] + pub fn add_known(&mut self, attr: &str, value: &str) -> Result<(), JsValue> { + maperr!(self.0.add_dec_known(attr, value)); + Ok(()) + } + + #[wasm_bindgen(js_name = addHidden)] + pub fn add_hidden(&mut self, attr: &str, value: &str) -> Result<(), JsValue> { + maperr!(self.0.add_dec_hidden(attr, value)); + Ok(()) + } + + #[wasm_bindgen(js_name = addCommitment)] + pub fn add_commitment( + &mut self, + attr: &str, + value: &str, + blinding_factor: &str, + ) -> Result<(), JsValue> { + maperr!(self.0.add_dec_commitment(attr, value, blinding_factor)); + Ok(()) + } +} #[wasm_bindgen] pub struct CredentialPrimaryPublicKey(cl::CredentialPrimaryPublicKey); @@ -79,14 +90,16 @@ pub struct CredentialPrimaryPublicKey(cl::CredentialPrimaryPublicKey); pub struct CredentialPublicKey(cl::CredentialPublicKey); #[wasm_bindgen] -#[allow(non_snake_case)] impl CredentialPublicKey { - pub fn getPrimaryKey(&self) -> Result { + #[wasm_bindgen(js_name = getPrimaryKey)] + pub fn get_primary_key(&self) -> Result { Ok(CredentialPrimaryPublicKey(maperr!(self .0 .get_primary_key()))) } - pub fn getRevocationKey(&self) -> Result { + + #[wasm_bindgen(js_name = getRevocationKey)] + pub fn get_revocation_key(&self) -> Result { match maperr!(self.0.get_revocation_key()) { Some(k) => Ok(JsValue::from_serde(&CredentialRevocationPublicKey(k)).unwrap()), None => Ok(JsValue::NULL), @@ -108,11 +121,11 @@ pub struct CredentialKeyCorrectnessProof(cl::CredentialKeyCorrectnessProof); /// instead of a tuple. The compiler complains about unused fields /// so allow(unused) is in place for now #[wasm_bindgen] -#[allow(non_snake_case, unused)] +#[allow(dead_code)] pub struct CredentialDefinition { - publicKey: CredentialPublicKey, - privateKey: CredentialPrivateKey, - keyCorrectnessProof: CredentialKeyCorrectnessProof, + public_key: CredentialPublicKey, + private_key: CredentialPrivateKey, + key_correctness_proof: CredentialKeyCorrectnessProof, } #[wasm_bindgen] @@ -120,6 +133,7 @@ pub struct MasterSecret(cl::MasterSecret); #[wasm_bindgen] impl MasterSecret { + #[wasm_bindgen(constructor)] pub fn new() -> Result { Ok(MasterSecret(maperr!( cl::prover::Prover::new_master_secret() @@ -132,6 +146,7 @@ pub struct Nonce(cl::Nonce); #[wasm_bindgen] impl Nonce { + #[wasm_bindgen(constructor)] pub fn new() -> Result { Ok(Nonce(maperr!(cl::new_nonce()))) } @@ -150,20 +165,20 @@ pub struct BlindedCredentialSecretsCorrectnessProof(cl::BlindedCredentialSecrets /// instead of a tuple. The compiler complains about unused fields /// so allow(unused) is in place for now #[wasm_bindgen] -#[allow(non_snake_case, unused)] +#[allow(dead_code)] pub struct ProverBlindedCredentialSecrets { - blindedCredentialSecrets: BlindedCredentialSecrets, - credentialSecretsBlindingFactors: CredentialSecretsBlindingFactors, - blindedCredentialSecretsCorrectnessProof: BlindedCredentialSecretsCorrectnessProof, + blinded_credential_secrets: BlindedCredentialSecrets, + credential_secrets_blinding_factors: CredentialSecretsBlindingFactors, + blinded_credential_secrets_correctness_proof: BlindedCredentialSecretsCorrectnessProof, } #[wasm_bindgen] pub struct CredentialSignature(cl::CredentialSignature); #[wasm_bindgen] -#[allow(non_snake_case)] impl CredentialSignature { - pub fn extractIndex(&self) -> Option { + #[wasm_bindgen(js_name = extractIndex)] + pub fn extract_index(&self) -> Option { self.0.extract_index() } } @@ -172,19 +187,18 @@ impl CredentialSignature { pub struct SignatureCorrectnessProof(cl::SignatureCorrectnessProof); #[wasm_bindgen] -#[allow(non_snake_case)] pub struct IssuedCredential { - credentialSignature: CredentialSignature, - signatureCorrectnessProof: SignatureCorrectnessProof, + credential_signature: CredentialSignature, + signature_correctness_proof: SignatureCorrectnessProof, } /// Convenience class for javascript. This provides a name-value pair structure /// instead of a tuple. The compiler complains about unused fields /// so allow(unused) is in place for now #[wasm_bindgen] -#[allow(non_snake_case, unused)] +#[allow(dead_code)] pub struct IssuedCredentialWithRevocation { - issuedCredential: IssuedCredential, + issued_credential: IssuedCredential, delta: Option, } @@ -207,9 +221,9 @@ pub struct RevocationRegistry(cl::RevocationRegistry); pub struct RevocationRegistryDelta(cl::RevocationRegistryDelta); #[wasm_bindgen] -#[allow(non_snake_case)] impl RevocationRegistryDelta { - pub fn fromParts( + #[wasm_bindgen(js_name = fromParts)] + pub fn from_parts( rev_reg_from: &JsValue, rev_reg_to: &RevocationRegistry, issued: &JsValue, @@ -242,6 +256,8 @@ impl RevocationTailsGenerator { pub fn count(&self) -> u32 { self.0.count() } + + #[wasm_bindgen(js_name = next)] pub fn try_next(&mut self) -> Result { let res = maperr!(self.0.try_next()); match res { @@ -256,6 +272,7 @@ pub struct SimpleTailsAccessor(cl::SimpleTailsAccessor); #[wasm_bindgen] impl SimpleTailsAccessor { + #[wasm_bindgen(constructor)] pub fn new( rev_tails_generator: &mut RevocationTailsGenerator, ) -> Result { @@ -263,6 +280,7 @@ impl SimpleTailsAccessor { Ok(SimpleTailsAccessor(sta)) } + #[wasm_bindgen(js_name = accessTail)] pub fn access_tail(&self, tail_id: u32, accessor: &Function) { let context = JsValue::NULL; self.0 @@ -281,6 +299,7 @@ pub struct Witness(cl::Witness); #[wasm_bindgen] impl Witness { + #[wasm_bindgen(constructor)] pub fn new( rev_idx: u32, max_cred_num: u32, @@ -322,18 +341,20 @@ pub struct Proof(cl::Proof); pub struct SubProofRequest(cl::SubProofRequestBuilder); #[wasm_bindgen] -#[allow(non_snake_case)] impl SubProofRequest { + #[wasm_bindgen(constructor)] pub fn new() -> SubProofRequest { let spr = cl::verifier::Verifier::new_sub_proof_request_builder().unwrap(); SubProofRequest(spr) } - pub fn addRevelatedAttribute(&mut self, attribute: &str) { + #[wasm_bindgen(js_name = addRevealedAttribute)] + pub fn add_revelated_attribute(&mut self, attribute: &str) { self.0.add_revealed_attr(attribute).unwrap(); } - pub fn addPredicate(&mut self, attribute: &str, p_type: &str, value: i32) { + #[wasm_bindgen(js_name = addPredicate)] + pub fn add_predicate(&mut self, attribute: &str, p_type: &str, value: i32) { self.0.add_predicate(attribute, p_type, value).unwrap(); } } @@ -342,17 +363,19 @@ impl SubProofRequest { pub struct ProofBuilder(cl::prover::ProofBuilder); #[wasm_bindgen] -#[allow(non_snake_case)] impl ProofBuilder { + #[wasm_bindgen(constructor)] pub fn new() -> ProofBuilder { ProofBuilder(cl::prover::Prover::new_proof_builder().unwrap()) } - pub fn addCommonAttribute(&mut self, attribute: &str) { + #[wasm_bindgen(js_name = addCommonAttribute)] + pub fn add_common_attribute(&mut self, attribute: &str) { self.0.add_common_attribute(attribute).unwrap(); } - pub fn addSubProofRequest( + #[wasm_bindgen(js_name = addSubProofRequest)] + pub fn add_sub_proof_request( &mut self, sub_proof_request: SubProofRequest, credential_schema: CredentialSchema, @@ -393,13 +416,14 @@ impl ProofBuilder { pub struct ProofVerifier(cl::verifier::ProofVerifier); #[wasm_bindgen] -#[allow(non_snake_case)] impl ProofVerifier { + #[wasm_bindgen(constructor)] pub fn new() -> ProofVerifier { ProofVerifier(cl::verifier::Verifier::new_proof_verifier().unwrap()) } - pub fn addSubProofRequest( + #[wasm_bindgen(js_name = addSubProofRequest)] + pub fn add_sub_proof_request( &mut self, sub_proof_request: SubProofRequest, credential_schema: CredentialSchema, @@ -425,19 +449,19 @@ impl ProofVerifier { Ok(()) } - pub fn verify(&self, proof: &Proof, nonce: &Nonce) -> Result { -// let res = maperr!(self.0.verify(&proof.0, &nonce.0)); -// Ok(res) - unimplemented!(); + pub fn verify(&mut self, proof: &Proof, nonce: &Nonce) -> Result { + let res = maperr!(self.0.verify(&proof.0, &nonce.0)); + Ok(res) } } +#[wasm_bindgen] pub struct Issuer; #[wasm_bindgen] -#[allow(non_snake_case)] impl Issuer { - pub fn newCredentialDef( + #[wasm_bindgen(js_name = newCredentialDefinition)] + pub fn new_credential_def( credential_schema: CredentialSchema, non_credential_schema: NonCredentialSchema, support_revocation: bool, @@ -450,13 +474,14 @@ impl Issuer { support_revocation )); Ok(CredentialDefinition { - publicKey: CredentialPublicKey(pk), - privateKey: CredentialPrivateKey(sk), - keyCorrectnessProof: CredentialKeyCorrectnessProof(kp), + public_key: CredentialPublicKey(pk), + private_key: CredentialPrivateKey(sk), + key_correctness_proof: CredentialKeyCorrectnessProof(kp), }) } - pub fn signCredential( + #[wasm_bindgen(js_name = signCredential)] + pub fn sign_credential( prover_id: &str, blinded_credential_secrets: &BlindedCredentialSecrets, blinded_credential_secrets_correctness_proof: &BlindedCredentialSecretsCorrectnessProof, @@ -479,12 +504,13 @@ impl Issuer { &credential_priv_key.0 )); Ok(IssuedCredential { - credentialSignature: CredentialSignature(cs), - signatureCorrectnessProof: SignatureCorrectnessProof(scp), + credential_signature: CredentialSignature(cs), + signature_correctness_proof: SignatureCorrectnessProof(scp), }) } - pub fn signCredentialWithRevocation( + #[wasm_bindgen(js_name = signCredentialWithRevocation)] + pub fn sign_credential_with_revocation( prover_id: &str, blinded_credential_secrets: &BlindedCredentialSecrets, blinded_credential_secrets_correctness_proof: &BlindedCredentialSecretsCorrectnessProof, @@ -519,21 +545,22 @@ impl Issuer { &rev_tails_accessor.0 )); Ok(IssuedCredentialWithRevocation { - issuedCredential: IssuedCredential { - credentialSignature: CredentialSignature(cs), - signatureCorrectnessProof: SignatureCorrectnessProof(scp), + issued_credential: IssuedCredential { + credential_signature: CredentialSignature(cs), + signature_correctness_proof: SignatureCorrectnessProof(scp), }, delta: delta.map(|d| RevocationRegistryDelta(d)), }) } } +#[wasm_bindgen] pub struct Prover; #[wasm_bindgen] -#[allow(non_snake_case)] impl Prover { - pub fn blindCredentialSecrets( + #[wasm_bindgen(js_name = blindedCredentialSecrets)] + pub fn blind_credential_secrets( credential_pub_key: &CredentialPublicKey, credential_key_correctness_proof: &CredentialKeyCorrectnessProof, credential_values: CredentialValues, @@ -551,16 +578,18 @@ impl Prover { &credential_nonce.0 )); Ok(ProverBlindedCredentialSecrets { - blindedCredentialSecrets: BlindedCredentialSecrets(blinded_credential_secrets), - credentialSecretsBlindingFactors: CredentialSecretsBlindingFactors( + blinded_credential_secrets: BlindedCredentialSecrets(blinded_credential_secrets), + credential_secrets_blinding_factors: CredentialSecretsBlindingFactors( credential_secrets_blinding_factors, ), - blindedCredentialSecretsCorrectnessProof: BlindedCredentialSecretsCorrectnessProof( + blinded_credential_secrets_correctness_proof: BlindedCredentialSecretsCorrectnessProof( blinded_credential_secrets_correctness_proof, ), }) } - pub fn processCredentialSignature( + + #[wasm_bindgen(js_name = processCredentialSignature)] + pub fn process_credential_signature( issued_credential: &IssuedCredential, credential_values: CredentialValues, credential_secrets_blinding_factors: &CredentialSecretsBlindingFactors, @@ -574,13 +603,13 @@ impl Prover { let rev_reg = check_opt_reference!(rev_reg, RevocationRegistry); let witness = check_opt_reference!(witness, Witness); - let mut cs = maperr!(issued_credential.credentialSignature.0.try_clone()); + let mut cs = maperr!(issued_credential.credential_signature.0.try_clone()); let cv = finalize!(credential_values); maperr!(cl::prover::Prover::process_credential_signature( &mut cs, &cv, - &issued_credential.signatureCorrectnessProof.0, + &issued_credential.signature_correctness_proof.0, &credential_secrets_blinding_factors.0, &credential_pub_key.0, &nonce.0, diff --git a/libursa/src/wasm/ed25519.rs b/libursa/src/wasm/ed25519.rs index 4f20961a..c7118fa5 100644 --- a/libursa/src/wasm/ed25519.rs +++ b/libursa/src/wasm/ed25519.rs @@ -3,57 +3,71 @@ use signatures::{prelude::Ed25519Sha512 as Ed25519Sha512Impl, SignatureScheme}; use wasm_bindgen::prelude::*; -use super::{KeyPair, WasmPrivateKey, WasmPublicKey}; +use crate::keys::{PrivateKey, PublicKey}; + +use super::KeyPair; #[wasm_bindgen] -pub struct Ed25519Sha512; +pub struct Ed25519Sha512 { + keypair: KeyPair, +} #[wasm_bindgen] -#[allow(non_snake_case)] impl Ed25519Sha512 { - pub fn new() -> Self { - Self - } - - pub fn keypair(&self) -> Result { + #[wasm_bindgen(constructor)] + pub fn new() -> Result { let scheme = Ed25519Sha512Impl {}; let (pk, sk) = maperr!(scheme.keypair(None)); - Ok(KeyPair { - pk: pk.into(), - sk: sk.into(), + Ok(Self { + keypair: KeyPair { + pk: pk.into(), + sk: sk.into(), + }, }) } - pub fn keyPairFromSeed(&self, seed: &[u8]) -> Result { + #[wasm_bindgen(js_name = fromSeed)] + pub fn from_seed(seed: &[u8]) -> Result { let scheme = Ed25519Sha512Impl {}; let (pk, sk) = maperr!(scheme.keypair(Some(KeyGenOption::UseSeed(seed.to_vec())))); - Ok(KeyPair { - pk: pk.into(), - sk: sk.into(), + Ok(Self { + keypair: KeyPair { + pk: pk.into(), + sk: sk.into(), + }, + }) + } + + #[wasm_bindgen(js_name = fromPrivateKey)] + pub fn from_private_key(sk: &[u8]) -> Result { + let scheme = Ed25519Sha512Impl {}; + let pk = PrivateKey(sk.into()); + let (pk, sk) = maperr!(scheme.keypair(Some(KeyGenOption::FromSecretKey(pk)))); + Ok(Self { + keypair: KeyPair { + pk: pk.into(), + sk: sk.into(), + }, }) } - pub fn getPublicKey(&self, sk: &WasmPrivateKey) -> Result { - let sk = sk.into(); + #[wasm_bindgen(js_name = getPulicKey)] + pub fn get_public_key(&self) -> Result, JsValue> { + let sk = &self.keypair.sk; let scheme = Ed25519Sha512Impl {}; - let (pk, _) = maperr!(scheme.keypair(Some(KeyGenOption::FromSecretKey(sk)))); - Ok(pk.into()) + let (pk, _) = maperr!(scheme.keypair(Some(KeyGenOption::FromSecretKey(sk.into())))); + Ok(pk.0.to_vec()) } - pub fn sign(&self, message: &[u8], sk: &WasmPrivateKey) -> Result, JsValue> { - let sk = sk.into(); + pub fn sign(&self, message: &[u8]) -> Result, JsValue> { + let sk = &self.keypair.sk; let scheme = Ed25519Sha512Impl {}; - let sig = maperr!(scheme.sign(message, &sk)); + let sig = maperr!(scheme.sign(message, &sk.into())); Ok(sig) } - pub fn verify( - &self, - message: &[u8], - signature: &[u8], - pk: &WasmPublicKey, - ) -> Result { - let pk = pk.into(); + pub fn verify(message: &[u8], signature: &[u8], pk: &[u8]) -> Result { + let pk = PublicKey(pk.into()); let scheme = Ed25519Sha512Impl {}; Ok(maperr!(scheme.verify(message, signature, &pk))) } diff --git a/libursa/src/wasm/encryption.rs b/libursa/src/wasm/encryption.rs index a7bdc936..503e20a8 100644 --- a/libursa/src/wasm/encryption.rs +++ b/libursa/src/wasm/encryption.rs @@ -40,6 +40,7 @@ impl UrsaEncryptor { }) } + #[wasm_bindgen(js_name = withKey)] pub fn with_key(cipher: &str, key: &str) -> Result { let cipher = maperr!(EncryptorType::from_str(cipher)); let key_size = get_keysize(cipher); diff --git a/libursa/src/wasm/mod.rs b/libursa/src/wasm/mod.rs index f86b464b..7b400e5b 100644 --- a/libursa/src/wasm/mod.rs +++ b/libursa/src/wasm/mod.rs @@ -1,8 +1,14 @@ +use errors::{UrsaCryptoError, UrsaCryptoErrorKind}; +use keys::{PrivateKey, PublicKey, SessionKey}; +use serde::{Deserialize, Serialize}; +use wasm_bindgen::prelude::*; + #[macro_use] mod macros; #[cfg(feature = "bls_bn254")] pub mod bls; - +#[cfg(feature = "cl")] +pub mod cl; #[cfg(feature = "ed25519")] pub mod ed25519; #[cfg(feature = "encryption")] @@ -12,19 +18,22 @@ pub mod secp256k1; #[cfg(feature = "x25519")] pub mod x25519; -use keys::{PrivateKey, PublicKey, SessionKey}; - -use errors::{UrsaCryptoError, UrsaCryptoErrorKind}; -use serde::{Deserialize, Serialize}; -use wasm_bindgen::prelude::*; - #[wasm_bindgen] #[derive(Debug, Serialize, Deserialize)] pub struct WasmPrivateKey(String); + #[wasm_bindgen] #[derive(Debug, Serialize, Deserialize)] pub struct WasmPublicKey(String); +#[wasm_bindgen] +impl WasmPublicKey { + #[wasm_bindgen(constructor)] + pub fn new(key: &[u8]) -> Result { + Ok(WasmPublicKey(hex::encode(&key[..]))) + } +} + #[wasm_bindgen] #[derive(Debug, Serialize, Deserialize)] pub struct WasmSessionKey(String); @@ -97,8 +106,7 @@ impl From for WasmSessionKey { impl From for JsValue { fn from(err: UrsaCryptoError) -> JsValue { - let error = format!("{:?}", err); - JsValue::from_serde(&error).unwrap() + JsValue::from_serde(&err.to_string()).unwrap() } } diff --git a/libursa/src/wasm/secp256k1.rs b/libursa/src/wasm/secp256k1.rs index b9d32fb3..cfb7280d 100644 --- a/libursa/src/wasm/secp256k1.rs +++ b/libursa/src/wasm/secp256k1.rs @@ -13,9 +13,9 @@ use super::{KeyPair, WasmPrivateKey, WasmPublicKey, WasmSessionKey}; pub struct EcdsaSecp256k1Sha256(EcdsaSecp256k1Sha256Impl); #[wasm_bindgen] -#[allow(non_snake_case)] impl EcdsaSecp256k1Sha256 { - pub fn new() -> EcdsaSecp256k1Sha256 { + #[wasm_bindgen(constructor)] + pub fn new() -> Self { EcdsaSecp256k1Sha256(EcdsaSecp256k1Sha256Impl::new()) } @@ -26,14 +26,16 @@ impl EcdsaSecp256k1Sha256 { Ok(KeyPair { pk, sk }) } - pub fn keyPairFromSeed(&self, seed: &[u8]) -> Result { + #[wasm_bindgen(js_name = keypairFromSeed)] + pub fn keypair_from_seed(&self, seed: &[u8]) -> Result { let (pk, sk) = maperr!(self.0.keypair(Some(KeyGenOption::UseSeed(seed.to_vec())))); let pk = WasmPublicKey::from(&pk); let sk = WasmPrivateKey::from(&sk); Ok(KeyPair { pk, sk }) } - pub fn getPublicKey(&self, sk: &WasmPrivateKey) -> Result { + #[wasm_bindgen(js_name = getPublicKey)] + pub fn get_public_key(&self, sk: &WasmPrivateKey) -> Result { let sk = PrivateKey::from(sk); let (pk, _) = maperr!(self .0 @@ -58,22 +60,26 @@ impl EcdsaSecp256k1Sha256 { Ok(maperr!(self.0.verify(message, signature, &pk))) } - pub fn normalizeS(&self, signature: &mut [u8]) -> Result<(), JsValue> { + #[wasm_bindgen(js_name = normalizeS)] + pub fn normalize_s(&self, signature: &mut [u8]) -> Result<(), JsValue> { maperr!(self.0.normalize_s(signature)); Ok(()) } - pub fn publicKeyCompressed(&self, pk: &WasmPublicKey) -> WasmPublicKey { + #[wasm_bindgen(js_name = publicKeyCompressed)] + pub fn public_key_compressed(&self, pk: &WasmPublicKey) -> WasmPublicKey { let pk = PublicKey::from(pk); WasmPublicKey::from(self.0.public_key_compressed(&pk)) } - pub fn publicKeyUncompressed(&self, pk: &WasmPublicKey) -> WasmPublicKey { + #[wasm_bindgen(js_name = publicKeyUnCompressed)] + pub fn public_key_uncompressed(&self, pk: &WasmPublicKey) -> WasmPublicKey { let pk = PublicKey::from(pk); WasmPublicKey::from(self.0.public_key_uncompressed(&pk)) } - pub fn parseToPublicKey(&self, bytes: &[u8]) -> Result { + #[wasm_bindgen(js_name = parseToPublicKey)] + pub fn parse_to_public_key(&self, bytes: &[u8]) -> Result { let pk = maperr!(self.0.parse(bytes)); Ok(WasmPublicKey::from(&pk)) } @@ -83,9 +89,9 @@ impl EcdsaSecp256k1Sha256 { pub struct EcdhSecp256k1Sha256(EcdhSecp256k1Sha256Impl); #[wasm_bindgen] -#[allow(non_snake_case)] impl EcdhSecp256k1Sha256 { - pub fn new() -> EcdhSecp256k1Sha256 { + #[wasm_bindgen(constructor)] + pub fn new() -> Self { EcdhSecp256k1Sha256(EcdhSecp256k1Sha256Impl::new()) } @@ -96,14 +102,16 @@ impl EcdhSecp256k1Sha256 { Ok(KeyPair { pk, sk }) } - pub fn keyPairFromSeed(&self, seed: &[u8]) -> Result { + #[wasm_bindgen(js_name = keypair_from_seed)] + pub fn keypair_from_seed(&self, seed: &[u8]) -> Result { let (pk, sk) = maperr!(self.0.keypair(Some(KeyGenOption::UseSeed(seed.to_vec())))); let pk = WasmPublicKey::from(&pk); let sk = WasmPrivateKey::from(&sk); Ok(KeyPair { pk, sk }) } - pub fn getPublicKey(&self, sk: &WasmPrivateKey) -> Result { + #[wasm_bindgen(js_name = getPublicKey)] + pub fn get_public_key(&self, sk: &WasmPrivateKey) -> Result { let sk = PrivateKey::from(sk); let (pk, _) = maperr!(self .0 @@ -112,7 +120,8 @@ impl EcdhSecp256k1Sha256 { Ok(pk) } - pub fn computeSharedSecret( + #[wasm_bindgen(js_name = computeSharedSecret)] + pub fn compute_shared_secret( &self, sk: &WasmPrivateKey, pk: &WasmPublicKey, diff --git a/libursa/src/wasm/x25519.rs b/libursa/src/wasm/x25519.rs index 6b89d585..e1df3bb2 100644 --- a/libursa/src/wasm/x25519.rs +++ b/libursa/src/wasm/x25519.rs @@ -9,8 +9,8 @@ use super::{KeyPair, WasmPrivateKey, WasmPublicKey, WasmSessionKey}; pub struct X25519Sha256; #[wasm_bindgen] -#[allow(non_snake_case)] impl X25519Sha256 { + #[wasm_bindgen(constructor)] pub fn new() -> Self { Self } @@ -24,7 +24,8 @@ impl X25519Sha256 { }) } - pub fn keyPairFromSeed(&self, seed: &[u8]) -> Result { + #[wasm_bindgen(js_name = keypair_from_seed)] + pub fn key_pair_from_seed(&self, seed: &[u8]) -> Result { let scheme = X25519Sha256Impl {}; let (pk, sk) = maperr!(scheme.keypair(Some(KeyGenOption::UseSeed(seed.to_vec())))); Ok(KeyPair { @@ -33,14 +34,16 @@ impl X25519Sha256 { }) } - pub fn getPublicKey(&self, sk: &WasmPrivateKey) -> Result { + #[wasm_bindgen(js_name = getPublicKey)] + pub fn get_public_key(&self, sk: &WasmPrivateKey) -> Result { let sk = sk.into(); let scheme = X25519Sha256Impl {}; let (pk, _) = maperr!(scheme.keypair(Some(KeyGenOption::FromSecretKey(sk)))); Ok(pk.into()) } - pub fn computeSharedSecret( + #[wasm_bindgen(js_name = computeSharedSecret)] + pub fn compute_shared_secret( &self, sk: &WasmPrivateKey, pk: &WasmPublicKey,