Merge PR #2040: Refactor Validator Account Types/Bech32 Prefixing
* Add new account bech32 prefixes with godocs * Restructure spacing of existing account code * Update account godocs * More account godoc updates + new tm pub/addr helpers * Update validator type to use new account types/bech32 prefixes * Fix account documentation errors * Update Bech32 prefix for consensus nodes * Update Bech32 spec doc * Fix account type tests * Add missing account consensus functions, clear up godocs, and fix tests * Add to TestRandBech32PubkeyConsistency check * Update initialization of validator public keys * Update query signing info command * Implement new ConsAddress type with associated unit tests * [WIP] Update stake and slashing parameters * Update all calls to MustBech32ifyValPub * [WIP] Validator operator API updates * [WIP] Fix and update unit tests * Fix gov logs (helping to debug failing tests) * Fix gov tally * Fix all broken x/ unit tests * Update gaia app genesis address logic * Fix linting errors * Fix broken LCD tests * Fix broken CLI tests * Implement command to get validator address and pubkey from key name * Add support for getting validator key information via REST endpoint * Update PENDING log * Update docs * Revert GaiaGenTx.PubKey bech32 prefix * Fix broken docs and cli tests * Update genesis to use correct Bech32 (cons) prefix for pubkeys * Update docs and unit tests to reflect new cosmos account bech32 prefix * minor formatting
This commit is contained in:
committed by
Rigel
parent
dc0f13214d
commit
2d92803b9f
+312
-108
@@ -13,251 +13,422 @@ import (
|
||||
"github.com/tendermint/tendermint/libs/bech32"
|
||||
)
|
||||
|
||||
// Bech32 prefixes
|
||||
const (
|
||||
// expected address length
|
||||
// AddrLen defines a valid address length
|
||||
AddrLen = 20
|
||||
|
||||
// Bech32 prefixes
|
||||
Bech32PrefixAccAddr = "cosmosaccaddr"
|
||||
Bech32PrefixAccPub = "cosmosaccpub"
|
||||
Bech32PrefixValAddr = "cosmosvaladdr"
|
||||
Bech32PrefixValPub = "cosmosvalpub"
|
||||
// Bech32PrefixAccAddr defines the Bech32 prefix of an account's address
|
||||
Bech32PrefixAccAddr = "cosmos"
|
||||
// Bech32PrefixAccPub defines the Bech32 prefix of an account's public key
|
||||
Bech32PrefixAccPub = "cosmospub"
|
||||
// Bech32PrefixValAddr defines the Bech32 prefix of a validator's operator address
|
||||
Bech32PrefixValAddr = "cosmosval"
|
||||
// Bech32PrefixValPub defines the Bech32 prefix of a validator's operator public key
|
||||
Bech32PrefixValPub = "cosmosvalpub"
|
||||
// Bech32PrefixConsAddr defines the Bech32 prefix of a consensus node address
|
||||
Bech32PrefixConsAddr = "cosmoscons"
|
||||
// Bech32PrefixConsPub defines the Bech32 prefix of a consensus node public key
|
||||
Bech32PrefixConsPub = "cosmosconspub"
|
||||
)
|
||||
|
||||
//__________________________________________________________
|
||||
// ----------------------------------------------------------------------------
|
||||
// account
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// AccAddress a wrapper around bytes meant to represent an account address
|
||||
// When marshaled to a string or json, it uses bech32
|
||||
// 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
|
||||
// AccAddressFromHex creates 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")
|
||||
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
|
||||
}
|
||||
|
||||
// create an AccAddress from a bech32 string
|
||||
// AccAddressFromBech32 creates 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
|
||||
// Returns boolean for whether two AccAddresses are Equal
|
||||
func (aa AccAddress) Equals(aa2 AccAddress) bool {
|
||||
if aa.Empty() && aa2.Empty() {
|
||||
return true
|
||||
}
|
||||
|
||||
return bytes.Compare(aa.Bytes(), aa2.Bytes()) == 0
|
||||
}
|
||||
|
||||
// Unmarshal needed for protobuf compatibility
|
||||
func (bz *AccAddress) Unmarshal(data []byte) error {
|
||||
*bz = data
|
||||
// Returns boolean for whether an AccAddress is empty
|
||||
func (aa AccAddress) Empty() bool {
|
||||
if aa == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
aa2 := AccAddress{}
|
||||
return bytes.Compare(aa.Bytes(), aa2.Bytes()) == 0
|
||||
}
|
||||
|
||||
// Marshal returns the raw address bytes. It is needed for protobuf
|
||||
// compatibility.
|
||||
func (aa AccAddress) Marshal() ([]byte, error) {
|
||||
return aa, nil
|
||||
}
|
||||
|
||||
// Unmarshal sets the address to the given data. It is needed for protobuf
|
||||
// compatibility.
|
||||
func (aa *AccAddress) Unmarshal(data []byte) error {
|
||||
*aa = data
|
||||
return nil
|
||||
}
|
||||
|
||||
// Marshals to JSON using Bech32
|
||||
func (bz AccAddress) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(bz.String())
|
||||
// MarshalJSON marshals to JSON using Bech32.
|
||||
func (aa AccAddress) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(aa.String())
|
||||
}
|
||||
|
||||
// Unmarshals from JSON assuming Bech32 encoding
|
||||
func (bz *AccAddress) UnmarshalJSON(data []byte) error {
|
||||
// UnmarshalJSON unmarshals from JSON assuming Bech32 encoding.
|
||||
func (aa *AccAddress) UnmarshalJSON(data []byte) error {
|
||||
var s string
|
||||
err := json.Unmarshal(data, &s)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
bz2, err := AccAddressFromBech32(s)
|
||||
aa2, err := AccAddressFromBech32(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*bz = bz2
|
||||
|
||||
*aa = aa2
|
||||
return nil
|
||||
}
|
||||
|
||||
// Allow it to fulfill various interfaces in light-client, etc...
|
||||
func (bz AccAddress) Bytes() []byte {
|
||||
return bz
|
||||
// Bytes returns the raw address bytes.
|
||||
func (aa AccAddress) Bytes() []byte {
|
||||
return aa
|
||||
}
|
||||
|
||||
func (bz AccAddress) String() string {
|
||||
bech32Addr, err := bech32.ConvertAndEncode(Bech32PrefixAccAddr, bz.Bytes())
|
||||
// String implements the Stringer interface.
|
||||
func (aa AccAddress) String() string {
|
||||
bech32Addr, err := bech32.ConvertAndEncode(Bech32PrefixAccAddr, aa.Bytes())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return bech32Addr
|
||||
}
|
||||
|
||||
// For Printf / Sprintf, returns bech32 when using %s
|
||||
func (bz AccAddress) Format(s fmt.State, verb rune) {
|
||||
// Format implements the fmt.Formatter interface.
|
||||
func (aa AccAddress) Format(s fmt.State, verb rune) {
|
||||
switch verb {
|
||||
case 's':
|
||||
s.Write([]byte(fmt.Sprintf("%s", bz.String())))
|
||||
s.Write([]byte(fmt.Sprintf("%s", aa.String())))
|
||||
case 'p':
|
||||
s.Write([]byte(fmt.Sprintf("%p", bz)))
|
||||
s.Write([]byte(fmt.Sprintf("%p", aa)))
|
||||
default:
|
||||
s.Write([]byte(fmt.Sprintf("%X", []byte(bz))))
|
||||
s.Write([]byte(fmt.Sprintf("%X", []byte(aa))))
|
||||
}
|
||||
}
|
||||
|
||||
// Returns boolean for whether two AccAddresses are Equal
|
||||
func (bz AccAddress) Equals(bz2 AccAddress) bool {
|
||||
if bz.Empty() && bz2.Empty() {
|
||||
return true
|
||||
}
|
||||
return bytes.Compare(bz.Bytes(), bz2.Bytes()) == 0
|
||||
}
|
||||
// ----------------------------------------------------------------------------
|
||||
// validator owner
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// Returns boolean for whether an AccAddress is empty
|
||||
func (bz AccAddress) Empty() bool {
|
||||
if bz == nil {
|
||||
return true
|
||||
}
|
||||
bz2 := AccAddress{}
|
||||
return bytes.Compare(bz.Bytes(), bz2.Bytes()) == 0
|
||||
}
|
||||
|
||||
//__________________________________________________________
|
||||
|
||||
// AccAddress a wrapper around bytes meant to represent a validator address
|
||||
// (from over ABCI). When marshaled to a string or json, it uses bech32
|
||||
// ValAddress defines a wrapper around bytes meant to present a validator's
|
||||
// operator. When marshaled to a string or JSON, it uses Bech32.
|
||||
type ValAddress []byte
|
||||
|
||||
// create a ValAddress from a hex string
|
||||
// ValAddressFromHex creates 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")
|
||||
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
|
||||
// ValAddressFromBech32 creates 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
|
||||
// Returns boolean for whether two ValAddresses are Equal
|
||||
func (va ValAddress) Equals(va2 ValAddress) bool {
|
||||
if va.Empty() && va2.Empty() {
|
||||
return true
|
||||
}
|
||||
|
||||
return bytes.Compare(va.Bytes(), va2.Bytes()) == 0
|
||||
}
|
||||
|
||||
// Unmarshal needed for protobuf compatibility
|
||||
func (bz *ValAddress) Unmarshal(data []byte) error {
|
||||
*bz = data
|
||||
// Returns boolean for whether an AccAddress is empty
|
||||
func (va ValAddress) Empty() bool {
|
||||
if va == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
va2 := ValAddress{}
|
||||
return bytes.Compare(va.Bytes(), va2.Bytes()) == 0
|
||||
}
|
||||
|
||||
// Marshal returns the raw address bytes. It is needed for protobuf
|
||||
// compatibility.
|
||||
func (va ValAddress) Marshal() ([]byte, error) {
|
||||
return va, nil
|
||||
}
|
||||
|
||||
// Unmarshal sets the address to the given data. It is needed for protobuf
|
||||
// compatibility.
|
||||
func (va *ValAddress) Unmarshal(data []byte) error {
|
||||
*va = data
|
||||
return nil
|
||||
}
|
||||
|
||||
// Marshals to JSON using Bech32
|
||||
func (bz ValAddress) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(bz.String())
|
||||
// MarshalJSON marshals to JSON using Bech32.
|
||||
func (va ValAddress) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(va.String())
|
||||
}
|
||||
|
||||
// Unmarshals from JSON assuming Bech32 encoding
|
||||
func (bz *ValAddress) UnmarshalJSON(data []byte) error {
|
||||
// UnmarshalJSON unmarshals from JSON assuming Bech32 encoding.
|
||||
func (va *ValAddress) UnmarshalJSON(data []byte) error {
|
||||
var s string
|
||||
|
||||
err := json.Unmarshal(data, &s)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
bz2, err := ValAddressFromBech32(s)
|
||||
va2, err := ValAddressFromBech32(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*bz = bz2
|
||||
|
||||
*va = va2
|
||||
return nil
|
||||
}
|
||||
|
||||
// Allow it to fulfill various interfaces in light-client, etc...
|
||||
func (bz ValAddress) Bytes() []byte {
|
||||
return bz
|
||||
// Bytes returns the raw address bytes.
|
||||
func (va ValAddress) Bytes() []byte {
|
||||
return va
|
||||
}
|
||||
|
||||
func (bz ValAddress) String() string {
|
||||
bech32Addr, err := bech32.ConvertAndEncode(Bech32PrefixValAddr, bz.Bytes())
|
||||
// String implements the Stringer interface.
|
||||
func (va ValAddress) String() string {
|
||||
bech32Addr, err := bech32.ConvertAndEncode(Bech32PrefixValAddr, va.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) {
|
||||
// Format implements the fmt.Formatter interface.
|
||||
func (va ValAddress) Format(s fmt.State, verb rune) {
|
||||
switch verb {
|
||||
case 's':
|
||||
s.Write([]byte(fmt.Sprintf("%s", bz.String())))
|
||||
s.Write([]byte(fmt.Sprintf("%s", va.String())))
|
||||
case 'p':
|
||||
s.Write([]byte(fmt.Sprintf("%p", bz)))
|
||||
s.Write([]byte(fmt.Sprintf("%p", va)))
|
||||
default:
|
||||
s.Write([]byte(fmt.Sprintf("%X", []byte(bz))))
|
||||
s.Write([]byte(fmt.Sprintf("%X", []byte(va))))
|
||||
}
|
||||
}
|
||||
|
||||
// Returns boolean for whether two ValAddresses are Equal
|
||||
func (bz ValAddress) Equals(bz2 ValAddress) bool {
|
||||
if bz.Empty() && bz2.Empty() {
|
||||
// ----------------------------------------------------------------------------
|
||||
// consensus node
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// ConsAddress defines a wrapper around bytes meant to present a consensus node.
|
||||
// When marshaled to a string or JSON, it uses Bech32.
|
||||
type ConsAddress []byte
|
||||
|
||||
// ConsAddressFromHex creates a ConsAddress from a hex string.
|
||||
func ConsAddressFromHex(address string) (addr ConsAddress, 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 ConsAddress(bz), nil
|
||||
}
|
||||
|
||||
// ConsAddressFromBech32 creates a ConsAddress from a Bech32 string.
|
||||
func ConsAddressFromBech32(address string) (addr ConsAddress, err error) {
|
||||
bz, err := GetFromBech32(address, Bech32PrefixConsAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ConsAddress(bz), nil
|
||||
}
|
||||
|
||||
// Returns boolean for whether two ConsAddress are Equal
|
||||
func (ca ConsAddress) Equals(ca2 ConsAddress) bool {
|
||||
if ca.Empty() && ca2.Empty() {
|
||||
return true
|
||||
}
|
||||
return bytes.Compare(bz.Bytes(), bz2.Bytes()) == 0
|
||||
|
||||
return bytes.Compare(ca.Bytes(), ca2.Bytes()) == 0
|
||||
}
|
||||
|
||||
// Returns boolean for whether an AccAddress is empty
|
||||
func (bz ValAddress) Empty() bool {
|
||||
if bz == nil {
|
||||
// Returns boolean for whether an ConsAddress is empty
|
||||
func (ca ConsAddress) Empty() bool {
|
||||
if ca == nil {
|
||||
return true
|
||||
}
|
||||
bz2 := ValAddress{}
|
||||
return bytes.Compare(bz.Bytes(), bz2.Bytes()) == 0
|
||||
|
||||
ca2 := ConsAddress{}
|
||||
return bytes.Compare(ca.Bytes(), ca2.Bytes()) == 0
|
||||
}
|
||||
|
||||
// Bech32ifyAccPub takes AccountPubKey and returns the bech32 encoded string
|
||||
// Marshal returns the raw address bytes. It is needed for protobuf
|
||||
// compatibility.
|
||||
func (ca ConsAddress) Marshal() ([]byte, error) {
|
||||
return ca, nil
|
||||
}
|
||||
|
||||
// Unmarshal sets the address to the given data. It is needed for protobuf
|
||||
// compatibility.
|
||||
func (ca *ConsAddress) Unmarshal(data []byte) error {
|
||||
*ca = data
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON marshals to JSON using Bech32.
|
||||
func (ca ConsAddress) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(ca.String())
|
||||
}
|
||||
|
||||
// UnmarshalJSON unmarshals from JSON assuming Bech32 encoding.
|
||||
func (ca *ConsAddress) UnmarshalJSON(data []byte) error {
|
||||
var s string
|
||||
|
||||
err := json.Unmarshal(data, &s)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
ca2, err := ConsAddressFromBech32(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*ca = ca2
|
||||
return nil
|
||||
}
|
||||
|
||||
// Bytes returns the raw address bytes.
|
||||
func (ca ConsAddress) Bytes() []byte {
|
||||
return ca
|
||||
}
|
||||
|
||||
// String implements the Stringer interface.
|
||||
func (ca ConsAddress) String() string {
|
||||
bech32Addr, err := bech32.ConvertAndEncode(Bech32PrefixConsAddr, ca.Bytes())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return bech32Addr
|
||||
}
|
||||
|
||||
// Format implements the fmt.Formatter interface.
|
||||
func (ca ConsAddress) Format(s fmt.State, verb rune) {
|
||||
switch verb {
|
||||
case 's':
|
||||
s.Write([]byte(fmt.Sprintf("%s", ca.String())))
|
||||
case 'p':
|
||||
s.Write([]byte(fmt.Sprintf("%p", ca)))
|
||||
default:
|
||||
s.Write([]byte(fmt.Sprintf("%X", []byte(ca))))
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// auxiliary
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// Bech32ifyAccPub returns a Bech32 encoded string containing the
|
||||
// Bech32PrefixAccPub prefix for a given account PubKey.
|
||||
func Bech32ifyAccPub(pub crypto.PubKey) (string, error) {
|
||||
return bech32.ConvertAndEncode(Bech32PrefixAccPub, pub.Bytes())
|
||||
}
|
||||
|
||||
// MustBech32ifyAccPub panics on bech32-encoding failure
|
||||
// MustBech32ifyAccPub returns the result of Bech32ifyAccPub panicing on failure.
|
||||
func MustBech32ifyAccPub(pub crypto.PubKey) string {
|
||||
enc, err := Bech32ifyAccPub(pub)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return enc
|
||||
}
|
||||
|
||||
// Bech32ifyValPub returns the bech32 encoded string for a validator pubkey
|
||||
// Bech32ifyValPub returns a Bech32 encoded string containing the
|
||||
// Bech32PrefixValPub prefix for a given validator operator's PubKey.
|
||||
func Bech32ifyValPub(pub crypto.PubKey) (string, error) {
|
||||
return bech32.ConvertAndEncode(Bech32PrefixValPub, pub.Bytes())
|
||||
}
|
||||
|
||||
// MustBech32ifyValPub panics on bech32-encoding failure
|
||||
// MustBech32ifyValPub returns the result of Bech32ifyValPub panicing on failure.
|
||||
func MustBech32ifyValPub(pub crypto.PubKey) string {
|
||||
enc, err := Bech32ifyValPub(pub)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return enc
|
||||
}
|
||||
|
||||
// create a Pubkey from a string
|
||||
func GetAccPubKeyBech32(address string) (pk crypto.PubKey, err error) {
|
||||
bz, err := GetFromBech32(address, Bech32PrefixAccPub)
|
||||
// Bech32ifyConsPub returns a Bech32 encoded string containing the
|
||||
// Bech32PrefixConsPub prefixfor a given consensus node's PubKey.
|
||||
func Bech32ifyConsPub(pub crypto.PubKey) (string, error) {
|
||||
return bech32.ConvertAndEncode(Bech32PrefixConsPub, pub.Bytes())
|
||||
}
|
||||
|
||||
// MustBech32ifyConsPub returns the result of Bech32ifyConsPub panicing on
|
||||
// failure.
|
||||
func MustBech32ifyConsPub(pub crypto.PubKey) string {
|
||||
enc, err := Bech32ifyConsPub(pub)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return enc
|
||||
}
|
||||
|
||||
// GetAccPubKeyBech32 creates a PubKey for an account with a given public key
|
||||
// string using the Bech32 Bech32PrefixAccPub prefix.
|
||||
func GetAccPubKeyBech32(pubkey string) (pk crypto.PubKey, err error) {
|
||||
bz, err := GetFromBech32(pubkey, Bech32PrefixAccPub)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -270,16 +441,19 @@ func GetAccPubKeyBech32(address string) (pk crypto.PubKey, err error) {
|
||||
return pk, nil
|
||||
}
|
||||
|
||||
// create an Pubkey from a string, panics on error
|
||||
func MustGetAccPubKeyBech32(address string) (pk crypto.PubKey) {
|
||||
pk, err := GetAccPubKeyBech32(address)
|
||||
// MustGetAccPubKeyBech32 returns the result of GetAccPubKeyBech32 panicing on
|
||||
// failure.
|
||||
func MustGetAccPubKeyBech32(pubkey string) (pk crypto.PubKey) {
|
||||
pk, err := GetAccPubKeyBech32(pubkey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return pk
|
||||
}
|
||||
|
||||
// decode a validator public key into a PubKey
|
||||
// GetValPubKeyBech32 creates a PubKey for a validator's operator with a given
|
||||
// public key string using the Bech32 Bech32PrefixValPub prefix.
|
||||
func GetValPubKeyBech32(pubkey string) (pk crypto.PubKey, err error) {
|
||||
bz, err := GetFromBech32(pubkey, Bech32PrefixValPub)
|
||||
if err != nil {
|
||||
@@ -294,27 +468,57 @@ func GetValPubKeyBech32(pubkey string) (pk crypto.PubKey, err error) {
|
||||
return pk, nil
|
||||
}
|
||||
|
||||
// create an Pubkey from a string, panics on error
|
||||
func MustGetValPubKeyBech32(address string) (pk crypto.PubKey) {
|
||||
pk, err := GetValPubKeyBech32(address)
|
||||
// MustGetValPubKeyBech32 returns the result of GetValPubKeyBech32 panicing on
|
||||
// failure.
|
||||
func MustGetValPubKeyBech32(pubkey string) (pk crypto.PubKey) {
|
||||
pk, err := GetValPubKeyBech32(pubkey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return pk
|
||||
}
|
||||
|
||||
// decode a bytestring from a bech32-encoded string
|
||||
// GetConsPubKeyBech32 creates a PubKey for a consensus node with a given public
|
||||
// key string using the Bech32 Bech32PrefixConsPub prefix.
|
||||
func GetConsPubKeyBech32(pubkey string) (pk crypto.PubKey, err error) {
|
||||
bz, err := GetFromBech32(pubkey, Bech32PrefixConsPub)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pk, err = cryptoAmino.PubKeyFromBytes(bz)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return pk, nil
|
||||
}
|
||||
|
||||
// MustGetConsPubKeyBech32 returns the result of GetConsPubKeyBech32 panicing on
|
||||
// failure.
|
||||
func MustGetConsPubKeyBech32(pubkey string) (pk crypto.PubKey) {
|
||||
pk, err := GetConsPubKeyBech32(pubkey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return pk
|
||||
}
|
||||
|
||||
// GetFromBech32 decodes a bytestring from a Bech32 encoded string.
|
||||
func GetFromBech32(bech32str, prefix string) ([]byte, error) {
|
||||
if len(bech32str) == 0 {
|
||||
return nil, errors.New("decoding bech32 address failed: must provide an address")
|
||||
return nil, errors.New("decoding Bech32 address failed: must provide an address")
|
||||
}
|
||||
|
||||
hrp, bz, err := bech32.DecodeAndConvert(bech32str)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if hrp != prefix {
|
||||
return nil, fmt.Errorf("invalid bech32 prefix. Expected %s, Got %s", prefix, hrp)
|
||||
return nil, fmt.Errorf("invalid Bech32 prefix; expected %s, got %s", prefix, hrp)
|
||||
}
|
||||
|
||||
return bz, nil
|
||||
|
||||
+64
-16
@@ -12,7 +12,7 @@ import (
|
||||
"github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
var invalidstrs = []string{
|
||||
var invalidStrs = []string{
|
||||
"",
|
||||
"hello, world!",
|
||||
"0xAA",
|
||||
@@ -21,6 +21,8 @@ var invalidstrs = []string{
|
||||
types.Bech32PrefixAccPub + "1234",
|
||||
types.Bech32PrefixValAddr + "5678",
|
||||
types.Bech32PrefixValPub + "BBAB",
|
||||
types.Bech32PrefixConsAddr + "FF04",
|
||||
types.Bech32PrefixConsPub + "6789",
|
||||
}
|
||||
|
||||
func testMarshal(t *testing.T, original interface{}, res interface{}, marshal func() ([]byte, error), unmarshal func([]byte) error) {
|
||||
@@ -37,27 +39,38 @@ func TestRandBech32PubkeyConsistency(t *testing.T) {
|
||||
for i := 0; i < 1000; i++ {
|
||||
rand.Read(pub[:])
|
||||
|
||||
mustbech32accpub := types.MustBech32ifyAccPub(pub)
|
||||
bech32accpub, err := types.Bech32ifyAccPub(pub)
|
||||
mustBech32AccPub := types.MustBech32ifyAccPub(pub)
|
||||
bech32AccPub, err := types.Bech32ifyAccPub(pub)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, bech32accpub, mustbech32accpub)
|
||||
require.Equal(t, bech32AccPub, mustBech32AccPub)
|
||||
|
||||
mustbech32valpub := types.MustBech32ifyValPub(pub)
|
||||
bech32valpub, err := types.Bech32ifyValPub(pub)
|
||||
mustBech32ValPub := types.MustBech32ifyValPub(pub)
|
||||
bech32ValPub, err := types.Bech32ifyValPub(pub)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, bech32valpub, mustbech32valpub)
|
||||
require.Equal(t, bech32ValPub, mustBech32ValPub)
|
||||
|
||||
mustaccpub := types.MustGetAccPubKeyBech32(bech32accpub)
|
||||
accpub, err := types.GetAccPubKeyBech32(bech32accpub)
|
||||
mustBech32ConsPub := types.MustBech32ifyConsPub(pub)
|
||||
bech32ConsPub, err := types.Bech32ifyConsPub(pub)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, accpub, mustaccpub)
|
||||
require.Equal(t, bech32ConsPub, mustBech32ConsPub)
|
||||
|
||||
mustvalpub := types.MustGetValPubKeyBech32(bech32valpub)
|
||||
valpub, err := types.GetValPubKeyBech32(bech32valpub)
|
||||
mustAccPub := types.MustGetAccPubKeyBech32(bech32AccPub)
|
||||
accPub, err := types.GetAccPubKeyBech32(bech32AccPub)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, valpub, mustvalpub)
|
||||
require.Equal(t, accPub, mustAccPub)
|
||||
|
||||
require.Equal(t, valpub, accpub)
|
||||
mustValPub := types.MustGetValPubKeyBech32(bech32ValPub)
|
||||
valPub, err := types.GetValPubKeyBech32(bech32ValPub)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, valPub, mustValPub)
|
||||
|
||||
mustConsPub := types.MustGetConsPubKeyBech32(bech32ConsPub)
|
||||
consPub, err := types.GetConsPubKeyBech32(bech32ConsPub)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, consPub, mustConsPub)
|
||||
|
||||
require.Equal(t, valPub, accPub)
|
||||
require.Equal(t, valPub, consPub)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +97,7 @@ func TestRandBech32AccAddrConsistency(t *testing.T) {
|
||||
require.Equal(t, acc, res)
|
||||
}
|
||||
|
||||
for _, str := range invalidstrs {
|
||||
for _, str := range invalidStrs {
|
||||
_, err := types.AccAddressFromHex(str)
|
||||
require.NotNil(t, err)
|
||||
|
||||
@@ -119,7 +132,7 @@ func TestValAddr(t *testing.T) {
|
||||
require.Equal(t, acc, res)
|
||||
}
|
||||
|
||||
for _, str := range invalidstrs {
|
||||
for _, str := range invalidStrs {
|
||||
_, err := types.ValAddressFromHex(str)
|
||||
require.NotNil(t, err)
|
||||
|
||||
@@ -130,3 +143,38 @@ func TestValAddr(t *testing.T) {
|
||||
require.NotNil(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConsAddress(t *testing.T) {
|
||||
var pub ed25519.PubKeyEd25519
|
||||
|
||||
for i := 0; i < 20; i++ {
|
||||
rand.Read(pub[:])
|
||||
|
||||
acc := types.ConsAddress(pub.Address())
|
||||
res := types.ConsAddress{}
|
||||
|
||||
testMarshal(t, &acc, &res, acc.MarshalJSON, (&res).UnmarshalJSON)
|
||||
testMarshal(t, &acc, &res, acc.Marshal, (&res).Unmarshal)
|
||||
|
||||
str := acc.String()
|
||||
res, err := types.ConsAddressFromBech32(str)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, acc, res)
|
||||
|
||||
str = hex.EncodeToString(acc)
|
||||
res, err = types.ConsAddressFromHex(str)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, acc, res)
|
||||
}
|
||||
|
||||
for _, str := range invalidStrs {
|
||||
_, err := types.ConsAddressFromHex(str)
|
||||
require.NotNil(t, err)
|
||||
|
||||
_, err = types.ConsAddressFromBech32(str)
|
||||
require.NotNil(t, err)
|
||||
|
||||
err = (*types.ConsAddress)(nil).UnmarshalJSON([]byte("\"" + str + "\""))
|
||||
require.NotNil(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -40,7 +40,7 @@ type Validator interface {
|
||||
GetJailed() bool // whether the validator is jailed
|
||||
GetMoniker() string // moniker of the validator
|
||||
GetStatus() BondStatus // status of the validator
|
||||
GetOperator() AccAddress // owner AccAddress to receive/return validators coins
|
||||
GetOperator() ValAddress // owner address to receive/return validators coins
|
||||
GetPubKey() crypto.PubKey // validation pubkey
|
||||
GetPower() Dec // validation power
|
||||
GetTokens() Dec // validation tokens
|
||||
@@ -67,7 +67,7 @@ type ValidatorSet interface {
|
||||
IterateValidatorsBonded(Context,
|
||||
func(index int64, validator Validator) (stop bool))
|
||||
|
||||
Validator(Context, AccAddress) Validator // get a particular validator by owner AccAddress
|
||||
Validator(Context, ValAddress) Validator // get a particular validator by operator
|
||||
ValidatorByPubKey(Context, crypto.PubKey) Validator // get a particular validator by signing PubKey
|
||||
TotalPower(Context) Dec // total power of the validator set
|
||||
|
||||
@@ -82,7 +82,7 @@ type ValidatorSet interface {
|
||||
// delegation bond for a delegated proof of stake system
|
||||
type Delegation interface {
|
||||
GetDelegator() AccAddress // delegator AccAddress for the bond
|
||||
GetValidator() AccAddress // validator owner AccAddress for the bond
|
||||
GetValidator() ValAddress // validator operator address
|
||||
GetBondShares() Dec // amount of validator's shares
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user