Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wealdtech/go-merkletree
https://github.com/wealdtech/go-merkletree
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/wealdtech/go-merkletree
- Owner: wealdtech
- License: apache-2.0
- Created: 2018-12-24T15:05:45.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-05-07T09:20:03.000Z (10 months ago)
- Last Synced: 2024-08-02T09:50:20.542Z (7 months ago)
- Language: Go
- Size: 142 KB
- Stars: 117
- Watchers: 4
- Forks: 33
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-merkletree
[](https://github.com/wealdtech/go-merkletree/releases/)
[](LICENSE)
[](https://godoc.org/github.com/wealdtech/go-merkletree)
[](https://travis-ci.org/wealdtech/go-merkletree)
[](https://codecov.io/github/wealdtech/go-merkletree)
[](https://goreportcard.com/report/github.com/wealdtech/go-merkletree)Go implementation of a Merkle tree.
## Table of Contents
- [Install](#install)
- [Usage](#usage)
- [Maintainers](#maintainers)
- [Contribute](#contribute)
- [License](#license)## Install
`go-merkletree` is a standard Go module which can be installed with:
```sh
go get github.com/wealdtech/go-merkletree/v2
```## 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](https://www.wealdtech.com/articles/understanding-merkle-pollards/) and [Understanding sparse Merkle multiproofs](https://www.wealdtech.com/articles/understanding-sparse-merkle-multiproofs/) for details.
This package can generate visualisations (in [DOT](https://graphviz.gitlab.io/_pages/doc/info/lang.html) format) for trees and proofs. Below is a tree visualisation:

and below is a proof visualisation with the value being proved in red, the intermediate branches in green and the root in blue:

### Example
```go
package mainimport (
merkletree "github.com/wealdtech/go-merkletree/v2"
)// Example using the Merkle tree to generate and verify proofs.
func main() {
// Data for the tree
data := [][]byte{
[]byte("Foo"),
[]byte("Bar"),
[]byte("Baz"),
}// Create the tree
tree, err := merkletree.NewTree(merkletree.WithData(data))
if err != nil {
panic(err)
}// Fetch the root hash of the tree
root := tree.Root()baz := data[2]
// Generate a proof for 'Baz'
proof, err := tree.GenerateProof(baz, 0)
if err != nil {
panic(err)
}// Verify the proof for 'Baz'
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](https://github.com/mcdee).
## Contribute
Contributions welcome. Please check out [the issues](https://github.com/wealdtech/go-merkletree/issues).
## License
[Apache-2.0](LICENSE) © 2019 Weald Technology Trading Ltd