Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add NewClaimFromBigInts constructor #458

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@ type Claim struct {
value [4]ElemBytes
}

// NewClaimFromBigInts creates new Claim from bigInts.
func NewClaimFromBigInts(raw [8]*big.Int) (*Claim, error) {
var c Claim
for i := 0; i < 4; i++ {
err := c.index[i].SetInt(raw[i])
if err != nil {
return nil, err
}
err = c.value[i].SetInt(raw[i+4])
if err != nil {
return nil, err
}
}
return &c, nil
}

// subjectFlag for the time being describes the location of ID (in index or value
// slots or nowhere at all).
//
Expand Down
25 changes: 25 additions & 0 deletions claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,3 +672,28 @@ func TestClaim_SetMerklizedRoot(t *testing.T) {
_, err = claim3.GetMerklizedRoot()
require.Error(t, ErrNoMerklizedRoot, err)
}

func TestNewClaimFromBigInts(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to test the reverse flow - from the Claim to array of bigints

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decided to do proper validation in the next task

toBigInt := func(s string) *big.Int {
i, ok := new(big.Int).SetString(s, 10)
require.True(t, ok, s)
return i
}

c := [8]*big.Int{
toBigInt("3537648966163034177119037898189471968122"),
toBigInt("25999649578069426716666405683037372395789852830344252783677755850030715394"),
toBigInt("657065114158124047812701241180089030040156354062"),
toBigInt("1995762661"),
toBigInt("58718019808110235945021734914"),
toBigInt("0"),
toBigInt("0"),
toBigInt("0"),
}

claim, err := NewClaimFromBigInts(c)
require.NoError(t, err)
userID, err := claim.GetID()
require.NoError(t, err)
require.Equal(t, "2qNkLwz97jdzZBkdJYwVdXyGARGfVSGLJxELVC9ceH", userID.String())
}