Blockchain e-sign library
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src ammended #18: added Signature toString elements <id> and <network> Sep 13, 2016
test
LICENSE
README.md
package.json

README.md

OpenSig Library (opensig-lib)

NPM

Blockchain digital signature library. A javascript library that implements the OpenSig digitial signature scheme providing functions to digitally sign and verify files, recording signatures on the bitcoin blockchain.

opensig-lib is built using bitcoinjs-lib.

Primary Features

  • Create: generate a new private key.
  • Sign: sign any file with your private key and record your signature on the blockchain.
  • Verify: retrieve a list of a file's signatures from the blockchain.

Secondary Features

  • Send: create and publish a transaction to spend any amount from one address to another.
  • Publish: publish a transaction on the blockchain.
  • Balance: retrieve the balance of your key (or any public key) from the blockchain.
  • Get Key: obtain private key, wif and public keys from any private key, WIF or file.

Installation

npm install opensig-lib

Setup

Node.js

const opensig = require('opensig-lib')

Usage

Create

Returns a KeyPair object containing a random private key and its associated wif and public keys.

opensig.create( [label] )   // returns a KeyPair object

label optional label to populate the key's label property

Sign

Returns a promise to resolve a Receipt object containing a transaction to sign the given file with the given key, and, optionally, to publish the transaction on the blockchain.

Equivalent to opensig.send( key, file, payment, fee, publish ).

opensig.sign( <file>, <key>, [publish], [payment], [fee] );  // returns a promise

file File to sign. (string containing a file path or a file's hex64 private key or WIF. Can also accept a KeyPair object).

key Key to sign with. (KeyPair, or a string containing a hex64 private key, a WIF or a file)

publish If true the transaction will be published on the blockchain. (boolean)

payment Amount to send in the transaction. Defaults to 5430 satoshis. (positive integer)

fee Amount to include as the miner's fee. Defaults to 10000 satoshis. (positive integer)

Verify

Returns a promise to resolve an array of Signature objects containing the list of signatures for the given file.

opensig.verify( <file> )   // returns a promise

file File to verify. (string containing a file path or a file's hex64 private key or WIF. Can also accept a KeyPair object).

Send

Returns a promise to resolve a Receipt object containing a transaction to send the given amount or amounts from the from key to the to address, and, optionally, to publish the transaction on the blockchain.

opensig.send( <from>, <to>, <amount>, [fee], [publish] );  // returns a promise

from Private key or wif of the address to spend from. (string containing a hex64 private key, WIF or file. Can also accept a KeyPair object).

to Address to send to. (string containing a public key, hex64 private key, WIF or file. Can also accept a KeyPair object)

amount Amount to spend in the transaction. If an array is passed then a transaction output for each element will be created. (positive integer or array of positive integers)

fee Amount to include as the miner's fee in addition to the amount. Defaults to 10000 satoshis. (positive integer)

publish If true the transaction will be published on the blockchain. Defaults to false. (boolean)

Balance

Returns a promise to resolve the sum of unspent transaction outputs retrieved from the blockchain for the given public key.

opensig.balance( <key> )   // returns an integer

key Public key. (string containing a public key, hex64 private key, WIF or file. Can also accept a KeyPair object)

Get Key

Returns a promise to resolve a KeyPair object from the given private key, WIF or file.

opensig.getKey( <key> )   // returns a promise

key Private key. (string containing a hex64 private key, WIF or file)

Examples

Require opensig-lib

opensig = require('opensig-lib');

Create a new random private key and log its information to the console in various formats...

var key = opensig.create();
console.log( key.toString() );
console.log( key.toString("<full>") );
console.log( key.toString("<id>") );
console.log( key.toString("<pub>") );
console.log( key.toString("id: <id>, public key: <pub>, wif: <wif>, private key: <priv>") );
console.log( key.toString("compressed keys: <pubc> <wifc>") );
console.log( key.toString("uncompressed keys: <pubu> <wifu>") );

Send 100000 satoshis from another address to your new key using the WIF of the other address. Use the default miner's fee...

const myWellFundedWIF = "L1FpYdmkgXyRHQrMjy4ChBmJ4dbgJmr5Y1h5eX9LmsPWKBZBqkUg";
opensig.send( myWellFundedWIF, key, 100000, undefined, true )
    .then( function log(response){ console.log(response); } )
    .catch( function logError(err){ console.error(err.message); } );

Get the blockchain balance for the key...

opensig.balance( key )
    .then( function log(balance){ console.log(balance); } )
    .catch( function logError(err){ console.error(err.message); } );

Generate a transaction to sign my_file.doc, including publishing it to the blockchain, and log the resulting Receipt object or error to the console...

opensig.sign( "my_file.doc", key, true )
    .then( function log(receipt){ console.log(receipt); } )
    .catch( function logError(err){ console.error(err.message); } );

Verify my_file.doc and output the signatures to the console...

opensig.verify( "my_file.doc" )
    .then( function log(signatures){
        for( var i in signatures ){
            console.log( signatures[i].toString() );
        } } )
    .catch( function logError(err){ console.error(err.message); } );

Publish a transaction taken from a Receipt obtained from a previous call to send, logging the blockchain api response or error to the console...

opensig.publish( myReceipt.txnHex )
    .then( function log(response){ console.log(response); } )
    .catch( function logError(err){ console.error(err.message); } );

Get a KeyPair object from a file and output its public key...

opensig.getKey( "my_file.doc" )
    .then( function log(keyPair){ console.log( keyPair.publicKey ); } )
    .catch( function logError(err){ console.error(err.message); } );

Projects utilizing opensig-lib

If you have a project that you feel could be listed here, please ask for it!

Run the tests

$ npm test
$ npm run-script test-cov

Copyright

OpenSig (c) 2016 D N Potter

Released under the MIT license