feat(types): add MustValAddressFromBech32 function (#18768)

Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
This commit is contained in:
Luke 2023-12-17 16:18:44 +08:00 committed by GitHub
parent 4ba0861b11
commit 98c6b1d712
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 0 deletions

View File

@ -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

View File

@ -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() {

View File

@ -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)
}