Skip to content

🟨 Use from JavaScript

npm

You can use this toolkit to sign and publish Nanopublications from JavaScript or TypeScript with the @nanopub/sign NPM package.

Demo

Visit the playground page to sign nanopubs, or generate and register a new key pair, directly in your browser using this NPM package. You can checkout the playground.html file as an example to use the package directly from HTML/JS.

Build a Nanopublication

This package takes an already prepared Nanopublication RDF string as input. If you want to easily display nanopubs checkout the @nanopub/display package, if you want to fetch and manipulate nanopubs check the @nanopub/utils package.

📥️ Install

Install the npm package (use yarn or pnpm if you prefer) to use it from your favorite framework:

npm install --save @nanopub/sign

Or directly import from a CDN in JavaScript code:

import init, { Nanopub, NpProfile, getNpServer } from "https://unpkg.com/@nanopub/sign";

ℹ️ How it works

This package provides several functionalities related to the handling of Nanopublications, including signing, publishing, verifying, and fetching them:

✍️ Sign Nanopubs

This process involves signing a Nanopublication RDF string using a specified RSA private key passed through the profile. The signing operation ensures that the Nanopub is authentically created by the holder of the private key.

Get a private key

You can easily create and register a new private key on the playground page after login with your ORCID.

import init, { Nanopub, NpProfile, getNpServer } from "@nanopub/sign";

// Change the RDF and private key as you wish
const rdfStr = `@prefix : <http://purl.org/nanopub/temp/mynanopub#> .
@prefix np: <http://www.nanopub.org/nschema#> .
@prefix npx: <http://purl.org/nanopub/x/> .
@prefix prov: <http://www.w3.org/ns/prov#> .
@prefix ex: <http://example.org/> .
:Head {
    : np:hasAssertion :assertion ;
        np:hasProvenance :provenance ;
        np:hasPublicationInfo :pubinfo ;
        a np:Nanopublication .
}
:assertion {
    ex:mosquito ex:transmits ex:malaria .
}
:provenance {
    :assertion prov:hadPrimarySource <http://dx.doi.org/10.3233/ISU-2010-0613> .
}
:pubinfo {
    : a npx:ExampleNanopub .
}`
const privateKey=`MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCjY1gsFxmak6SOCouJPuEzHNForkqFhgfHE3aAIAx+Y5q6UDEDM9Q0EksheNffJB4iPqsAfiFpY0ARQY92K5r8P4+a78eu9reYrb2WxZb1qPJm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WBpdHzR9HN+hGjvwlsYny2L9Qbfhgxxmsuf7zeFLpQLijjwKBgH7TD28k8IOk5VKec2CNjKd600OYaA3UfCpP/OhDl/RmVtYoHWDcrBrRvkvEEd2/DZ8qw165Zl7gJs3vK+FTYvYVcfIzGPWA1KU7nkntwewmf3i7V8lT8ZTwVRsmObWU60ySJ8qKuwoBQodki2VX12NpMN1wgWe3qUUlr6gLJU4xAoGAet6nD3QKwk6TTmcGVfSWOzvpaDEzGkXjCLaxLKh9GreM/OE+h5aN2gUoFeQapG5rUwI/7Qq0xiLbRXw+OmfAoV2XKv7iI8DjdIh0F06mlEAwQ/B0CpbqkuuxphIbchtdcz/5ra233r3BMNIqBl3VDDVoJlgHPg9msOTRy13lFqc=`;

// Instantiate Nanopub profile (ORCID and name are optional)
const profile = new NpProfile(privateKey, "https://orcid.org/0000-0000-0000-0000", "Your Name");

// Sign the Nanopub RDF
const signed = new Nanopub(rdfStr).sign(profile);
console.log("Signed:", signed.info(), signed.rdf());

📬 Publish Nanopubs

Signed Nanopubs can be published to a Nanopub server. This makes the Nanopub accessible to others in the network.

Use the publish function on a Nanopub, the 2 arguments are optional:

  • 🔑 profile is required if you want to also sign the nanopub, it is not required if you provide a signed nanopub
  • 🧫 If the server_url is null it will be published to the test server
import { Nanopub, NpProfile } from "@nanopub/sign";

const profile = new NpProfile(privateKey, "https://orcid.org/0000-0000-0000-0000", "Your Name", "");
const np = await new Nanopub(rdfStr).publish(profile, null);
console.log("Published:", np.info(), signed.rdf());

Provide the nanopub signed or unsigned

  • If signed nanopub and profile not provided, we publish the signed nanopub as it is
  • If signed nanopub and profile provided, we re-sign the nanopub (signature triples are updated)
  • If unsigned nanopub and profile provided, we sign the nanopub
  • If unsigned nanopub and profile not provided, we throw an error

🧪 Test and productions servers

If the the last argument of publish() is null the nanopub will be published to the test server. In this case the nanopub will not be available at https://w3id.org/np/, but at https://np.test.knowledgepixels.com/, e.g. https://np.test.knowledgepixels.com/RAKObyGXmbgTYWj2iN0XGgJv0yWNDQd_DTmAWUouGfIsM

You can publish to the production network by getting the URL of a server using getNpServer(true) (true will pick a random nanopub server on the production network, while false will pick the main nanopub server):

import init, { Nanopub, NpProfile, getNpServer } from "@nanopub/sign";

const np = await new Nanopub(rdfStr).publish(profile, getNpServer(true));

☑️ Verify Nanopubs

This operation involves checking the integrity of Nanopubs. It ensures that a Nanopub is valid, regardless of whether it is signed or unsigned.

const checked = new Nanopub(rdfStr).check();

📡 Fetch Nanopubs

This function allows you to retrieve Nanopubs from the network using their URI. It’s useful for accessing and using Nanopubs created by others.

const npUri = "https://w3id.org/np/RAltRkGOtHoj5LcBJZ62AMVOAVc0hnxt45LMaCXgxJ4fw";
const np = await Nanopub.fetch(npUri);
console.log(np.info())

🔑 Generate private key and publish introduction

You can generate a new private/public key pair, and publish a nanopub introduction to register this key under your ORCID in the Nanopublications network:

import init, { Nanopub, NpProfile, KeyPair } from "@nanopub/sign";

// Randomly generate a new private/public key pair, and convert it to a JS object
let keypair = new KeyPair();
keypair = keypair.toJs();

// Create a profile with this new private key
const orcid = "https://orcid.org/0000-0000-0000-0000"
const profile = new NpProfile(keypair.private, orcid, "Your Name", "");

// Publish a nanopub introduction for this profile
Nanopub.publish_intro(profile, getNpServer(false))
    .then(np => {
        console.log("Published Introduction Nanopub:", np.info());
    })
    .catch(err => {
        console.error("Error publishing the Nanopub Introduction:", err);
    });