Merge PR #5495: Replace Redundant Bech32 PubKey Functions

This commit is contained in:
Alexander Bezobchuk
2020-01-09 09:04:28 -05:00
committed by GitHub
parent 67c2acd75a
commit 869531ca92
31 changed files with 191 additions and 235 deletions
+53 -109
View File
@@ -9,7 +9,7 @@ import (
"strings"
"github.com/tendermint/tendermint/crypto"
cryptoAmino "github.com/tendermint/tendermint/crypto/encoding/amino"
tmamino "github.com/tendermint/tendermint/crypto/encoding/amino"
yaml "gopkg.in/yaml.v2"
"github.com/tendermint/tendermint/libs/bech32"
@@ -574,68 +574,69 @@ func (ca ConsAddress) Format(s fmt.State, verb rune) {
// auxiliary
// ----------------------------------------------------------------------------
// Bech32ifyAccPub returns a Bech32 encoded string containing the
// Bech32PrefixAccPub prefix for a given account PubKey.
func Bech32ifyAccPub(pub crypto.PubKey) (string, error) {
bech32PrefixAccPub := GetConfig().GetBech32AccountPubPrefix()
return bech32.ConvertAndEncode(bech32PrefixAccPub, pub.Bytes())
// Bech32PubKeyType defines a string type alias for a Bech32 public key type.
type Bech32PubKeyType string
// Bech32 conversion constants
const (
Bech32PubKeyTypeAccPub Bech32PubKeyType = "accpub"
Bech32PubKeyTypeValPub Bech32PubKeyType = "valpub"
Bech32PubKeyTypeConsPub Bech32PubKeyType = "conspub"
)
// Bech32ifyPubKey returns a Bech32 encoded string containing the appropriate
// prefix based on the key type provided for a given PublicKey.
func Bech32ifyPubKey(pkt Bech32PubKeyType, pubkey crypto.PubKey) (string, error) {
var bech32Prefix string
switch pkt {
case Bech32PubKeyTypeAccPub:
bech32Prefix = GetConfig().GetBech32AccountPubPrefix()
case Bech32PubKeyTypeValPub:
bech32Prefix = GetConfig().GetBech32ValidatorPubPrefix()
case Bech32PubKeyTypeConsPub:
bech32Prefix = GetConfig().GetBech32ConsensusPubPrefix()
}
return bech32.ConvertAndEncode(bech32Prefix, pubkey.Bytes())
}
// MustBech32ifyAccPub returns the result of Bech32ifyAccPub panicing on failure.
func MustBech32ifyAccPub(pub crypto.PubKey) string {
enc, err := Bech32ifyAccPub(pub)
// MustBech32ifyPubKey calls Bech32ifyPubKey except it panics on error.
func MustBech32ifyPubKey(pkt Bech32PubKeyType, pubkey crypto.PubKey) string {
res, err := Bech32ifyPubKey(pkt, pubkey)
if err != nil {
panic(err)
}
return enc
return res
}
// Bech32ifyValPub returns a Bech32 encoded string containing the
// Bech32PrefixValPub prefix for a given validator operator's PubKey.
func Bech32ifyValPub(pub crypto.PubKey) (string, error) {
bech32PrefixValPub := GetConfig().GetBech32ValidatorPubPrefix()
return bech32.ConvertAndEncode(bech32PrefixValPub, pub.Bytes())
}
// GetPubKeyFromBech32 returns a PublicKey from a bech32-encoded PublicKey with
// a given key type.
func GetPubKeyFromBech32(pkt Bech32PubKeyType, pubkeyStr string) (crypto.PubKey, error) {
var bech32Prefix string
switch pkt {
case Bech32PubKeyTypeAccPub:
bech32Prefix = GetConfig().GetBech32AccountPubPrefix()
case Bech32PubKeyTypeValPub:
bech32Prefix = GetConfig().GetBech32ValidatorPubPrefix()
case Bech32PubKeyTypeConsPub:
bech32Prefix = GetConfig().GetBech32ConsensusPubPrefix()
// 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
}
// Bech32ifyConsPub returns a Bech32 encoded string containing the
// Bech32PrefixConsPub prefixfor a given consensus node's PubKey.
func Bech32ifyConsPub(pub crypto.PubKey) (string, error) {
bech32PrefixConsPub := GetConfig().GetBech32ConsensusPubPrefix()
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) {
bech32PrefixAccPub := GetConfig().GetBech32AccountPubPrefix()
bz, err := GetFromBech32(pubkey, bech32PrefixAccPub)
bz, err := GetFromBech32(pubkeyStr, bech32Prefix)
if err != nil {
return nil, err
}
pk, err = cryptoAmino.PubKeyFromBytes(bz)
pk, err := tmamino.PubKeyFromBytes(bz)
if err != nil {
return nil, err
}
@@ -643,71 +644,14 @@ func GetAccPubKeyBech32(pubkey string) (pk crypto.PubKey, err error) {
return pk, nil
}
// MustGetAccPubKeyBech32 returns the result of GetAccPubKeyBech32 panicing on
// failure.
func MustGetAccPubKeyBech32(pubkey string) (pk crypto.PubKey) {
pk, err := GetAccPubKeyBech32(pubkey)
// MustGetPubKeyFromBech32 calls GetPubKeyFromBech32 except it panics on error.
func MustGetPubKeyFromBech32(pkt Bech32PubKeyType, pubkeyStr string) crypto.PubKey {
res, err := GetPubKeyFromBech32(pkt, pubkeyStr)
if err != nil {
panic(err)
}
return pk
}
// 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) {
bech32PrefixValPub := GetConfig().GetBech32ValidatorPubPrefix()
bz, err := GetFromBech32(pubkey, bech32PrefixValPub)
if err != nil {
return nil, err
}
pk, err = cryptoAmino.PubKeyFromBytes(bz)
if err != nil {
return nil, err
}
return pk, nil
}
// 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
}
// 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) {
bech32PrefixConsPub := GetConfig().GetBech32ConsensusPubPrefix()
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
return res
}
// GetFromBech32 decodes a bytestring from a Bech32 encoded string.
+15 -16
View File
@@ -60,33 +60,33 @@ 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.MustBech32ifyPubKey(types.Bech32PubKeyTypeAccPub, pub)
bech32AccPub, err := types.Bech32ifyPubKey(types.Bech32PubKeyTypeAccPub, pub)
require.Nil(t, err)
require.Equal(t, bech32AccPub, mustBech32AccPub)
mustBech32ValPub := types.MustBech32ifyValPub(pub)
bech32ValPub, err := types.Bech32ifyValPub(pub)
mustBech32ValPub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeValPub, pub)
bech32ValPub, err := types.Bech32ifyPubKey(types.Bech32PubKeyTypeValPub, pub)
require.Nil(t, err)
require.Equal(t, bech32ValPub, mustBech32ValPub)
mustBech32ConsPub := types.MustBech32ifyConsPub(pub)
bech32ConsPub, err := types.Bech32ifyConsPub(pub)
mustBech32ConsPub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeConsPub, pub)
bech32ConsPub, err := types.Bech32ifyPubKey(types.Bech32PubKeyTypeConsPub, pub)
require.Nil(t, err)
require.Equal(t, bech32ConsPub, mustBech32ConsPub)
mustAccPub := types.MustGetAccPubKeyBech32(bech32AccPub)
accPub, err := types.GetAccPubKeyBech32(bech32AccPub)
mustAccPub := types.MustGetPubKeyFromBech32(types.Bech32PubKeyTypeAccPub, bech32AccPub)
accPub, err := types.GetPubKeyFromBech32(types.Bech32PubKeyTypeAccPub, bech32AccPub)
require.Nil(t, err)
require.Equal(t, accPub, mustAccPub)
mustValPub := types.MustGetValPubKeyBech32(bech32ValPub)
valPub, err := types.GetValPubKeyBech32(bech32ValPub)
mustValPub := types.MustGetPubKeyFromBech32(types.Bech32PubKeyTypeValPub, bech32ValPub)
valPub, err := types.GetPubKeyFromBech32(types.Bech32PubKeyTypeValPub, bech32ValPub)
require.Nil(t, err)
require.Equal(t, valPub, mustValPub)
mustConsPub := types.MustGetConsPubKeyBech32(bech32ConsPub)
consPub, err := types.GetConsPubKeyBech32(bech32ConsPub)
mustConsPub := types.MustGetPubKeyFromBech32(types.Bech32PubKeyTypeConsPub, bech32ConsPub)
consPub, err := types.GetPubKeyFromBech32(types.Bech32PubKeyTypeConsPub, bech32ConsPub)
require.Nil(t, err)
require.Equal(t, consPub, mustConsPub)
@@ -246,7 +246,7 @@ func TestConfiguredPrefix(t *testing.T) {
acc.String(),
prefix+types.PrefixAccount), acc.String())
bech32Pub := types.MustBech32ifyAccPub(pub)
bech32Pub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeAccPub, pub)
require.True(t, strings.HasPrefix(
bech32Pub,
prefix+types.PrefixPublic))
@@ -260,7 +260,7 @@ func TestConfiguredPrefix(t *testing.T) {
val.String(),
prefix+types.PrefixValidator+types.PrefixAddress))
bech32ValPub := types.MustBech32ifyValPub(pub)
bech32ValPub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeValPub, pub)
require.True(t, strings.HasPrefix(
bech32ValPub,
prefix+types.PrefixValidator+types.PrefixPublic))
@@ -274,12 +274,11 @@ func TestConfiguredPrefix(t *testing.T) {
cons.String(),
prefix+types.PrefixConsensus+types.PrefixAddress))
bech32ConsPub := types.MustBech32ifyConsPub(pub)
bech32ConsPub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeConsPub, pub)
require.True(t, strings.HasPrefix(
bech32ConsPub,
prefix+types.PrefixConsensus+types.PrefixPublic))
}
}
}