2018-08-06 12:00:49 -07:00
|
|
|
package types_test
|
|
|
|
|
|
|
|
import (
|
2020-03-04 09:49:59 -08:00
|
|
|
"bytes"
|
2018-08-06 12:00:49 -07:00
|
|
|
"encoding/hex"
|
2019-05-02 12:36:42 -07:00
|
|
|
"fmt"
|
2018-08-06 12:00:49 -07:00
|
|
|
"math/rand"
|
2018-10-31 12:13:13 -07:00
|
|
|
"strings"
|
2019-05-31 06:14:35 -07:00
|
|
|
"testing"
|
2018-10-31 12:13:13 -07:00
|
|
|
|
2019-02-08 12:45:23 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-09-27 14:51:14 -07:00
|
|
|
"github.com/stretchr/testify/suite"
|
2021-09-24 07:37:34 -07:00
|
|
|
"sigs.k8s.io/yaml"
|
2019-04-04 07:36:39 -07:00
|
|
|
|
2020-09-25 01:41:16 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
|
2020-08-28 09:02:38 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
2020-11-09 08:01:43 -08:00
|
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
2019-04-04 07:36:39 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types"
|
2021-03-25 07:53:22 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/bech32/legacybech32"
|
2018-10-03 08:48:23 -07:00
|
|
|
)
|
2018-08-06 12:00:49 -07:00
|
|
|
|
2020-09-28 03:46:49 -07:00
|
|
|
type addressTestSuite struct {
|
2020-09-27 14:51:14 -07:00
|
|
|
suite.Suite
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddressTestSuite(t *testing.T) {
|
2020-09-28 03:46:49 -07:00
|
|
|
suite.Run(t, new(addressTestSuite))
|
2020-09-27 14:51:14 -07:00
|
|
|
}
|
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *addressTestSuite) SetupSuite() {
|
|
|
|
s.T().Parallel()
|
|
|
|
}
|
|
|
|
|
2018-08-30 21:06:44 -07:00
|
|
|
var invalidStrs = []string{
|
2018-08-06 12:00:49 -07:00
|
|
|
"hello, world!",
|
|
|
|
"0xAA",
|
|
|
|
"AAA",
|
|
|
|
types.Bech32PrefixAccAddr + "AB0C",
|
|
|
|
types.Bech32PrefixAccPub + "1234",
|
|
|
|
types.Bech32PrefixValAddr + "5678",
|
|
|
|
types.Bech32PrefixValPub + "BBAB",
|
2018-08-30 21:06:44 -07:00
|
|
|
types.Bech32PrefixConsAddr + "FF04",
|
|
|
|
types.Bech32PrefixConsPub + "6789",
|
2018-08-06 12:00:49 -07:00
|
|
|
}
|
|
|
|
|
2020-09-28 03:46:49 -07:00
|
|
|
func (s *addressTestSuite) testMarshal(original interface{}, res interface{}, marshal func() ([]byte, error), unmarshal func([]byte) error) {
|
2018-08-06 12:00:49 -07:00
|
|
|
bz, err := marshal()
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().Nil(err)
|
|
|
|
s.Require().Nil(unmarshal(bz))
|
|
|
|
s.Require().Equal(original, res)
|
2018-08-06 12:00:49 -07:00
|
|
|
}
|
|
|
|
|
2020-09-28 03:46:49 -07:00
|
|
|
func (s *addressTestSuite) TestEmptyAddresses() {
|
2020-09-27 14:51:14 -07:00
|
|
|
s.T().Parallel()
|
|
|
|
s.Require().Equal((types.AccAddress{}).String(), "")
|
|
|
|
s.Require().Equal((types.ValAddress{}).String(), "")
|
|
|
|
s.Require().Equal((types.ConsAddress{}).String(), "")
|
2019-02-08 15:54:40 -08:00
|
|
|
|
|
|
|
accAddr, err := types.AccAddressFromBech32("")
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().True(accAddr.Empty())
|
|
|
|
s.Require().Error(err)
|
2019-02-08 15:54:40 -08:00
|
|
|
|
|
|
|
valAddr, err := types.ValAddressFromBech32("")
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().True(valAddr.Empty())
|
|
|
|
s.Require().Error(err)
|
2019-02-08 15:54:40 -08:00
|
|
|
|
|
|
|
consAddr, err := types.ConsAddressFromBech32("")
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().True(consAddr.Empty())
|
|
|
|
s.Require().Error(err)
|
2019-02-08 15:54:40 -08:00
|
|
|
}
|
|
|
|
|
2020-09-28 03:46:49 -07:00
|
|
|
func (s *addressTestSuite) TestYAMLMarshalers() {
|
2019-05-31 06:14:35 -07:00
|
|
|
addr := secp256k1.GenPrivKey().PubKey().Address()
|
|
|
|
|
|
|
|
acc := types.AccAddress(addr)
|
|
|
|
val := types.ValAddress(addr)
|
|
|
|
cons := types.ConsAddress(addr)
|
|
|
|
|
|
|
|
got, _ := yaml.Marshal(&acc)
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().Equal(acc.String()+"\n", string(got))
|
2019-05-31 06:14:35 -07:00
|
|
|
|
|
|
|
got, _ = yaml.Marshal(&val)
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().Equal(val.String()+"\n", string(got))
|
2019-05-31 06:14:35 -07:00
|
|
|
|
|
|
|
got, _ = yaml.Marshal(&cons)
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().Equal(cons.String()+"\n", string(got))
|
2019-05-31 06:14:35 -07:00
|
|
|
}
|
|
|
|
|
2020-09-28 03:46:49 -07:00
|
|
|
func (s *addressTestSuite) TestRandBech32AccAddrConsistency() {
|
2020-09-25 01:41:16 -07:00
|
|
|
pubBz := make([]byte, ed25519.PubKeySize)
|
|
|
|
pub := &ed25519.PubKey{Key: pubBz}
|
2018-08-06 12:00:49 -07:00
|
|
|
|
|
|
|
for i := 0; i < 1000; i++ {
|
2020-09-25 01:41:16 -07:00
|
|
|
rand.Read(pub.Key)
|
2018-08-06 12:00:49 -07:00
|
|
|
|
|
|
|
acc := types.AccAddress(pub.Address())
|
|
|
|
res := types.AccAddress{}
|
|
|
|
|
2020-09-27 14:51:14 -07:00
|
|
|
s.testMarshal(&acc, &res, acc.MarshalJSON, (&res).UnmarshalJSON)
|
|
|
|
s.testMarshal(&acc, &res, acc.Marshal, (&res).Unmarshal)
|
2018-08-06 12:00:49 -07:00
|
|
|
|
|
|
|
str := acc.String()
|
|
|
|
res, err := types.AccAddressFromBech32(str)
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().Nil(err)
|
|
|
|
s.Require().Equal(acc, res)
|
2018-08-06 12:00:49 -07:00
|
|
|
|
|
|
|
str = hex.EncodeToString(acc)
|
|
|
|
res, err = types.AccAddressFromHex(str)
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().Nil(err)
|
|
|
|
s.Require().Equal(acc, res)
|
2018-08-06 12:00:49 -07:00
|
|
|
}
|
|
|
|
|
2018-08-30 21:06:44 -07:00
|
|
|
for _, str := range invalidStrs {
|
2018-08-06 12:00:49 -07:00
|
|
|
_, err := types.AccAddressFromHex(str)
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().NotNil(err)
|
2018-08-06 12:00:49 -07:00
|
|
|
|
|
|
|
_, err = types.AccAddressFromBech32(str)
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().NotNil(err)
|
2018-08-06 12:00:49 -07:00
|
|
|
|
|
|
|
err = (*types.AccAddress)(nil).UnmarshalJSON([]byte("\"" + str + "\""))
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().NotNil(err)
|
2018-08-06 12:00:49 -07:00
|
|
|
}
|
2020-03-04 09:49:59 -08:00
|
|
|
|
|
|
|
_, err := types.AccAddressFromHex("")
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().Equal("decoding Bech32 address failed: must provide an address", err.Error())
|
2018-08-06 12:00:49 -07:00
|
|
|
}
|
|
|
|
|
2020-09-28 03:46:49 -07:00
|
|
|
func (s *addressTestSuite) TestValAddr() {
|
2020-09-25 01:41:16 -07:00
|
|
|
pubBz := make([]byte, ed25519.PubKeySize)
|
|
|
|
pub := &ed25519.PubKey{Key: pubBz}
|
2018-08-06 12:00:49 -07:00
|
|
|
|
|
|
|
for i := 0; i < 20; i++ {
|
2020-09-25 01:41:16 -07:00
|
|
|
rand.Read(pub.Key)
|
2018-08-06 12:00:49 -07:00
|
|
|
|
|
|
|
acc := types.ValAddress(pub.Address())
|
|
|
|
res := types.ValAddress{}
|
|
|
|
|
2020-09-27 14:51:14 -07:00
|
|
|
s.testMarshal(&acc, &res, acc.MarshalJSON, (&res).UnmarshalJSON)
|
|
|
|
s.testMarshal(&acc, &res, acc.Marshal, (&res).Unmarshal)
|
2018-08-06 12:00:49 -07:00
|
|
|
|
|
|
|
str := acc.String()
|
|
|
|
res, err := types.ValAddressFromBech32(str)
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().Nil(err)
|
|
|
|
s.Require().Equal(acc, res)
|
2018-08-06 12:00:49 -07:00
|
|
|
|
|
|
|
str = hex.EncodeToString(acc)
|
|
|
|
res, err = types.ValAddressFromHex(str)
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().Nil(err)
|
|
|
|
s.Require().Equal(acc, res)
|
2020-03-04 09:49:59 -08:00
|
|
|
|
2018-08-06 12:00:49 -07:00
|
|
|
}
|
|
|
|
|
2018-08-30 21:06:44 -07:00
|
|
|
for _, str := range invalidStrs {
|
2018-08-06 12:00:49 -07:00
|
|
|
_, err := types.ValAddressFromHex(str)
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().NotNil(err)
|
2018-08-06 12:00:49 -07:00
|
|
|
|
|
|
|
_, err = types.ValAddressFromBech32(str)
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().NotNil(err)
|
2018-08-06 12:00:49 -07:00
|
|
|
|
|
|
|
err = (*types.ValAddress)(nil).UnmarshalJSON([]byte("\"" + str + "\""))
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().NotNil(err)
|
2018-08-06 12:00:49 -07:00
|
|
|
}
|
2020-03-04 09:49:59 -08:00
|
|
|
|
|
|
|
// test empty string
|
|
|
|
_, err := types.ValAddressFromHex("")
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().Equal("decoding Bech32 address failed: must provide an address", err.Error())
|
2018-08-06 12:00:49 -07:00
|
|
|
}
|
2018-08-30 21:06:44 -07:00
|
|
|
|
2020-09-28 03:46:49 -07:00
|
|
|
func (s *addressTestSuite) TestConsAddress() {
|
2020-09-25 01:41:16 -07:00
|
|
|
pubBz := make([]byte, ed25519.PubKeySize)
|
|
|
|
pub := &ed25519.PubKey{Key: pubBz}
|
2018-08-30 21:06:44 -07:00
|
|
|
|
|
|
|
for i := 0; i < 20; i++ {
|
2020-09-25 01:41:16 -07:00
|
|
|
rand.Read(pub.Key[:])
|
2018-08-30 21:06:44 -07:00
|
|
|
|
|
|
|
acc := types.ConsAddress(pub.Address())
|
|
|
|
res := types.ConsAddress{}
|
|
|
|
|
2020-09-27 14:51:14 -07:00
|
|
|
s.testMarshal(&acc, &res, acc.MarshalJSON, (&res).UnmarshalJSON)
|
|
|
|
s.testMarshal(&acc, &res, acc.Marshal, (&res).Unmarshal)
|
2018-08-30 21:06:44 -07:00
|
|
|
|
|
|
|
str := acc.String()
|
|
|
|
res, err := types.ConsAddressFromBech32(str)
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().Nil(err)
|
|
|
|
s.Require().Equal(acc, res)
|
2018-08-30 21:06:44 -07:00
|
|
|
|
|
|
|
str = hex.EncodeToString(acc)
|
|
|
|
res, err = types.ConsAddressFromHex(str)
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().Nil(err)
|
|
|
|
s.Require().Equal(acc, res)
|
2018-08-30 21:06:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, str := range invalidStrs {
|
|
|
|
_, err := types.ConsAddressFromHex(str)
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().NotNil(err)
|
2018-08-30 21:06:44 -07:00
|
|
|
|
|
|
|
_, err = types.ConsAddressFromBech32(str)
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().NotNil(err)
|
2018-08-30 21:06:44 -07:00
|
|
|
|
|
|
|
err = (*types.ConsAddress)(nil).UnmarshalJSON([]byte("\"" + str + "\""))
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().NotNil(err)
|
2018-08-30 21:06:44 -07:00
|
|
|
}
|
2020-03-04 09:49:59 -08:00
|
|
|
|
|
|
|
// test empty string
|
|
|
|
_, err := types.ConsAddressFromHex("")
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().Equal("decoding Bech32 address failed: must provide an address", err.Error())
|
2018-08-30 21:06:44 -07:00
|
|
|
}
|
2018-10-31 12:13:13 -07:00
|
|
|
|
|
|
|
const letterBytes = "abcdefghijklmnopqrstuvwxyz"
|
|
|
|
|
|
|
|
func RandString(n int) string {
|
|
|
|
b := make([]byte, n)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
2020-09-28 03:46:49 -07:00
|
|
|
func (s *addressTestSuite) TestConfiguredPrefix() {
|
2020-09-25 01:41:16 -07:00
|
|
|
pubBz := make([]byte, ed25519.PubKeySize)
|
|
|
|
pub := &ed25519.PubKey{Key: pubBz}
|
2018-10-31 12:13:13 -07:00
|
|
|
for length := 1; length < 10; length++ {
|
|
|
|
for times := 1; times < 20; times++ {
|
2020-09-25 01:41:16 -07:00
|
|
|
rand.Read(pub.Key[:])
|
2018-10-31 12:13:13 -07:00
|
|
|
// Test if randomly generated prefix of a given length works
|
|
|
|
prefix := RandString(length)
|
2019-02-18 13:35:08 -08:00
|
|
|
|
2018-10-31 12:13:13 -07:00
|
|
|
// Assuming that GetConfig is not sealed.
|
|
|
|
config := types.GetConfig()
|
2019-02-18 13:35:08 -08:00
|
|
|
config.SetBech32PrefixForAccount(
|
|
|
|
prefix+types.PrefixAccount,
|
|
|
|
prefix+types.PrefixPublic)
|
|
|
|
|
2018-10-31 12:13:13 -07:00
|
|
|
acc := types.AccAddress(pub.Address())
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().True(strings.HasPrefix(
|
2019-02-18 13:35:08 -08:00
|
|
|
acc.String(),
|
|
|
|
prefix+types.PrefixAccount), acc.String())
|
|
|
|
|
2021-03-25 07:53:22 -07:00
|
|
|
bech32Pub := legacybech32.MustMarshalPubKey(legacybech32.AccPK, pub)
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().True(strings.HasPrefix(
|
2019-02-18 13:35:08 -08:00
|
|
|
bech32Pub,
|
|
|
|
prefix+types.PrefixPublic))
|
|
|
|
|
|
|
|
config.SetBech32PrefixForValidator(
|
|
|
|
prefix+types.PrefixValidator+types.PrefixAddress,
|
|
|
|
prefix+types.PrefixValidator+types.PrefixPublic)
|
2018-10-31 12:13:13 -07:00
|
|
|
|
|
|
|
val := types.ValAddress(pub.Address())
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().True(strings.HasPrefix(
|
2019-02-18 13:35:08 -08:00
|
|
|
val.String(),
|
|
|
|
prefix+types.PrefixValidator+types.PrefixAddress))
|
|
|
|
|
2021-03-25 07:53:22 -07:00
|
|
|
bech32ValPub := legacybech32.MustMarshalPubKey(legacybech32.ValPK, pub)
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().True(strings.HasPrefix(
|
2019-02-18 13:35:08 -08:00
|
|
|
bech32ValPub,
|
|
|
|
prefix+types.PrefixValidator+types.PrefixPublic))
|
|
|
|
|
|
|
|
config.SetBech32PrefixForConsensusNode(
|
|
|
|
prefix+types.PrefixConsensus+types.PrefixAddress,
|
|
|
|
prefix+types.PrefixConsensus+types.PrefixPublic)
|
2018-10-31 12:13:13 -07:00
|
|
|
|
|
|
|
cons := types.ConsAddress(pub.Address())
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().True(strings.HasPrefix(
|
2019-02-18 13:35:08 -08:00
|
|
|
cons.String(),
|
|
|
|
prefix+types.PrefixConsensus+types.PrefixAddress))
|
|
|
|
|
2021-03-25 07:53:22 -07:00
|
|
|
bech32ConsPub := legacybech32.MustMarshalPubKey(legacybech32.ConsPK, pub)
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().True(strings.HasPrefix(
|
2019-02-18 13:35:08 -08:00
|
|
|
bech32ConsPub,
|
|
|
|
prefix+types.PrefixConsensus+types.PrefixPublic))
|
2018-10-31 12:13:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-04 14:09:39 -08:00
|
|
|
|
2020-09-28 03:46:49 -07:00
|
|
|
func (s *addressTestSuite) TestAddressInterface() {
|
2020-09-25 01:41:16 -07:00
|
|
|
pubBz := make([]byte, ed25519.PubKeySize)
|
|
|
|
pub := &ed25519.PubKey{Key: pubBz}
|
|
|
|
rand.Read(pub.Key)
|
2019-01-04 14:09:39 -08:00
|
|
|
|
|
|
|
addrs := []types.Address{
|
|
|
|
types.ConsAddress(pub.Address()),
|
|
|
|
types.ValAddress(pub.Address()),
|
|
|
|
types.AccAddress(pub.Address()),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range addrs {
|
|
|
|
switch addr := addr.(type) {
|
|
|
|
case types.AccAddress:
|
|
|
|
_, err := types.AccAddressFromBech32(addr.String())
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().Nil(err)
|
2019-01-04 14:09:39 -08:00
|
|
|
case types.ValAddress:
|
|
|
|
_, err := types.ValAddressFromBech32(addr.String())
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().Nil(err)
|
2019-01-04 14:09:39 -08:00
|
|
|
case types.ConsAddress:
|
|
|
|
_, err := types.ConsAddressFromBech32(addr.String())
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().Nil(err)
|
2019-01-04 14:09:39 -08:00
|
|
|
default:
|
2020-09-27 14:51:14 -07:00
|
|
|
s.T().Fail()
|
2019-01-04 14:09:39 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-05-02 12:36:42 -07:00
|
|
|
|
2020-09-28 03:46:49 -07:00
|
|
|
func (s *addressTestSuite) TestVerifyAddressFormat() {
|
2020-08-12 06:03:28 -07:00
|
|
|
addr0 := make([]byte, 0)
|
|
|
|
addr5 := make([]byte, 5)
|
|
|
|
addr20 := make([]byte, 20)
|
|
|
|
addr32 := make([]byte, 32)
|
2021-02-01 05:17:44 -08:00
|
|
|
addr256 := make([]byte, 256)
|
2020-08-12 06:03:28 -07:00
|
|
|
|
|
|
|
err := types.VerifyAddressFormat(addr0)
|
2021-02-01 05:17:44 -08:00
|
|
|
s.Require().EqualError(err, "addresses cannot be empty: unknown address")
|
2020-08-12 06:03:28 -07:00
|
|
|
err = types.VerifyAddressFormat(addr5)
|
2021-02-01 05:17:44 -08:00
|
|
|
s.Require().NoError(err)
|
2020-08-12 06:03:28 -07:00
|
|
|
err = types.VerifyAddressFormat(addr20)
|
2021-02-01 05:17:44 -08:00
|
|
|
s.Require().NoError(err)
|
2020-08-12 06:03:28 -07:00
|
|
|
err = types.VerifyAddressFormat(addr32)
|
2021-02-01 05:17:44 -08:00
|
|
|
s.Require().NoError(err)
|
|
|
|
err = types.VerifyAddressFormat(addr256)
|
|
|
|
s.Require().EqualError(err, "address max length is 255, got 256: unknown address")
|
2020-08-12 06:03:28 -07:00
|
|
|
}
|
|
|
|
|
2020-09-28 03:46:49 -07:00
|
|
|
func (s *addressTestSuite) TestCustomAddressVerifier() {
|
2019-05-02 12:36:42 -07:00
|
|
|
// Create a 10 byte address
|
|
|
|
addr := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
|
|
|
accBech := types.AccAddress(addr).String()
|
|
|
|
valBech := types.ValAddress(addr).String()
|
|
|
|
consBech := types.ConsAddress(addr).String()
|
2021-02-01 05:17:44 -08:00
|
|
|
// Verify that the default logic doesn't reject this 10 byte address
|
|
|
|
// The default verifier is nil, we're only checking address length is
|
|
|
|
// between 1-255 bytes.
|
2019-05-02 12:36:42 -07:00
|
|
|
err := types.VerifyAddressFormat(addr)
|
2021-02-01 05:17:44 -08:00
|
|
|
s.Require().Nil(err)
|
2019-05-02 12:36:42 -07:00
|
|
|
_, err = types.AccAddressFromBech32(accBech)
|
2021-02-01 05:17:44 -08:00
|
|
|
s.Require().Nil(err)
|
2019-05-02 12:36:42 -07:00
|
|
|
_, err = types.ValAddressFromBech32(valBech)
|
2021-02-01 05:17:44 -08:00
|
|
|
s.Require().Nil(err)
|
2019-05-02 12:36:42 -07:00
|
|
|
_, err = types.ConsAddressFromBech32(consBech)
|
2021-02-01 05:17:44 -08:00
|
|
|
s.Require().Nil(err)
|
2019-05-02 12:36:42 -07:00
|
|
|
|
2021-02-01 05:17:44 -08:00
|
|
|
// Set a custom address verifier only accepts 20 byte addresses
|
2019-05-02 12:36:42 -07:00
|
|
|
types.GetConfig().SetAddressVerifier(func(bz []byte) error {
|
|
|
|
n := len(bz)
|
2021-02-01 05:17:44 -08:00
|
|
|
if n == 20 {
|
2019-05-02 12:36:42 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("incorrect address length %d", n)
|
|
|
|
})
|
|
|
|
|
2021-02-01 05:17:44 -08:00
|
|
|
// Verifiy that the custom logic rejects this 10 byte address
|
2019-05-02 12:36:42 -07:00
|
|
|
err = types.VerifyAddressFormat(addr)
|
2021-02-01 05:17:44 -08:00
|
|
|
s.Require().NotNil(err)
|
2019-05-02 12:36:42 -07:00
|
|
|
_, err = types.AccAddressFromBech32(accBech)
|
2021-02-01 05:17:44 -08:00
|
|
|
s.Require().NotNil(err)
|
2019-05-02 12:36:42 -07:00
|
|
|
_, err = types.ValAddressFromBech32(valBech)
|
2021-02-01 05:17:44 -08:00
|
|
|
s.Require().NotNil(err)
|
2019-05-02 12:36:42 -07:00
|
|
|
_, err = types.ConsAddressFromBech32(consBech)
|
2021-02-01 05:17:44 -08:00
|
|
|
s.Require().NotNil(err)
|
|
|
|
|
|
|
|
// Reinitialize the global config to default address verifier (nil)
|
|
|
|
types.GetConfig().SetAddressVerifier(nil)
|
2019-05-02 12:36:42 -07:00
|
|
|
}
|
2020-01-29 01:48:53 -08:00
|
|
|
|
2020-09-28 03:46:49 -07:00
|
|
|
func (s *addressTestSuite) TestBech32ifyAddressBytes() {
|
2020-01-29 01:48:53 -08:00
|
|
|
addr10byte := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
|
|
|
addr20byte := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
|
|
|
|
type args struct {
|
|
|
|
prefix string
|
|
|
|
bs []byte
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2021-09-17 08:50:40 -07:00
|
|
|
{"empty address", args{"prefixa", []byte{}}, "", false},
|
2020-01-29 01:48:53 -08:00
|
|
|
{"empty prefix", args{"", addr20byte}, "", true},
|
2021-09-17 08:50:40 -07:00
|
|
|
{"10-byte address", args{"prefixa", addr10byte}, "prefixa1qqqsyqcyq5rqwzqf3953cc", false},
|
|
|
|
{"10-byte address", args{"prefixb", addr10byte}, "prefixb1qqqsyqcyq5rqwzqf20xxpc", false},
|
|
|
|
{"20-byte address", args{"prefixa", addr20byte}, "prefixa1qqqsyqcyq5rqwzqfpg9scrgwpugpzysn7hzdtn", false},
|
|
|
|
{"20-byte address", args{"prefixb", addr20byte}, "prefixb1qqqsyqcyq5rqwzqfpg9scrgwpugpzysnrujsuw", false},
|
2020-01-29 01:48:53 -08:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2020-02-05 11:32:45 -08:00
|
|
|
tt := tt
|
2020-09-27 14:51:14 -07:00
|
|
|
s.T().Run(tt.name, func(t *testing.T) {
|
2020-01-29 01:48:53 -08:00
|
|
|
got, err := types.Bech32ifyAddressBytes(tt.args.prefix, tt.args.bs)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("Bech32ifyBytes() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.Equal(t, tt.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 03:46:49 -07:00
|
|
|
func (s *addressTestSuite) TestMustBech32ifyAddressBytes() {
|
2020-01-29 01:48:53 -08:00
|
|
|
addr10byte := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
|
|
|
addr20byte := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
|
|
|
|
type args struct {
|
|
|
|
prefix string
|
|
|
|
bs []byte
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
wantPanic bool
|
|
|
|
}{
|
2021-09-17 08:50:40 -07:00
|
|
|
{"empty address", args{"prefixa", []byte{}}, "", false},
|
2020-01-29 01:48:53 -08:00
|
|
|
{"empty prefix", args{"", addr20byte}, "", true},
|
2021-09-17 08:50:40 -07:00
|
|
|
{"10-byte address", args{"prefixa", addr10byte}, "prefixa1qqqsyqcyq5rqwzqf3953cc", false},
|
|
|
|
{"10-byte address", args{"prefixb", addr10byte}, "prefixb1qqqsyqcyq5rqwzqf20xxpc", false},
|
|
|
|
{"20-byte address", args{"prefixa", addr20byte}, "prefixa1qqqsyqcyq5rqwzqfpg9scrgwpugpzysn7hzdtn", false},
|
|
|
|
{"20-byte address", args{"prefixb", addr20byte}, "prefixb1qqqsyqcyq5rqwzqfpg9scrgwpugpzysnrujsuw", false},
|
2020-01-29 01:48:53 -08:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2020-02-05 11:32:45 -08:00
|
|
|
tt := tt
|
2020-09-27 14:51:14 -07:00
|
|
|
s.T().Run(tt.name, func(t *testing.T) {
|
2020-01-29 01:48:53 -08:00
|
|
|
if tt.wantPanic {
|
|
|
|
require.Panics(t, func() { types.MustBech32ifyAddressBytes(tt.args.prefix, tt.args.bs) })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.Equal(t, tt.want, types.MustBech32ifyAddressBytes(tt.args.prefix, tt.args.bs))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 09:49:59 -08:00
|
|
|
|
2020-09-28 03:46:49 -07:00
|
|
|
func (s *addressTestSuite) TestAddressTypesEquals() {
|
2020-03-04 09:49:59 -08:00
|
|
|
addr1 := secp256k1.GenPrivKey().PubKey().Address()
|
|
|
|
accAddr1 := types.AccAddress(addr1)
|
|
|
|
consAddr1 := types.ConsAddress(addr1)
|
|
|
|
valAddr1 := types.ValAddress(addr1)
|
|
|
|
|
|
|
|
addr2 := secp256k1.GenPrivKey().PubKey().Address()
|
|
|
|
accAddr2 := types.AccAddress(addr2)
|
|
|
|
consAddr2 := types.ConsAddress(addr2)
|
|
|
|
valAddr2 := types.ValAddress(addr2)
|
|
|
|
|
|
|
|
// equality
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().True(accAddr1.Equals(accAddr1))
|
|
|
|
s.Require().True(consAddr1.Equals(consAddr1))
|
|
|
|
s.Require().True(valAddr1.Equals(valAddr1))
|
2020-03-04 09:49:59 -08:00
|
|
|
|
|
|
|
// emptiness
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().True(types.AccAddress{}.Equals(types.AccAddress{}))
|
|
|
|
s.Require().True(types.AccAddress{}.Equals(types.AccAddress(nil)))
|
|
|
|
s.Require().True(types.AccAddress(nil).Equals(types.AccAddress{}))
|
|
|
|
s.Require().True(types.AccAddress(nil).Equals(types.AccAddress(nil)))
|
|
|
|
|
|
|
|
s.Require().True(types.ConsAddress{}.Equals(types.ConsAddress{}))
|
|
|
|
s.Require().True(types.ConsAddress{}.Equals(types.ConsAddress(nil)))
|
|
|
|
s.Require().True(types.ConsAddress(nil).Equals(types.ConsAddress{}))
|
|
|
|
s.Require().True(types.ConsAddress(nil).Equals(types.ConsAddress(nil)))
|
|
|
|
|
|
|
|
s.Require().True(types.ValAddress{}.Equals(types.ValAddress{}))
|
|
|
|
s.Require().True(types.ValAddress{}.Equals(types.ValAddress(nil)))
|
|
|
|
s.Require().True(types.ValAddress(nil).Equals(types.ValAddress{}))
|
|
|
|
s.Require().True(types.ValAddress(nil).Equals(types.ValAddress(nil)))
|
|
|
|
|
|
|
|
s.Require().False(accAddr1.Equals(accAddr2))
|
|
|
|
s.Require().Equal(accAddr1.Equals(accAddr2), accAddr2.Equals(accAddr1))
|
|
|
|
s.Require().False(consAddr1.Equals(consAddr2))
|
|
|
|
s.Require().Equal(consAddr1.Equals(consAddr2), consAddr2.Equals(consAddr1))
|
|
|
|
s.Require().False(valAddr1.Equals(valAddr2))
|
|
|
|
s.Require().Equal(valAddr1.Equals(valAddr2), valAddr2.Equals(valAddr1))
|
2020-03-04 09:49:59 -08:00
|
|
|
}
|
|
|
|
|
2020-09-28 03:46:49 -07:00
|
|
|
func (s *addressTestSuite) TestNilAddressTypesEmpty() {
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().True(types.AccAddress(nil).Empty())
|
|
|
|
s.Require().True(types.ConsAddress(nil).Empty())
|
|
|
|
s.Require().True(types.ValAddress(nil).Empty())
|
2020-03-04 09:49:59 -08:00
|
|
|
}
|
|
|
|
|
2020-09-28 03:46:49 -07:00
|
|
|
func (s *addressTestSuite) TestGetConsAddress() {
|
2020-03-04 09:49:59 -08:00
|
|
|
pk := secp256k1.GenPrivKey().PubKey()
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().NotEqual(types.GetConsAddress(pk), pk.Address())
|
|
|
|
s.Require().True(bytes.Equal(types.GetConsAddress(pk).Bytes(), pk.Address().Bytes()))
|
2020-11-09 08:01:43 -08:00
|
|
|
s.Require().Panics(func() { types.GetConsAddress(cryptotypes.PubKey(nil)) })
|
2020-03-04 09:49:59 -08:00
|
|
|
}
|
|
|
|
|
2020-09-28 03:46:49 -07:00
|
|
|
func (s *addressTestSuite) TestGetFromBech32() {
|
2020-03-04 09:49:59 -08:00
|
|
|
_, err := types.GetFromBech32("", "prefix")
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().Error(err)
|
2021-03-25 07:53:22 -07:00
|
|
|
s.Require().Equal("decoding Bech32 address failed: must provide a non empty address", err.Error())
|
2020-03-04 09:49:59 -08:00
|
|
|
_, err = types.GetFromBech32("cosmos1qqqsyqcyq5rqwzqfys8f67", "x")
|
2020-09-27 14:51:14 -07:00
|
|
|
s.Require().Error(err)
|
|
|
|
s.Require().Equal("invalid Bech32 prefix; expected x, got cosmos", err.Error())
|
2020-03-04 09:49:59 -08:00
|
|
|
}
|