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"),
}
// The FillSalts method is a helper function that fills all fields with 32
// random bytes. SaltedExampleDocument is a protobuf message that has the
// same structure as ExampleDocument but has all `bytes` field types.
salts := documentspb.SaltedExampleDocument{}
FillSalts(&document, &salts)
doctree := NewDocumentTree(TreeOptions{Hash: sha256.New()})
doctree.AddLeavesFromDocument(&document, &salts)
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)