Skip to content

Commit 26a0cd8

Browse files
authored
Merge pull request #276 from InjectiveLabs/fix/eip712v1-wrapper
Fix: EIP712Wrapper v1
2 parents 6bf611d + e8625f9 commit 26a0cd8

File tree

3 files changed

+904
-14
lines changed

3 files changed

+904
-14
lines changed

eip712_cosmos.go

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ante
1+
package sdk
22

33
import (
44
"bytes"
@@ -16,15 +16,24 @@ import (
1616
cosmtypes "github.com/cosmos/cosmos-sdk/types"
1717
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
1818
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
19+
"github.com/cosmos/gogoproto/proto"
1920
"github.com/ethereum/go-ethereum/common"
2021
ethmath "github.com/ethereum/go-ethereum/common/math"
2122
"github.com/pkg/errors"
2223

2324
"github.com/InjectiveLabs/sdk-go/typeddata"
2425
)
2526

27+
var _ AminoProtoCodecMarshaler = (*codec.ProtoCodec)(nil)
28+
29+
type AminoProtoCodecMarshaler interface {
30+
codec.Codec
31+
32+
MarshalAminoJSON(msg proto.Message) ([]byte, error)
33+
}
34+
2635
type EIP712Wrapper func(
27-
cdc codec.ProtoCodecMarshaler,
36+
cdc AminoProtoCodecMarshaler,
2837
chainID uint64,
2938
signerData *authsigning.SignerData,
3039
timeoutHeight uint64,
@@ -37,7 +46,7 @@ type EIP712Wrapper func(
3746
// WrapTxToEIP712 is an ultimate method that wraps Amino-encoded Cosmos Tx JSON data
3847
// into an EIP712-compatible request. All messages must be of the same type.
3948
func WrapTxToEIP712(
40-
cdc codec.ProtoCodecMarshaler,
49+
cdc AminoProtoCodecMarshaler,
4150
chainID uint64,
4251
signerData *authsigning.SignerData,
4352
timeoutHeight uint64,
@@ -52,7 +61,7 @@ func WrapTxToEIP712(
5261
signerData.Sequence,
5362
timeoutHeight,
5463
feeInfo,
55-
msgs, memo,
64+
[]cosmtypes.Msg{}, memo,
5665
)
5766

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

73+
msgsJsons := make([]json.RawMessage, len(msgs))
74+
for idx, m := range msgs {
75+
bzMsg, err := cdc.MarshalAminoJSON(m)
76+
if err != nil {
77+
return typeddata.TypedData{}, errors.Wrapf(err, "cannot marshal msg JSON at index %d", idx)
78+
}
79+
80+
msgsJsons[idx] = bzMsg
81+
}
82+
83+
bzMsgs, err := json.Marshal(map[string][]json.RawMessage{
84+
"msgs": msgsJsons,
85+
})
86+
if err != nil {
87+
return typeddata.TypedData{}, errors.Wrap(err, "marshal msgs JSON")
88+
}
89+
90+
msgsData := make(map[string]interface{})
91+
if err := json.Unmarshal(bzMsgs, &msgsData); err != nil {
92+
err = errors.Wrap(err, "failed to unmarshal msgs from proto-compatible amino JSON")
93+
return typeddata.TypedData{}, err
94+
}
95+
96+
txData["msgs"] = msgsData["msgs"]
97+
6498
domain := typeddata.TypedDataDomain{
6599
Name: "Injective Web3",
66100
Version: "1.0.0",
@@ -74,7 +108,7 @@ func WrapTxToEIP712(
74108
return typeddata.TypedData{}, err
75109
}
76110

77-
if feeDelegation != nil {
111+
if feeDelegation != nil && feeDelegation.FeePayer != nil {
78112
feeInfo := txData["fee"].(map[string]interface{})
79113
feeInfo["feePayer"] = feeDelegation.FeePayer.String()
80114

@@ -488,7 +522,7 @@ func signableTypes() typeddata.Types {
488522
}
489523

490524
func WrapTxToEIP712V2(
491-
cdc codec.ProtoCodecMarshaler,
525+
cdc AminoProtoCodecMarshaler,
492526
chainID uint64,
493527
signerData *authsigning.SignerData,
494528
timeoutHeight uint64,

0 commit comments

Comments
 (0)