Merge PR #3676: Refactored prefixes as constants
* Refactored prefixes as constants * Fixing test
This commit is contained in:
parent
17a9ea0736
commit
6967de1073
|
@ -54,7 +54,7 @@ func showKeysCmd() *cobra.Command {
|
|||
RunE: runShowCmd,
|
||||
}
|
||||
|
||||
cmd.Flags().String(FlagBechPrefix, "acc", "The Bech32 prefix encoding for a key (acc|val|cons)")
|
||||
cmd.Flags().String(FlagBechPrefix, sdk.PrefixAccount, "The Bech32 prefix encoding for a key (acc|val|cons)")
|
||||
cmd.Flags().BoolP(FlagAddress, "a", false, "output the address only (overrides --output)")
|
||||
cmd.Flags().BoolP(FlagPublicKey, "p", false, "output the public key only (overrides --output)")
|
||||
cmd.Flags().Uint(flagMultiSigThreshold, 1, "K out of N required signatures")
|
||||
|
@ -139,11 +139,11 @@ func validateMultisigThreshold(k, nKeys int) error {
|
|||
|
||||
func getBechKeyOut(bechPrefix string) (bechKeyOutFn, error) {
|
||||
switch bechPrefix {
|
||||
case "acc":
|
||||
case sdk.PrefixAccount:
|
||||
return Bech32KeyOutput, nil
|
||||
case "val":
|
||||
case sdk.PrefixValidator:
|
||||
return Bech32ValKeyOutput, nil
|
||||
case "cons":
|
||||
case sdk.PrefixConsensus:
|
||||
return Bech32ConsKeyOutput, nil
|
||||
}
|
||||
|
||||
|
@ -161,7 +161,7 @@ func GetKeyRequestHandler(indent bool) http.HandlerFunc {
|
|||
bechPrefix := r.URL.Query().Get(FlagBechPrefix)
|
||||
|
||||
if bechPrefix == "" {
|
||||
bechPrefix = "acc"
|
||||
bechPrefix = sdk.PrefixAccount
|
||||
}
|
||||
|
||||
bechKeyOut, err := getBechKeyOut(bechPrefix)
|
||||
|
|
|
@ -3,15 +3,15 @@ package keys
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys"
|
||||
"github.com/cosmos/cosmos-sdk/tests"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"github.com/tendermint/tendermint/libs/cli"
|
||||
|
||||
"github.com/tendermint/tendermint/crypto/secp256k1"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys"
|
||||
"github.com/tendermint/tendermint/crypto/secp256k1"
|
||||
"github.com/tendermint/tendermint/libs/cli"
|
||||
)
|
||||
|
||||
func Test_multiSigKey_Properties(t *testing.T) {
|
||||
|
@ -64,17 +64,17 @@ func Test_runShowCmd(t *testing.T) {
|
|||
assert.EqualError(t, err, "invalid Bech32 prefix encoding provided: ")
|
||||
|
||||
// Now try single key - set bech to acc
|
||||
viper.Set(FlagBechPrefix, "acc")
|
||||
viper.Set(FlagBechPrefix, sdk.PrefixAccount)
|
||||
err = runShowCmd(cmd, []string{fakeKeyName1})
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Now try multisig key - set bech to acc
|
||||
viper.Set(FlagBechPrefix, "acc")
|
||||
viper.Set(FlagBechPrefix, sdk.PrefixAccount)
|
||||
err = runShowCmd(cmd, []string{fakeKeyName1, fakeKeyName2})
|
||||
assert.EqualError(t, err, "threshold must be a positive integer")
|
||||
|
||||
// Now try multisig key - set bech to acc + threshold=2
|
||||
viper.Set(FlagBechPrefix, "acc")
|
||||
viper.Set(FlagBechPrefix, sdk.PrefixAccount)
|
||||
viper.Set(flagMultiSigThreshold, 2)
|
||||
err = runShowCmd(cmd, []string{fakeKeyName1, fakeKeyName2})
|
||||
assert.NoError(t, err)
|
||||
|
@ -119,9 +119,9 @@ func Test_getBechKeyOut(t *testing.T) {
|
|||
}{
|
||||
{"empty", args{""}, nil, true},
|
||||
{"wrong", args{"???"}, nil, true},
|
||||
{"acc", args{"acc"}, Bech32KeyOutput, false},
|
||||
{"val", args{"val"}, Bech32ValKeyOutput, false},
|
||||
{"cons", args{"cons"}, Bech32ConsKeyOutput, false},
|
||||
{"acc", args{sdk.PrefixAccount}, Bech32KeyOutput, false},
|
||||
{"val", args{sdk.PrefixValidator}, Bech32ValKeyOutput, false},
|
||||
{"cons", args{sdk.PrefixConsensus}, Bech32ConsKeyOutput, false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
|
|
@ -5,10 +5,18 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/tendermint/tendermint/libs/bech32"
|
||||
)
|
||||
|
||||
var bech32Prefixes = []string{"cosmos", "cosmospub", "cosmosvaloper", "cosmosvaloperpub", "cosmosvalcons", "cosmosvalconspub"}
|
||||
var bech32Prefixes = []string{
|
||||
sdk.Bech32PrefixAccAddr,
|
||||
sdk.Bech32PrefixAccPub,
|
||||
sdk.Bech32PrefixValAddr,
|
||||
sdk.Bech32PrefixValPub,
|
||||
sdk.Bech32PrefixConsAddr,
|
||||
sdk.Bech32PrefixConsPub,
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
|
|
|
@ -8,7 +8,10 @@ import (
|
|||
"github.com/cosmos/cosmos-sdk/store/gaskv"
|
||||
"github.com/cosmos/cosmos-sdk/store/iavl"
|
||||
"github.com/cosmos/cosmos-sdk/store/types"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
tiavl "github.com/tendermint/iavl"
|
||||
dbm "github.com/tendermint/tendermint/libs/db"
|
||||
)
|
||||
|
@ -243,7 +246,7 @@ func mockStoreWithStuff() types.KVStore {
|
|||
store.Set(bz("key3"), bz("value3"))
|
||||
store.Set(bz("something"), bz("else"))
|
||||
store.Set(bz(""), bz(""))
|
||||
store.Set(bz("k"), bz("val"))
|
||||
store.Set(bz("k"), bz(sdk.PrefixValidator))
|
||||
store.Set(bz("ke"), bz("valu"))
|
||||
store.Set(bz("kee"), bz("valuu"))
|
||||
return store
|
||||
|
|
|
@ -17,19 +17,35 @@ import (
|
|||
const (
|
||||
// AddrLen defines a valid address length
|
||||
AddrLen = 20
|
||||
// Bech32PrefixAccAddr defines the Bech32 prefix of an account's address
|
||||
Bech32MainPrefix = "cosmos"
|
||||
|
||||
// PrefixAccount is the prefix for account keys
|
||||
PrefixAccount = "acc"
|
||||
// PrefixValidator is the prefix for validator keys
|
||||
PrefixValidator = "val"
|
||||
// PrefixConsensus is the prefix for consensus keys
|
||||
PrefixConsensus = "cons"
|
||||
// PrefixPublic is the prefix for public keys
|
||||
PrefixPublic = "pub"
|
||||
// PrefixOperator is the prefix for operator keys
|
||||
PrefixOperator = "oper"
|
||||
|
||||
// PrefixAddress is the prefix for addresses
|
||||
PrefixAddress = "addr"
|
||||
|
||||
// Bech32PrefixAccAddr defines the Bech32 prefix of an account's address
|
||||
Bech32PrefixAccAddr = "cosmos"
|
||||
Bech32PrefixAccAddr = Bech32MainPrefix
|
||||
// Bech32PrefixAccPub defines the Bech32 prefix of an account's public key
|
||||
Bech32PrefixAccPub = "cosmospub"
|
||||
Bech32PrefixAccPub = Bech32MainPrefix + PrefixPublic
|
||||
// Bech32PrefixValAddr defines the Bech32 prefix of a validator's operator address
|
||||
Bech32PrefixValAddr = "cosmosvaloper"
|
||||
Bech32PrefixValAddr = Bech32MainPrefix + PrefixValidator + PrefixOperator
|
||||
// Bech32PrefixValPub defines the Bech32 prefix of a validator's operator public key
|
||||
Bech32PrefixValPub = "cosmosvaloperpub"
|
||||
Bech32PrefixValPub = Bech32MainPrefix + PrefixValidator + PrefixOperator + PrefixPublic
|
||||
// Bech32PrefixConsAddr defines the Bech32 prefix of a consensus node address
|
||||
Bech32PrefixConsAddr = "cosmosvalcons"
|
||||
Bech32PrefixConsAddr = Bech32MainPrefix + PrefixValidator + PrefixConsensus
|
||||
// Bech32PrefixConsPub defines the Bech32 prefix of a consensus node public key
|
||||
Bech32PrefixConsPub = "cosmosvalconspub"
|
||||
Bech32PrefixConsPub = Bech32MainPrefix + PrefixValidator + PrefixConsensus + PrefixPublic
|
||||
)
|
||||
|
||||
// Address is a common interface for different types of addresses used by the SDK
|
||||
|
|
|
@ -213,25 +213,50 @@ func TestConfiguredPrefix(t *testing.T) {
|
|||
rand.Read(pub[:])
|
||||
// Test if randomly generated prefix of a given length works
|
||||
prefix := RandString(length)
|
||||
|
||||
// Assuming that GetConfig is not sealed.
|
||||
config := types.GetConfig()
|
||||
config.SetBech32PrefixForAccount(prefix+"acc", prefix+"pub")
|
||||
config.SetBech32PrefixForAccount(
|
||||
prefix+types.PrefixAccount,
|
||||
prefix+types.PrefixPublic)
|
||||
|
||||
acc := types.AccAddress(pub.Address())
|
||||
require.True(t, strings.HasPrefix(acc.String(), prefix+"acc"), acc.String())
|
||||
require.True(t, strings.HasPrefix(
|
||||
acc.String(),
|
||||
prefix+types.PrefixAccount), acc.String())
|
||||
|
||||
bech32Pub := types.MustBech32ifyAccPub(pub)
|
||||
require.True(t, strings.HasPrefix(bech32Pub, prefix+"pub"))
|
||||
require.True(t, strings.HasPrefix(
|
||||
bech32Pub,
|
||||
prefix+types.PrefixPublic))
|
||||
|
||||
config.SetBech32PrefixForValidator(
|
||||
prefix+types.PrefixValidator+types.PrefixAddress,
|
||||
prefix+types.PrefixValidator+types.PrefixPublic)
|
||||
|
||||
config.SetBech32PrefixForValidator(prefix+"valaddr", prefix+"valpub")
|
||||
val := types.ValAddress(pub.Address())
|
||||
require.True(t, strings.HasPrefix(val.String(), prefix+"valaddr"))
|
||||
bech32ValPub := types.MustBech32ifyValPub(pub)
|
||||
require.True(t, strings.HasPrefix(bech32ValPub, prefix+"valpub"))
|
||||
require.True(t, strings.HasPrefix(
|
||||
val.String(),
|
||||
prefix+types.PrefixValidator+types.PrefixAddress))
|
||||
|
||||
bech32ValPub := types.MustBech32ifyValPub(pub)
|
||||
require.True(t, strings.HasPrefix(
|
||||
bech32ValPub,
|
||||
prefix+types.PrefixValidator+types.PrefixPublic))
|
||||
|
||||
config.SetBech32PrefixForConsensusNode(
|
||||
prefix+types.PrefixConsensus+types.PrefixAddress,
|
||||
prefix+types.PrefixConsensus+types.PrefixPublic)
|
||||
|
||||
config.SetBech32PrefixForConsensusNode(prefix+"consaddr", prefix+"conspub")
|
||||
cons := types.ConsAddress(pub.Address())
|
||||
require.True(t, strings.HasPrefix(cons.String(), prefix+"consaddr"))
|
||||
require.True(t, strings.HasPrefix(
|
||||
cons.String(),
|
||||
prefix+types.PrefixConsensus+types.PrefixAddress))
|
||||
|
||||
bech32ConsPub := types.MustBech32ifyConsPub(pub)
|
||||
require.True(t, strings.HasPrefix(bech32ConsPub, prefix+"conspub"))
|
||||
require.True(t, strings.HasPrefix(
|
||||
bech32ConsPub,
|
||||
prefix+types.PrefixConsensus+types.PrefixPublic))
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue