Skip to content

Commit

Permalink
add test, improve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
vemonet committed Nov 27, 2023
1 parent 65af28b commit b8739d5
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/docs/assets/turtle.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var module = module ? module : {}; // shim for browser use
function hljsDefineTurtle(hljs) {
// export default function (hljs) {
var KEYWORDS = {
keyword: "BASE|2 PREFIX|5 @base|10 @prefix|10",
keyword: "BASE|2 PREFIX|5 GRAPH|3 @base|10 @prefix|10",
literal: "true false",
built_in: "a",
};
Expand Down
13 changes: 9 additions & 4 deletions lib/docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

This project aims to provide a comprehensive cross-platform toolkit to sign, publish, and check **[Nanopublications](https://nanopub.net)**.

Sign and publish providing a private RSA key string, or a `profile.yml` file. Check the validity of signed or unsigned Nanopublications.
It enables developers to:

- Sign and publish nanopubs using a RSA private key, with support for configuration through a `profile.yml` file.
- Check the validity of signed or unsigned nanopubs.

It is packaged to be used easily through various popular interfaces:

Expand All @@ -28,9 +31,11 @@ The library automatically handles most RDF serializations supporting quads for t
- Nquads
- JSON-LD

When signing a nanopub, some metadata in the pubinfo graph are created automatically if they are not already set in the RDF provided:
When signing a nanopub, some metadata are created automatically in the pubinfo graph if they are not already set in the provided RDF:

- Date and time of the nanopub creation is added using `dct:created`
- ORCID of the creator is added using `dct:creator` if an ORCID was provided in the profile used to sign the nanopub (we also check if the ORCID has been set with `prov:wasAttributedTo` or `pav:createdBy`)

- Date and time of the Nanopublication creation using `dct:created`.
- ORCID of the creator using `dct:creator`, if an ORCID was provided in the profile used to sign the Nanopublication (we also check if the ORCID has been set with `prov:wasAttributedTo`, or `pav:createdBy`)
Checkout the page most adapted to your use-case to get started.

> 💡 If you are facing any problem, or have ideas to help improve this project, please [create an issue](https://github.com/vemonet/nanopub-rs/issues) on GitHub.
4 changes: 0 additions & 4 deletions lib/docs/use_python.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

You can easily publish Nanopubs from Python.

```admonish warning title="Early stage"
This component is in a really early stage, and the below documentation might not be right.
```

## 📥️ Install

Install the `pip` package:
Expand Down
9 changes: 8 additions & 1 deletion lib/src/nanopub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub struct Nanopub {
pub orcid: String,
pub published: bool,
// pub info: NpInfo,
// dataset: LightDataset,
// pub dataset: LightDataset,
}

impl fmt::Display for Nanopub {
Expand All @@ -68,6 +68,13 @@ impl fmt::Display for Nanopub {
}

impl Nanopub {
// // TODO: change approach to use Nanopub::new(rdf).sign()?
// pub fn new<T: RdfSource>(rdf: T) -> Result<Self, NpError> {
// let mut dataset = rdf.get_dataset()?;
// let np_info = extract_np_info(&dataset, false)?;
// // Ok(Self { info: np_info, dataset, published: false })
// }

/// Fetch a Nanopub given its URI.
///
/// # Arguments
Expand Down
12 changes: 6 additions & 6 deletions lib/src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,17 @@ pub fn gen_keys() -> Result<(String, String), NpError> {
// Because of wasm we can't use the rand crate
struct WasmRng;
impl RngCore for WasmRng {
fn next_u32(&mut self) -> u32 {
impls::next_u32_via_fill(self)
}
fn next_u64(&mut self) -> u64 {
impls::next_u64_via_fill(self)
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
getrandom(dest).expect("Error generating random bytes");
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
getrandom(dest).map_err(rand_core::Error::new)
}
fn next_u32(&mut self) -> u32 {
impls::next_u32_via_fill(self)
}
fn next_u64(&mut self) -> u64 {
impls::next_u64_via_fill(self)
}
}
impl CryptoRng for WasmRng {}
5 changes: 1 addition & 4 deletions lib/src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ pub async fn publish_np(server: &str, np: &str) -> Result<bool, NpError> {
reqwest::StatusCode::CREATED => Ok(true),
_ => {
// Get the error message from the response body
let error_msg = res
.text()
.await
.unwrap_or_else(|_| "Unknown error while publishing the nanopub".to_string());
let error_msg = res.text().await?;
Err(NpError(error_msg))
}
}
Expand Down
14 changes: 12 additions & 2 deletions lib/tests/nanopub_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use nanopub::{
constants::TEST_SERVER,
extract::extract_np_info,
get_np_server,
profile::gen_keys,
publish::publish_np,
sign::normalize_dataset,
utils::{ns, parse_rdf},
Nanopub, NpProfile,
Expand Down Expand Up @@ -50,7 +52,8 @@ fn sign_nanopub_blank() -> Result<(), Box<dyn Error>> {
&get_test_key(),
None,
)?;
println!("{}", profile); // required for coverage
println!("{}", profile); // cov
let _pubkey = profile.get_public_key(); // cov
let np = Nanopub::sign(&np_rdf, &profile)?;
assert!(!np.published);
Ok(())
Expand All @@ -60,7 +63,7 @@ fn sign_nanopub_blank() -> Result<(), Box<dyn Error>> {
fn check_valid_unsigned() -> Result<(), Box<dyn Error>> {
let np_rdf = fs::read_to_string("./tests/resources/simple1-rsa.trig")?;
let np = Nanopub::check(&np_rdf);
assert!(!np.is_err());
assert!(np.is_ok());
Ok(())
}

Expand Down Expand Up @@ -181,3 +184,10 @@ fn test_gen_keys() -> Result<(), Box<dyn Error>> {
assert!(privkey.len() > 10);
Ok(())
}

#[tokio::test]
async fn unit_publish_np_fail() -> Result<(), Box<dyn Error>> {
let res = publish_np(TEST_SERVER, "wrong").await;
assert!(res.is_err());
Ok(())
}

0 comments on commit b8739d5

Please sign in to comment.