go-merkletree
![Go Report Card](https://goreportcard.com/badge/github.com/wealdtech/go-merkletree)
Go implementation of a Merkle tree.
Table of Contents
Install
go-merkletree
is a standard Go module which can be installed with:
go get github.com/wealdtech/go-merkletree
Usage
go-merkletree
generates Merkle trees from an array of []byte
values and uses them to generate proofs. Proofs can be verified, and graphs generated
This package uses pollards and sparese multiproofs for efficient generation of multiple proofs against the same tree; see the articles Understanding Merkle pollards and Understanding sparse Merkle multiproofs for details.
This package can generate visualisations (in DOT format) for trees and proofs. Below is a tree visualisation:
![Merkle tree](https://github.com/desperatee/go-merkletree/raw/HEAD/images/tree.svg)
and below is a proof visualisation with the value being proved in red, the intermediate branches in green and the root in blue:
![Merkle proof](https://github.com/desperatee/go-merkletree/raw/HEAD/images/proof.svg)
Example
package main
import (
merkletree "github.com/wealdtech/go-merkletree"
)
func main() {
data := [][]byte{
[]byte("Foo"),
[]byte("Bar"),
[]byte("Baz"),
}
tree, err := merkletree.New(data)
if err != nil {
panic(err)
}
root := tree.Root()
baz := data[2]
proof, err := tree.GenerateProof(baz, 0)
if err != nil {
panic(err)
}
verified, err := merkletree.VerifyProof(baz, false, proof, [][]byte{root})
if err != nil {
panic(err)
}
if !verified {
panic("failed to verify proof for Baz")
}
}
Maintainers
Jim McDonald: @mcdee.
Contribute
Contributions welcome. Please check out the issues.
License
Apache-2.0 © 2019 Weald Technology Trading Ltd