arnaucube
arnaucube/go-merkletree-iden3Public Self-hosted repository
go-merkletree-iden3/README.md
README.mdRaw
 1# go-merkletree [![GoDoc](https://godoc.org/github.com/iden3/go-merkletree?status.svg)](https://godoc.org/github.com/iden3/go-merkletree) [![Go Report Card](https://goreportcard.com/badge/github.com/iden3/go-merkletree)](https://goreportcard.com/report/github.com/iden3/go-merkletree) [![Test](https://github.com/iden3/go-merkletree/workflows/Test/badge.svg)](https://github.com/iden3/go-merkletree/actions?query=workflow%3ATest)
 2
 3MerkleTree compatible with version from [circomlib](https://github.com/iden3/circomlib).
 4
 5Adaptation of the merkletree from https://github.com/iden3/go-iden3-core/tree/v0.0.8 with several changes and more functionalities.
 6
 7## Usage
 8More detailed examples can be found at the [tests](https://github.com/iden3/go-merkletree/blob/master/merkletree_test.go), and in the [documentation](https://godoc.org/github.com/iden3/go-merkletree).
 9
10```go
11import (
12	"fmt"
13	"math/big"
14	"testing"
15
16	"github.com/iden3/go-iden3-core/db"
17	"github.com/stretchr/testify/assert"
18)
19
20[...]
21
22func TestExampleMerkleTree(t *testing.T) {
23	mt, err := NewMerkleTree(db.NewMemoryStorage(), 10)
24	assert.Nil(t, err)
25
26	key := big.NewInt(1)
27	value := big.NewInt(2)
28	err = mt.Add(key, value)
29	assert.Nil(t, err)
30	fmt.Println(mt.Root().String())
31
32	v, err := mt.Get(key)
33	asseert.Equal(t, value, v)
34
35	value = big.NewInt(3)
36	err = mt.Update(key, value)
37
38	proof, err := mt.GenerateProof(key, nil)
39	assert.Nil(t, err)
40
41	assert.True(t, VerifyProof(mt.Root(), proof, key, value))
42
43	err := mt.Delete(big.NewInt(1)) // delete the leaf of key=1
44	assert.Nil(t, err)
45}
46```