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

feat: add Bytes/SetBytes for Bigendian encoding for Uint #185

Merged
merged 1 commit into from
Apr 19, 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
add Bytes/SetBytes for Bigendian encoding for Uint
  • Loading branch information
fynn-0xc authored and yutianwu committed Apr 19, 2023
commit ca306d57320f958b054a3248eb4b69aeb88195ab
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)

}