Code Monkey home page Code Monkey logo

opensig-js's Introduction

opensig-js

Javascript e-signature library for digitally signing and verifying files on EVM-based blockchains using the OpenSig standard. See https://opensig.net/about.

Also supports public and private notarisation to the blockchain.

Installation

Browser

<script src="https://cdn.jsdelivr.net/gh/opensig/opensig-js/dist/opensig-js.js"></script>

Node.js

npm install opensig-js

Usage

Browser

// Construct a blockchain provider (see opensig.providers)

const provider = new window.opensig.providers.MetamaskProvider({
  chainId: 1,
  name: "Ethereum",
  contract: "0x73eF7A3643aCbC3D616Bd5f7Ee5153Aa5f14DB30", 
  blockTime: 12000,
  creationBlock: 16764681
});


// Construct an OpenSig Document object from a File 
// (or construct one from a hash - see Document Class below)

const myDoc = new window.opensig.File(provider, new File('./myfile.txt'));


// Verify signatures on the blockchain

const signatures = await myDoc.verify();

signatures.forEach(sig => console.log(sig.time, sig.signatory, sig.data));


// Sign using Metamask

const signData = {
  type: 'string',
  encrypted: true,
  content: 'some data'
};

const result = await myDoc.sign(signData);

console.log(result.txHash, result.signatory, result.signature);

const receipt = await result.confirmationInformer;

console.log('signature published successfully', receipt));

Node.js

import * as opensig from 'opensig-js';

import MetaMaskSDK from '@metamask/sdk';


const MMSDK = new MetaMaskSDK(options);

const ethereumProvider = MMSDK.getProvider();


// Construct a blockchain provider (see opensig.providers)

const provider = new opensig.providers.MetamaskProvider({
  chainId: 1,
  name: "Ethereum",
  contract: "0x73eF7A3643aCbC3D616Bd5f7Ee5153Aa5f14DB30", 
  blockTime: 12000,
  creationBlock: 16764681,
  ethereum: ethereumProvider,
});

...

Document Class

The Document class is an alternative to the File class. It takes a pre-determined document hash instead of a file.

const myDocHash = ...

const myDoc = new opensig.Document(provider, myDocHash);

const signatures = await myDoc.verify();
...

Blockchain Providers

Blockchain providers publish sign transactions to the blockchain and query the blockchain for signatures using whatever transport protocol is appropriate.

Implement your own or use one of the bundled providers. See src/providers.js for the BlockchainProvider interface.

OpenSig is bundled with 3 types of provider accessed via opensig.providers. At this time all three use Metamask to sign signature transactions but use different services to query the blockchain for signatures:

MetamaskProvider - Uses the local Metamask wallet to query the blockchain for signatures and to sign transactions.

HTTPProvider - Uses a web3 http provider to query the blockchain for signatures. Uses the local Metamask wallet to sign and publish transactions.

AnkrProvider - Uses the Ankr network to query the blockchain for signatures. Uses the local Metamask wallet to sign and publish transactions.

Parameters

A BlockchainProvider takes the following parameters:

  • chainId - blockchain's chain id
  • name - label
  • contract - address of that blockchain's Registry Contract (see https://opensig.net/about#contracts)
  • blockTime - the network block time in ms
  • creationBlock - the registry contract's creation block number (minimises search window when querying for signatures)

MetamaskProvider extends BlockchainProvider and takes in addition:

  • ethereum - optional Metamask ethereum provider. Defaults to window.ethereum

HTTPProvider extends MetamaskProvider and takes in addition:

  • url - url of the http service, e.g. Infura

AnkrProvider extends MetamaskProvider and takes in addition:

  • endpoint - url of the Ankr network endpoint, e.g. https://rpc.ankr.com/multichain

opensig-js's People

Contributors

dnpotter avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

opensig-js's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.