Skip to content

Commit

Permalink
add Bytes/SetBytes for Bigendian encoding for Uint
Browse files Browse the repository at this point in the history
  • Loading branch information
fynn-0xc committed Feb 27, 2023
1 parent 0b61c55 commit 42c3a8e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
17 changes: 17 additions & 0 deletions math/uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,23 @@ 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()
}

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

// 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 @@ -361,3 +361,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 42c3a8e

Please sign in to comment.