2021-09-14 02:42:42 -07:00
|
|
|
package tx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-12-08 03:43:15 -08:00
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
2021-09-14 02:42:42 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-10-18 03:32:43 -07:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2021-09-14 02:42:42 -07:00
|
|
|
types "github.com/cosmos/cosmos-sdk/types/tx"
|
|
|
|
signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/signing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ signing.SignModeHandler = signModeDirectAuxHandler{}
|
|
|
|
|
|
|
|
// signModeDirectAuxHandler defines the SIGN_MODE_DIRECT_AUX SignModeHandler
|
|
|
|
type signModeDirectAuxHandler struct{}
|
|
|
|
|
|
|
|
// DefaultMode implements SignModeHandler.DefaultMode
|
|
|
|
func (signModeDirectAuxHandler) DefaultMode() signingtypes.SignMode {
|
|
|
|
return signingtypes.SignMode_SIGN_MODE_DIRECT_AUX
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modes implements SignModeHandler.Modes
|
|
|
|
func (signModeDirectAuxHandler) Modes() []signingtypes.SignMode {
|
|
|
|
return []signingtypes.SignMode{signingtypes.SignMode_SIGN_MODE_DIRECT_AUX}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSignBytes implements SignModeHandler.GetSignBytes
|
|
|
|
func (signModeDirectAuxHandler) GetSignBytes(
|
|
|
|
mode signingtypes.SignMode, data signing.SignerData, tx sdk.Tx,
|
|
|
|
) ([]byte, error) {
|
|
|
|
|
|
|
|
if mode != signingtypes.SignMode_SIGN_MODE_DIRECT_AUX {
|
|
|
|
return nil, fmt.Errorf("expected %s, got %s", signingtypes.SignMode_SIGN_MODE_DIRECT_AUX, mode)
|
|
|
|
}
|
|
|
|
|
|
|
|
protoTx, ok := tx.(*wrapper)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("can only handle a protobuf Tx, got %T", tx)
|
|
|
|
}
|
|
|
|
|
2021-12-08 03:43:15 -08:00
|
|
|
pkAny, err := codectypes.NewAnyWithValue(data.PubKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-10-18 03:32:43 -07:00
|
|
|
}
|
|
|
|
|
2021-11-16 09:55:12 -08:00
|
|
|
addr := data.Address
|
|
|
|
if addr == "" {
|
|
|
|
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "got empty address in %s handler", signingtypes.SignMode_SIGN_MODE_DIRECT_AUX)
|
|
|
|
}
|
|
|
|
|
|
|
|
feePayer := protoTx.FeePayer().String()
|
|
|
|
|
|
|
|
// Fee payer cannot use SIGN_MODE_DIRECT_AUX, because SIGN_MODE_DIRECT_AUX
|
|
|
|
// does not sign over fees, which would create malleability issues.
|
|
|
|
if feePayer == data.Address {
|
|
|
|
return nil, sdkerrors.ErrUnauthorized.Wrapf("fee payer %s cannot sign with %s", feePayer, signingtypes.SignMode_SIGN_MODE_DIRECT_AUX)
|
|
|
|
}
|
|
|
|
|
2021-09-14 02:42:42 -07:00
|
|
|
signDocDirectAux := types.SignDocDirectAux{
|
|
|
|
BodyBytes: protoTx.getBodyBytes(),
|
|
|
|
ChainId: data.ChainID,
|
|
|
|
AccountNumber: data.AccountNumber,
|
2021-12-08 03:43:15 -08:00
|
|
|
Sequence: data.Sequence,
|
2021-09-14 02:42:42 -07:00
|
|
|
Tip: protoTx.tx.AuthInfo.Tip,
|
2021-12-08 03:43:15 -08:00
|
|
|
PublicKey: pkAny,
|
2021-09-14 02:42:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return signDocDirectAux.Marshal()
|
|
|
|
}
|