Skip to content

Commit

Permalink
add Bytes/SetBytes for Bigendian encoding for Uint (#185)
Browse files Browse the repository at this point in the history
Co-authored-by: fynn-0xc <[email protected]>
  • Loading branch information
yutianwu and fynn-0xc authored Apr 19, 2023
1 parent c5adbb8 commit 88fc2fe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
12 changes: 12 additions & 0 deletions math/uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,18 @@ func (u *Uint) Unmarshal(data []byte) error {
return UintOverflow(u.i)
}

// Bytes returns the value of x as a big-endian byte slice.
func (u Uint) Bytes() []byte {
return u.i.Bytes()
}

// SetBytes interprets buf as the bytes of a big-endian unsigned
// integer, sets z to that value, and returns z.
func (u Uint) SetBytes(buf []byte) Uint {
u.i = u.i.SetBytes(buf)
return u
}

// Size implements the gogo proto custom type interface.
func (u *Uint) Size() int {
bz, _ := u.Marshal()
Expand Down
11 changes: 11 additions & 0 deletions math/uint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,14 @@ func TestWeakUnmarshalOverflow(t *testing.T) {
t.Fatalf("out of range value not reported, got instead %q", errStr)
}
}

func (s *uintTestSuite) TestUintBigEndian() {
u1 := sdkmath.NewUint(256)
u1b := u1.Bytes()

u2 := sdkmath.NewUint(0)
u2 = u2.SetBytes(u1b)

s.Require().Equal(u1, u2)

}

0 comments on commit 88fc2fe

Please sign in to comment.