From b51936b4b5064b163e5cf5a40af570a9475e5a27 Mon Sep 17 00:00:00 2001 From: Oleg Lomaka Date: Thu, 18 Apr 2024 11:23:56 -0400 Subject: [PATCH] fix getting SchemaHash from int for short ints --- claim.go | 2 +- claim_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/claim.go b/claim.go index b2f7e05..6ee4e53 100644 --- a/claim.go +++ b/claim.go @@ -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 } diff --git a/claim_test.go b/claim_test.go index bd33b27..1ee64cd 100644 --- a/claim_test.go +++ b/claim_test.go @@ -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)) + }) + } +}