Skip to content

Commit

Permalink
fix getting SchemaHash from int for short ints (#462)
Browse files Browse the repository at this point in the history
  • Loading branch information
olomix authored Apr 18, 2024
1 parent 482d12f commit 51634fe
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func NewSchemaHashFromHex(s string) (SchemaHash, error) {
func NewSchemaHashFromInt(i *big.Int) SchemaHash {
var sh SchemaHash
b := intToBytes(i)
copy(sh[len(sh)-len(b):], b)
copy(sh[:], b)

return sh
}
Expand Down
34 changes: 34 additions & 0 deletions claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -697,3 +697,37 @@ func TestNewClaimFromBigInts(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "2qNkLwz97jdzZBkdJYwVdXyGARGfVSGLJxELVC9ceH", userID.String())
}

func TestNewSchemaHashFromInt(t *testing.T) {
testCases := []struct {
title string
input string
want string
}{
{
title: "very long int",
input: "3044275373607097841737703342662468617701234",
want: "109319132382347594354104577870198015858",
},
{
title: "16-bytes int",
input: "304427537360709784173770334266246861770",
want: "304427537360709784173770334266246861770",
},
{
title: "15-bytes int",
input: "1161257907061317498916776531476452695",
want: "1161257907061317498916776531476452695",
},
}

for i := range testCases {
tc := testCases[i]
t.Run(tc.title, func(t *testing.T) {
bi, ok := new(big.Int).SetString(tc.input, 10)
require.True(t, ok)
sc := NewSchemaHashFromInt(bi)
require.Equal(t, tc.want, bytesToInt(sc[:]).Text(10))
})
}
}

0 comments on commit 51634fe

Please sign in to comment.