Merge PR #1575: ready-for-review: sdk.AccAddress natively bech32
This commit is contained in:
+164
-87
@@ -2,18 +2,15 @@ package types
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/tendermint/tendermint/crypto"
|
||||
"github.com/tendermint/tendermint/libs/bech32"
|
||||
cmn "github.com/tendermint/tendermint/libs/common"
|
||||
)
|
||||
|
||||
//Address is a go crypto-style Address
|
||||
type Address = cmn.HexBytes
|
||||
|
||||
// nolint
|
||||
// Bech32 prefixes
|
||||
const (
|
||||
// expected address length
|
||||
AddrLen = 20
|
||||
@@ -25,18 +22,172 @@ const (
|
||||
Bech32PrefixValPub = "cosmosvalpub"
|
||||
)
|
||||
|
||||
// Bech32ifyAcc takes Address and returns the bech32 encoded string
|
||||
func Bech32ifyAcc(addr Address) (string, error) {
|
||||
return bech32.ConvertAndEncode(Bech32PrefixAccAddr, addr.Bytes())
|
||||
//__________________________________________________________
|
||||
|
||||
// AccAddress a wrapper around bytes meant to represent an account address
|
||||
// When marshaled to a string or json, it uses bech32
|
||||
type AccAddress []byte
|
||||
|
||||
// create an AccAddress from a hex string
|
||||
func AccAddressFromHex(address string) (addr AccAddress, err error) {
|
||||
if len(address) == 0 {
|
||||
return addr, errors.New("decoding bech32 address failed: must provide an address")
|
||||
}
|
||||
bz, err := hex.DecodeString(address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return AccAddress(bz), nil
|
||||
}
|
||||
|
||||
// MustBech32ifyAcc panics on bech32-encoding failure
|
||||
func MustBech32ifyAcc(addr Address) string {
|
||||
enc, err := Bech32ifyAcc(addr)
|
||||
// create an AccAddress from a bech32 string
|
||||
func AccAddressFromBech32(address string) (addr AccAddress, err error) {
|
||||
bz, err := GetFromBech32(address, Bech32PrefixAccAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return AccAddress(bz), nil
|
||||
}
|
||||
|
||||
// Marshal needed for protobuf compatibility
|
||||
func (bz AccAddress) Marshal() ([]byte, error) {
|
||||
return bz, nil
|
||||
}
|
||||
|
||||
// Unmarshal needed for protobuf compatibility
|
||||
func (bz *AccAddress) Unmarshal(data []byte) error {
|
||||
*bz = data
|
||||
return nil
|
||||
}
|
||||
|
||||
// Marshals to JSON using Bech32
|
||||
func (bz AccAddress) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(bz.String())
|
||||
}
|
||||
|
||||
// Unmarshals from JSON assuming Bech32 encoding
|
||||
func (bz *AccAddress) UnmarshalJSON(data []byte) error {
|
||||
var s string
|
||||
err := json.Unmarshal(data, &s)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
bz2, err := AccAddressFromBech32(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*bz = bz2
|
||||
return nil
|
||||
}
|
||||
|
||||
// Allow it to fulfill various interfaces in light-client, etc...
|
||||
func (bz AccAddress) Bytes() []byte {
|
||||
return bz
|
||||
}
|
||||
|
||||
func (bz AccAddress) String() string {
|
||||
bech32Addr, err := bech32.ConvertAndEncode(Bech32PrefixAccAddr, bz.Bytes())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return enc
|
||||
return bech32Addr
|
||||
}
|
||||
|
||||
// For Printf / Sprintf, returns bech32 when using %s
|
||||
func (bz AccAddress) Format(s fmt.State, verb rune) {
|
||||
switch verb {
|
||||
case 's':
|
||||
s.Write([]byte(fmt.Sprintf("%s", bz.String())))
|
||||
case 'p':
|
||||
s.Write([]byte(fmt.Sprintf("%p", bz)))
|
||||
default:
|
||||
s.Write([]byte(fmt.Sprintf("%X", []byte(bz))))
|
||||
}
|
||||
}
|
||||
|
||||
//__________________________________________________________
|
||||
|
||||
// AccAddress a wrapper around bytes meant to represent a validator address
|
||||
// (from over ABCI). When marshaled to a string or json, it uses bech32
|
||||
type ValAddress []byte
|
||||
|
||||
// create a ValAddress from a hex string
|
||||
func ValAddressFromHex(address string) (addr ValAddress, err error) {
|
||||
if len(address) == 0 {
|
||||
return addr, errors.New("decoding bech32 address failed: must provide an address")
|
||||
}
|
||||
bz, err := hex.DecodeString(address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ValAddress(bz), nil
|
||||
}
|
||||
|
||||
// create a ValAddress from a bech32 string
|
||||
func ValAddressFromBech32(address string) (addr ValAddress, err error) {
|
||||
bz, err := GetFromBech32(address, Bech32PrefixValAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ValAddress(bz), nil
|
||||
}
|
||||
|
||||
// Marshal needed for protobuf compatibility
|
||||
func (bz ValAddress) Marshal() ([]byte, error) {
|
||||
return bz, nil
|
||||
}
|
||||
|
||||
// Unmarshal needed for protobuf compatibility
|
||||
func (bz *ValAddress) Unmarshal(data []byte) error {
|
||||
*bz = data
|
||||
return nil
|
||||
}
|
||||
|
||||
// Marshals to JSON using Bech32
|
||||
func (bz ValAddress) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(bz.String())
|
||||
}
|
||||
|
||||
// Unmarshals from JSON assuming Bech32 encoding
|
||||
func (bz *ValAddress) UnmarshalJSON(data []byte) error {
|
||||
var s string
|
||||
err := json.Unmarshal(data, &s)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
bz2, err := ValAddressFromBech32(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*bz = bz2
|
||||
return nil
|
||||
}
|
||||
|
||||
// Allow it to fulfill various interfaces in light-client, etc...
|
||||
func (bz ValAddress) Bytes() []byte {
|
||||
return bz
|
||||
}
|
||||
|
||||
func (bz ValAddress) String() string {
|
||||
bech32Addr, err := bech32.ConvertAndEncode(Bech32PrefixValAddr, bz.Bytes())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return bech32Addr
|
||||
}
|
||||
|
||||
// For Printf / Sprintf, returns bech32 when using %s
|
||||
func (bz ValAddress) Format(s fmt.State, verb rune) {
|
||||
switch verb {
|
||||
case 's':
|
||||
s.Write([]byte(fmt.Sprintf("%s", bz.String())))
|
||||
case 'p':
|
||||
s.Write([]byte(fmt.Sprintf("%p", bz)))
|
||||
default:
|
||||
s.Write([]byte(fmt.Sprintf("%X", []byte(bz))))
|
||||
}
|
||||
}
|
||||
|
||||
// Bech32ifyAccPub takes AccountPubKey and returns the bech32 encoded string
|
||||
@@ -53,26 +204,12 @@ func MustBech32ifyAccPub(pub crypto.PubKey) string {
|
||||
return enc
|
||||
}
|
||||
|
||||
// Bech32ifyVal returns the bech32 encoded string for a validator address
|
||||
func Bech32ifyVal(addr Address) (string, error) {
|
||||
return bech32.ConvertAndEncode(Bech32PrefixValAddr, addr.Bytes())
|
||||
}
|
||||
|
||||
// MustBech32ifyVal panics on bech32-encoding failure
|
||||
func MustBech32ifyVal(addr Address) string {
|
||||
enc, err := Bech32ifyVal(addr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return enc
|
||||
}
|
||||
|
||||
// Bech32ifyValPub returns the bech32 encoded string for a validator pubkey
|
||||
func Bech32ifyValPub(pub crypto.PubKey) (string, error) {
|
||||
return bech32.ConvertAndEncode(Bech32PrefixValPub, pub.Bytes())
|
||||
}
|
||||
|
||||
// MustBech32ifyValPub pancis on bech32-encoding failure
|
||||
// MustBech32ifyValPub panics on bech32-encoding failure
|
||||
func MustBech32ifyValPub(pub crypto.PubKey) string {
|
||||
enc, err := Bech32ifyValPub(pub)
|
||||
if err != nil {
|
||||
@@ -81,36 +218,6 @@ func MustBech32ifyValPub(pub crypto.PubKey) string {
|
||||
return enc
|
||||
}
|
||||
|
||||
// create an Address from a string
|
||||
func GetAccAddressHex(address string) (addr Address, err error) {
|
||||
if len(address) == 0 {
|
||||
return addr, errors.New("decoding bech32 address failed: must provide an address")
|
||||
}
|
||||
bz, err := hex.DecodeString(address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return Address(bz), nil
|
||||
}
|
||||
|
||||
// create an Address from a string
|
||||
func GetAccAddressBech32(address string) (addr Address, err error) {
|
||||
bz, err := GetFromBech32(address, Bech32PrefixAccAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return Address(bz), nil
|
||||
}
|
||||
|
||||
// create an Address from a string, panics on error
|
||||
func MustGetAccAddressBech32(address string) (addr Address) {
|
||||
addr, err := GetAccAddressBech32(address)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
// create a Pubkey from a string
|
||||
func GetAccPubKeyBech32(address string) (pk crypto.PubKey, err error) {
|
||||
bz, err := GetFromBech32(address, Bech32PrefixAccPub)
|
||||
@@ -135,36 +242,6 @@ func MustGetAccPubKeyBech32(address string) (pk crypto.PubKey) {
|
||||
return pk
|
||||
}
|
||||
|
||||
// create an Address from a hex string
|
||||
func GetValAddressHex(address string) (addr Address, err error) {
|
||||
if len(address) == 0 {
|
||||
return addr, errors.New("decoding bech32 address failed: must provide an address")
|
||||
}
|
||||
bz, err := hex.DecodeString(address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return Address(bz), nil
|
||||
}
|
||||
|
||||
// create an Address from a bech32 string
|
||||
func GetValAddressBech32(address string) (addr Address, err error) {
|
||||
bz, err := GetFromBech32(address, Bech32PrefixValAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return Address(bz), nil
|
||||
}
|
||||
|
||||
// create an Address from a string, panics on error
|
||||
func MustGetValAddressBech32(address string) (addr Address) {
|
||||
addr, err := GetValAddressBech32(address)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
// decode a validator public key into a PubKey
|
||||
func GetValPubKeyBech32(pubkey string) (pk crypto.PubKey, err error) {
|
||||
bz, err := GetFromBech32(pubkey, Bech32PrefixValPub)
|
||||
|
||||
+10
-10
@@ -35,7 +35,7 @@ type Validator interface {
|
||||
GetRevoked() bool // whether the validator is revoked
|
||||
GetMoniker() string // moniker of the validator
|
||||
GetStatus() BondStatus // status of the validator
|
||||
GetOwner() Address // owner address to receive/return validators coins
|
||||
GetOwner() AccAddress // owner AccAddress to receive/return validators coins
|
||||
GetPubKey() crypto.PubKey // validation pubkey
|
||||
GetPower() Rat // validation power
|
||||
GetDelegatorShares() Rat // Total out standing delegator shares
|
||||
@@ -52,16 +52,16 @@ func ABCIValidator(v Validator) abci.Validator {
|
||||
|
||||
// properties for the set of all validators
|
||||
type ValidatorSet interface {
|
||||
// iterate through validator by owner-address, execute func for each validator
|
||||
// iterate through validator by owner-AccAddress, execute func for each validator
|
||||
IterateValidators(Context,
|
||||
func(index int64, validator Validator) (stop bool))
|
||||
|
||||
// iterate through bonded validator by pubkey-address, execute func for each validator
|
||||
// iterate through bonded validator by pubkey-AccAddress, execute func for each validator
|
||||
IterateValidatorsBonded(Context,
|
||||
func(index int64, validator Validator) (stop bool))
|
||||
|
||||
Validator(Context, Address) Validator // get a particular validator by owner address
|
||||
TotalPower(Context) Rat // total power of the validator set
|
||||
Validator(Context, AccAddress) Validator // get a particular validator by owner AccAddress
|
||||
TotalPower(Context) Rat // total power of the validator set
|
||||
|
||||
// slash the validator and delegators of the validator, specifying offence height, offence power, and slash fraction
|
||||
Slash(Context, crypto.PubKey, int64, int64, Rat)
|
||||
@@ -73,17 +73,17 @@ type ValidatorSet interface {
|
||||
|
||||
// delegation bond for a delegated proof of stake system
|
||||
type Delegation interface {
|
||||
GetDelegator() Address // delegator address for the bond
|
||||
GetValidator() Address // validator owner address for the bond
|
||||
GetBondShares() Rat // amount of validator's shares
|
||||
GetDelegator() AccAddress // delegator AccAddress for the bond
|
||||
GetValidator() AccAddress // validator owner AccAddress for the bond
|
||||
GetBondShares() Rat // amount of validator's shares
|
||||
}
|
||||
|
||||
// properties for the set of all delegations for a particular
|
||||
type DelegationSet interface {
|
||||
GetValidatorSet() ValidatorSet // validator set for which delegation set is based upon
|
||||
|
||||
// iterate through all delegations from one delegator by validator-address,
|
||||
// iterate through all delegations from one delegator by validator-AccAddress,
|
||||
// execute func for each validator
|
||||
IterateDelegations(ctx Context, delegator Address,
|
||||
IterateDelegations(ctx Context, delegator AccAddress,
|
||||
fn func(index int64, delegation Delegation) (stop bool))
|
||||
}
|
||||
|
||||
+4
-4
@@ -21,7 +21,7 @@ type Msg interface {
|
||||
// Signers returns the addrs of signers that must sign.
|
||||
// CONTRACT: All signatures must be present to be valid.
|
||||
// CONTRACT: Returns addrs in some deterministic order.
|
||||
GetSigners() []Address
|
||||
GetSigners() []AccAddress
|
||||
}
|
||||
|
||||
//__________________________________________________________
|
||||
@@ -44,10 +44,10 @@ var _ Msg = (*TestMsg)(nil)
|
||||
|
||||
// msg type for testing
|
||||
type TestMsg struct {
|
||||
signers []Address
|
||||
signers []AccAddress
|
||||
}
|
||||
|
||||
func NewTestMsg(addrs ...Address) *TestMsg {
|
||||
func NewTestMsg(addrs ...AccAddress) *TestMsg {
|
||||
return &TestMsg{
|
||||
signers: addrs,
|
||||
}
|
||||
@@ -63,6 +63,6 @@ func (msg *TestMsg) GetSignBytes() []byte {
|
||||
return MustSortJSON(bz)
|
||||
}
|
||||
func (msg *TestMsg) ValidateBasic() Error { return nil }
|
||||
func (msg *TestMsg) GetSigners() []Address {
|
||||
func (msg *TestMsg) GetSigners() []AccAddress {
|
||||
return msg.signers
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user