From 2cc424f7dba5f62cf1a412d33bf64512db7842a0 Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Fri, 21 Apr 2023 11:19:46 -0500 Subject: [PATCH 01/13] v2 adaptable tx --- tests/integration/gov/keeper/keeper_test.go | 7 +- x/auth/ante/sigverify.go | 2 +- x/auth/signing/adapter.go | 183 ++++++++++++++++++++ x/auth/signing/verify.go | 61 ------- x/auth/tx/builder.go | 17 +- x/auth/tx/config.go | 11 +- 6 files changed, 207 insertions(+), 74 deletions(-) create mode 100644 x/auth/signing/adapter.go diff --git a/tests/integration/gov/keeper/keeper_test.go b/tests/integration/gov/keeper/keeper_test.go index 18d277bef2b4..039253133404 100644 --- a/tests/integration/gov/keeper/keeper_test.go +++ b/tests/integration/gov/keeper/keeper_test.go @@ -3,10 +3,11 @@ package keeper_test import ( "testing" - "cosmossdk.io/simapp" cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" "gotest.tools/v3/assert" + "cosmossdk.io/simapp" + "github.com/cosmos/cosmos-sdk/baseapp" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" @@ -52,7 +53,7 @@ func initFixture(t *testing.T) *fixture { queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry()) v1.RegisterQueryServer(queryHelper, app.GovKeeper) legacyQueryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry()) - v1beta1.RegisterQueryServer(legacyQueryHelper, keeper.NewLegacyQueryServer(app.GovKeeper)) + v1beta1.RegisterQueryServer(legacyQueryHelper, keeper.NewLegacyQueryServer(&app.GovKeeper)) queryClient := v1.NewQueryClient(queryHelper) legacyQueryClient := v1beta1.NewQueryClient(legacyQueryHelper) @@ -60,7 +61,7 @@ func initFixture(t *testing.T) *fixture { f.ctx = ctx f.queryClient = queryClient f.legacyQueryClient = legacyQueryClient - f.msgSrvr = keeper.NewMsgServerImpl(f.app.GovKeeper) + f.msgSrvr = keeper.NewMsgServerImpl(&f.app.GovKeeper) govAcct := f.app.GovKeeper.GetGovernanceAccount(f.ctx).GetAddress() f.legacyMsgSrvr = keeper.NewLegacyMsgServerImpl(govAcct.String(), f.msgSrvr) diff --git a/x/auth/ante/sigverify.go b/x/auth/ante/sigverify.go index c38ec4741d94..7280f4205d5a 100644 --- a/x/auth/ante/sigverify.go +++ b/x/auth/ante/sigverify.go @@ -239,7 +239,7 @@ func OnlyLegacyAminoSigners(sigData signing.SignatureData) bool { } func (svd SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { - sigTx, ok := tx.(authsigning.SigVerifiableTx) + sigTx, ok := tx.(authsigning.Tx) if !ok { return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "invalid transaction type") } diff --git a/x/auth/signing/adapter.go b/x/auth/signing/adapter.go new file mode 100644 index 000000000000..01f00a008aa8 --- /dev/null +++ b/x/auth/signing/adapter.go @@ -0,0 +1,183 @@ +package signing + +import ( + "context" + "fmt" + + "google.golang.org/protobuf/types/known/anypb" + + basev1beta1 "cosmossdk.io/api/cosmos/base/v1beta1" + multisigv1beta1 "cosmossdk.io/api/cosmos/crypto/multisig/v1beta1" + signingv1beta1 "cosmossdk.io/api/cosmos/tx/signing/v1beta1" + txv1beta1 "cosmossdk.io/api/cosmos/tx/v1beta1" + txsigning "cosmossdk.io/x/tx/signing" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/tx" + "github.com/cosmos/cosmos-sdk/types/tx/signing" +) + +type V2AdaptableTx interface { + Tx + GetBodyBytes() []byte + GetAuthInfoBytes() []byte + GetSignerInfos() []*tx.SignerInfo + GetTxBody() *tx.TxBody +} + +// GetSignBytesAdapter returns the sign bytes for a given transaction and sign mode. It accepts the arguments expected +// for signing in x/auth/tx and converts them to the arguments expected by the txsigning.HandlerMap, then applies +// HandlerMap.GetSignBytes to get the sign bytes. +func GetSignBytesAdapter( + ctx context.Context, + encoder sdk.TxEncoder, + handlerMap *txsigning.HandlerMap, + mode signing.SignMode, + signerData SignerData, + tx sdk.Tx, +) ([]byte, error) { + adaptableTx, ok := tx.(V2AdaptableTx) + if !ok { + return nil, fmt.Errorf("expected tx to be V2AdaptableTx, got %T", tx) + } + txData := AdapterToTxData(adaptableTx) + + txSignMode, err := internalSignModeToAPI(mode) + if err != nil { + return nil, err + } + + anyPk, err := codectypes.NewAnyWithValue(signerData.PubKey) + if err != nil { + return nil, err + } + + txSignerData := txsigning.SignerData{ + ChainID: signerData.ChainID, + AccountNumber: signerData.AccountNumber, + Sequence: signerData.Sequence, + Address: signerData.Address, + PubKey: &anypb.Any{ + TypeUrl: anyPk.TypeUrl, + Value: anyPk.Value, + }, + } + // Generate the bytes to be signed. + return handlerMap.GetSignBytes(ctx, txSignMode, txSignerData, txData) +} + +func AdapterToTxData(adaptableTx V2AdaptableTx) txsigning.TxData { + body := adaptableTx.GetTxBody() + + msgs := make([]*anypb.Any, len(body.Messages)) + for i, msg := range body.Messages { + msgs[i] = &anypb.Any{ + TypeUrl: msg.TypeUrl, + Value: msg.Value, + } + } + + extOptions := make([]*anypb.Any, len(body.ExtensionOptions)) + for i, extOption := range body.ExtensionOptions { + extOptions[i] = &anypb.Any{ + TypeUrl: extOption.TypeUrl, + Value: extOption.Value, + } + } + + nonCriticalExtOptions := make([]*anypb.Any, len(body.NonCriticalExtensionOptions)) + for i, extOption := range body.NonCriticalExtensionOptions { + nonCriticalExtOptions[i] = &anypb.Any{ + TypeUrl: extOption.TypeUrl, + Value: extOption.Value, + } + } + + feeCoins := adaptableTx.GetFee() + feeAmount := make([]*basev1beta1.Coin, len(feeCoins)) + for i, coin := range feeCoins { + feeAmount[i] = &basev1beta1.Coin{ + Denom: coin.Denom, + Amount: coin.Amount.String(), + } + } + + tip := adaptableTx.GetTip() + tipCoins := tip.GetAmount() + tipAmount := make([]*basev1beta1.Coin, len(tipCoins)) + for i, coin := range tipCoins { + tipAmount[i] = &basev1beta1.Coin{ + Denom: coin.Denom, + Amount: coin.Amount.String(), + } + } + + signerInfos := adaptableTx.GetSignerInfos() + txSignerInfos := make([]*txv1beta1.SignerInfo, len(signerInfos)) + for i, signerInfo := range signerInfos { + modeInfo := &txv1beta1.ModeInfo{} + AdaptModeInfo(signerInfo.ModeInfo, modeInfo) + txSignerInfo := &txv1beta1.SignerInfo{ + PublicKey: &anypb.Any{ + TypeUrl: signerInfo.PublicKey.TypeUrl, + Value: signerInfo.PublicKey.Value, + }, + Sequence: signerInfo.Sequence, + ModeInfo: modeInfo, + } + txSignerInfos[i] = txSignerInfo + } + + txAuthInfo := &txv1beta1.AuthInfo{ + SignerInfos: txSignerInfos, + Fee: &txv1beta1.Fee{ + Amount: feeAmount, + GasLimit: adaptableTx.GetGas(), + Payer: adaptableTx.FeePayer().String(), + Granter: adaptableTx.FeeGranter().String(), + }, + Tip: &txv1beta1.Tip{ + Amount: tipAmount, + Tipper: tip.Tipper, + }, + } + + txBody := &txv1beta1.TxBody{ + Messages: msgs, + Memo: body.Memo, + TimeoutHeight: body.TimeoutHeight, + ExtensionOptions: extOptions, + NonCriticalExtensionOptions: nonCriticalExtOptions, + } + return txsigning.TxData{ + AuthInfo: txAuthInfo, + AuthInfoBytes: adaptableTx.GetAuthInfoBytes(), + Body: txBody, + BodyBytes: adaptableTx.GetBodyBytes(), + } +} + +func AdaptModeInfo(legacy *tx.ModeInfo, res *txv1beta1.ModeInfo) { + switch mi := legacy.Sum.(type) { + case *tx.ModeInfo_Single_: + res.Sum = &txv1beta1.ModeInfo_Single_{ + Single: &txv1beta1.ModeInfo_Single{ + Mode: signingv1beta1.SignMode(legacy.GetSingle().Mode), + }, + } + case *tx.ModeInfo_Multi_: + modeInfos := make([]*txv1beta1.ModeInfo, len(legacy.GetMulti().ModeInfos)) + for i, modeInfo := range legacy.GetMulti().ModeInfos { + AdaptModeInfo(modeInfo, modeInfos[i]) + } + res.Sum = &txv1beta1.ModeInfo_Multi_{ + Multi: &txv1beta1.ModeInfo_Multi{ + Bitarray: &multisigv1beta1.CompactBitArray{ + Elems: mi.Multi.Bitarray.Elems, + ExtraBitsStored: mi.Multi.Bitarray.ExtraBitsStored, + }, + ModeInfos: modeInfos, + }, + } + } +} diff --git a/x/auth/signing/verify.go b/x/auth/signing/verify.go index 6455e60b782a..6dbf7543a26e 100644 --- a/x/auth/signing/verify.go +++ b/x/auth/signing/verify.go @@ -4,16 +4,10 @@ import ( "context" "fmt" - "google.golang.org/protobuf/types/known/anypb" - signingv1beta1 "cosmossdk.io/api/cosmos/tx/signing/v1beta1" - "cosmossdk.io/x/tx/decode" txsigning "cosmossdk.io/x/tx/signing" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/crypto/types/multisig" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/registry" "github.com/cosmos/cosmos-sdk/types/tx/signing" ) @@ -107,58 +101,3 @@ func VerifySignature( return fmt.Errorf("unexpected SignatureData %T", signatureData) } } - -// GetSignBytesAdapter returns the sign bytes for a given transaction and sign mode. It accepts the arguments expected -// for signing in x/auth/tx and converts them to the arguments expected by the txsigning.HandlerMap, then applies -// HandlerMap.GetSignBytes to get the sign bytes. -func GetSignBytesAdapter( - ctx context.Context, - encoder sdk.TxEncoder, - handlerMap *txsigning.HandlerMap, - mode signing.SignMode, - signerData SignerData, - tx sdk.Tx, -) ([]byte, error) { - // round trip performance hit. - // could be avoided if we had a way to get the bytes from the txBuilder. - txBytes, err := encoder(tx) - if err != nil { - return nil, err - } - decodeCtx, err := decode.NewDecoder(decode.Options{ProtoFiles: registry.MergedProtoRegistry()}) - if err != nil { - return nil, err - } - decodedTx, err := decodeCtx.Decode(txBytes) - if err != nil { - return nil, err - } - txData := txsigning.TxData{ - Body: decodedTx.Tx.Body, - AuthInfo: decodedTx.Tx.AuthInfo, - AuthInfoBytes: decodedTx.TxRaw.AuthInfoBytes, - BodyBytes: decodedTx.TxRaw.BodyBytes, - } - txSignMode, err := internalSignModeToAPI(mode) - if err != nil { - return nil, err - } - - anyPk, err := codectypes.NewAnyWithValue(signerData.PubKey) - if err != nil { - return nil, err - } - - txSignerData := txsigning.SignerData{ - ChainID: signerData.ChainID, - AccountNumber: signerData.AccountNumber, - Sequence: signerData.Sequence, - Address: signerData.Address, - PubKey: &anypb.Any{ - TypeUrl: anyPk.TypeUrl, - Value: anyPk.Value, - }, - } - // Generate the bytes to be signed. - return handlerMap.GetSignBytes(ctx, txSignMode, txSignerData, txData) -} diff --git a/x/auth/tx/builder.go b/x/auth/tx/builder.go index e796f6884d0c..54e25f2bb4b0 100644 --- a/x/auth/tx/builder.go +++ b/x/auth/tx/builder.go @@ -4,7 +4,6 @@ import ( "github.com/cosmos/gogoproto/proto" errorsmod "cosmossdk.io/errors" - "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" @@ -467,3 +466,19 @@ func (w *wrapper) AddAuxSignerData(data tx.AuxSignerData) error { return nil } + +func (w *wrapper) GetSignerInfos() []*tx.SignerInfo { + return w.tx.AuthInfo.SignerInfos +} + +func (w *wrapper) GetBody() *tx.TxBody { + return w.tx.Body +} + +func (w *wrapper) GetBodyBytes() []byte { + return w.bodyBz +} + +func (w *wrapper) GetAuthInfoBytes() []byte { + return w.authInfoBz +} diff --git a/x/auth/tx/config.go b/x/auth/tx/config.go index 6d89cd41ecd8..75d41cf0438b 100644 --- a/x/auth/tx/config.go +++ b/x/auth/tx/config.go @@ -40,11 +40,7 @@ func NewTxConfig(protoCodec codec.ProtoCodecMarshaler, enabledSignModes []signin customSignModes ...txsigning.SignModeHandler, ) client.TxConfig { typeResolver := protoregistry.GlobalTypes - protoFiles := protoCodec.InterfaceRegistry() - signersContext, err := txsigning.NewGetSignersContext(txsigning.GetSignersOptions{ProtoFiles: protoFiles}) - if err != nil { - panic(err) - } + signingContext := protoCodec.InterfaceRegistry().SigningContext() signModeOptions := &SignModeOptions{} for _, m := range enabledSignModes { @@ -53,14 +49,13 @@ func NewTxConfig(protoCodec codec.ProtoCodecMarshaler, enabledSignModes []signin signModeOptions.Direct = &direct.SignModeHandler{} case signingtypes.SignMode_SIGN_MODE_DIRECT_AUX: signModeOptions.DirectAux = &directaux.SignModeHandlerOptions{ - FileResolver: protoFiles, TypeResolver: typeResolver, - SignersContext: signersContext, + SignersContext: signingContext, } case signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON: aminoJSONEncoder := aminojson.NewAminoJSON() signModeOptions.AminoJSON = &aminojson.SignModeHandlerOptions{ - FileResolver: protoFiles, + FileResolver: protoCodec.InterfaceRegistry(), TypeResolver: typeResolver, Encoder: &aminoJSONEncoder, } From b4aeecea90202be9955a9d1a66e5f89905d03065 Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Fri, 21 Apr 2023 11:28:13 -0500 Subject: [PATCH 02/13] clean up --- x/auth/ante/sigverify.go | 21 ++++----------------- x/auth/tx/config.go | 11 ++++++++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/x/auth/ante/sigverify.go b/x/auth/ante/sigverify.go index 7280f4205d5a..844101ecb481 100644 --- a/x/auth/ante/sigverify.go +++ b/x/auth/ante/sigverify.go @@ -10,10 +10,8 @@ import ( errorsmod "cosmossdk.io/errors" storetypes "cosmossdk.io/store/types" - "cosmossdk.io/x/tx/decode" txsigning "cosmossdk.io/x/tx/signing" codectypes "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/cosmos/cosmos-sdk/types/registry" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" @@ -300,22 +298,11 @@ func (svd SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simul Value: anyPk.Value, }, } - decodeCtx, err := decode.NewDecoder(decode.Options{ProtoFiles: registry.MergedProtoRegistry()}) - if err != nil { - return ctx, err - } - // note: this is performance hit is temporary. Ultimately, the tx will be decoded once in BaseApp, - // but for now we need double decoding to support both SignModeHandlers. - decodedTx, err := decodeCtx.Decode(ctx.TxBytes()) - if err != nil { - return ctx, err - } - txData := txsigning.TxData{ - Body: decodedTx.Tx.Body, - AuthInfo: decodedTx.Tx.AuthInfo, - AuthInfoBytes: decodedTx.TxRaw.AuthInfoBytes, - BodyBytes: decodedTx.TxRaw.BodyBytes, + adaptableTx, ok := tx.(authsigning.V2AdaptableTx) + if !ok { + return ctx, fmt.Errorf("expected tx to implement V2AdaptableTx, got %T", tx) } + txData := authsigning.AdapterToTxData(adaptableTx) err = authsigning.VerifySignature(ctx, pubKey, signerData, sig.Data, svd.signModeHandler, txData) if err != nil { var errMsg string diff --git a/x/auth/tx/config.go b/x/auth/tx/config.go index 75d41cf0438b..6d89cd41ecd8 100644 --- a/x/auth/tx/config.go +++ b/x/auth/tx/config.go @@ -40,7 +40,11 @@ func NewTxConfig(protoCodec codec.ProtoCodecMarshaler, enabledSignModes []signin customSignModes ...txsigning.SignModeHandler, ) client.TxConfig { typeResolver := protoregistry.GlobalTypes - signingContext := protoCodec.InterfaceRegistry().SigningContext() + protoFiles := protoCodec.InterfaceRegistry() + signersContext, err := txsigning.NewGetSignersContext(txsigning.GetSignersOptions{ProtoFiles: protoFiles}) + if err != nil { + panic(err) + } signModeOptions := &SignModeOptions{} for _, m := range enabledSignModes { @@ -49,13 +53,14 @@ func NewTxConfig(protoCodec codec.ProtoCodecMarshaler, enabledSignModes []signin signModeOptions.Direct = &direct.SignModeHandler{} case signingtypes.SignMode_SIGN_MODE_DIRECT_AUX: signModeOptions.DirectAux = &directaux.SignModeHandlerOptions{ + FileResolver: protoFiles, TypeResolver: typeResolver, - SignersContext: signingContext, + SignersContext: signersContext, } case signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON: aminoJSONEncoder := aminojson.NewAminoJSON() signModeOptions.AminoJSON = &aminojson.SignModeHandlerOptions{ - FileResolver: protoCodec.InterfaceRegistry(), + FileResolver: protoFiles, TypeResolver: typeResolver, Encoder: &aminoJSONEncoder, } From 00a357cc02ff59b6b6a2bc710d1871dd11b62188 Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Fri, 21 Apr 2023 11:31:43 -0500 Subject: [PATCH 03/13] implement V2AdaptableTx in wrapper --- x/auth/ante/sigverify.go | 2 +- x/auth/signing/adapter.go | 4 ++-- x/auth/tx/builder.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/x/auth/ante/sigverify.go b/x/auth/ante/sigverify.go index 844101ecb481..3a4d837952d5 100644 --- a/x/auth/ante/sigverify.go +++ b/x/auth/ante/sigverify.go @@ -302,7 +302,7 @@ func (svd SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simul if !ok { return ctx, fmt.Errorf("expected tx to implement V2AdaptableTx, got %T", tx) } - txData := authsigning.AdapterToTxData(adaptableTx) + txData := authsigning.AdaptableToTxData(adaptableTx) err = authsigning.VerifySignature(ctx, pubKey, signerData, sig.Data, svd.signModeHandler, txData) if err != nil { var errMsg string diff --git a/x/auth/signing/adapter.go b/x/auth/signing/adapter.go index 01f00a008aa8..7c75712c0d34 100644 --- a/x/auth/signing/adapter.go +++ b/x/auth/signing/adapter.go @@ -40,7 +40,7 @@ func GetSignBytesAdapter( if !ok { return nil, fmt.Errorf("expected tx to be V2AdaptableTx, got %T", tx) } - txData := AdapterToTxData(adaptableTx) + txData := AdaptableToTxData(adaptableTx) txSignMode, err := internalSignModeToAPI(mode) if err != nil { @@ -66,7 +66,7 @@ func GetSignBytesAdapter( return handlerMap.GetSignBytes(ctx, txSignMode, txSignerData, txData) } -func AdapterToTxData(adaptableTx V2AdaptableTx) txsigning.TxData { +func AdaptableToTxData(adaptableTx V2AdaptableTx) txsigning.TxData { body := adaptableTx.GetTxBody() msgs := make([]*anypb.Any, len(body.Messages)) diff --git a/x/auth/tx/builder.go b/x/auth/tx/builder.go index 54e25f2bb4b0..e04733c01fd8 100644 --- a/x/auth/tx/builder.go +++ b/x/auth/tx/builder.go @@ -471,7 +471,7 @@ func (w *wrapper) GetSignerInfos() []*tx.SignerInfo { return w.tx.AuthInfo.SignerInfos } -func (w *wrapper) GetBody() *tx.TxBody { +func (w *wrapper) GetTxBody() *tx.TxBody { return w.tx.Body } From 2a8956374328ff73a2ea9e77fa007a64579aba6a Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Fri, 21 Apr 2023 11:51:06 -0500 Subject: [PATCH 04/13] fix nil ref bug --- x/auth/signing/adapter.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/x/auth/signing/adapter.go b/x/auth/signing/adapter.go index 7c75712c0d34..1ce7751f4288 100644 --- a/x/auth/signing/adapter.go +++ b/x/auth/signing/adapter.go @@ -105,6 +105,10 @@ func AdaptableToTxData(adaptableTx V2AdaptableTx) txsigning.TxData { tip := adaptableTx.GetTip() tipCoins := tip.GetAmount() tipAmount := make([]*basev1beta1.Coin, len(tipCoins)) + var tipper string + if tip != nil { + tipper = tip.GetTipper() + } for i, coin := range tipCoins { tipAmount[i] = &basev1beta1.Coin{ Denom: coin.Denom, @@ -138,7 +142,7 @@ func AdaptableToTxData(adaptableTx V2AdaptableTx) txsigning.TxData { }, Tip: &txv1beta1.Tip{ Amount: tipAmount, - Tipper: tip.Tipper, + Tipper: tipper, }, } From 17bd6fad037e0f643583e831ef8b1c2d024931a6 Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Fri, 21 Apr 2023 13:18:10 -0500 Subject: [PATCH 05/13] bug fix --- x/auth/signing/adapter.go | 26 +++++++++++++------------- x/auth/tx/builder.go | 4 ++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/x/auth/signing/adapter.go b/x/auth/signing/adapter.go index 1ce7751f4288..2b9ccb09eddc 100644 --- a/x/auth/signing/adapter.go +++ b/x/auth/signing/adapter.go @@ -102,17 +102,20 @@ func AdaptableToTxData(adaptableTx V2AdaptableTx) txsigning.TxData { } } + var txTip *txv1beta1.Tip tip := adaptableTx.GetTip() - tipCoins := tip.GetAmount() - tipAmount := make([]*basev1beta1.Coin, len(tipCoins)) - var tipper string if tip != nil { - tipper = tip.GetTipper() - } - for i, coin := range tipCoins { - tipAmount[i] = &basev1beta1.Coin{ - Denom: coin.Denom, - Amount: coin.Amount.String(), + tipCoins := tip.GetAmount() + tipAmount := make([]*basev1beta1.Coin, len(tipCoins)) + for i, coin := range tipCoins { + tipAmount[i] = &basev1beta1.Coin{ + Denom: coin.Denom, + Amount: coin.Amount.String(), + } + } + txTip = &txv1beta1.Tip{ + Amount: tipAmount, + Tipper: tip.Tipper, } } @@ -140,10 +143,7 @@ func AdaptableToTxData(adaptableTx V2AdaptableTx) txsigning.TxData { Payer: adaptableTx.FeePayer().String(), Granter: adaptableTx.FeeGranter().String(), }, - Tip: &txv1beta1.Tip{ - Amount: tipAmount, - Tipper: tipper, - }, + Tip: txTip, } txBody := &txv1beta1.TxBody{ diff --git a/x/auth/tx/builder.go b/x/auth/tx/builder.go index e04733c01fd8..07a32f1c6b4d 100644 --- a/x/auth/tx/builder.go +++ b/x/auth/tx/builder.go @@ -476,9 +476,9 @@ func (w *wrapper) GetTxBody() *tx.TxBody { } func (w *wrapper) GetBodyBytes() []byte { - return w.bodyBz + return w.getBodyBytes() } func (w *wrapper) GetAuthInfoBytes() []byte { - return w.authInfoBz + return w.getAuthInfoBytes() } From 71314629ff5158f1e576cb91ef085855cf7a7cee Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Mon, 24 Apr 2023 10:32:32 -0500 Subject: [PATCH 06/13] Smaller interface --- x/auth/ante/sigverify.go | 2 +- x/auth/signing/adapter.go | 133 +------------------------------------- x/auth/tx/adapter.go | 133 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+), 132 deletions(-) create mode 100644 x/auth/tx/adapter.go diff --git a/x/auth/ante/sigverify.go b/x/auth/ante/sigverify.go index 3a4d837952d5..6021842b2a8f 100644 --- a/x/auth/ante/sigverify.go +++ b/x/auth/ante/sigverify.go @@ -302,7 +302,7 @@ func (svd SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simul if !ok { return ctx, fmt.Errorf("expected tx to implement V2AdaptableTx, got %T", tx) } - txData := authsigning.AdaptableToTxData(adaptableTx) + txData := adaptableTx.GetSigningTxData() err = authsigning.VerifySignature(ctx, pubKey, signerData, sig.Data, svd.signModeHandler, txData) if err != nil { var errMsg string diff --git a/x/auth/signing/adapter.go b/x/auth/signing/adapter.go index 2b9ccb09eddc..f8bf705e3972 100644 --- a/x/auth/signing/adapter.go +++ b/x/auth/signing/adapter.go @@ -6,23 +6,14 @@ import ( "google.golang.org/protobuf/types/known/anypb" - basev1beta1 "cosmossdk.io/api/cosmos/base/v1beta1" - multisigv1beta1 "cosmossdk.io/api/cosmos/crypto/multisig/v1beta1" - signingv1beta1 "cosmossdk.io/api/cosmos/tx/signing/v1beta1" - txv1beta1 "cosmossdk.io/api/cosmos/tx/v1beta1" txsigning "cosmossdk.io/x/tx/signing" codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/tx" "github.com/cosmos/cosmos-sdk/types/tx/signing" ) type V2AdaptableTx interface { - Tx - GetBodyBytes() []byte - GetAuthInfoBytes() []byte - GetSignerInfos() []*tx.SignerInfo - GetTxBody() *tx.TxBody + GetSigningTxData() txsigning.TxData } // GetSignBytesAdapter returns the sign bytes for a given transaction and sign mode. It accepts the arguments expected @@ -40,7 +31,7 @@ func GetSignBytesAdapter( if !ok { return nil, fmt.Errorf("expected tx to be V2AdaptableTx, got %T", tx) } - txData := AdaptableToTxData(adaptableTx) + txData := adaptableTx.GetSigningTxData() txSignMode, err := internalSignModeToAPI(mode) if err != nil { @@ -65,123 +56,3 @@ func GetSignBytesAdapter( // Generate the bytes to be signed. return handlerMap.GetSignBytes(ctx, txSignMode, txSignerData, txData) } - -func AdaptableToTxData(adaptableTx V2AdaptableTx) txsigning.TxData { - body := adaptableTx.GetTxBody() - - msgs := make([]*anypb.Any, len(body.Messages)) - for i, msg := range body.Messages { - msgs[i] = &anypb.Any{ - TypeUrl: msg.TypeUrl, - Value: msg.Value, - } - } - - extOptions := make([]*anypb.Any, len(body.ExtensionOptions)) - for i, extOption := range body.ExtensionOptions { - extOptions[i] = &anypb.Any{ - TypeUrl: extOption.TypeUrl, - Value: extOption.Value, - } - } - - nonCriticalExtOptions := make([]*anypb.Any, len(body.NonCriticalExtensionOptions)) - for i, extOption := range body.NonCriticalExtensionOptions { - nonCriticalExtOptions[i] = &anypb.Any{ - TypeUrl: extOption.TypeUrl, - Value: extOption.Value, - } - } - - feeCoins := adaptableTx.GetFee() - feeAmount := make([]*basev1beta1.Coin, len(feeCoins)) - for i, coin := range feeCoins { - feeAmount[i] = &basev1beta1.Coin{ - Denom: coin.Denom, - Amount: coin.Amount.String(), - } - } - - var txTip *txv1beta1.Tip - tip := adaptableTx.GetTip() - if tip != nil { - tipCoins := tip.GetAmount() - tipAmount := make([]*basev1beta1.Coin, len(tipCoins)) - for i, coin := range tipCoins { - tipAmount[i] = &basev1beta1.Coin{ - Denom: coin.Denom, - Amount: coin.Amount.String(), - } - } - txTip = &txv1beta1.Tip{ - Amount: tipAmount, - Tipper: tip.Tipper, - } - } - - signerInfos := adaptableTx.GetSignerInfos() - txSignerInfos := make([]*txv1beta1.SignerInfo, len(signerInfos)) - for i, signerInfo := range signerInfos { - modeInfo := &txv1beta1.ModeInfo{} - AdaptModeInfo(signerInfo.ModeInfo, modeInfo) - txSignerInfo := &txv1beta1.SignerInfo{ - PublicKey: &anypb.Any{ - TypeUrl: signerInfo.PublicKey.TypeUrl, - Value: signerInfo.PublicKey.Value, - }, - Sequence: signerInfo.Sequence, - ModeInfo: modeInfo, - } - txSignerInfos[i] = txSignerInfo - } - - txAuthInfo := &txv1beta1.AuthInfo{ - SignerInfos: txSignerInfos, - Fee: &txv1beta1.Fee{ - Amount: feeAmount, - GasLimit: adaptableTx.GetGas(), - Payer: adaptableTx.FeePayer().String(), - Granter: adaptableTx.FeeGranter().String(), - }, - Tip: txTip, - } - - txBody := &txv1beta1.TxBody{ - Messages: msgs, - Memo: body.Memo, - TimeoutHeight: body.TimeoutHeight, - ExtensionOptions: extOptions, - NonCriticalExtensionOptions: nonCriticalExtOptions, - } - return txsigning.TxData{ - AuthInfo: txAuthInfo, - AuthInfoBytes: adaptableTx.GetAuthInfoBytes(), - Body: txBody, - BodyBytes: adaptableTx.GetBodyBytes(), - } -} - -func AdaptModeInfo(legacy *tx.ModeInfo, res *txv1beta1.ModeInfo) { - switch mi := legacy.Sum.(type) { - case *tx.ModeInfo_Single_: - res.Sum = &txv1beta1.ModeInfo_Single_{ - Single: &txv1beta1.ModeInfo_Single{ - Mode: signingv1beta1.SignMode(legacy.GetSingle().Mode), - }, - } - case *tx.ModeInfo_Multi_: - modeInfos := make([]*txv1beta1.ModeInfo, len(legacy.GetMulti().ModeInfos)) - for i, modeInfo := range legacy.GetMulti().ModeInfos { - AdaptModeInfo(modeInfo, modeInfos[i]) - } - res.Sum = &txv1beta1.ModeInfo_Multi_{ - Multi: &txv1beta1.ModeInfo_Multi{ - Bitarray: &multisigv1beta1.CompactBitArray{ - Elems: mi.Multi.Bitarray.Elems, - ExtraBitsStored: mi.Multi.Bitarray.ExtraBitsStored, - }, - ModeInfos: modeInfos, - }, - } - } -} diff --git a/x/auth/tx/adapter.go b/x/auth/tx/adapter.go new file mode 100644 index 000000000000..0833227cc8e7 --- /dev/null +++ b/x/auth/tx/adapter.go @@ -0,0 +1,133 @@ +package tx + +import ( + "google.golang.org/protobuf/types/known/anypb" + + basev1beta1 "cosmossdk.io/api/cosmos/base/v1beta1" + multisigv1beta1 "cosmossdk.io/api/cosmos/crypto/multisig/v1beta1" + signingv1beta1 "cosmossdk.io/api/cosmos/tx/signing/v1beta1" + txv1beta1 "cosmossdk.io/api/cosmos/tx/v1beta1" + txsigning "cosmossdk.io/x/tx/signing" + "github.com/cosmos/cosmos-sdk/types/tx" +) + +func (w *wrapper) GetSigningTxData() txsigning.TxData { + body := w.tx.Body + authInfo := w.tx.AuthInfo + + msgs := make([]*anypb.Any, len(body.Messages)) + for i, msg := range body.Messages { + msgs[i] = &anypb.Any{ + TypeUrl: msg.TypeUrl, + Value: msg.Value, + } + } + + extOptions := make([]*anypb.Any, len(body.ExtensionOptions)) + for i, extOption := range body.ExtensionOptions { + extOptions[i] = &anypb.Any{ + TypeUrl: extOption.TypeUrl, + Value: extOption.Value, + } + } + + nonCriticalExtOptions := make([]*anypb.Any, len(body.NonCriticalExtensionOptions)) + for i, extOption := range body.NonCriticalExtensionOptions { + nonCriticalExtOptions[i] = &anypb.Any{ + TypeUrl: extOption.TypeUrl, + Value: extOption.Value, + } + } + + feeCoins := authInfo.Fee.Amount + feeAmount := make([]*basev1beta1.Coin, len(feeCoins)) + for i, coin := range feeCoins { + feeAmount[i] = &basev1beta1.Coin{ + Denom: coin.Denom, + Amount: coin.Amount.String(), + } + } + + var txTip *txv1beta1.Tip + tip := authInfo.Tip + if tip != nil { + tipCoins := tip.GetAmount() + tipAmount := make([]*basev1beta1.Coin, len(tipCoins)) + for i, coin := range tipCoins { + tipAmount[i] = &basev1beta1.Coin{ + Denom: coin.Denom, + Amount: coin.Amount.String(), + } + } + txTip = &txv1beta1.Tip{ + Amount: tipAmount, + Tipper: tip.Tipper, + } + } + + signerInfos := w.GetSignerInfos() + txSignerInfos := make([]*txv1beta1.SignerInfo, len(signerInfos)) + for i, signerInfo := range signerInfos { + modeInfo := &txv1beta1.ModeInfo{} + adaptModeInfo(signerInfo.ModeInfo, modeInfo) + txSignerInfo := &txv1beta1.SignerInfo{ + PublicKey: &anypb.Any{ + TypeUrl: signerInfo.PublicKey.TypeUrl, + Value: signerInfo.PublicKey.Value, + }, + Sequence: signerInfo.Sequence, + ModeInfo: modeInfo, + } + txSignerInfos[i] = txSignerInfo + } + + txAuthInfo := &txv1beta1.AuthInfo{ + SignerInfos: txSignerInfos, + Fee: &txv1beta1.Fee{ + Amount: feeAmount, + GasLimit: authInfo.Fee.GasLimit, + Payer: authInfo.Fee.Payer, + Granter: authInfo.Fee.Granter, + }, + Tip: txTip, + } + + txBody := &txv1beta1.TxBody{ + Messages: msgs, + Memo: body.Memo, + TimeoutHeight: body.TimeoutHeight, + ExtensionOptions: extOptions, + NonCriticalExtensionOptions: nonCriticalExtOptions, + } + return txsigning.TxData{ + AuthInfo: txAuthInfo, + AuthInfoBytes: w.authInfoBz, + Body: txBody, + BodyBytes: w.bodyBz, + } +} + +func adaptModeInfo(legacy *tx.ModeInfo, res *txv1beta1.ModeInfo) { + switch mi := legacy.Sum.(type) { + case *tx.ModeInfo_Single_: + res.Sum = &txv1beta1.ModeInfo_Single_{ + Single: &txv1beta1.ModeInfo_Single{ + Mode: signingv1beta1.SignMode(legacy.GetSingle().Mode), + }, + } + case *tx.ModeInfo_Multi_: + modeInfos := make([]*txv1beta1.ModeInfo, len(legacy.GetMulti().ModeInfos)) + for i, modeInfo := range legacy.GetMulti().ModeInfos { + adaptModeInfo(modeInfo, modeInfos[i]) + } + res.Sum = &txv1beta1.ModeInfo_Multi_{ + Multi: &txv1beta1.ModeInfo_Multi{ + Bitarray: &multisigv1beta1.CompactBitArray{ + Elems: mi.Multi.Bitarray.Elems, + ExtraBitsStored: mi.Multi.Bitarray.ExtraBitsStored, + }, + ModeInfos: modeInfos, + }, + } + } +} From 485599f0befa98dab032599fd760b94a0d85ca81 Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Mon, 24 Apr 2023 11:14:28 -0500 Subject: [PATCH 07/13] use private getter to gen bytes in tests --- x/auth/tx/adapter.go | 12 ++++++------ x/auth/tx/builder.go | 16 ---------------- 2 files changed, 6 insertions(+), 22 deletions(-) diff --git a/x/auth/tx/adapter.go b/x/auth/tx/adapter.go index 0833227cc8e7..370ba8b88096 100644 --- a/x/auth/tx/adapter.go +++ b/x/auth/tx/adapter.go @@ -65,9 +65,8 @@ func (w *wrapper) GetSigningTxData() txsigning.TxData { } } - signerInfos := w.GetSignerInfos() - txSignerInfos := make([]*txv1beta1.SignerInfo, len(signerInfos)) - for i, signerInfo := range signerInfos { + txSignerInfos := make([]*txv1beta1.SignerInfo, len(authInfo.SignerInfos)) + for i, signerInfo := range authInfo.SignerInfos { modeInfo := &txv1beta1.ModeInfo{} adaptModeInfo(signerInfo.ModeInfo, modeInfo) txSignerInfo := &txv1beta1.SignerInfo{ @@ -99,12 +98,13 @@ func (w *wrapper) GetSigningTxData() txsigning.TxData { ExtensionOptions: extOptions, NonCriticalExtensionOptions: nonCriticalExtOptions, } - return txsigning.TxData{ + txData := txsigning.TxData{ AuthInfo: txAuthInfo, - AuthInfoBytes: w.authInfoBz, + AuthInfoBytes: w.getAuthInfoBytes(), Body: txBody, - BodyBytes: w.bodyBz, + BodyBytes: w.getBodyBytes(), } + return txData } func adaptModeInfo(legacy *tx.ModeInfo, res *txv1beta1.ModeInfo) { diff --git a/x/auth/tx/builder.go b/x/auth/tx/builder.go index 07a32f1c6b4d..d9452a170a7f 100644 --- a/x/auth/tx/builder.go +++ b/x/auth/tx/builder.go @@ -466,19 +466,3 @@ func (w *wrapper) AddAuxSignerData(data tx.AuxSignerData) error { return nil } - -func (w *wrapper) GetSignerInfos() []*tx.SignerInfo { - return w.tx.AuthInfo.SignerInfos -} - -func (w *wrapper) GetTxBody() *tx.TxBody { - return w.tx.Body -} - -func (w *wrapper) GetBodyBytes() []byte { - return w.getBodyBytes() -} - -func (w *wrapper) GetAuthInfoBytes() []byte { - return w.getAuthInfoBytes() -} From 81c910571960cca4c7ca497ddb64cdb9d53994df Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Mon, 24 Apr 2023 11:50:30 -0500 Subject: [PATCH 08/13] fix mistake in recursive callg --- x/auth/tx/adapter.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/x/auth/tx/adapter.go b/x/auth/tx/adapter.go index 370ba8b88096..e6c82c8d0f56 100644 --- a/x/auth/tx/adapter.go +++ b/x/auth/tx/adapter.go @@ -116,9 +116,10 @@ func adaptModeInfo(legacy *tx.ModeInfo, res *txv1beta1.ModeInfo) { }, } case *tx.ModeInfo_Multi_: - modeInfos := make([]*txv1beta1.ModeInfo, len(legacy.GetMulti().ModeInfos)) - for i, modeInfo := range legacy.GetMulti().ModeInfos { - adaptModeInfo(modeInfo, modeInfos[i]) + multiModeInfos := legacy.GetMulti().ModeInfos + modeInfos := make([]*txv1beta1.ModeInfo, len(multiModeInfos)) + for _, modeInfo := range multiModeInfos { + adaptModeInfo(modeInfo, &txv1beta1.ModeInfo{}) } res.Sum = &txv1beta1.ModeInfo_Multi_{ Multi: &txv1beta1.ModeInfo_Multi{ From 6b9de696f4c03ceb3d3e9104182afbc5a094573b Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Mon, 24 Apr 2023 11:53:31 -0500 Subject: [PATCH 09/13] revert keeper_test change --- tests/integration/gov/keeper/keeper_test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/integration/gov/keeper/keeper_test.go b/tests/integration/gov/keeper/keeper_test.go index 039253133404..18d277bef2b4 100644 --- a/tests/integration/gov/keeper/keeper_test.go +++ b/tests/integration/gov/keeper/keeper_test.go @@ -3,11 +3,10 @@ package keeper_test import ( "testing" + "cosmossdk.io/simapp" cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" "gotest.tools/v3/assert" - "cosmossdk.io/simapp" - "github.com/cosmos/cosmos-sdk/baseapp" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" @@ -53,7 +52,7 @@ func initFixture(t *testing.T) *fixture { queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry()) v1.RegisterQueryServer(queryHelper, app.GovKeeper) legacyQueryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry()) - v1beta1.RegisterQueryServer(legacyQueryHelper, keeper.NewLegacyQueryServer(&app.GovKeeper)) + v1beta1.RegisterQueryServer(legacyQueryHelper, keeper.NewLegacyQueryServer(app.GovKeeper)) queryClient := v1.NewQueryClient(queryHelper) legacyQueryClient := v1beta1.NewQueryClient(legacyQueryHelper) @@ -61,7 +60,7 @@ func initFixture(t *testing.T) *fixture { f.ctx = ctx f.queryClient = queryClient f.legacyQueryClient = legacyQueryClient - f.msgSrvr = keeper.NewMsgServerImpl(&f.app.GovKeeper) + f.msgSrvr = keeper.NewMsgServerImpl(f.app.GovKeeper) govAcct := f.app.GovKeeper.GetGovernanceAccount(f.ctx).GetAddress() f.legacyMsgSrvr = keeper.NewLegacyMsgServerImpl(govAcct.String(), f.msgSrvr) From 11365336d0c6c37548b0b55940a62cf47502bb10 Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Mon, 24 Apr 2023 11:57:24 -0500 Subject: [PATCH 10/13] remove now unused argument --- client/tx/tx.go | 4 ++-- testutil/sims/tx_helpers.go | 2 +- testutil/testnet/genesis.go | 1 - x/auth/ante/feegrant_test.go | 2 +- x/auth/signing/adapter.go | 4 +++- x/auth/tx/aux_test.go | 2 +- x/auth/tx/direct_test.go | 4 ++-- x/auth/tx/testutil/suite.go | 4 ++-- 8 files changed, 12 insertions(+), 11 deletions(-) diff --git a/client/tx/tx.go b/client/tx/tx.go index 78de623a460e..d9446906c42a 100644 --- a/client/tx/tx.go +++ b/client/tx/tx.go @@ -173,7 +173,7 @@ func SignWithPrivKey( // Generate the bytes to be signed. signBytes, err := authsigning.GetSignBytesAdapter( - ctx, txConfig.TxEncoder(), txConfig.SignModeHandler(), signMode, signerData, txBuilder.GetTx()) + ctx, txConfig.SignModeHandler(), signMode, signerData, txBuilder.GetTx()) if err != nil { return sigV2, err } @@ -319,7 +319,7 @@ func Sign(ctx context.Context, txf Factory, name string, txBuilder client.TxBuil } bytesToSign, err := authsigning.GetSignBytesAdapter( - ctx, txf.txConfig.TxEncoder(), txf.txConfig.SignModeHandler(), + ctx, txf.txConfig.SignModeHandler(), signMode, signerData, txBuilder.GetTx()) if err != nil { return err diff --git a/testutil/sims/tx_helpers.go b/testutil/sims/tx_helpers.go index 415ea292be4a..dc4ae15ed301 100644 --- a/testutil/sims/tx_helpers.go +++ b/testutil/sims/tx_helpers.go @@ -67,7 +67,7 @@ func GenSignedMockTx(r *rand.Rand, txConfig client.TxConfig, msgs []sdk.Msg, fee } signBytes, err := authsign.GetSignBytesAdapter( - context.Background(), txConfig.TxEncoder(), txConfig.SignModeHandler(), signMode, signerData, + context.Background(), txConfig.SignModeHandler(), signMode, signerData, tx.GetTx()) if err != nil { panic(err) diff --git a/testutil/testnet/genesis.go b/testutil/testnet/genesis.go index bfc1755c8160..fb62d9e4f165 100644 --- a/testutil/testnet/genesis.go +++ b/testutil/testnet/genesis.go @@ -143,7 +143,6 @@ func (b *GenesisBuilder) GenTx(privVal secp256k1.PrivKey, val cmttypes.GenesisVa // Generate bytes to be signed. bytesToSign, err := authsigning.GetSignBytesAdapter( context.Background(), - txConf.TxEncoder(), txConf.SignModeHandler(), signing.SignMode_SIGN_MODE_DIRECT, authsigning.SignerData{ diff --git a/x/auth/ante/feegrant_test.go b/x/auth/ante/feegrant_test.go index 31cebeaec981..b8b1a893dc35 100644 --- a/x/auth/ante/feegrant_test.go +++ b/x/auth/ante/feegrant_test.go @@ -242,7 +242,7 @@ func genTxWithFeeGranter(gen client.TxConfig, msgs []sdk.Msg, feeAmt sdk.Coins, PubKey: p.PubKey(), } signBytes, err := authsign.GetSignBytesAdapter( - context.Background(), gen.TxEncoder(), gen.SignModeHandler(), signMode, signerData, tx.GetTx()) + context.Background(), gen.SignModeHandler(), signMode, signerData, tx.GetTx()) if err != nil { panic(err) } diff --git a/x/auth/signing/adapter.go b/x/auth/signing/adapter.go index f8bf705e3972..db1bb5fc65c8 100644 --- a/x/auth/signing/adapter.go +++ b/x/auth/signing/adapter.go @@ -12,6 +12,9 @@ import ( "github.com/cosmos/cosmos-sdk/types/tx/signing" ) +// V2AdaptableTx is an interface that wraps the GetSigningTxData method. +// GetSigningTxData returns an x/tx/signing.TxData representation of a transaction for use in signing +// interoperability with x/tx. type V2AdaptableTx interface { GetSigningTxData() txsigning.TxData } @@ -21,7 +24,6 @@ type V2AdaptableTx interface { // HandlerMap.GetSignBytes to get the sign bytes. func GetSignBytesAdapter( ctx context.Context, - encoder sdk.TxEncoder, handlerMap *txsigning.HandlerMap, mode signing.SignMode, signerData SignerData, diff --git a/x/auth/tx/aux_test.go b/x/auth/tx/aux_test.go index 90481f5767cc..5f62efa2987d 100644 --- a/x/auth/tx/aux_test.go +++ b/x/auth/tx/aux_test.go @@ -140,7 +140,7 @@ func TestBuilderWithAux(t *testing.T) { } signBz, err = authsigning.GetSignBytesAdapter( - context.Background(), txConfig.TxEncoder(), txConfig.SignModeHandler(), signing.SignMode_SIGN_MODE_DIRECT, + context.Background(), txConfig.SignModeHandler(), signing.SignMode_SIGN_MODE_DIRECT, signerData, w.GetTx()) require.NoError(t, err) diff --git a/x/auth/tx/direct_test.go b/x/auth/tx/direct_test.go index 09365541ee63..737deda61ffb 100644 --- a/x/auth/tx/direct_test.go +++ b/x/auth/tx/direct_test.go @@ -78,7 +78,7 @@ func TestDirectModeHandler(t *testing.T) { } signBytes, err := signing.GetSignBytesAdapter( - context.Background(), txConfig.TxEncoder(), txConfig.SignModeHandler(), defaultSignMode, signingData, + context.Background(), txConfig.SignModeHandler(), defaultSignMode, signingData, txBuilder.GetTx()) require.NoError(t, err) require.NotNil(t, signBytes) @@ -124,7 +124,7 @@ func TestDirectModeHandler(t *testing.T) { err = txBuilder.SetSignatures(sig) require.NoError(t, err) signBytes, err = signing.GetSignBytesAdapter( - context.Background(), txConfig.TxEncoder(), txConfig.SignModeHandler(), defaultSignMode, signingData, + context.Background(), txConfig.SignModeHandler(), defaultSignMode, signingData, txBuilder.GetTx()) require.NoError(t, err) require.Equal(t, expectedSignBytes, signBytes) diff --git a/x/auth/tx/testutil/suite.go b/x/auth/tx/testutil/suite.go index ab9ab69096d6..d4f2cbd884bb 100644 --- a/x/auth/tx/testutil/suite.go +++ b/x/auth/tx/testutil/suite.go @@ -138,7 +138,7 @@ func (s *TxConfigTestSuite) TestTxBuilderSetSignatures() { Sequence: seq1, PubKey: pubkey, } - signBytes, err := signing.GetSignBytesAdapter(context.Background(), s.TxConfig.TxEncoder(), + signBytes, err := signing.GetSignBytesAdapter(context.Background(), s.TxConfig.SignModeHandler(), defaultSignMode, signerData, sigTx) s.Require().NoError(err) sigBz, err := privKey.Sign(signBytes) @@ -151,7 +151,7 @@ func (s *TxConfigTestSuite) TestTxBuilderSetSignatures() { Sequence: mseq, PubKey: multisigPk, } - mSignBytes, err := signing.GetSignBytesAdapter(context.Background(), s.TxConfig.TxEncoder(), + mSignBytes, err := signing.GetSignBytesAdapter(context.Background(), s.TxConfig.SignModeHandler(), defaultSignMode, signerData, sigTx) s.Require().NoError(err) mSigBz1, err := privKey.Sign(mSignBytes) From 8598ac5d2f772e0bb96bbeb544a153bb4c2d234a Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Mon, 24 Apr 2023 12:22:50 -0500 Subject: [PATCH 11/13] rosetta fix --- tools/rosetta/converter.go | 2 +- tools/rosetta/go.mod | 3 +++ tools/rosetta/go.sum | 2 -- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/tools/rosetta/converter.go b/tools/rosetta/converter.go index 429add923a75..3bbdbe9fb42e 100644 --- a/tools/rosetta/converter.go +++ b/tools/rosetta/converter.go @@ -116,7 +116,7 @@ func NewConverter(cdc *codec.ProtoCodec, ir codectypes.InterfaceRegistry, cfg sd txEncode: cfg.TxEncoder(), bytesToSign: func(tx authsigning.Tx, signerData authsigning.SignerData) (b []byte, err error) { bytesToSign, err := authsigning.GetSignBytesAdapter( - context.Background(), cfg.TxEncoder(), cfg.SignModeHandler(), + context.Background(), cfg.SignModeHandler(), signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signerData, tx) if err != nil { return nil, err diff --git a/tools/rosetta/go.mod b/tools/rosetta/go.mod index 7979dd8039f5..6f855eda4170 100644 --- a/tools/rosetta/go.mod +++ b/tools/rosetta/go.mod @@ -138,3 +138,6 @@ require ( pgregory.net/rapid v0.5.5 // indirect sigs.k8s.io/yaml v1.3.0 // indirect ) + +// this seems to be required +replace github.com/cosmos/cosmos-sdk => ../.. diff --git a/tools/rosetta/go.sum b/tools/rosetta/go.sum index c7cc27856918..91ffa461dfe8 100644 --- a/tools/rosetta/go.sum +++ b/tools/rosetta/go.sum @@ -172,8 +172,6 @@ github.com/cosmos/cosmos-db v1.0.0-rc.1 h1:SjnT8B6WKMW9WEIX32qMhnEEKcI7ZP0+G1Sa9 github.com/cosmos/cosmos-db v1.0.0-rc.1/go.mod h1:Dnmk3flSf5lkwCqvvjNpoxjpXzhxnCAFzKHlbaForso= github.com/cosmos/cosmos-proto v1.0.0-beta.3 h1:VitvZ1lPORTVxkmF2fAp3IiA61xVwArQYKXTdEcpW6o= github.com/cosmos/cosmos-proto v1.0.0-beta.3/go.mod h1:t8IASdLaAq+bbHbjq4p960BvcTqtwuAxid3b/2rOD6I= -github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230424095137-b73c17cb9cc8 h1:zIl1WnrW5ZP1VwhpbwVBZtCntkNKYNIkg4233/dZ3BU= -github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230424095137-b73c17cb9cc8/go.mod h1:JicgV9n3SAu5uuoyDvQ2gSHYLyFvyRrIUYB5T2Q4HRw= github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d/go.mod h1:tSxLoYXyBmiFeKpvmq4dzayMdCjCnu8uqmCysIGBT2Y= github.com/cosmos/go-bip39 v1.0.0 h1:pcomnQdrdH22njcAatO0yWojsUnCO3y2tNoV1cb6hHY= github.com/cosmos/go-bip39 v1.0.0/go.mod h1:RNJv0H/pOIVgxw6KS7QeX2a0Uo0aKUlfhZ4xuwvCdJw= From e461f2ccfec17bd8e86a324e9fb609eb4d5c0362 Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Mon, 24 Apr 2023 12:32:05 -0500 Subject: [PATCH 12/13] include comment --- x/auth/tx/adapter.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/x/auth/tx/adapter.go b/x/auth/tx/adapter.go index e6c82c8d0f56..6c811d796d07 100644 --- a/x/auth/tx/adapter.go +++ b/x/auth/tx/adapter.go @@ -11,6 +11,8 @@ import ( "github.com/cosmos/cosmos-sdk/types/tx" ) +// GetSigningTxData returns an x/tx/signing.TxData representation of a transaction for use in the signing +// API defined in x/tx. func (w *wrapper) GetSigningTxData() txsigning.TxData { body := w.tx.Body authInfo := w.tx.AuthInfo From 4340a37bcbfa6673eebd1e7b6649f63f4ecdeb1c Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Mon, 24 Apr 2023 12:50:54 -0500 Subject: [PATCH 13/13] add nil guard --- x/auth/tx/adapter.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/x/auth/tx/adapter.go b/x/auth/tx/adapter.go index 6c811d796d07..1c2f113a7b83 100644 --- a/x/auth/tx/adapter.go +++ b/x/auth/tx/adapter.go @@ -110,6 +110,14 @@ func (w *wrapper) GetSigningTxData() txsigning.TxData { } func adaptModeInfo(legacy *tx.ModeInfo, res *txv1beta1.ModeInfo) { + // handle nil modeInfo. this is permissible through the code path: + // https://github.com/cosmos/cosmos-sdk/blob/4a6a1e3cb8de459891cb0495052589673d14ef51/x/auth/tx/builder.go#L295 + // -> https://github.com/cosmos/cosmos-sdk/blob/b7841e3a76a38d069c1b9cb3d48368f7a67e9c26/x/auth/tx/sigs.go#L15-L17 + // when signature.Data is nil. + if legacy == nil { + return + } + switch mi := legacy.Sum.(type) { case *tx.ModeInfo_Single_: res.Sum = &txv1beta1.ModeInfo_Single_{