From ca306d57320f958b054a3248eb4b69aeb88195ab Mon Sep 17 00:00:00 2001 From: fynn-0xc Date: Mon, 27 Feb 2023 12:16:18 +0800 Subject: [PATCH] add Bytes/SetBytes for Bigendian encoding for Uint --- math/uint.go | 12 ++++++++++++ math/uint_test.go | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/math/uint.go b/math/uint.go index 406d9418ce..c5baf11135 100644 --- a/math/uint.go +++ b/math/uint.go @@ -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() diff --git a/math/uint_test.go b/math/uint_test.go index 428672cb2c..ceb3e87d72 100644 --- a/math/uint_test.go +++ b/math/uint_test.go @@ -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) + +}