Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vemonet committed Oct 31, 2023
1 parent 2b244c4 commit 8f2adb4
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 81 deletions.
6 changes: 3 additions & 3 deletions js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ https://developer.mozilla.org/en-US/docs/WebAssembly/Rust_to_wasm
## Install

```bash
dnf install perl
# cf. https://github.com/openssl/openssl/issues/13761
cargo install wasm-pack
```

Expand All @@ -16,7 +14,9 @@ cargo install wasm-pack
wasm-pack build --target web
```

## Try
## Try it

Code in the `index.html` file

```bash
python3 -m http.server
Expand Down
11 changes: 5 additions & 6 deletions js/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
<p id="rdf-text"></p>
<script type="module">
import init, { Nanopub } from "./pkg/nanopub_js.js";
const rdfText = document.getElementById('rdf-text');

const private_key=`MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCjY1gsFxmak6SOCouJPuEzHNForkqFhgfHE3aAIAx+Y5q6UDEDM9Q0EksheNffJB4iPqsAfiFpY0ARQY92K5r8P4+a78eu9reYrb2WxZb1qPJmvR7XZ6sN1oHD7dd/EyQoJmQsmOKdrqaLRbzR7tZrf52yvKkwNWXcIVhW8uxe7iUgxiojZpW9srKoK/qFRpaUZSKn7Z/zgtDH9FJkYbBsGPDMqp78Kzt+sJb+U2W+wCSSy34jIUxx6QRbzvn6uexc/emFw/1DU5y7zBudhgC7mVk8vX1gUNKyjZBzlOmRcretrANgffqs5fx/TMHN1xtkA/H1u1IKBfKoyk/xThMLAgMBAAECggEAECuG0GZA3HF8OaqFgMG+W+agOvH04h4Pqv4cHjYNxnxpFcNV9nEssTKWSOvCwYy7hrwZBGV3PQzbjFmmrxVFs20+8yCD7KbyKKQZPVC0zf84bj6NTNgvr6DpGtDxINxuGaMjCt7enqhoRyRRuZ0fj2gD3Wqae/Ds8cp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sttMIdG4lSBbEjA7t4IQC5lyr7xiQKBgQCN87YGJ40Y5ZXCSgOZDepz9hqX2KGOIfnUv2HvXsIfiUwqTXs6HbD18xg3KL4myIBOvywSM+4ABYp+foY+Cpcq2btLIeZhiWjsKIrw71+Q/vIe0YDb1PGf6DsoYhmWBpdHzR9HN+hGjvwlsYny2L9Qbfhgxxmsuf7zeFLpQLijjwKBgH7TD28k8IOk5VKec2CNjKd600OYaA3UfCpP/OhDl/RmVtYoHWDcrBrRvkvEEd2/DZ8qw165Zl7gJs3vK+FTYvYVcfIzGPWA1KU7nkntwewmf3i7V8lT8ZTwVRsmObWU60ySJ8qKuwoBQodki2VX12NpMN1wgWe3qUUlr6gLJU4xAoGAet6nD3QKwk6TTmcGVfSWOzvpaDEzGkXjCLaxLKh9GreM/OE+h5aN2gUoFeQapG5rUwI/7Qq0xiLbRXw+OmfAoV2XKv7iI8DjdIh0F06mlEAwQ/B0CpbqkuuxphIbchtdcz/5ra233r3BMNIqBl3VDDVoJlgHPg9msOTRy13lFqc=`;

const rdfStr = `@prefix : <http://purl.org/nanopub/temp/mynanopub#> .
@prefix drugbank: <http://identifiers.org/drugbank/> .
Expand Down Expand Up @@ -50,18 +52,15 @@
: prov:wasAttributedTo orcid:0000-0000-0000-0000 .
}`

const private_key=`-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCjY1gsFxmak6SOCouJPuEzHNForkqFhgfHE3aAIAx+Y5q6UDEDM9Q0EksheNffJB4iPqsAfiFpY0ARQY92K5r8P4+a78eu9reYrb2WxZb1qPJmvR7XZ6sN1oHD7dd/EyQoJmQsmOKdrqaLRbzR7tZrf52yvKkwNWXcIVhW8uxe7iUgxiojZpW9srKoK/qFRpaUZSKn7Z/zgtDH9FJkYbBsGPDMqp78Kzt+sJb+U2W+wCSSy34jIUxx6QRbzvn6uexc/emFw/1DU5y7zBudhgC7mVk8vX1gUNKyjZBzlOmRcretrANgffqs5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N1wgWe3qUUlr6gLJU4xAoGAet6nD3QKwk6TTmcGVfSWOzvpaDEzGkXjCLaxLKh9GreM/OE+h5aN2gUoFeQapG5rUwI/7Qq0xiLbRXw+OmfAoV2XKv7iI8DjdIh0F06mlEAwQ/B0CpbqkuuxphIbchtdcz/5ra233r3BMNIqBl3VDDVoJlgHPg9msOTRy13lFqc=
-----END PRIVATE KEY-----`;

const orcid="https://orcid.org/0000-0000-0000-0000";
// TODO: Defaults don't work
const server_url='';
const publish=false;
const rdfText = document.getElementById('rdf-text');

init().then(() => {
const np = new Nanopub(
`<http://s> <http://p> <http://o> <http://g> .`,
rdfStr,
private_key, orcid,
server_url, publish
);
Expand Down
14 changes: 4 additions & 10 deletions js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@ use wasm_bindgen::prelude::*;
mod nanopub;
mod utils;


#[wasm_bindgen(start)]
pub fn main() {
pub fn set_panic_hook() {
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}

// #[wasm_bindgen]
// extern {
// pub fn alert(s: &str);
// }

// #[wasm_bindgen]
// pub fn greet(name: &str) {
// alert(&format!("Hello, {}!", name));
// }
7 changes: 4 additions & 3 deletions js/src/nanopub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use nanopub_rs::nanopub::Nanopub;
use wasm_bindgen::prelude::*;

#[wasm_bindgen(js_name = Nanopub)]
pub struct JsNanopub {
pub struct NanopubJs {
np: Nanopub,
}

// Maybe try https://rustwasm.github.io/wasm-bindgen/reference/arbitrary-data-with-serde.html
#[wasm_bindgen(js_class = Nanopub)]
impl JsNanopub {
impl NanopubJs {
// pub fn new(rdf: Option<&str>) -> Result<JsNanopub, JsValue> {

#[wasm_bindgen(constructor)]
Expand All @@ -18,7 +18,8 @@ impl JsNanopub {
orcid: &str,
server_url: &str,
publish: bool,
) -> Result<JsNanopub, JsValue> {
) -> Result<NanopubJs, JsValue> {
console_error_panic_hook::set_once();
Ok(Self {
np: Nanopub::new(&rdf, private_key, orcid, Some(server_url), Some(&publish))
.expect_throw("Error signing the Nanopub"),
Expand Down
1 change: 0 additions & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ keywords = ["RDF", "nanopublications"]

[dependencies]
base64 = "0.13.0"
# openssl = "0.10.41"
sophia = "0.7.2"
rsa = { version = "0.9.3", default-features = false, features = ["pem", "sha2"] }
openssl-probe = "0.1.5"
Expand Down
27 changes: 5 additions & 22 deletions lib/src/nanopub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ impl Nanopub {
server_url: Option<&str>,
publish: Option<&bool>,
) -> Result<Self, Box<dyn Error>> {
// Self::default()
openssl_probe::init_ssl_cert_env_vars();

let tmp_ns = Namespace::new(TEMP_NP_NS)?;
// let tmp_uri = Namespace::new(TEMP_NP_URI)?;
let npx: Namespace<&str> = Namespace::new(NPX)?;

let mut dataset: LightDataset = trig::parse_str(rdf).collect_quads()?;
Expand All @@ -90,19 +89,8 @@ impl Nanopub {
println!(" NORMED QUADS");
println!("{}", norm_quads);

// DEPRECATED: Get the keypair with OpenSSL
// let keypair = Rsa::private_key_from_pem(private_key.as_bytes()).unwrap();
// let keypair = PKey::from_rsa(keypair).unwrap();

openssl_probe::init_ssl_cert_env_vars();
println!("GETTING READY");
// let priv_key = RsaPrivateKey::from_pkcs1_pem(private_key)?;

let priv_key_bytes = base64::decode(private_key).expect("Failed to decode base64 private key");
let priv_key_bytes = base64::decode(private_key)?;
let priv_key = RsaPrivateKey::from_pkcs8_der(&priv_key_bytes).expect("Failed to parse RSA private key");
// let priv_key = RsaPrivateKey::from_pkcs1_der(private_key).expect("Failed to parse RSA private key");

println!("private_key GOOD");

let public_key = RsaPublicKey::from(&priv_key);
// let pub_key_str = normalize_key(&ToRsaPublicKey::to_pkcs1_pem(&public_key).unwrap()).unwrap();
Expand Down Expand Up @@ -173,16 +161,11 @@ impl Nanopub {
// Some(&tmp_ns.get("pubinfo")?),
// )?;

// TODO: add the signature and re sign
// TODO: add the signature to ubinfo graph, and re-sign
let trusty_str = base64::encode_config(norm_quads, base64::URL_SAFE);
println!("Trusty URI artefact:\n{}\n", trusty_str);

// https://stackoverflow.com/questions/73716046/how-to-display-an-openssl-signature
// println!("{}", signature.to_);

// TODO: Sign the file with the private_key
// Add signature to the graph
// Generate trusty-uri
// TODO: Generate trusty-uri

// for quad in
// dataset.quads().for_each_quad(|q| {
Expand All @@ -196,13 +179,13 @@ impl Nanopub {

// Prepare the trig serializer
let prefixes = get_prefixes();
// TrigConfig
let trig_config = TrigConfig::new()
.with_pretty(true)
.with_prefix_map(&prefixes[..]);
let mut trig_stringifier = TrigSerializer::new_stringifier_with_config(trig_config);
// TODO: replace all }GRAPH by }\n ? Or fix pretty code

// Return the Nanopub object
Ok(Self {
rdf: trig_stringifier
.serialize_dataset(&mut dataset)?
Expand Down
Loading

0 comments on commit 8f2adb4

Please sign in to comment.