Skip to content

Commit

Permalink
Merge PR cosmos#3159: Add address interface
Browse files Browse the repository at this point in the history
* Add address interface
* Add test for addresses
* Address comments
  • Loading branch information
jackzampolin authored and cwgoes committed Jan 4, 2019
1 parent 966baa5 commit 23819b1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ IMPROVEMENTS
* Gaia CLI (`gaiacli`)

* Gaia
* [\#2186](https://github.com/cosmos/cosmos-sdk/issues/2186) Add Address Interface
* [\#3158](https://github.com/cosmos/cosmos-sdk/pull/3158) Validate slashing genesis
* [\#3172](https://github.com/cosmos/cosmos-sdk/pull/3172) Support minimum fees
in a local testnet.
Expand Down
22 changes: 19 additions & 3 deletions types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ const (
Bech32PrefixConsPub = "cosmosvalconspub"
)

// Address is a common interface for different types of addresses used by the SDK
type Address interface {
Equals(Address) bool
Empty() bool
Marshal() ([]byte, error)
MarshalJSON() ([]byte, error)
Bytes() []byte
String() string
Format(s fmt.State, verb rune)
}

// Ensure that different address types implement the interface
var _ Address = AccAddress{}
var _ Address = ValAddress{}
var _ Address = ConsAddress{}

// ----------------------------------------------------------------------------
// account
// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -65,7 +81,7 @@ func AccAddressFromBech32(address string) (addr AccAddress, err error) {
}

// Returns boolean for whether two AccAddresses are Equal
func (aa AccAddress) Equals(aa2 AccAddress) bool {
func (aa AccAddress) Equals(aa2 Address) bool {
if aa.Empty() && aa2.Empty() {
return true
}
Expand Down Expand Up @@ -181,7 +197,7 @@ func ValAddressFromBech32(address string) (addr ValAddress, err error) {
}

// Returns boolean for whether two ValAddresses are Equal
func (va ValAddress) Equals(va2 ValAddress) bool {
func (va ValAddress) Equals(va2 Address) bool {
if va.Empty() && va2.Empty() {
return true
}
Expand Down Expand Up @@ -303,7 +319,7 @@ func GetConsAddress(pubkey crypto.PubKey) ConsAddress {
}

// Returns boolean for whether two ConsAddress are Equal
func (ca ConsAddress) Equals(ca2 ConsAddress) bool {
func (ca ConsAddress) Equals(ca2 Address) bool {
if ca.Empty() && ca2.Empty() {
return true
}
Expand Down
28 changes: 28 additions & 0 deletions types/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,31 @@ func TestConfiguredPrefix(t *testing.T) {

}
}

func TestAddressInterface(t *testing.T) {
var pub ed25519.PubKeyEd25519
rand.Read(pub[:])

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())
require.Nil(t, err)
case types.ValAddress:
_, err := types.ValAddressFromBech32(addr.String())
require.Nil(t, err)
case types.ConsAddress:
_, err := types.ConsAddressFromBech32(addr.String())
require.Nil(t, err)
default:
t.Fail()
}
}

}

0 comments on commit 23819b1

Please sign in to comment.