Skip to content

Commit

Permalink
Remove warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
vemonet committed Nov 19, 2023
1 parent c95bcd3 commit 4ed63cd
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
10 changes: 5 additions & 5 deletions js/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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()))
// }
5 changes: 5 additions & 0 deletions lib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ impl From<rsa::Error> for NpError {
NpError(format!("RSA signing error: {err}"))
}
}
impl From<rsa::pkcs8::Error> for NpError {
fn from(err: rsa::pkcs8::Error) -> Self {
NpError(format!("Invalid RSA public key error: {err}"))
}
}
impl From<rsa::pkcs8::spki::Error> for NpError {
fn from(err: rsa::pkcs8::spki::Error) -> Self {
NpError(format!("Invalid RSA public key error: {err}"))
Expand Down
5 changes: 2 additions & 3 deletions lib/src/nanopub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -244,7 +243,7 @@ impl Nanopub {
pub fn sign(rdf: &str, profile: &NpProfile) -> Result<Self, NpError> {
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
Expand Down
14 changes: 6 additions & 8 deletions lib/src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -26,7 +26,7 @@ impl NpProfile {
private_key: &str,
introduction_nanopub_uri: Option<&str>,
) -> Result<Self, NpError> {
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(),
Expand Down Expand Up @@ -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`
Expand Down
2 changes: 0 additions & 2 deletions lib/src/publish.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// use crate::error::NpError;
use std::error::Error;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
Expand Down
18 changes: 17 additions & 1 deletion lib/tests/nanopub_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ fn get_test_key() -> String {
#[test]
fn publish_nanopub_simple_rsa() -> Result<(), Box<dyn Error>> {
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);
Expand Down Expand Up @@ -41,6 +40,23 @@ fn sign_nanopub_test_blank() -> Result<(), Box<dyn Error>> {
Ok(())
}

#[test]
fn publish_fail() -> Result<(), Box<dyn Error>> {
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<dyn Error>> {
let profile = NpProfile::new("", "", "failing", None);
assert!(profile.is_err());
Ok(())
}


#[test]
fn check_nanopub_test_blank() -> Result<(), Box<dyn Error>> {
let np_rdf = fs::read_to_string("./tests/resources/signed.nanopub_test_blank.trig")?;
Expand Down

0 comments on commit 4ed63cd

Please sign in to comment.