diff --git a/README.md b/README.md index ba649e3..8c5163f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# ✍️⚔️ Nanopub cross-platform toolkit +# ✍️ Nanopub cross-platform toolkit ⚔️ [![Build](https://github.com/vemonet/nanopub-rs/actions/workflows/build.yml/badge.svg)](https://github.com/vemonet/nanopub-rs/actions/workflows/build.yml) [![Lint and Test](https://github.com/vemonet/nanopub-rs/actions/workflows/test.yml/badge.svg)](https://github.com/vemonet/nanopub-rs/actions/workflows/test.yml) [![codecov](https://codecov.io/gh/vemonet/nanopub-rs/graph/badge.svg?token=BF15PSO6GN)](https://codecov.io/gh/vemonet/nanopub-rs) diff --git a/js/src/utils.rs b/js/src/utils.rs index a362c31..d88fe37 100644 --- a/js/src/utils.rs +++ b/js/src/utils.rs @@ -1,5 +1,5 @@ -use js_sys::Error; -use wasm_bindgen::JsValue; +// use js_sys::Error; +// use wasm_bindgen::JsValue; #[macro_export] macro_rules! format_err { @@ -11,6 +11,6 @@ macro_rules! format_err { }; } -pub fn to_err(e: impl ToString) -> JsValue { - JsValue::from(Error::new(&e.to_string())) -} +// pub fn to_err(e: impl ToString) -> JsValue { +// JsValue::from(Error::new(&e.to_string())) +// } diff --git a/lib/src/error.rs b/lib/src/error.rs index f8d2bc1..b140a88 100644 --- a/lib/src/error.rs +++ b/lib/src/error.rs @@ -77,6 +77,11 @@ impl From for NpError { NpError(format!("RSA signing error: {err}")) } } +impl From for NpError { + fn from(err: rsa::pkcs8::Error) -> Self { + NpError(format!("Invalid RSA public key error: {err}")) + } +} impl From for NpError { fn from(err: rsa::pkcs8::spki::Error) -> Self { NpError(format!("Invalid RSA public key error: {err}")) diff --git a/lib/src/nanopub.rs b/lib/src/nanopub.rs index 867c9e1..2f38579 100644 --- a/lib/src/nanopub.rs +++ b/lib/src/nanopub.rs @@ -7,12 +7,11 @@ use crate::utils::{get_ns, parse_rdf, serialize_rdf}; use base64; use base64::{engine, Engine as _}; -use chrono::Utc; use regex::Regex; use rsa::pkcs8::DecodePublicKey; use rsa::{sha2::Digest, sha2::Sha256, Pkcs1v15Sign, RsaPublicKey}; use sophia::api::dataset::{Dataset, MutableDataset}; -use sophia::api::ns::{rdf, xsd, Namespace}; +use sophia::api::ns::{rdf, Namespace}; use sophia::api::quad::Quad; use sophia::api::term::{matcher::Any, Term}; use sophia::inmem::dataset::LightDataset; @@ -244,7 +243,7 @@ impl Nanopub { pub fn sign(rdf: &str, profile: &NpProfile) -> Result { openssl_probe::init_ssl_cert_env_vars(); - let (priv_key, pubkey) = get_keys(&profile.private_key); + let (priv_key, pubkey) = get_keys(&profile.private_key)?; let pubkey_str = get_pubkey_str(&pubkey); // Parse the provided RDF diff --git a/lib/src/profile.rs b/lib/src/profile.rs index 1a74dab..c0564d2 100644 --- a/lib/src/profile.rs +++ b/lib/src/profile.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use serde_yaml; use std::io::Read; use std::{env, fs}; -use std::{error::Error, fmt}; +use std::fmt; use crate::constants::DEFAULT_NP_PROFILE; use crate::constants::{BOLD, END}; @@ -26,7 +26,7 @@ impl NpProfile { private_key: &str, introduction_nanopub_uri: Option<&str>, ) -> Result { - let (_priv_key, pubkey) = get_keys(private_key); + let (_priv_key, pubkey) = get_keys(private_key)?; Ok(Self { orcid_id: orcid_id.to_string(), name: name.to_string(), @@ -77,15 +77,13 @@ impl fmt::Display for NpProfile { } /// Get `RsaPrivateKey` and `RsaPublicKey` given a private key string -pub fn get_keys(private_key: &str) -> (RsaPrivateKey, RsaPublicKey) { +pub fn get_keys(private_key: &str) -> Result<(RsaPrivateKey, RsaPublicKey), NpError> { let priv_key_bytes = engine::general_purpose::STANDARD - .decode(private_key) - .expect("Error decoding private key"); + .decode(private_key)?; let priv_key = - RsaPrivateKey::from_pkcs8_der(&priv_key_bytes).expect("Failed to parse RSA private key"); - + RsaPrivateKey::from_pkcs8_der(&priv_key_bytes)?; let public_key = RsaPublicKey::from(&priv_key); - (priv_key, public_key) + Ok((priv_key, public_key)) } /// Get a public key string for a `RsaPublicKey` diff --git a/lib/src/publish.rs b/lib/src/publish.rs index 2b139d9..66167b0 100644 --- a/lib/src/publish.rs +++ b/lib/src/publish.rs @@ -1,5 +1,3 @@ -// use crate::error::NpError; -use std::error::Error; #[cfg(target_arch = "wasm32")] use wasm_bindgen::prelude::*; #[cfg(target_arch = "wasm32")] diff --git a/lib/tests/nanopub_test.rs b/lib/tests/nanopub_test.rs index 26c3087..4eaea18 100644 --- a/lib/tests/nanopub_test.rs +++ b/lib/tests/nanopub_test.rs @@ -8,7 +8,6 @@ fn get_test_key() -> String { #[test] fn publish_nanopub_simple_rsa() -> Result<(), Box> { let np_rdf = fs::read_to_string("./tests/resources/simple1-rsa.trig")?; - let profile = NpProfile::new("", "", &get_test_key(), None)?; let np = Nanopub::publish(&np_rdf, &profile, None)?; // println!("{}", np); @@ -41,6 +40,23 @@ fn sign_nanopub_test_blank() -> Result<(), Box> { Ok(()) } +#[test] +fn publish_fail() -> Result<(), Box> { + let np_rdf = fs::read_to_string("./tests/resources/simple1-rsa.trig")?; + let profile = NpProfile::new("", "", &get_test_key(), None)?; + let np = Nanopub::publish(&np_rdf, &profile, Some("failing"))?; + assert!(!np.published); + Ok(()) +} + +#[test] +fn profile_fail() -> Result<(), Box> { + let profile = NpProfile::new("", "", "failing", None); + assert!(profile.is_err()); + Ok(()) +} + + #[test] fn check_nanopub_test_blank() -> Result<(), Box> { let np_rdf = fs::read_to_string("./tests/resources/signed.nanopub_test_blank.trig")?;