diff --git a/CHANGELOG.md b/CHANGELOG.md index 5274e00ff7be..bba783f92b97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/auth/vesting) [#17810](https://github.com/cosmos/cosmos-sdk/pull/17810) Add the ability to specify a start time for continuous vesting accounts. * (runtime) [#18475](https://github.com/cosmos/cosmos-sdk/pull/18475) Adds an implementation for core.branch.Service. * (baseapp) [#18499](https://github.com/cosmos/cosmos-sdk/pull/18499) Add `MsgRouter` response type from message name function. +* (types) [#18768](https://github.com/cosmos/cosmos-sdk/pull/18768) Add MustValAddressFromBech32 function. ### Improvements diff --git a/types/address.go b/types/address.go index fce62a681564..eca97cee2973 100644 --- a/types/address.go +++ b/types/address.go @@ -362,6 +362,16 @@ func ValAddressFromBech32(address string) (addr ValAddress, err error) { return ValAddress(bz), nil } +// MustValAddressFromBech32 calls ValAddressFromBech32 and panics on error. +func MustValAddressFromBech32(address string) ValAddress { + addr, err := ValAddressFromBech32(address) + if err != nil { + panic(err) + } + + return addr +} + // Returns boolean for whether two ValAddresses are Equal func (va ValAddress) Equals(va2 Address) bool { if va.Empty() && va2.Empty() { diff --git a/types/address_test.go b/types/address_test.go index 53b06475079c..414751c7ea9d 100644 --- a/types/address_test.go +++ b/types/address_test.go @@ -567,3 +567,16 @@ func (s *addressTestSuite) TestGetFromBech32() { s.Require().Error(err) s.Require().Equal("invalid Bech32 prefix; expected x, got cosmos", err.Error()) } + +func (s *addressTestSuite) TestMustAccAddressFromBech32() { + bech32PrefixValAddr := types.GetConfig().GetBech32ValidatorAddrPrefix() + addr20byte := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19} + address := types.MustBech32ifyAddressBytes(bech32PrefixValAddr, addr20byte) + + valAddress1, err := types.ValAddressFromBech32(address) + s.Require().Nil(err) + + valAddress2 := types.MustValAddressFromBech32(address) + + s.Require().Equal(valAddress1, valAddress2) +}