diff --git a/claim.go b/claim.go index 9dfa28c..ddd8229 100644 --- a/claim.go +++ b/claim.go @@ -110,6 +110,20 @@ func NewSchemaHashFromHex(s string) (SchemaHash, error) { return schemaHash, nil } +// NewSchemaHashFromInt creates new SchemaHash from big.Int +func NewSchemaHashFromInt(i *big.Int) SchemaHash { + var schemaHash SchemaHash + b := intToBytes(i) + copy(schemaHash[len(schemaHash)-len(b):], b) + + return schemaHash +} + +// BigInt returns a bigInt presentation of SchemaHash +func (sc SchemaHash) BigInt() *big.Int { + return bytesToInt(sc[:]) +} + type Claim struct { index [4]ElemBytes value [4]ElemBytes @@ -300,13 +314,13 @@ func (c *Claim) HiHv() (*big.Int, *big.Int, error) { // SetSchemaHash updates claim's schema hash. func (c *Claim) SetSchemaHash(schema SchemaHash) { - copy(c.index[0][:schemaHashLn], utils.SwapEndianness(schema[:])) + copy(c.index[0][:schemaHashLn], schema[:]) } // GetSchemaHash return copy of claim's schema hash. func (c *Claim) GetSchemaHash() SchemaHash { var schemaHash SchemaHash - copy(schemaHash[:], utils.SwapEndianness(c.index[0][:schemaHashLn])) + copy(schemaHash[:], c.index[0][:schemaHashLn]) return schemaHash } diff --git a/claim_test.go b/claim_test.go index 92f6d79..95d8ad9 100644 --- a/claim_test.go +++ b/claim_test.go @@ -14,6 +14,7 @@ import ( "github.com/iden3/go-iden3-crypto/poseidon" "github.com/iden3/go-iden3-crypto/utils" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -76,7 +77,7 @@ func TestClaim_GetSchemaHash(t *testing.T) { claim, err := NewClaim(sc) require.NoError(t, err) require.True(t, - bytes.Equal(sc[:], utils.SwapEndianness(claim.index[0][:schemaHashLn]))) + bytes.Equal(sc[:], claim.index[0][:schemaHashLn])) shFromClaim := claim.GetSchemaHash() shFromClaimHexBytes, err := shFromClaim.MarshalText() @@ -378,3 +379,29 @@ func TestClaimBinarySerialization(t *testing.T) { require.Equal(t, binData, result) }) } + +func TestNewSchemaHashFromHex(t *testing.T) { + + hash := "ca938857241db9451ea329256b9c06e5" + got, err := NewSchemaHashFromHex(hash) + require.NoError(t, err) + + exp, err := hex.DecodeString(hash) + require.NoError(t, err) + + assert.Equal(t, exp[:], got[:]) + +} + +func TestSchemaHash_BigInt(t *testing.T) { + schema, err := NewSchemaHashFromHex("ca938857241db9451ea329256b9c06e5") + require.NoError(t, err) + + exp, b := new(big.Int).SetString("304427537360709784173770334266246861770", 10) + require.True(t, b) + + got := schema.BigInt() + + assert.Equal(t, exp, got) + +} diff --git a/id.go b/id.go index 1ff603b..7adef94 100644 --- a/id.go +++ b/id.go @@ -55,13 +55,7 @@ func (id *ID) Equal(id2 *ID) bool { return bytes.Equal(id[:], id2[:]) } -// func (id ID) MarshalJSON() ([]byte, error) { -// fmt.Println(id.String()) -// return json.Marshal(id.String()) -// } - func (id ID) MarshalText() ([]byte, error) { - // return json.Marshal(id.String()) return []byte(id.String()), nil }