Skip to content

Commit

Permalink
Feature/schema big int (#424)
Browse files Browse the repository at this point in the history
NewSchemaHashFromInt func added

SchemaHash BigInt

Fix Set/Get schema from claim
  • Loading branch information
demonsh authored Apr 22, 2022
1 parent 09de5cb commit 301fec8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
18 changes: 16 additions & 2 deletions claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
29 changes: 28 additions & 1 deletion claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)

}
6 changes: 0 additions & 6 deletions id.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 301fec8

Please sign in to comment.