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
7 changes: 4 additions & 3 deletions baseapp/block_gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,20 @@ func TestBaseApp_BlockGas(t *testing.T) {
stateBytes, err := tmjson.MarshalIndent(genState, "", " ")
require.NoError(t, err)
app.InitChain(abci.RequestInitChain{
ChainId: simapp.DefaultChainId,
Validators: []abci.ValidatorUpdate{},
ConsensusParams: simapp.DefaultConsensusParams,
AppStateBytes: stateBytes,
})

ctx := app.NewContext(false, tmproto.Header{})
ctx := app.NewContext(false, tmproto.Header{}).WithChainID(simapp.DefaultChainId)

// tx fee
feeCoin := sdk.NewCoin("atom", sdk.NewInt(150))
feeAmount := sdk.NewCoins(feeCoin)

// test account and fund
priv1, _, addr1 := testdata.KeyTestPubAddr()
priv1, _, addr1 := testdata.KeyEthSecp256k1TestPubAddr()
err = app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, feeAmount)
require.NoError(t, err)
err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr1, feeAmount)
Expand Down Expand Up @@ -123,7 +124,7 @@ func TestBaseApp_BlockGas(t *testing.T) {
require.Equal(t, []byte("ok"), okValue)
}
// check block gas is always consumed
baseGas := uint64(3420) // baseGas is the gas consumed before tx msg
baseGas := uint64(7530) // baseGas is the gas consumed before tx msg
expGasConsumed := addUint64Saturating(tc.gasToConsume, baseGas)
if expGasConsumed > txtypes.MaxGasWanted {
// capped by gasLimit
Expand Down
4 changes: 4 additions & 0 deletions baseapp/msg_service_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import (

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
)

func TestRegisterMsgService(t *testing.T) {
Expand Down Expand Up @@ -70,6 +72,8 @@ func TestRegisterMsgServiceTwice(t *testing.T) {
func TestMsgService(t *testing.T) {
priv, _, _ := testdata.KeyTestPubAddr()
encCfg := simapp.MakeTestEncodingConfig()
protoCodec := codec.NewProtoCodec(encCfg.InterfaceRegistry)
encCfg.TxConfig = authtx.NewTxConfig(protoCodec, authtx.DefaultSignModes)
testdata.RegisterInterfaces(encCfg.InterfaceRegistry)
db := dbm.NewMemDB()
app := baseapp.NewBaseApp("test", log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, encCfg.TxConfig.TxDecoder())
Expand Down
4 changes: 2 additions & 2 deletions client/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func TestContext_PrintObjectLegacy(t *testing.T) {
}

// amino
amino := testdata.NewTestAmino()
ctx = ctx.WithLegacyAmino(&codec.LegacyAmino{Amino: amino})
amino := testdata.Amino
ctx = ctx.WithLegacyAmino(amino)

// json
buf := &bytes.Buffer{}
Expand Down
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
4 changes: 2 additions & 2 deletions client/tx/legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const (

var (
fee = types.NewCoins(types.NewInt64Coin("bam", 100))
_, pub1, addr1 = testdata.KeyTestPubAddr()
_, _, addr2 = testdata.KeyTestPubAddr()
_, pub1, addr1 = testdata.KeyEthSecp256k1TestPubAddr()
_, _, addr2 = testdata.KeyEthSecp256k1TestPubAddr()
rawSig = []byte("dummy")
sig = signing2.SignatureV2{
PubKey: pub1,
Expand Down
6 changes: 5 additions & 1 deletion client/tx/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
Expand All @@ -18,6 +19,7 @@ import (
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)

Expand Down Expand Up @@ -155,6 +157,8 @@ func TestSign(t *testing.T) {
requireT := require.New(t)
path := hd.CreateHDPath(118, 0, 0).String()
encCfg := simapp.MakeTestEncodingConfig()
protoCodec := codec.NewProtoCodec(encCfg.InterfaceRegistry)
encCfg.TxConfig = authtx.NewTxConfig(protoCodec, authtx.DefaultSignModes)
kb, err := keyring.New(t.Name(), "test", t.TempDir(), nil, encCfg.Codec)
requireT.NoError(err)

Expand All @@ -179,7 +183,7 @@ func TestSign(t *testing.T) {
t.Log("Pub keys:", pubKey1, pubKey2)

txfNoKeybase := tx.Factory{}.
WithTxConfig(NewTestTxConfig()).
WithTxConfig(encCfg.TxConfig).
WithAccountNumber(50).
WithSequence(23).
WithFees("50stake").
Expand Down
2 changes: 1 addition & 1 deletion codec/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ x: "0"
`, string(bz))

// amino
aminoCdc := codec.NewAminoCodec(&codec.LegacyAmino{testdata.NewTestAmino()})
aminoCdc := codec.NewAminoCodec(testdata.Amino)
bz, err = codec.MarshalYAML(aminoCdc, hasAnimal)
require.NoError(t, err)
require.Equal(t, `type: testdata/HasAnimal
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
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
Loading