From 17b369835a41947211acb4db70eb328de81c0c90 Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Mon, 8 Jun 2020 17:31:14 -0400 Subject: [PATCH 1/9] Add SignModeHandler and LegacyAminoJSONHandler --- x/auth/signing/amino/amino.go | 38 +++++++++++++++++ x/auth/signing/amino/amino_test.go | 63 +++++++++++++++++++++++++++++ x/auth/signing/sign_mode_handler.go | 21 ++++++++++ 3 files changed, 122 insertions(+) create mode 100644 x/auth/signing/amino/amino.go create mode 100644 x/auth/signing/amino/amino_test.go create mode 100644 x/auth/signing/sign_mode_handler.go diff --git a/x/auth/signing/amino/amino.go b/x/auth/signing/amino/amino.go new file mode 100644 index 000000000000..d24d793950ed --- /dev/null +++ b/x/auth/signing/amino/amino.go @@ -0,0 +1,38 @@ +package amino + +import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" + types "github.com/cosmos/cosmos-sdk/types/tx" + "github.com/cosmos/cosmos-sdk/x/auth/ante" + "github.com/cosmos/cosmos-sdk/x/auth/signing" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" +) + +type LegacyAminoJSONHandler struct{} + +var _ signing.SignModeHandler = LegacyAminoJSONHandler{} + +func (h LegacyAminoJSONHandler) DefaultMode() types.SignMode { + return types.SignMode_SIGN_MODE_LEGACY_AMINO_JSON +} + +func (LegacyAminoJSONHandler) Modes() []types.SignMode { + return []types.SignMode{types.SignMode_SIGN_MODE_LEGACY_AMINO_JSON} +} + +func (LegacyAminoJSONHandler) GetSignBytes(_ types.SignMode, data signing.SigningData, tx sdk.Tx) ([]byte, error) { + feeTx, ok := tx.(ante.FeeTx) + if !ok { + return nil, fmt.Errorf("expected FeeTx, got %T", tx) + } + + memoTx, ok := tx.(ante.TxWithMemo) + if !ok { + return nil, fmt.Errorf("expected TxWithMemo, got %T", tx) + } + + return authtypes.StdSignBytes( + data.ChainID, data.AccountNumber, data.AccountSequence, authtypes.StdFee{Amount: feeTx.GetFee(), Gas: feeTx.GetGas()}, tx.GetMsgs(), memoTx.GetMemo(), + ), nil +} diff --git a/x/auth/signing/amino/amino_test.go b/x/auth/signing/amino/amino_test.go new file mode 100644 index 000000000000..5ee8f1e1dc9b --- /dev/null +++ b/x/auth/signing/amino/amino_test.go @@ -0,0 +1,63 @@ +package amino_test + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + types2 "github.com/cosmos/cosmos-sdk/types/tx" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/auth/signing" + "github.com/cosmos/cosmos-sdk/x/auth/signing/amino" + "github.com/cosmos/cosmos-sdk/x/auth/types" + "github.com/cosmos/cosmos-sdk/x/bank" + "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/crypto/secp256k1" + "testing" +) + +func TestLegacyAminoJSONHandler(t *testing.T) { + priv1 := secp256k1.GenPrivKey() + addr1 := sdk.AccAddress(priv1.PubKey().Address()) + priv2 := secp256k1.GenPrivKey() + addr2 := sdk.AccAddress(priv2.PubKey().Address()) + + coins := sdk.Coins{sdk.NewInt64Coin("foocoin", 10)} + + fee := types.StdFee{ + Amount: coins, + Gas: 10000, + } + memo := "foo" + msgs := []sdk.Msg{ + &bank.MsgSend{ + FromAddress: addr1, + ToAddress: addr2, + Amount: coins, + }, + } + + tx := auth.StdTx{ + Msgs: msgs, + Fee: fee, + Signatures: nil, + Memo: memo, + } + + var ( + chainId = "test-chain" + accNum uint64 = 7 + seqNum uint64 = 7 + ) + + handler := amino.LegacyAminoJSONHandler{} + signBz, err := handler.GetSignBytes(types2.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signing.SigningData{ + ChainID: chainId, + AccountNumber: accNum, + AccountSequence: seqNum, + PublicKey: priv1.PubKey(), + }, tx) + require.NoError(t, err) + + expectedSignBz := types.StdSignBytes(chainId, accNum, seqNum, fee, msgs, memo) + + require.Equal(t, expectedSignBz, signBz) + +} diff --git a/x/auth/signing/sign_mode_handler.go b/x/auth/signing/sign_mode_handler.go new file mode 100644 index 000000000000..8ddc19d64199 --- /dev/null +++ b/x/auth/signing/sign_mode_handler.go @@ -0,0 +1,21 @@ +package signing + +import ( + txtypes "github.com/cosmos/cosmos-sdk/types/tx" + "github.com/tendermint/tendermint/crypto" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +type SigningData struct { + ChainID string + AccountNumber uint64 + AccountSequence uint64 + PublicKey crypto.PubKey +} + +type SignModeHandler interface { + DefaultMode() txtypes.SignMode + Modes() []txtypes.SignMode + GetSignBytes(mode txtypes.SignMode, data SigningData, tx sdk.Tx) ([]byte, error) +} From 2c64a381ef824a9c18d27c1a8004091e0f3eb365 Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Mon, 8 Jun 2020 17:36:23 -0400 Subject: [PATCH 2/9] Simplify, add tests --- x/auth/signing/amino/amino.go | 6 +++++- x/auth/signing/amino/amino_test.go | 23 ++++++++++++++++++----- x/auth/signing/sign_mode_handler.go | 5 +---- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/x/auth/signing/amino/amino.go b/x/auth/signing/amino/amino.go index d24d793950ed..a32f8b6ef165 100644 --- a/x/auth/signing/amino/amino.go +++ b/x/auth/signing/amino/amino.go @@ -21,7 +21,11 @@ func (LegacyAminoJSONHandler) Modes() []types.SignMode { return []types.SignMode{types.SignMode_SIGN_MODE_LEGACY_AMINO_JSON} } -func (LegacyAminoJSONHandler) GetSignBytes(_ types.SignMode, data signing.SigningData, tx sdk.Tx) ([]byte, error) { +func (LegacyAminoJSONHandler) GetSignBytes(mode types.SignMode, data signing.SigningData, tx sdk.Tx) ([]byte, error) { + if mode != types.SignMode_SIGN_MODE_LEGACY_AMINO_JSON { + return nil, fmt.Errorf("expected %s, got %s", types.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, mode) + } + feeTx, ok := tx.(ante.FeeTx) if !ok { return nil, fmt.Errorf("expected FeeTx, got %T", tx) diff --git a/x/auth/signing/amino/amino_test.go b/x/auth/signing/amino/amino_test.go index 5ee8f1e1dc9b..1e1a7f41bc55 100644 --- a/x/auth/signing/amino/amino_test.go +++ b/x/auth/signing/amino/amino_test.go @@ -2,7 +2,7 @@ package amino_test import ( sdk "github.com/cosmos/cosmos-sdk/types" - types2 "github.com/cosmos/cosmos-sdk/types/tx" + txtypes "github.com/cosmos/cosmos-sdk/types/tx" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/auth/signing" "github.com/cosmos/cosmos-sdk/x/auth/signing/amino" @@ -13,7 +13,7 @@ import ( "testing" ) -func TestLegacyAminoJSONHandler(t *testing.T) { +func TestLegacyAminoJSONHandler_GetSignBytes(t *testing.T) { priv1 := secp256k1.GenPrivKey() addr1 := sdk.AccAddress(priv1.PubKey().Address()) priv2 := secp256k1.GenPrivKey() @@ -48,16 +48,29 @@ func TestLegacyAminoJSONHandler(t *testing.T) { ) handler := amino.LegacyAminoJSONHandler{} - signBz, err := handler.GetSignBytes(types2.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signing.SigningData{ + signingData := signing.SigningData{ ChainID: chainId, AccountNumber: accNum, AccountSequence: seqNum, - PublicKey: priv1.PubKey(), - }, tx) + } + signBz, err := handler.GetSignBytes(txtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signingData, tx) require.NoError(t, err) expectedSignBz := types.StdSignBytes(chainId, accNum, seqNum, fee, msgs, memo) require.Equal(t, expectedSignBz, signBz) + // expect error with wrong sign mode + _, err = handler.GetSignBytes(txtypes.SignMode_SIGN_MODE_DIRECT, signingData, tx) + require.Error(t, err) +} + +func TestLegacyAminoJSONHandler_DefaultMode(t *testing.T) { + handler := amino.LegacyAminoJSONHandler{} + require.Equal(t, txtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, handler.DefaultMode()) +} + +func TestLegacyAminoJSONHandler_Modes(t *testing.T) { + handler := amino.LegacyAminoJSONHandler{} + require.Equal(t, []txtypes.SignMode{txtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON}, handler.DefaultMode()) } diff --git a/x/auth/signing/sign_mode_handler.go b/x/auth/signing/sign_mode_handler.go index 8ddc19d64199..744051e0cec5 100644 --- a/x/auth/signing/sign_mode_handler.go +++ b/x/auth/signing/sign_mode_handler.go @@ -1,17 +1,14 @@ package signing import ( - txtypes "github.com/cosmos/cosmos-sdk/types/tx" - "github.com/tendermint/tendermint/crypto" - sdk "github.com/cosmos/cosmos-sdk/types" + txtypes "github.com/cosmos/cosmos-sdk/types/tx" ) type SigningData struct { ChainID string AccountNumber uint64 AccountSequence uint64 - PublicKey crypto.PubKey } type SignModeHandler interface { From 2f10005866c68f75ed3c4b73075ab87a2312b21a Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Mon, 8 Jun 2020 17:42:56 -0400 Subject: [PATCH 3/9] Add godocs --- x/auth/signing/amino/amino.go | 4 ++++ x/auth/signing/sign_mode_handler.go | 28 +++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/x/auth/signing/amino/amino.go b/x/auth/signing/amino/amino.go index a32f8b6ef165..bf6bd060effe 100644 --- a/x/auth/signing/amino/amino.go +++ b/x/auth/signing/amino/amino.go @@ -9,18 +9,22 @@ import ( authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" ) +// LegacyAminoJSONHandler is a SignModeHandler that handles SIGN_MODE_LEGACY_AMINO_JSON type LegacyAminoJSONHandler struct{} var _ signing.SignModeHandler = LegacyAminoJSONHandler{} +// DefaultMode implements SignModeHandler.DefaultMode func (h LegacyAminoJSONHandler) DefaultMode() types.SignMode { return types.SignMode_SIGN_MODE_LEGACY_AMINO_JSON } +// DefaultMode implements SignModeHandler.Modes func (LegacyAminoJSONHandler) Modes() []types.SignMode { return []types.SignMode{types.SignMode_SIGN_MODE_LEGACY_AMINO_JSON} } +// DefaultMode implements SignModeHandler.GetSignBytes func (LegacyAminoJSONHandler) GetSignBytes(mode types.SignMode, data signing.SigningData, tx sdk.Tx) ([]byte, error) { if mode != types.SignMode_SIGN_MODE_LEGACY_AMINO_JSON { return nil, fmt.Errorf("expected %s, got %s", types.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, mode) diff --git a/x/auth/signing/sign_mode_handler.go b/x/auth/signing/sign_mode_handler.go index 744051e0cec5..4cfbd9906e03 100644 --- a/x/auth/signing/sign_mode_handler.go +++ b/x/auth/signing/sign_mode_handler.go @@ -5,14 +5,32 @@ import ( txtypes "github.com/cosmos/cosmos-sdk/types/tx" ) +// SignModeHandler defines a interface to be implemented by types which will handle +// SignMode's by generating sign bytes from a Tx and SigningData +type SignModeHandler interface { + // DefaultMode is the default mode that is to be used with this handler if no + // other mode is specified. This can be useful for testing and CLI usage + DefaultMode() txtypes.SignMode + + // Modes is the list of modes supporting by this handler + Modes() []txtypes.SignMode + + // GetSignBytes returns the sign bytes for the provided SignMode, SigningData and Tx, + // or an error + GetSignBytes(mode txtypes.SignMode, data SigningData, tx sdk.Tx) ([]byte, error) +} + +// SigningData is the specific information needed to sign a transaction that generally +// isn't included in the transaction body itself type SigningData struct { + // ChainID is the chain that this transaction is targetted ChainID string + + // AccountNumber is the account number of the signer AccountNumber uint64 + + // AccountSequence is the account sequence number of the signer that is used + // for replay protection AccountSequence uint64 } -type SignModeHandler interface { - DefaultMode() txtypes.SignMode - Modes() []txtypes.SignMode - GetSignBytes(mode txtypes.SignMode, data SigningData, tx sdk.Tx) ([]byte, error) -} From cd92a572c5359632db81bc687b32d0a2239b8619 Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Mon, 8 Jun 2020 19:00:13 -0400 Subject: [PATCH 4/9] Add handler map --- x/auth/signing/amino/amino.go | 2 +- x/auth/signing/amino/amino_test.go | 16 +++--- x/auth/signing/handler_map.go | 59 +++++++++++++++++++ x/auth/signing/handler_map_test.go | 92 ++++++++++++++++++++++++++++++ 4 files changed, 161 insertions(+), 8 deletions(-) create mode 100644 x/auth/signing/handler_map.go create mode 100644 x/auth/signing/handler_map_test.go diff --git a/x/auth/signing/amino/amino.go b/x/auth/signing/amino/amino.go index bf6bd060effe..8bc5f69dfdaa 100644 --- a/x/auth/signing/amino/amino.go +++ b/x/auth/signing/amino/amino.go @@ -19,7 +19,7 @@ func (h LegacyAminoJSONHandler) DefaultMode() types.SignMode { return types.SignMode_SIGN_MODE_LEGACY_AMINO_JSON } -// DefaultMode implements SignModeHandler.Modes +// Modes implements SignModeHandler.Modes func (LegacyAminoJSONHandler) Modes() []types.SignMode { return []types.SignMode{types.SignMode_SIGN_MODE_LEGACY_AMINO_JSON} } diff --git a/x/auth/signing/amino/amino_test.go b/x/auth/signing/amino/amino_test.go index 1e1a7f41bc55..5fc8559ad41d 100644 --- a/x/auth/signing/amino/amino_test.go +++ b/x/auth/signing/amino/amino_test.go @@ -1,16 +1,18 @@ package amino_test import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/tendermint/tendermint/crypto/secp256k1" + sdk "github.com/cosmos/cosmos-sdk/types" txtypes "github.com/cosmos/cosmos-sdk/types/tx" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/auth/signing" "github.com/cosmos/cosmos-sdk/x/auth/signing/amino" - "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/cosmos/cosmos-sdk/x/bank" - "github.com/stretchr/testify/require" - "github.com/tendermint/tendermint/crypto/secp256k1" - "testing" ) func TestLegacyAminoJSONHandler_GetSignBytes(t *testing.T) { @@ -21,7 +23,7 @@ func TestLegacyAminoJSONHandler_GetSignBytes(t *testing.T) { coins := sdk.Coins{sdk.NewInt64Coin("foocoin", 10)} - fee := types.StdFee{ + fee := auth.StdFee{ Amount: coins, Gas: 10000, } @@ -56,7 +58,7 @@ func TestLegacyAminoJSONHandler_GetSignBytes(t *testing.T) { signBz, err := handler.GetSignBytes(txtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signingData, tx) require.NoError(t, err) - expectedSignBz := types.StdSignBytes(chainId, accNum, seqNum, fee, msgs, memo) + expectedSignBz := auth.StdSignBytes(chainId, accNum, seqNum, fee, msgs, memo) require.Equal(t, expectedSignBz, signBz) @@ -72,5 +74,5 @@ func TestLegacyAminoJSONHandler_DefaultMode(t *testing.T) { func TestLegacyAminoJSONHandler_Modes(t *testing.T) { handler := amino.LegacyAminoJSONHandler{} - require.Equal(t, []txtypes.SignMode{txtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON}, handler.DefaultMode()) + require.Equal(t, []txtypes.SignMode{txtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON}, handler.Modes()) } diff --git a/x/auth/signing/handler_map.go b/x/auth/signing/handler_map.go new file mode 100644 index 000000000000..41d0dc11b830 --- /dev/null +++ b/x/auth/signing/handler_map.go @@ -0,0 +1,59 @@ +package signing + +import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" + types "github.com/cosmos/cosmos-sdk/types/tx" +) + +// HandlerMap is SignModeHandler that aggregates multiple SignModeHandler's into +// a single handler +type HandlerMap struct { + defaultMode types.SignMode + modes []types.SignMode + signModeHandlers map[types.SignMode]SignModeHandler +} + +var _ SignModeHandler = HandlerMap{} + +// NewHandlerMap returns a new HandlerMap with the provided defaultMode and handlers +func NewHandlerMap(defaultMode types.SignMode, handlers []SignModeHandler) *HandlerMap { + handlerMap := make(map[types.SignMode]SignModeHandler) + var modes []types.SignMode + + for _, h := range handlers { + for _, m := range h.Modes() { + if _, have := handlerMap[m]; have { + panic(fmt.Errorf("duplicate sign mode handler for mode %s", m)) + } + handlerMap[m] = h + modes = append(modes, m) + } + } + + return &HandlerMap{ + defaultMode: defaultMode, + modes: modes, + signModeHandlers: handlerMap, + } +} + +// DefaultMode implements SignModeHandler.DefaultMode +func (h HandlerMap) DefaultMode() types.SignMode { + return h.defaultMode +} + +// Modes implements SignModeHandler.Modes +func (h HandlerMap) Modes() []types.SignMode { + return h.modes +} + +// DefaultMode implements SignModeHandler.GetSignBytes +func (h HandlerMap) GetSignBytes(mode types.SignMode, data SigningData, tx sdk.Tx) ([]byte, error) { + handler, found := h.signModeHandlers[mode] + if !found { + return nil, fmt.Errorf("can't verify sign mode %s", mode.String()) + } + return handler.GetSignBytes(mode, data, tx) +} + diff --git a/x/auth/signing/handler_map_test.go b/x/auth/signing/handler_map_test.go new file mode 100644 index 000000000000..8fe9bffe07b1 --- /dev/null +++ b/x/auth/signing/handler_map_test.go @@ -0,0 +1,92 @@ +package signing_test + +import ( + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/tendermint/tendermint/crypto/secp256k1" + + sdk "github.com/cosmos/cosmos-sdk/types" + txtypes "github.com/cosmos/cosmos-sdk/types/tx" + "github.com/cosmos/cosmos-sdk/x/auth/signing" + "github.com/cosmos/cosmos-sdk/x/auth/signing/amino" + "github.com/cosmos/cosmos-sdk/x/bank" +) + +func MakeTestHandlerMap() signing.SignModeHandler { + return signing.NewHandlerMap( + txtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, + []signing.SignModeHandler{ + amino.LegacyAminoJSONHandler{}, + }, + ) +} + +func TestHandlerMap_GetSignBytes(t *testing.T) { + priv1 := secp256k1.GenPrivKey() + addr1 := sdk.AccAddress(priv1.PubKey().Address()) + priv2 := secp256k1.GenPrivKey() + addr2 := sdk.AccAddress(priv2.PubKey().Address()) + + coins := sdk.Coins{sdk.NewInt64Coin("foocoin", 10)} + + fee := authtypes.StdFee{ + Amount: coins, + Gas: 10000, + } + memo := "foo" + msgs := []sdk.Msg{ + &bank.MsgSend{ + FromAddress: addr1, + ToAddress: addr2, + Amount: coins, + }, + } + + tx := authtypes.StdTx{ + Msgs: msgs, + Fee: fee, + Signatures: nil, + Memo: memo, + } + + var ( + chainId = "test-chain" + accNum uint64 = 7 + seqNum uint64 = 7 + ) + + handler := MakeTestHandlerMap() + aminoJSONHandler := amino.LegacyAminoJSONHandler{} + + signingData := signing.SigningData{ + ChainID: chainId, + AccountNumber: accNum, + AccountSequence: seqNum, + } + signBz, err := handler.GetSignBytes(txtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signingData, tx) + require.NoError(t, err) + + expectedSignBz, err := aminoJSONHandler.GetSignBytes(txtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signingData, tx) + require.NoError(t, err) + + require.Equal(t, expectedSignBz, signBz) + + // expect error with wrong sign mode + _, err = aminoJSONHandler.GetSignBytes(txtypes.SignMode_SIGN_MODE_DIRECT, signingData, tx) + require.Error(t, err) +} + +func TestHandlerMap_DefaultMode(t *testing.T) { + handler := MakeTestHandlerMap() + require.Equal(t, txtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, handler.DefaultMode()) +} + +func TestHandlerMap_Modes(t *testing.T) { + handler := MakeTestHandlerMap() + modes := handler.Modes() + require.Contains(t, modes, txtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON) + require.Len(t, modes, 1) +} From 144610799cf1015a580d7ec0b89a5f6bb242c0ed Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Mon, 8 Jun 2020 19:04:12 -0400 Subject: [PATCH 5/9] Rename HandlerMap --- x/auth/signing/handler_map.go | 19 +++++++++---------- x/auth/signing/handler_map_test.go | 2 +- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/x/auth/signing/handler_map.go b/x/auth/signing/handler_map.go index 41d0dc11b830..5c36ee2702f1 100644 --- a/x/auth/signing/handler_map.go +++ b/x/auth/signing/handler_map.go @@ -6,18 +6,18 @@ import ( types "github.com/cosmos/cosmos-sdk/types/tx" ) -// HandlerMap is SignModeHandler that aggregates multiple SignModeHandler's into +// SignModeHandlerMap is SignModeHandler that aggregates multiple SignModeHandler's into // a single handler -type HandlerMap struct { +type SignModeHandlerMap struct { defaultMode types.SignMode modes []types.SignMode signModeHandlers map[types.SignMode]SignModeHandler } -var _ SignModeHandler = HandlerMap{} +var _ SignModeHandler = SignModeHandlerMap{} -// NewHandlerMap returns a new HandlerMap with the provided defaultMode and handlers -func NewHandlerMap(defaultMode types.SignMode, handlers []SignModeHandler) *HandlerMap { +// NewSignModeHandlerMap returns a new SignModeHandlerMap with the provided defaultMode and handlers +func NewSignModeHandlerMap(defaultMode types.SignMode, handlers []SignModeHandler) *SignModeHandlerMap { handlerMap := make(map[types.SignMode]SignModeHandler) var modes []types.SignMode @@ -31,7 +31,7 @@ func NewHandlerMap(defaultMode types.SignMode, handlers []SignModeHandler) *Hand } } - return &HandlerMap{ + return &SignModeHandlerMap{ defaultMode: defaultMode, modes: modes, signModeHandlers: handlerMap, @@ -39,21 +39,20 @@ func NewHandlerMap(defaultMode types.SignMode, handlers []SignModeHandler) *Hand } // DefaultMode implements SignModeHandler.DefaultMode -func (h HandlerMap) DefaultMode() types.SignMode { +func (h SignModeHandlerMap) DefaultMode() types.SignMode { return h.defaultMode } // Modes implements SignModeHandler.Modes -func (h HandlerMap) Modes() []types.SignMode { +func (h SignModeHandlerMap) Modes() []types.SignMode { return h.modes } // DefaultMode implements SignModeHandler.GetSignBytes -func (h HandlerMap) GetSignBytes(mode types.SignMode, data SigningData, tx sdk.Tx) ([]byte, error) { +func (h SignModeHandlerMap) GetSignBytes(mode types.SignMode, data SigningData, tx sdk.Tx) ([]byte, error) { handler, found := h.signModeHandlers[mode] if !found { return nil, fmt.Errorf("can't verify sign mode %s", mode.String()) } return handler.GetSignBytes(mode, data, tx) } - diff --git a/x/auth/signing/handler_map_test.go b/x/auth/signing/handler_map_test.go index 8fe9bffe07b1..5a8e16597908 100644 --- a/x/auth/signing/handler_map_test.go +++ b/x/auth/signing/handler_map_test.go @@ -16,7 +16,7 @@ import ( ) func MakeTestHandlerMap() signing.SignModeHandler { - return signing.NewHandlerMap( + return signing.NewSignModeHandlerMap( txtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, []signing.SignModeHandler{ amino.LegacyAminoJSONHandler{}, From 012eeafe1ec2833f56e68ebd1024a09ecfa2b81a Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Mon, 8 Jun 2020 19:05:54 -0400 Subject: [PATCH 6/9] Don't use pointer --- x/auth/signing/handler_map.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/auth/signing/handler_map.go b/x/auth/signing/handler_map.go index 5c36ee2702f1..e1b27d346277 100644 --- a/x/auth/signing/handler_map.go +++ b/x/auth/signing/handler_map.go @@ -17,7 +17,7 @@ type SignModeHandlerMap struct { var _ SignModeHandler = SignModeHandlerMap{} // NewSignModeHandlerMap returns a new SignModeHandlerMap with the provided defaultMode and handlers -func NewSignModeHandlerMap(defaultMode types.SignMode, handlers []SignModeHandler) *SignModeHandlerMap { +func NewSignModeHandlerMap(defaultMode types.SignMode, handlers []SignModeHandler) SignModeHandlerMap { handlerMap := make(map[types.SignMode]SignModeHandler) var modes []types.SignMode @@ -31,7 +31,7 @@ func NewSignModeHandlerMap(defaultMode types.SignMode, handlers []SignModeHandle } } - return &SignModeHandlerMap{ + return SignModeHandlerMap{ defaultMode: defaultMode, modes: modes, signModeHandlers: handlerMap, From 7b69941368c07118decc0d545cc1dc0919da3f3d Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Mon, 8 Jun 2020 19:17:02 -0400 Subject: [PATCH 7/9] Lint --- x/auth/signing/amino/amino.go | 1 + x/auth/signing/handler_map.go | 1 + x/auth/signing/handler_map_test.go | 2 +- x/auth/signing/sign_mode_handler.go | 5 ++--- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/x/auth/signing/amino/amino.go b/x/auth/signing/amino/amino.go index 8bc5f69dfdaa..de5dafdad78b 100644 --- a/x/auth/signing/amino/amino.go +++ b/x/auth/signing/amino/amino.go @@ -2,6 +2,7 @@ package amino import ( "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" types "github.com/cosmos/cosmos-sdk/types/tx" "github.com/cosmos/cosmos-sdk/x/auth/ante" diff --git a/x/auth/signing/handler_map.go b/x/auth/signing/handler_map.go index e1b27d346277..bc6cc6357fc6 100644 --- a/x/auth/signing/handler_map.go +++ b/x/auth/signing/handler_map.go @@ -2,6 +2,7 @@ package signing import ( "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" types "github.com/cosmos/cosmos-sdk/types/tx" ) diff --git a/x/auth/signing/handler_map_test.go b/x/auth/signing/handler_map_test.go index 5a8e16597908..eee5490fdd66 100644 --- a/x/auth/signing/handler_map_test.go +++ b/x/auth/signing/handler_map_test.go @@ -1,7 +1,6 @@ package signing_test import ( - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "testing" "github.com/stretchr/testify/require" @@ -12,6 +11,7 @@ import ( txtypes "github.com/cosmos/cosmos-sdk/types/tx" "github.com/cosmos/cosmos-sdk/x/auth/signing" "github.com/cosmos/cosmos-sdk/x/auth/signing/amino" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/cosmos/cosmos-sdk/x/bank" ) diff --git a/x/auth/signing/sign_mode_handler.go b/x/auth/signing/sign_mode_handler.go index 4cfbd9906e03..b8cf11625bf7 100644 --- a/x/auth/signing/sign_mode_handler.go +++ b/x/auth/signing/sign_mode_handler.go @@ -24,13 +24,12 @@ type SignModeHandler interface { // isn't included in the transaction body itself type SigningData struct { // ChainID is the chain that this transaction is targetted - ChainID string + ChainID string // AccountNumber is the account number of the signer - AccountNumber uint64 + AccountNumber uint64 // AccountSequence is the account sequence number of the signer that is used // for replay protection AccountSequence uint64 } - From fef3c586b574ba7f8eda0c7eb5266533da35fcc7 Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Mon, 8 Jun 2020 19:34:22 -0400 Subject: [PATCH 8/9] Lint --- x/auth/signing/amino/amino.go | 2 +- x/auth/signing/amino/amino_test.go | 2 +- x/auth/signing/handler_map.go | 2 +- x/auth/signing/handler_map_test.go | 2 +- x/auth/signing/sign_mode_handler.go | 12 ++++++------ 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/x/auth/signing/amino/amino.go b/x/auth/signing/amino/amino.go index de5dafdad78b..8eb9133d2cdb 100644 --- a/x/auth/signing/amino/amino.go +++ b/x/auth/signing/amino/amino.go @@ -26,7 +26,7 @@ func (LegacyAminoJSONHandler) Modes() []types.SignMode { } // DefaultMode implements SignModeHandler.GetSignBytes -func (LegacyAminoJSONHandler) GetSignBytes(mode types.SignMode, data signing.SigningData, tx sdk.Tx) ([]byte, error) { +func (LegacyAminoJSONHandler) GetSignBytes(mode types.SignMode, data signing.SignerData, tx sdk.Tx) ([]byte, error) { if mode != types.SignMode_SIGN_MODE_LEGACY_AMINO_JSON { return nil, fmt.Errorf("expected %s, got %s", types.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, mode) } diff --git a/x/auth/signing/amino/amino_test.go b/x/auth/signing/amino/amino_test.go index 5fc8559ad41d..b72eaaffb576 100644 --- a/x/auth/signing/amino/amino_test.go +++ b/x/auth/signing/amino/amino_test.go @@ -50,7 +50,7 @@ func TestLegacyAminoJSONHandler_GetSignBytes(t *testing.T) { ) handler := amino.LegacyAminoJSONHandler{} - signingData := signing.SigningData{ + signingData := signing.SignerData{ ChainID: chainId, AccountNumber: accNum, AccountSequence: seqNum, diff --git a/x/auth/signing/handler_map.go b/x/auth/signing/handler_map.go index bc6cc6357fc6..90421db94304 100644 --- a/x/auth/signing/handler_map.go +++ b/x/auth/signing/handler_map.go @@ -50,7 +50,7 @@ func (h SignModeHandlerMap) Modes() []types.SignMode { } // DefaultMode implements SignModeHandler.GetSignBytes -func (h SignModeHandlerMap) GetSignBytes(mode types.SignMode, data SigningData, tx sdk.Tx) ([]byte, error) { +func (h SignModeHandlerMap) GetSignBytes(mode types.SignMode, data SignerData, tx sdk.Tx) ([]byte, error) { handler, found := h.signModeHandlers[mode] if !found { return nil, fmt.Errorf("can't verify sign mode %s", mode.String()) diff --git a/x/auth/signing/handler_map_test.go b/x/auth/signing/handler_map_test.go index eee5490fdd66..0133c38a1df6 100644 --- a/x/auth/signing/handler_map_test.go +++ b/x/auth/signing/handler_map_test.go @@ -61,7 +61,7 @@ func TestHandlerMap_GetSignBytes(t *testing.T) { handler := MakeTestHandlerMap() aminoJSONHandler := amino.LegacyAminoJSONHandler{} - signingData := signing.SigningData{ + signingData := signing.SignerData{ ChainID: chainId, AccountNumber: accNum, AccountSequence: seqNum, diff --git a/x/auth/signing/sign_mode_handler.go b/x/auth/signing/sign_mode_handler.go index b8cf11625bf7..2ad40e31a2ee 100644 --- a/x/auth/signing/sign_mode_handler.go +++ b/x/auth/signing/sign_mode_handler.go @@ -6,7 +6,7 @@ import ( ) // SignModeHandler defines a interface to be implemented by types which will handle -// SignMode's by generating sign bytes from a Tx and SigningData +// SignMode's by generating sign bytes from a Tx and SignerData type SignModeHandler interface { // DefaultMode is the default mode that is to be used with this handler if no // other mode is specified. This can be useful for testing and CLI usage @@ -15,15 +15,15 @@ type SignModeHandler interface { // Modes is the list of modes supporting by this handler Modes() []txtypes.SignMode - // GetSignBytes returns the sign bytes for the provided SignMode, SigningData and Tx, + // GetSignBytes returns the sign bytes for the provided SignMode, SignerData and Tx, // or an error - GetSignBytes(mode txtypes.SignMode, data SigningData, tx sdk.Tx) ([]byte, error) + GetSignBytes(mode txtypes.SignMode, data SignerData, tx sdk.Tx) ([]byte, error) } -// SigningData is the specific information needed to sign a transaction that generally +// SignerData is the specific information needed to sign a transaction that generally // isn't included in the transaction body itself -type SigningData struct { - // ChainID is the chain that this transaction is targetted +type SignerData struct { + // ChainID is the chain that this transaction is targeted ChainID string // AccountNumber is the account number of the signer From d6adc61e371f6ab326e278b53876357014491249 Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Tue, 9 Jun 2020 10:51:36 -0400 Subject: [PATCH 9/9] Update x/auth/signing/amino/amino.go Co-authored-by: Marko --- x/auth/signing/amino/amino.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/auth/signing/amino/amino.go b/x/auth/signing/amino/amino.go index 8eb9133d2cdb..5788fca75494 100644 --- a/x/auth/signing/amino/amino.go +++ b/x/auth/signing/amino/amino.go @@ -42,6 +42,6 @@ func (LegacyAminoJSONHandler) GetSignBytes(mode types.SignMode, data signing.Sig } return authtypes.StdSignBytes( - data.ChainID, data.AccountNumber, data.AccountSequence, authtypes.StdFee{Amount: feeTx.GetFee(), Gas: feeTx.GetGas()}, tx.GetMsgs(), memoTx.GetMemo(), + data.ChainID, data.AccountNumber, data.AccountSequence, authtypes.StdFee{Amount: feeTx.GetFee(), Gas: feeTx.GetGas()}, tx.GetMsgs(), memoTx.GetMemo(), // nolint:staticcheck // SA1019: authtypes.StdFee is deprecated, will be removed once proto migration is completed ), nil }