arnaucube
arnaucube/go-merkletree-iden3Public Self-hosted repository
Add Hash type hex parser
arnaucube root@arnaucube.com
Sat, 19 Sep 2020 11:57:36 +0200
commit

fb20b52185aaa4aedcb1057d031f018494fabf1b

parent

eeb949f8c334abb6a48d2c448399c02a14994d11

2 files changed, 17 insertions(+), 0 deletions(-)

jump to
M merkletree.go → merkletree.go

@@ -7,6 +7,7 @@ "errors"

"fmt" "io" "math/big" + "strings" "sync" "github.com/iden3/go-iden3-core/common"

@@ -118,6 +119,16 @@ }

var h Hash copy(h[:], common.SwapEndianness(b)) 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
M merkletree_test.go → merkletree_test.go

@@ -55,6 +55,12 @@

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) {