Skip to content

Commit

Permalink
remove ETHAddress and replace all ValAddress with AccAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonberg1997 committed Dec 26, 2022
1 parent dbc99e3 commit 7d17b9a
Show file tree
Hide file tree
Showing 130 changed files with 786 additions and 918 deletions.
109 changes: 42 additions & 67 deletions client/debug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/version"

Expand All @@ -32,7 +31,7 @@ func Cmd() *cobra.Command {

cmd.AddCommand(PubkeyCmd())
cmd.AddCommand(PubkeyRawCmd())
cmd.AddCommand(AddrCmd())
// cmd.AddCommand(AddrCmd())
cmd.AddCommand(RawBytesCmd())

return cmd
Expand Down Expand Up @@ -69,7 +68,7 @@ $ %s debug pubkey '{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AurroA7jvfP
}

func bytesToPubkey(bz []byte, keytype string) (cryptotypes.PubKey, bool) {
if keytype == "ed25519" { //nolint:goconst
if keytype == "ed25519" {
if len(bz) == ed25519.PubKeySize {
return &ed25519.PubKey{Key: bz}, true
}
Expand Down Expand Up @@ -122,7 +121,7 @@ func getPubKeyFromRawString(pkstr string, keytype string) (cryptotypes.PubKey, e

func PubkeyRawCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "pubkey-raw [pubkey] -t [{ed25519, secp256k1}]",
Use: "pubkey-raw [pubkey] -t [{ed25519, eth_secp256k1}]",
Short: "Decode a ED25519 or eth_secp256k1 pubkey from hex, or base64",
Long: fmt.Sprintf(`Decode a pubkey from hex, or base64.
Example:
Expand All @@ -146,84 +145,60 @@ $ %s debug pubkey-raw 0x9f86D081884C7d659A2fEaA0C55AD015A3bf4F1B
if err != nil {
return err
}

var consensusPub string
edPK, ok := pk.(*ed25519.PubKey)
if ok && pubkeyType == "ed25519" {
consensusPub, err = legacybech32.MarshalPubKey(legacybech32.ConsPK, edPK) //nolint:staticcheck
if err != nil {
return err
}

cmd.Printf("Hex: %X\n", edPK.Key)
}
cmd.Println("Parsed key as", pk.Type())

pubKeyJSONBytes, err := clientCtx.LegacyAmino.MarshalJSON(pk)
if err != nil {
return err
}
accPub, err := legacybech32.MarshalPubKey(legacybech32.AccPK, pk) //nolint:staticcheck
if err != nil {
return err
}
valPub, err := legacybech32.MarshalPubKey(legacybech32.ValPK, pk) //nolint:staticcheck
if err != nil {
return err
}
cmd.Println("Address:", pk.Address())
cmd.Println("JSON (base64):", string(pubKeyJSONBytes))
cmd.Println("Bech32 Acc:", accPub)
cmd.Println("Bech32 Validator Operator:", valPub)
if pubkeyType == "ed25519" {
cmd.Println("Bech32 Validator Consensus:", consensusPub)
}

return nil
},
}
cmd.Flags().StringP(flagPubkeyType, "t", "ed25519", "Pubkey type to decode (oneof secp256k1, ed25519)")
cmd.Flags().StringP(flagPubkeyType, "t", "ed25519", "Pubkey type to decode (oneof eth_secp256k1, ed25519)")
return cmd
}

func AddrCmd() *cobra.Command {
return &cobra.Command{
Use: "addr [address]",
Short: "Convert an address between hex and bech32",
Long: fmt.Sprintf(`Convert an address between hex encoding and bech32.
Example:
$ %s debug addr cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
`, version.AppName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
addrString := args[0]
var addr []byte

// try hex, then bech32
var err error
addr, err = hex.DecodeString(addrString)
if err != nil {
var err2 error
addr, err2 = sdk.AccAddressFromHexUnsafe(addrString)
if err2 != nil {
var err3 error
addr, err3 = sdk.ValAddressFromHex(addrString)

if err3 != nil {
return fmt.Errorf("expected hex or bech32. Got errors: hex: %v, bech32 acc: %v, bech32 val: %v", err, err2, err3)
}
}
}

cmd.Println("Address:", addr)
cmd.Printf("Address (hex): %X\n", addr)
cmd.Printf("Bech32 Acc: %s\n", sdk.AccAddress(addr))
cmd.Printf("Bech32 Val: %s\n", sdk.ValAddress(addr))
return nil
},
}
}
// func AddrCmd() *cobra.Command {
// return &cobra.Command{
// Use: "addr [address]",
// Short: "Convert an address between hex and bech32",
// Long: fmt.Sprintf(`Convert an address between hex encoding and bech32.
//
// Example:
// $ %s debug addr cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
// `, version.AppName),
// Args: cobra.ExactArgs(1),
// RunE: func(cmd *cobra.Command, args []string) error {
// addrString := args[0]
// var addr []byte
//
// // try hex, then bech32
// var err error
// addr, err = hex.DecodeString(addrString)
// if err != nil {
// var err2 error
// addr, err2 = sdk.AccAddressFromHexUnsafe(addrString)
// if err2 != nil {
// var err3 error
// addr, err3 = sdk.AccAddressFromHex(addrString)
//
// if err3 != nil {
// return fmt.Errorf("expected hex or bech32. Got errors: hex: %v, bech32 acc: %v, bech32 val: %v", err, err2, err3)
// }
// }
// }
//
// cmd.Println("Address:", addr)
// cmd.Printf("Address (hex): %X\n", addr)
// cmd.Printf("Bech32 Acc: %s\n", sdk.AccAddress(addr))
// cmd.Printf("Bech32 Val: %s\n", sdk.AccAddress(addr))
// return nil
// },
// }
// }

func RawBytesCmd() *cobra.Command {
return &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion client/grpc/tmservice/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func convertHeader(h tmprototypes.Header) Header {
EvidenceHash: h.EvidenceHash,
LastResultsHash: h.LastResultsHash,
LastCommitHash: h.LastCommitHash,
ProposerAddress: sdk.ValAddress(h.ProposerAddress).String(),
ProposerAddress: sdk.AccAddress(h.ProposerAddress).String(),
}
}

Expand Down
49 changes: 24 additions & 25 deletions crypto/keyring/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,29 @@ func NewKeyOutput(name string, keyType KeyType, a sdk.Address, pk cryptotypes.Pu
}, nil
}

// MkConsKeyOutput create a KeyOutput in with "cons" Bech32 prefixes.
func MkConsKeyOutput(k *Record) (KeyOutput, error) {
pk, err := k.GetPubKey()
if err != nil {
return KeyOutput{}, err
}
addr := sdk.ConsAddress(pk.Address())
return NewKeyOutput(k.Name, k.GetType(), addr, pk)
}

// MkValKeyOutput create a KeyOutput in with "val" Bech32 prefixes.
func MkValKeyOutput(k *Record) (KeyOutput, error) {
pk, err := k.GetPubKey()
if err != nil {
return KeyOutput{}, err
}

addr := sdk.ValAddress(pk.Address())

return NewKeyOutput(k.Name, k.GetType(), addr, pk)
}
// // MkConsKeyOutput create a KeyOutput in with "cons" Bech32 prefixes.
// func MkConsKeyOutput(k *Record) (KeyOutput, error) {
// pk, err := k.GetPubKey()
// if err != nil {
// return KeyOutput{}, err
// }
// addr := sdk.ConsAddress(pk.Address())
// return NewKeyOutput(k.Name, k.GetType(), addr, pk)
// }
//
// // MkValKeyOutput create a KeyOutput in with "val" Bech32 prefixes.
// func MkValKeyOutput(k *Record) (KeyOutput, error) {
// pk, err := k.GetPubKey()
// if err != nil {
// return KeyOutput{}, err
// }
//
// addr := sdk.AccAddress(pk.Address())
//
// return NewKeyOutput(k.Name, k.GetType(), addr, pk)
// }

// MkAccKeyOutput create a KeyOutput in with "acc" Bech32 prefixes. If the
// MkAccKeyOutput create a KeyOutput. If the
// public key is a multisig public key, then the threshold and constituent
// public keys will be added.
func MkAccKeyOutput(k *Record) (KeyOutput, error) {
Expand All @@ -76,9 +76,8 @@ func MkAccKeyOutput(k *Record) (KeyOutput, error) {
return NewKeyOutput(k.Name, k.GetType(), addr, pk)
}

// MkAccKeysOutput returns a slice of KeyOutput objects, each with the "acc"
// Bech32 prefixes, given a slice of Record objects. It returns an error if any
// call to MkKeyOutput fails.
// MkAccKeysOutput returns a slice of KeyOutput objects, given a slice of Record objects.
// It returns an error if any all to MkKeyOutput fails.
func MkAccKeysOutput(records []*Record) ([]KeyOutput, error) {
kos := make([]KeyOutput, len(records))
var err error
Expand Down
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func NewSimApp(

// add keepers
app.AccountKeeper = authkeeper.NewAccountKeeper(
appCodec, keys[authtypes.StoreKey], app.GetSubspace(authtypes.ModuleName), authtypes.ProtoBaseAccount, maccPerms, sdk.Bech32MainPrefix,
appCodec, keys[authtypes.StoreKey], app.GetSubspace(authtypes.ModuleName), authtypes.ProtoBaseAccount, maccPerms,
)
app.AuthzKeeper = authzkeeper.NewKeeper(
keys[authzkeeper.StoreKey], appCodec, app.MsgServiceRouter(), app.AccountKeeper,
Expand Down
8 changes: 4 additions & 4 deletions simapp/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []
allowedAddrsMap := make(map[string]bool)

for _, addr := range jailAllowedAddrs {
_, err := sdk.ValAddressFromHex(addr)
_, err := sdk.AccAddressFromHexUnsafe(addr)
if err != nil {
log.Fatal(err)
}
Expand All @@ -81,7 +81,7 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []
// withdraw all delegator rewards
dels := app.StakingKeeper.GetAllDelegations(ctx)
for _, delegation := range dels {
valAddr, err := sdk.ValAddressFromHex(delegation.ValidatorAddress)
valAddr, err := sdk.AccAddressFromHexUnsafe(delegation.ValidatorAddress)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []

// reinitialize all delegations
for _, del := range dels {
valAddr, err := sdk.ValAddressFromHex(del.ValidatorAddress)
valAddr, err := sdk.AccAddressFromHexUnsafe(del.ValidatorAddress)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -164,7 +164,7 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []
counter := int16(0)

for ; iter.Valid(); iter.Next() {
addr := sdk.ValAddress(stakingtypes.AddressFromValidatorsKey(iter.Key()))
addr := sdk.AccAddress(stakingtypes.AddressFromValidatorsKey(iter.Key()))
validator, found := app.StakingKeeper.GetValidator(ctx, addr)
if !found {
panic("expected validator, not found")
Expand Down
2 changes: 1 addition & 1 deletion simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func initTestnetFiles(
blsSecretKey, _ := bls.RandKey()
blsPk := hex.EncodeToString(blsSecretKey.PublicKey().Marshal())
createValMsg, err := stakingtypes.NewMsgCreateValidator(
sdk.ValAddress(addr),
addr,
valPubKeys[i],
sdk.NewCoin(sdk.DefaultBondDenom, valTokens),
stakingtypes.NewDescription(nodeDirName, "", "", "", ""),
Expand Down
11 changes: 4 additions & 7 deletions simapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func genesisStateWithValSet(t *testing.T,
pkAny, err := codectypes.NewAnyWithValue(pk)
require.NoError(t, err)
validator := stakingtypes.Validator{
OperatorAddress: sdk.ValAddress(val.Address).String(),
OperatorAddress: sdk.AccAddress(val.Address).String(),
ConsensusPubkey: pkAny,
Jailed: false,
Status: stakingtypes.Bonded,
Expand Down Expand Up @@ -367,12 +367,9 @@ func initAccountWithCoins(app *SimApp, ctx sdk.Context, addr sdk.AccAddress, coi
}

// ConvertAddrsToValAddrs converts the provided addresses to ValAddress.
func ConvertAddrsToValAddrs(addrs []sdk.AccAddress) []sdk.ValAddress {
valAddrs := make([]sdk.ValAddress, len(addrs))

for i, addr := range addrs {
valAddrs[i] = sdk.ValAddress(addr)
}
func ConvertAddrsToValAddrs(addrs []sdk.AccAddress) []sdk.AccAddress {
valAddrs := make([]sdk.AccAddress, len(addrs))
copy(valAddrs, addrs)

return valAddrs
}
Expand Down
6 changes: 3 additions & 3 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ type (
RPCAddress string
P2PAddress string
Address sdk.AccAddress
ValAddress sdk.ValAddress
ValAddress sdk.AccAddress
RPCClient tmclient.Client

tmNode *node.Node
Expand Down Expand Up @@ -409,7 +409,7 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
blsPubKey := hex.EncodeToString(blsSecretKey.PublicKey().Marshal())

createValMsg, err := stakingtypes.NewMsgCreateValidator(
sdk.ValAddress(addr),
addr,
valPubKeys[i],
sdk.NewCoin(cfg.BondDenom, cfg.BondedTokens),
stakingtypes.NewDescription(nodeDirName, "", "", "", ""),
Expand Down Expand Up @@ -484,7 +484,7 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
P2PAddress: tmCfg.P2P.ListenAddress,
APIAddress: apiAddr,
Address: addr,
ValAddress: sdk.ValAddress(addr),
ValAddress: addr,
}
}

Expand Down
Loading

0 comments on commit 7d17b9a

Please sign in to comment.