2022-12-16 09:48:38 +00:00
|
|
|
// Copyright 2021 Evmos Foundation
|
|
|
|
// This file is part of Evmos' Ethermint library.
|
|
|
|
//
|
|
|
|
// The Ethermint library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The Ethermint library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
|
2022-11-07 16:50:25 +00:00
|
|
|
package eip712
|
|
|
|
|
|
|
|
import (
|
2022-11-23 12:38:23 +00:00
|
|
|
"encoding/json"
|
2022-11-07 16:50:25 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/simapp/params"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
|
|
|
|
|
2022-11-23 12:38:23 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2022-11-07 16:50:25 +00:00
|
|
|
txTypes "github.com/cosmos/cosmos-sdk/types/tx"
|
|
|
|
|
|
|
|
apitypes "github.com/ethereum/go-ethereum/signer/core/apitypes"
|
|
|
|
ethermint "github.com/evmos/ethermint/types"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
)
|
|
|
|
|
2022-11-23 12:38:23 +00:00
|
|
|
type aminoMessage struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Value interface{} `json:"value"`
|
|
|
|
}
|
|
|
|
|
2022-11-07 16:50:25 +00:00
|
|
|
var (
|
2022-11-23 12:38:23 +00:00
|
|
|
protoCodec codec.ProtoCodecMarshaler
|
|
|
|
aminoCodec *codec.LegacyAmino
|
2022-11-07 16:50:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SetEncodingConfig set the encoding config to the singleton codecs (Amino and Protobuf).
|
|
|
|
// The process of unmarshaling SignDoc bytes into a SignDoc object requires having a codec
|
|
|
|
// populated with all relevant message types. As a result, we must call this method on app
|
|
|
|
// initialization with the app's encoding config.
|
|
|
|
func SetEncodingConfig(cfg params.EncodingConfig) {
|
2022-11-23 12:38:23 +00:00
|
|
|
aminoCodec = cfg.Amino
|
|
|
|
protoCodec = codec.NewProtoCodec(cfg.InterfaceRegistry)
|
2022-11-07 16:50:25 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 00:51:24 +00:00
|
|
|
// Get the EIP-712 object bytes for the given SignDoc bytes by first decoding the bytes into
|
2022-11-07 16:50:25 +00:00
|
|
|
// an EIP-712 object, then hashing the EIP-712 object to create the bytes to be signed.
|
|
|
|
// See https://eips.ethereum.org/EIPS/eip-712 for more.
|
2022-12-09 00:51:24 +00:00
|
|
|
func GetEIP712BytesForMsg(signDocBytes []byte) ([]byte, error) {
|
2022-11-07 16:50:25 +00:00
|
|
|
typedData, err := GetEIP712TypedDataForMsg(signDocBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-09 00:51:24 +00:00
|
|
|
_, rawData, err := apitypes.TypedDataAndHash(typedData)
|
2022-11-07 16:50:25 +00:00
|
|
|
if err != nil {
|
2022-12-09 00:51:24 +00:00
|
|
|
return nil, fmt.Errorf("could not get EIP-712 object bytes: %w", err)
|
2022-11-07 16:50:25 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 00:51:24 +00:00
|
|
|
return []byte(rawData), nil
|
2022-11-07 16:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetEIP712TypedDataForMsg returns the EIP-712 TypedData representation for either
|
|
|
|
// Amino or Protobuf encoded signature doc bytes.
|
|
|
|
func GetEIP712TypedDataForMsg(signDocBytes []byte) (apitypes.TypedData, error) {
|
|
|
|
// Attempt to decode as both Amino and Protobuf since the message format is unknown.
|
|
|
|
// If either decode works, we can move forward with the corresponding typed data.
|
|
|
|
typedDataAmino, errAmino := decodeAminoSignDoc(signDocBytes)
|
2022-11-23 12:38:23 +00:00
|
|
|
if errAmino == nil && isValidEIP712Payload(typedDataAmino) {
|
2022-11-07 16:50:25 +00:00
|
|
|
return typedDataAmino, nil
|
|
|
|
}
|
|
|
|
typedDataProtobuf, errProtobuf := decodeProtobufSignDoc(signDocBytes)
|
2022-11-23 12:38:23 +00:00
|
|
|
if errProtobuf == nil && isValidEIP712Payload(typedDataProtobuf) {
|
2022-11-07 16:50:25 +00:00
|
|
|
return typedDataProtobuf, nil
|
|
|
|
}
|
|
|
|
|
2022-11-23 12:38:23 +00:00
|
|
|
return apitypes.TypedData{}, fmt.Errorf("could not decode sign doc as either Amino or Protobuf.\n amino: %v\n protobuf: %v", errAmino, errProtobuf)
|
2022-11-07 16:50:25 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 12:38:23 +00:00
|
|
|
// isValidEIP712Payload ensures that the given TypedData does not contain empty fields from
|
2022-11-07 16:50:25 +00:00
|
|
|
// an improper initialization.
|
2022-11-23 12:38:23 +00:00
|
|
|
func isValidEIP712Payload(typedData apitypes.TypedData) bool {
|
2022-11-07 16:50:25 +00:00
|
|
|
return len(typedData.Message) != 0 && len(typedData.Types) != 0 && typedData.PrimaryType != "" && typedData.Domain != apitypes.TypedDataDomain{}
|
|
|
|
}
|
|
|
|
|
2022-11-23 12:38:23 +00:00
|
|
|
// decodeAminoSignDoc attempts to decode the provided sign doc (bytes) as an Amino payload
|
|
|
|
// and returns a signable EIP-712 TypedData object.
|
2022-11-07 16:50:25 +00:00
|
|
|
func decodeAminoSignDoc(signDocBytes []byte) (apitypes.TypedData, error) {
|
2022-12-09 16:37:00 +00:00
|
|
|
// Ensure codecs have been initialized
|
|
|
|
if err := validateCodecInit(); err != nil {
|
|
|
|
return apitypes.TypedData{}, err
|
|
|
|
}
|
|
|
|
|
2022-11-07 16:50:25 +00:00
|
|
|
var aminoDoc legacytx.StdSignDoc
|
2022-11-23 12:38:23 +00:00
|
|
|
if err := aminoCodec.UnmarshalJSON(signDocBytes, &aminoDoc); err != nil {
|
2022-11-07 16:50:25 +00:00
|
|
|
return apitypes.TypedData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var fees legacytx.StdFee
|
2022-11-23 12:38:23 +00:00
|
|
|
if err := aminoCodec.UnmarshalJSON(aminoDoc.Fee, &fees); err != nil {
|
2022-11-07 16:50:25 +00:00
|
|
|
return apitypes.TypedData{}, err
|
|
|
|
}
|
|
|
|
|
2022-11-23 12:38:23 +00:00
|
|
|
// Validate payload messages
|
|
|
|
msgs := make([]sdk.Msg, len(aminoDoc.Msgs))
|
|
|
|
for i, jsonMsg := range aminoDoc.Msgs {
|
|
|
|
var m sdk.Msg
|
|
|
|
if err := aminoCodec.UnmarshalJSON(jsonMsg, &m); err != nil {
|
|
|
|
return apitypes.TypedData{}, fmt.Errorf("failed to unmarshal sign doc message: %w", err)
|
|
|
|
}
|
|
|
|
msgs[i] = m
|
2022-11-07 16:50:25 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 12:38:23 +00:00
|
|
|
if err := validatePayloadMessages(msgs); err != nil {
|
|
|
|
return apitypes.TypedData{}, err
|
2022-11-07 16:50:25 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 12:38:23 +00:00
|
|
|
// Use first message for fee payer and type inference
|
|
|
|
msg := msgs[0]
|
|
|
|
|
|
|
|
// By convention, the fee payer is the first address in the list of signers.
|
2022-11-07 16:50:25 +00:00
|
|
|
feePayer := msg.GetSigners()[0]
|
|
|
|
feeDelegation := &FeeDelegationOptions{
|
|
|
|
FeePayer: feePayer,
|
|
|
|
}
|
|
|
|
|
|
|
|
chainID, err := ethermint.ParseChainID(aminoDoc.ChainID)
|
|
|
|
if err != nil {
|
|
|
|
return apitypes.TypedData{}, errors.New("invalid chain ID passed as argument")
|
|
|
|
}
|
|
|
|
|
|
|
|
typedData, err := WrapTxToTypedData(
|
2022-11-23 12:38:23 +00:00
|
|
|
protoCodec,
|
2022-11-07 16:50:25 +00:00
|
|
|
chainID.Uint64(),
|
|
|
|
msg,
|
2022-11-23 12:38:23 +00:00
|
|
|
signDocBytes,
|
2022-11-07 16:50:25 +00:00
|
|
|
feeDelegation,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return apitypes.TypedData{}, fmt.Errorf("could not convert to EIP712 representation: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return typedData, nil
|
|
|
|
}
|
|
|
|
|
2022-11-23 12:38:23 +00:00
|
|
|
// decodeProtobufSignDoc attempts to decode the provided sign doc (bytes) as a Protobuf payload
|
|
|
|
// and returns a signable EIP-712 TypedData object.
|
2022-11-07 16:50:25 +00:00
|
|
|
func decodeProtobufSignDoc(signDocBytes []byte) (apitypes.TypedData, error) {
|
2022-12-09 16:37:00 +00:00
|
|
|
// Ensure codecs have been initialized
|
|
|
|
if err := validateCodecInit(); err != nil {
|
|
|
|
return apitypes.TypedData{}, err
|
|
|
|
}
|
|
|
|
|
2022-11-07 16:50:25 +00:00
|
|
|
signDoc := &txTypes.SignDoc{}
|
|
|
|
if err := signDoc.Unmarshal(signDocBytes); err != nil {
|
|
|
|
return apitypes.TypedData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
authInfo := &txTypes.AuthInfo{}
|
|
|
|
if err := authInfo.Unmarshal(signDoc.AuthInfoBytes); err != nil {
|
|
|
|
return apitypes.TypedData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
body := &txTypes.TxBody{}
|
|
|
|
if err := body.Unmarshal(signDoc.BodyBytes); err != nil {
|
|
|
|
return apitypes.TypedData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Until support for these fields is added, throw an error at their presence
|
|
|
|
if body.TimeoutHeight != 0 || len(body.ExtensionOptions) != 0 || len(body.NonCriticalExtensionOptions) != 0 {
|
|
|
|
return apitypes.TypedData{}, errors.New("body contains unsupported fields: TimeoutHeight, ExtensionOptions, or NonCriticalExtensionOptions")
|
|
|
|
}
|
|
|
|
|
2022-11-23 12:38:23 +00:00
|
|
|
if len(authInfo.SignerInfos) != 1 {
|
|
|
|
return apitypes.TypedData{}, fmt.Errorf("invalid number of signer infos provided, expected 1 got %v", len(authInfo.SignerInfos))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate payload messages
|
|
|
|
msgs := make([]sdk.Msg, len(body.Messages))
|
|
|
|
for i, protoMsg := range body.Messages {
|
|
|
|
var m sdk.Msg
|
|
|
|
if err := protoCodec.UnpackAny(protoMsg, &m); err != nil {
|
|
|
|
return apitypes.TypedData{}, fmt.Errorf("could not unpack message object with error %w", err)
|
|
|
|
}
|
|
|
|
msgs[i] = m
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := validatePayloadMessages(msgs); err != nil {
|
|
|
|
return apitypes.TypedData{}, err
|
2022-11-07 16:50:25 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 12:38:23 +00:00
|
|
|
// Use first message for fee payer and type inference
|
|
|
|
msg := msgs[0]
|
|
|
|
|
2022-11-07 16:50:25 +00:00
|
|
|
signerInfo := authInfo.SignerInfos[0]
|
|
|
|
|
|
|
|
chainID, err := ethermint.ParseChainID(signDoc.ChainId)
|
|
|
|
if err != nil {
|
|
|
|
return apitypes.TypedData{}, fmt.Errorf("invalid chain ID passed as argument: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stdFee := &legacytx.StdFee{
|
|
|
|
Amount: authInfo.Fee.Amount,
|
|
|
|
Gas: authInfo.Fee.GasLimit,
|
|
|
|
}
|
|
|
|
|
|
|
|
feePayer := msg.GetSigners()[0]
|
|
|
|
feeDelegation := &FeeDelegationOptions{
|
|
|
|
FeePayer: feePayer,
|
|
|
|
}
|
|
|
|
|
|
|
|
tip := authInfo.Tip
|
|
|
|
|
2022-11-23 12:38:23 +00:00
|
|
|
// WrapTxToTypedData expects the payload as an Amino Sign Doc
|
2022-11-07 16:50:25 +00:00
|
|
|
signBytes := legacytx.StdSignBytes(
|
|
|
|
signDoc.ChainId,
|
|
|
|
signDoc.AccountNumber,
|
|
|
|
signerInfo.Sequence,
|
|
|
|
body.TimeoutHeight,
|
|
|
|
*stdFee,
|
2022-11-23 12:38:23 +00:00
|
|
|
msgs,
|
2022-11-07 16:50:25 +00:00
|
|
|
body.Memo,
|
|
|
|
tip,
|
|
|
|
)
|
|
|
|
|
|
|
|
typedData, err := WrapTxToTypedData(
|
2022-11-23 12:38:23 +00:00
|
|
|
protoCodec,
|
2022-11-07 16:50:25 +00:00
|
|
|
chainID.Uint64(),
|
|
|
|
msg,
|
|
|
|
signBytes,
|
|
|
|
feeDelegation,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return apitypes.TypedData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return typedData, nil
|
|
|
|
}
|
2022-11-23 12:38:23 +00:00
|
|
|
|
2022-12-09 16:37:00 +00:00
|
|
|
// validateCodecInit ensures that both Amino and Protobuf encoding codecs have been set on app init,
|
|
|
|
// so the module does not panic if either codec is not found.
|
|
|
|
func validateCodecInit() error {
|
|
|
|
if aminoCodec == nil || protoCodec == nil {
|
|
|
|
return errors.New("missing codec: codecs have not been properly initialized using SetEncodingConfig")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-23 12:38:23 +00:00
|
|
|
// validatePayloadMessages ensures that the transaction messages can be represented in an EIP-712
|
|
|
|
// encoding by checking that messages exist, are of the same type, and share a single signer.
|
|
|
|
func validatePayloadMessages(msgs []sdk.Msg) error {
|
|
|
|
if len(msgs) == 0 {
|
|
|
|
return errors.New("unable to build EIP-712 payload: transaction does contain any messages")
|
|
|
|
}
|
|
|
|
|
|
|
|
var msgType string
|
|
|
|
var msgSigner sdk.AccAddress
|
|
|
|
|
|
|
|
for i, m := range msgs {
|
|
|
|
t, err := getMsgType(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(m.GetSigners()) != 1 {
|
|
|
|
return errors.New("unable to build EIP-712 payload: expect exactly 1 signer")
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == 0 {
|
|
|
|
msgType = t
|
|
|
|
msgSigner = m.GetSigners()[0]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if t != msgType {
|
|
|
|
return errors.New("unable to build EIP-712 payload: different types of messages detected")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !msgSigner.Equals(m.GetSigners()[0]) {
|
|
|
|
return errors.New("unable to build EIP-712 payload: multiple signers detected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getMsgType returns the message type prefix for the given Cosmos SDK Msg
|
|
|
|
func getMsgType(msg sdk.Msg) (string, error) {
|
|
|
|
jsonBytes, err := aminoCodec.MarshalJSON(msg)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsonMsg aminoMessage
|
|
|
|
if err := json.Unmarshal(jsonBytes, &jsonMsg); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify Type was successfully filled in
|
|
|
|
if jsonMsg.Type == "" {
|
|
|
|
return "", errors.New("could not decode message: type is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
return jsonMsg.Type, nil
|
|
|
|
}
|