Skip to content

Commit

Permalink
add NewClaimFromBigInts constructor (#458)
Browse files Browse the repository at this point in the history
Co-authored-by: vmidyllic <74898029+vmidyllic@users.noreply.github.com>
  • Loading branch information
ilya-korotya and vmidyllic authored Dec 13, 2023
1 parent f4fd2e6 commit 28c3feb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
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) {
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())
}

0 comments on commit 28c3feb

Please sign in to comment.