Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update EIP712 related functions #78

Merged
merged 8 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ require (
github.com/tendermint/go-amino v0.16.0
github.com/tendermint/tendermint v0.34.22
github.com/tendermint/tm-db v0.6.7
github.com/wealdtech/go-eth2-util v1.6.3
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
golang.org/x/text v0.3.7
Expand Down Expand Up @@ -164,7 +165,6 @@ require (
github.com/urfave/cli/v2 v2.3.0 // indirect
github.com/wealdtech/go-bytesutil v1.1.1 // indirect
github.com/wealdtech/go-eth2-types/v2 v2.5.2 // indirect
github.com/wealdtech/go-eth2-util v1.6.3 // indirect
github.com/zondax/hid v0.9.1-0.20220302062450-5552068d2266 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
go.opencensus.io v0.23.0 // indirect
Expand Down
32 changes: 16 additions & 16 deletions proto/cosmos/auth/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ service Query {
option (google.api.http).get = "/cosmos/auth/v1beta1/module_accounts/{name}";
}

// Bech32Prefix queries bech32Prefix
//
// Since: cosmos-sdk 0.46
rpc Bech32Prefix(Bech32PrefixRequest) returns (Bech32PrefixResponse) {
option (google.api.http).get = "/cosmos/auth/v1beta1/bech32";
}
// // Bech32Prefix queries bech32Prefix
// //
// // Since: cosmos-sdk 0.46
// rpc Bech32Prefix(Bech32PrefixRequest) returns (Bech32PrefixResponse) {
// option (google.api.http).get = "/cosmos/auth/v1beta1/bech32";
// }

// AddressBytesToString converts Account Address bytes to string
//
Expand Down Expand Up @@ -135,17 +135,17 @@ message QueryModuleAccountByNameResponse {
google.protobuf.Any account = 1 [(cosmos_proto.accepts_interface) = "ModuleAccountI"];
}

// Bech32PrefixRequest is the request type for Bech32Prefix rpc method.
//
// Since: cosmos-sdk 0.46
message Bech32PrefixRequest {}
//// Bech32PrefixRequest is the request type for Bech32Prefix rpc method.
////
//// Since: cosmos-sdk 0.46
//message Bech32PrefixRequest {}

// Bech32PrefixResponse is the response type for Bech32Prefix rpc method.
//
// Since: cosmos-sdk 0.46
message Bech32PrefixResponse {
string bech32_prefix = 1;
}
//// Bech32PrefixResponse is the response type for Bech32Prefix rpc method.
////
//// Since: cosmos-sdk 0.46
//message Bech32PrefixResponse {
// string bech32_prefix = 1;
//}

// AddressBytesToStringRequest is the request type for AddressString rpc method.
//
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
Loading