Add Hash type hex parser
arnaucube root@arnaucube.com
Sat, 19 Sep 2020 11:57:36 +0200
2 files changed,
17 insertions(+),
0 deletions(-)
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) {