You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.5 KiB

3 years ago
3 years ago
  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. MerkleTree compatible with version from [circomlib](https://github.com/iden3/circomlib).
  3. Adaptation of the merkletree from https://github.com/iden3/go-iden3-core/tree/v0.0.8 with several changes and more functionalities.
  4. ## Usage
  5. More 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).
  6. ```go
  7. import (
  8. "fmt"
  9. "math/big"
  10. "testing"
  11. "github.com/iden3/go-iden3-core/db"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. [...]
  15. func TestExampleMerkleTree(t *testing.T) {
  16. mt, err := NewMerkleTree(db.NewMemoryStorage(), 10)
  17. assert.Nil(t, err)
  18. key := big.NewInt(1)
  19. value := big.NewInt(2)
  20. err = mt.Add(key, value)
  21. assert.Nil(t, err)
  22. fmt.Println(mt.Root().String())
  23. v, err := mt.Get(key)
  24. asseert.Equal(t, value, v)
  25. value = big.NewInt(3)
  26. err = mt.Update(key, value)
  27. proof, err := mt.GenerateProof(key, nil)
  28. assert.Nil(t, err)
  29. assert.True(t, VerifyProof(mt.Root(), proof, key, value))
  30. err := mt.Delete(big.NewInt(1)) // delete the leaf of key=1
  31. assert.Nil(t, err)
  32. }
  33. ```