https://github.com/purehyperbole/merkletree
A merkle tree for golang
https://github.com/purehyperbole/merkletree
Last synced: about 1 year ago
JSON representation
A merkle tree for golang
- Host: GitHub
- URL: https://github.com/purehyperbole/merkletree
- Owner: purehyperbole
- License: mit
- Created: 2022-06-12T15:10:37.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-06-15T14:21:52.000Z (about 4 years ago)
- Last Synced: 2025-02-03T15:56:10.801Z (over 1 year ago)
- Language: Go
- Size: 10.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# merkletree
A thread safe merkle tree for golang
# Usage
```go
package main
import (
"crypto/sha256"
"fmt"
"github.com/purehyperbole/merkletree"
)
func main() {
m := merkletree.New(1024, func() hash.Hash {
return sha256.New()
})
// insert the hashes of some values into the tree
// the hash of the value will be returned
h1 := m.Insert([]byte("my data 1"))
h2 := m.Insert([]byte("my data 2"))
h3 := m.Insert([]byte("my data 3"))
// generate the merkle root hash. Note, this can only be called once
mrh := m.Root()
// generate a proof for 'my data 2' from the generated merkle tree
proof, err := m.Proof(h2)
if err != nil {
panic(err)
}
// validate the proof against the hash of the value and merkle root
err = merkletree.Validate(sha256.New(), h2, mrh, proof)
if err != nil {
panic(err)
}
}
```