Skip to content

Commit

Permalink
Merge pull request #276 from InjectiveLabs/fix/eip712v1-wrapper
Browse files Browse the repository at this point in the history
Fix: EIP712Wrapper v1
  • Loading branch information
maxim-inj authored Jan 23, 2025
2 parents 6bf611d + e8625f9 commit 26a0cd8
Show file tree
Hide file tree
Showing 3 changed files with 904 additions and 14 deletions.
46 changes: 40 additions & 6 deletions eip712_cosmos.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ante
package sdk

import (
"bytes"
Expand All @@ -16,15 +16,24 @@ import (
cosmtypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/cosmos/gogoproto/proto"
"github.com/ethereum/go-ethereum/common"
ethmath "github.com/ethereum/go-ethereum/common/math"
"github.com/pkg/errors"

"github.com/InjectiveLabs/sdk-go/typeddata"
)

var _ AminoProtoCodecMarshaler = (*codec.ProtoCodec)(nil)

type AminoProtoCodecMarshaler interface {
codec.Codec

MarshalAminoJSON(msg proto.Message) ([]byte, error)
}

type EIP712Wrapper func(
cdc codec.ProtoCodecMarshaler,
cdc AminoProtoCodecMarshaler,
chainID uint64,
signerData *authsigning.SignerData,
timeoutHeight uint64,
Expand All @@ -37,7 +46,7 @@ type EIP712Wrapper func(
// WrapTxToEIP712 is an ultimate method that wraps Amino-encoded Cosmos Tx JSON data
// into an EIP712-compatible request. All messages must be of the same type.
func WrapTxToEIP712(
cdc codec.ProtoCodecMarshaler,
cdc AminoProtoCodecMarshaler,
chainID uint64,
signerData *authsigning.SignerData,
timeoutHeight uint64,
Expand All @@ -52,7 +61,7 @@ func WrapTxToEIP712(
signerData.Sequence,
timeoutHeight,
feeInfo,
msgs, memo,
[]cosmtypes.Msg{}, memo,
)

txData := make(map[string]interface{})
Expand All @@ -61,6 +70,31 @@ func WrapTxToEIP712(
return typeddata.TypedData{}, err
}

msgsJsons := make([]json.RawMessage, len(msgs))
for idx, m := range msgs {
bzMsg, err := cdc.MarshalAminoJSON(m)
if err != nil {
return typeddata.TypedData{}, errors.Wrapf(err, "cannot marshal msg JSON at index %d", idx)
}

msgsJsons[idx] = bzMsg
}

bzMsgs, err := json.Marshal(map[string][]json.RawMessage{
"msgs": msgsJsons,
})
if err != nil {
return typeddata.TypedData{}, errors.Wrap(err, "marshal msgs JSON")
}

msgsData := make(map[string]interface{})
if err := json.Unmarshal(bzMsgs, &msgsData); err != nil {
err = errors.Wrap(err, "failed to unmarshal msgs from proto-compatible amino JSON")
return typeddata.TypedData{}, err
}

txData["msgs"] = msgsData["msgs"]

domain := typeddata.TypedDataDomain{
Name: "Injective Web3",
Version: "1.0.0",
Expand All @@ -74,7 +108,7 @@ func WrapTxToEIP712(
return typeddata.TypedData{}, err
}

if feeDelegation != nil {
if feeDelegation != nil && feeDelegation.FeePayer != nil {
feeInfo := txData["fee"].(map[string]interface{})
feeInfo["feePayer"] = feeDelegation.FeePayer.String()

Expand Down Expand Up @@ -488,7 +522,7 @@ func signableTypes() typeddata.Types {
}

func WrapTxToEIP712V2(
cdc codec.ProtoCodecMarshaler,
cdc AminoProtoCodecMarshaler,
chainID uint64,
signerData *authsigning.SignerData,
timeoutHeight uint64,
Expand Down
Loading

0 comments on commit 26a0cd8

Please sign in to comment.