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:
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
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
You can customize the encoder to use for field values:
type customEncoder struct{}
func (valueEncoder *customEncoder) encodeToString(value []byte) string {
return hex.EncodeToString(value)
}
doctree := NewDocumentTree(TreeOptions{ValueEncoder: &customEncoder{}})
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)