Giter VIP home page Giter VIP logo

precise-proofs-js's Introduction

Precise Proofs Js

Read the introduction on Precise-Proofs

Precise Proofs Js is a library for validating fields/leafs from Merkle proofs created with precise-proofs. The library supports 2 hashing functions(SHA2_256SHA3_256) and hex or base base64 encoding for the hashes. Out of the box it uses hex encoding and SHA2_256.

In case you generate your proofs with SHA3_256 and have them base64 encoded. You can switch using the following code:

import {PreciseProofs, SHA3_256} from "precise-proofs-js";

const preciseProofs = new PreciseProofs('base64',SHA3_256);

Supported Proof format

// Sorted Hashes
{
    "property": "valueA",
    "value": "Example",
    "salt": "d555901541825e5d40612d220142c7428c385c48e0245fd3a40b8e3b64679be1",
    "sorted_hashes": [
        "60c3dfcdd85fcef1d4826d4695fb5064aa8434d014f2c1547f9398bdf72bdb75",
        "8951fa82c3f5563d88769a9ecb8f6e03b389c06ff64a1b26dc5898cefe95a42f",
        "46d0a998333fc2c8b4cfff1d5e122f7c32ff46f0a4410f63821f45cc02fd3723"
    ]
}


// Hex with our without the 0x prefix
{
    "property": "valueA",
    "value": "Example",
    "salt": "d555901541825e5d40612d220142c7428c385c48e0245fd3a40b8e3b64679be1",
    "hashes":[
        { "right": "60c3dfcdd85fcef1d4826d4695fb5064aa8434d014f2c1547f9398bdf72bdb75=" },
        { "left": "8951fa82c3f5563d88769a9ecb8f6e03b389c06ff64a1b26dc5898cefe95a42f" },
        { "right": "46d0a998333fc2c8b4cfff1d5e122f7c32ff46f0a4410f63821f45cc02fd3723" }
    ]
}


// Base64 Ecoded
{
    "property":"ValueA",
    "value":"Example",
    "salt":"1VWQFUGCXl1AYS0iAULHQow4XEjgJF/TpAuOO2Rnm+E=",
    "hashes":[
        { "right": "kYXAGhDdPiFMq1ZQMOZiKmSf1S1eHNgJ6BIPSIExOj8=" },
        { "left":" GDgT7Km6NK6k4N/Id4CZXErL3p6clNX7sVnlNyegdG0=" },
        { "right": "qOZzS+YM8t1OfC87zEKgkKz6q0f3wwk5+ed+PR/2cDA=" }
    ]
}

Usage:

import {PreciseProofs} from "precise-proofs";

const rootHash = "29dbffa68e5cd48bb4cb2504190f10883fb187793b2f70eab81fb5440c6809b6";
let proof = {
    "property": "valueA",
    "value": "Example",
    "salt": "d555901541825e5d40612d220142c7428c385c48e0245fd3a40b8e3b64679be1",
    "hashes": [
        "60c3dfcdd85fcef1d4826d4695fb5064aa8434d014f2c1547f9398bdf72bdb75",
        "8951fa82c3f5563d88769a9ecb8f6e03b389c06ff64a1b26dc5898cefe95a42f",
        "46d0a998333fc2c8b4cfff1d5e122f7c32ff46f0a4410f63821f45cc02fd3723"

    ]
}

const preciseProofs = new PreciseProofs();
const valid = preciseProofs.isValidField(proof, rootHash)
// valid  === true


// With base64 encoding
const base64RootHash = "Qpvx6jMpfee4eXYeDm+DO+Z8iArdnYdm3J3BH0AUxHU=";
const base64Proof = {
    "property": "value1",
    "value": "1",
    "salt": "Bs89XQyxE05c57R5KVb8aFk7rqf+A5XUgxlH3t1hRoY=",
    "hashes": [
    {
      "left": "bklbhC4cmgYc+w3k5an7PWICQHNZtozeqD16b7GE5ZI="
    },
    {
      "right": "5hFb4MZvIgE9RQnQDlOGGjahlgDIJwX7Id98oxFECbo="
    },
    {
      "right": "CT59ksUtUWOq+HagFoJgJziPOiHkHlvsvytA6TnTLq8="
    }
    ]
}

const base64PreciseProofs = new PreciseProofs('base64');

const base64Valid = base64PreciseProofs.isValidField(base64Proof, base64RootHash)
// base64Valid  === true

About Transformation Between Compact Property and Literal String of Proof

PropertyTransformHelper can be used to achieve this target First of all, you shoud install protobufjs to generate json definition from proto file.

pbjs example.proto -t json -p "/usr/local/include" -o examples.json

To transform between Compact Property and Literal String, you should provide generated json file and you should provide the package name and the message type defined in proto file.

import {PropertyTransformHelper} from "precise-proofs";
var jsonFormat = require('examples.json');
var helper = new PropertyTransformHelper(jsonFormat,'documents','ExampleDocument', '', []);
helper.compactPropertyToLiteral([3]);
helper.literalToPropertyCompact('value_bytes1')

Missing features

The following features are being worked on:

  • construct tree and generate proof

precise-proofs-js's People

Contributors

rdinicut avatar zyfrank avatar

Stargazers

 avatar

Watchers

Lucas Vogelsang avatar James Cloos avatar  avatar Miguel Hervas avatar  avatar  avatar

Forkers

rdinicut zyfrank

precise-proofs-js's Issues

Implement a helper to encode/decode human readable properties to compact properties

When implementing centrifuge/precise-proofs#29 we need to add support for these new properties. A major feature of precise-proofs is to render such proofs in a UI for human consumption. The compact properties wouldn't give you much insight into what the Merkle proof is for.

Therefore we should have a function that can take a .proto file (or some javascript version of it) and a compact property and translate it to a human readable property and vice versa.

The scope of this ticket is:

  1. A simple way to specify which protobuf message the proof is for
  2. Document the process of how to load protobuf files and convert them to the necessary format if possible
  3. Test coverage for the process
  4. Take the protobuf files from the example in the precise-proofs go library and implement an end to end test case showing validation of a proof generated by the precise-proofs go library.
  5. An example of how to use it in the README
  6. Updating the proof parser to the new format implemented in centrifuge/precise-proofs#29 (with compact & literal support)

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.