Giter Club home page Giter Club logo

node-hasher-mtp's Introduction

node-hasher-mtp

This is a Node module for simple hashing and verifying inputs using the MTP (Merkle Tree Proof) proof-of-work algorithm as implemented by Zcoin.

This module has been developed and tested on Node v8.12 and Ubuntu 16.04

Usage

Hash

const mtp = require('hasher-mtp');

/**
 * Solve the hash problem. This function will try different nonce until it finds one such that the
 * computed hash is less than the `target` difficulty.
 *
 * @param  mtpInput   {Buffer}  80-byte header data to hash
 * @param  target     {Buffer}  32-byte target
 * @param  nonceStart {Buffer}  4-byte nonce to start with.
 * @param  nonceEnd   {Buffer}  4-byte nonce to stop at.
 *
 * @returns {boolean | {
 *     nonce: {Buffer},
 *     hashValue: {Buffer},
 *     hashRoot: {Buffer},
 *     block: {Buffer},
 *     proof: {Buffer}
 * }} False if a nonce was not found, otherwise an object containing nonce and MTP proofs
 * in Buffers.
 */
const result = mtp.hash(mtpInput, target, nonceStart, nonceEnd);
if (!result) {
    throw new Error('Failed to find a nonce that meets the target');
}

Verify

const mtp = require('hasher-mtp');
const hashValueOut = Buffer.alloc(32);

/**
 * Verify the given input and proofs are valid.
 *
 * This function verifies that the provided `nonce` does produce a hash value
 * that is less than `target`.
 *
 * @param mtpInput     {Buffer}  80-byte header that was hashed.
 * @param nonce        {Buffer}  4-byte nonce to check.
 * @param hashRoot     {Buffer}  16-byte MTP hash root used for verification.
 * @param block        {Buffer}  MTP block data used for verification.
 * @param proof        {Buffer}  NTP proof data used for verification.
 * @param hashValueOut {Buffer}  A 32-byte buffer to put the hash result into.
 *
 * @returns {boolean} True if verification is successful, otherwise false.
 */
const isValid = mtp.verify(mtpInput, nonce, hashRoot, block, proof, hashValueOut);

if (isValid) {
    console.log(hashValueOut.toString('hex'));
}
else {
    console.log('Invalid Proof');
}

Dependencies

The Boost library is used.

In Ubuntu:

   sudo apt-get install build-essential
   sudo apt-get install libboost-system-dev

node-hasher-mtp's People

Contributors

jcthepants avatar

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.