A library to generate merkle proofs for complex data structures
Branch: master
Clone or download

README.md

Precise Proofs

GoDoc Travis CI codecov

Read the introduction on Precise-Proofs

Precise-Proofs is a library for creating Merkle proofs out of protobuf messages. It handles flattening of objects, ordering the fields by label and creating shareable and independently verifiable proofs.

This library takes arbitrary protobuf messages and makes sure a Merkle tree can be reliable calculated from the values with each value representing a leaf in the tree.

{
    "Amount": "$1500",
    "InvoiceDate": "2018-03-01",
    "DueDate": "2018-08-01",
    "Country": "USA",
    "Supplier": "Arbor Tree Inc",
    "Buyer": "ACME Paper Inc",
    "Status": "APPROVED"
}

Above example would result in the following tree:

Merkle tree example

Why protobuf?

Google's protobuf is a space efficient and fast format to serialize data in a portable way. It's easy to generate JSON out of

Binary fields encoding

For []byte fields the default encoding used for tree and proof generation is Hexadecimal using https://godoc.org/github.com/ethereum/go-ethereum/common/hexutil

Usage:

See below code sample (examples/simple.go) for a usage example. For detailed usage, check godocs.

  // ExampleDocument is a protobuf message
	document := documentspb.ExampleDocument{
		Value1:      1,
		ValueA:      "Foo",
		ValueB:      "Bar",
		ValueBytes1: []byte("foobar"),
	}

  // You can also use compacts salts mapping generated in last session.
	salts := Salts{}
	doctree := NewDocumentTree(TreeOptions{Hash: sha256.New(), Salts: &salts})
	doctree.AddLeavesFromDocument(&document)
  // Here salts will contain generated salts for those fields whose salts are not provided in the slice

	doctree.Generate()
	fmt.Printf("Generated tree: %s\n", doctree.String())

	proof, _ := doctree.CreateProof("ValueA")
	proofJson, _ := json.Marshal(proof)
	fmt.Println("Proof:\n", string(proofJson))

	valid, _ := doctree.ValidateProof(&proof)

	fmt.Printf("Proof validated: %v\n", valid)

Development

Building protobuf messages

If the protobuf messages are ever changed, the recommended procedure is to build them using prototools. Use prototools version 1.3.0 and execute the command prototol generate to udpate both the example and the proofs messages.