mirror of
https://github.com/arnaucube/go-merkletree-iden3.git
synced 2026-02-06 19:16:43 +01:00
Add Hash type hex parser
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/iden3/go-iden3-core/common"
|
"github.com/iden3/go-iden3-core/common"
|
||||||
@@ -120,6 +121,16 @@ func NewHashFromBytes(b []byte) (*Hash, error) {
|
|||||||
return &h, nil
|
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
|
// MerkleTree is the struct with the main elements of the MerkleTree
|
||||||
type MerkleTree struct {
|
type MerkleTree struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
|
|||||||
@@ -55,6 +55,12 @@ func TestHashParsers(t *testing.T) {
|
|||||||
b2, err := NewHashFromBytes(b.Bytes())
|
b2, err := NewHashFromBytes(b.Bytes())
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, b.String(), b2.BigInt().String())
|
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) {
|
func TestNewTree(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user