Browse Source

Add Hash type hex parser

fix/hash-parsers
arnaucube 3 years ago
parent
commit
fb20b52185
2 changed files with 17 additions and 0 deletions
  1. +11
    -0
      merkletree.go
  2. +6
    -0
      merkletree_test.go

+ 11
- 0
merkletree.go

@ -7,6 +7,7 @@ import (
"fmt"
"io"
"math/big"
"strings"
"sync"
"github.com/iden3/go-iden3-core/common"
@ -120,6 +121,16 @@ func NewHashFromBytes(b []byte) (*Hash, error) {
return &h, nil
}
// NewHashFromHex returns a *Hash representation of the given hex string
func NewHashFromHex(h string) (*Hash, error) {
h = strings.TrimPrefix(h, "0x")
b, err := hex.DecodeString(h)
if err != nil {
return nil, err
}
return NewHashFromBytes(b)
}
// MerkleTree is the struct with the main elements of the MerkleTree
type MerkleTree struct {
sync.RWMutex

+ 6
- 0
merkletree_test.go

@ -55,6 +55,12 @@ func TestHashParsers(t *testing.T) {
b2, err := NewHashFromBytes(b.Bytes())
assert.Nil(t, err)
assert.Equal(t, b.String(), b2.BigInt().String())
h2, err := NewHashFromHex(h.Hex())
assert.Nil(t, err)
assert.Equal(t, h, h2)
_, err = NewHashFromHex("0x12")
assert.NotNil(t, err)
}
func TestNewTree(t *testing.T) {

Loading…
Cancel
Save