2020-08-03 12:47:25 -07:00
package tx
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
2020-09-22 07:35:18 -07:00
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
2020-08-03 12:47:25 -07:00
signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"
2021-07-05 07:55:51 -07:00
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
2020-08-03 12:47:25 -07:00
"github.com/cosmos/cosmos-sdk/x/auth/signing"
)
2020-08-07 16:32:22 -07:00
const aminoNonCriticalFieldsError = "protobuf transaction contains unknown non-critical fields. This is a transaction malleability issue and SIGN_MODE_LEGACY_AMINO_JSON cannot be used."
var _ signing . SignModeHandler = signModeLegacyAminoJSONHandler { }
// signModeLegacyAminoJSONHandler defines the SIGN_MODE_LEGACY_AMINO_JSON
// SignModeHandler.
2020-08-03 12:47:25 -07:00
type signModeLegacyAminoJSONHandler struct { }
func ( s signModeLegacyAminoJSONHandler ) DefaultMode ( ) signingtypes . SignMode {
return signingtypes . SignMode_SIGN_MODE_LEGACY_AMINO_JSON
}
func ( s signModeLegacyAminoJSONHandler ) Modes ( ) [ ] signingtypes . SignMode {
return [ ] signingtypes . SignMode { signingtypes . SignMode_SIGN_MODE_LEGACY_AMINO_JSON }
}
func ( s signModeLegacyAminoJSONHandler ) GetSignBytes ( mode signingtypes . SignMode , data signing . SignerData , tx sdk . Tx ) ( [ ] byte , error ) {
if mode != signingtypes . SignMode_SIGN_MODE_LEGACY_AMINO_JSON {
return nil , fmt . Errorf ( "expected %s, got %s" , signingtypes . SignMode_SIGN_MODE_LEGACY_AMINO_JSON , mode )
}
2020-08-24 07:41:08 -07:00
protoTx , ok := tx . ( * wrapper )
2020-08-03 12:47:25 -07:00
if ! ok {
return nil , fmt . Errorf ( "can only handle a protobuf Tx, got %T" , tx )
}
if protoTx . txBodyHasUnknownNonCriticals {
return nil , sdkerrors . Wrap ( sdkerrors . ErrInvalidRequest , aminoNonCriticalFieldsError )
}
body := protoTx . tx . Body
if len ( body . ExtensionOptions ) != 0 || len ( body . NonCriticalExtensionOptions ) != 0 {
2020-08-07 16:32:22 -07:00
return nil , sdkerrors . Wrap ( sdkerrors . ErrInvalidRequest , "SIGN_MODE_LEGACY_AMINO_JSON does not support protobuf extension options." )
2020-08-03 12:47:25 -07:00
}
2020-09-22 07:35:18 -07:00
return legacytx . StdSignBytes (
2020-08-21 07:20:47 -07:00
data . ChainID , data . AccountNumber , data . Sequence , protoTx . GetTimeoutHeight ( ) ,
2020-09-22 07:35:18 -07:00
legacytx . StdFee { Amount : protoTx . GetFee ( ) , Gas : protoTx . GetGas ( ) } ,
2020-08-03 12:47:25 -07:00
tx . GetMsgs ( ) , protoTx . GetMemo ( ) ,
) , nil
}