-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy patheip712_cosmos_test.go
344 lines (318 loc) · 7.75 KB
/
eip712_cosmos_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package sdk
import (
"encoding/json"
"fmt"
"strconv"
"testing"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
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/pkg/errors"
"github.com/stretchr/testify/require"
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
"github.com/InjectiveLabs/sdk-go/typeddata"
)
func TestEIP712Wrapper(t *testing.T) {
require := require.New(t)
// Setup test codec
chainCtx, _ := chainclient.NewClientContext("", "", nil)
protoCodec := codec.NewProtoCodec(chainCtx.InterfaceRegistry)
// Test fixture
var testReq prepareEip712Payload
err := json.Unmarshal([]byte(testEIP712WrapperJSON), &testReq)
require.NoError(err)
legacytx.RegressionTestingAminoCodec = codec.NewLegacyAmino()
msgs, err := unmarshalTestMsgs(protoCodec, testReq.Msgs)
require.NoError(err)
feePriceAmount, ok := math.NewIntFromString(testReq.Fee.Price[0].Amount)
if !ok {
require.True(ok, fmt.Sprintf("wrong fee price amount: %s", testReq.Fee.Price[0].Amount))
}
// Create wrapper function
wrapper := func(
cdc *codec.ProtoCodec,
chainID uint64,
signerData *authsigning.SignerData,
timeoutHeight uint64,
memo string,
feeInfo legacytx.StdFee,
msgs []cosmtypes.Msg,
feeDelegation *FeeDelegationOptions,
) (typeddata.TypedData, error) {
return WrapTxToEIP712(cdc, chainID, signerData, timeoutHeight, memo, feeInfo, msgs, feeDelegation)
}
// Test wrapper execution
typedData, err := wrapper(
protoCodec,
uint64(testReq.ChainID),
&authsigning.SignerData{
ChainID: "injective-777",
AccountNumber: 3,
Sequence: 0,
},
uint64(testReq.TimeoutHeight),
testReq.Memo,
legacytx.StdFee{
Gas: uint64(testReq.Fee.Gas),
Amount: []cosmtypes.Coin{
{
Denom: testReq.Fee.Price[0].Denom,
Amount: feePriceAmount,
},
},
},
msgs,
&FeeDelegationOptions{},
)
require.NoError(err)
require.NotNil(typedData)
require.Equal("Injective Web3", typedData.Domain.Name)
require.Equal("1.0.0", typedData.Domain.Version)
require.Equal("cosmos", typedData.Domain.VerifyingContract)
require.Equal("0", typedData.Domain.Salt)
require.Equal("Tx", typedData.PrimaryType)
typedDataRaw, err := json.MarshalIndent(typedData, "", "\t")
require.NoError(err)
require.Equal(testEIP712WrapperOutJSON, string(typedDataRaw))
}
func unmarshalTestMsgs(cdc codec.ProtoCodecMarshaler, msgsBytes []json.RawMessage) (msgs []cosmtypes.Msg, err error) {
for _, protoMsg := range msgsBytes {
var msg cosmtypes.Msg
if err := cdc.UnmarshalInterfaceJSON(protoMsg, &msg); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal msg")
}
msgs = append(msgs, msg)
}
return msgs, nil
}
var testEIP712WrapperJSON = `{
"chainId": "11155111",
"signerAddress": "0xaf79152ac5df276d9a8e1e2e22822f9713474902",
"sequence": "0",
"accountNumber": "3",
"memo": "",
"timeoutHeight": "999999999",
"fee": {
"price": [
{
"denom": "inj",
"amount": "160000000"
}
],
"gas": "400000",
"delegateFee": false
},
"msgs": [
{
"@type": "/injective.exchange.v1beta1.MsgCancelSpotOrder",
"sender": "inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku",
"market_id": "0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0",
"subaccount_id": "0xaf79152ac5df276d9a8e1e2e22822f9713474902000000000000000000000000",
"order_hash": "0x3b1bcc8ab01e1e0f1f2cf9de5f44267bed6368fabd62adbcf3826a207340194e",
"cid": "order-123"
}
],
"eip712Wrapper": "v1"
}`
var testEIP712WrapperOutJSON = `{
"types": {
"Coin": [
{
"name": "denom",
"type": "string"
},
{
"name": "amount",
"type": "string"
}
],
"EIP712Domain": [
{
"name": "name",
"type": "string"
},
{
"name": "version",
"type": "string"
},
{
"name": "chainId",
"type": "uint256"
},
{
"name": "verifyingContract",
"type": "string"
},
{
"name": "salt",
"type": "string"
}
],
"Fee": [
{
"name": "amount",
"type": "Coin[]"
},
{
"name": "gas",
"type": "string"
}
],
"Msg": [
{
"name": "type",
"type": "string"
},
{
"name": "value",
"type": "MsgValue"
}
],
"MsgValue": [
{
"name": "sender",
"type": "string"
},
{
"name": "market_id",
"type": "string"
},
{
"name": "subaccount_id",
"type": "string"
},
{
"name": "order_hash",
"type": "string"
},
{
"name": "cid",
"type": "string"
}
],
"Tx": [
{
"name": "account_number",
"type": "string"
},
{
"name": "chain_id",
"type": "string"
},
{
"name": "fee",
"type": "Fee"
},
{
"name": "memo",
"type": "string"
},
{
"name": "msgs",
"type": "Msg[]"
},
{
"name": "sequence",
"type": "string"
},
{
"name": "timeout_height",
"type": "string"
}
]
},
"primaryType": "Tx",
"domain": {
"name": "Injective Web3",
"version": "1.0.0",
"chainId": "0xaa36a7",
"verifyingContract": "cosmos",
"salt": "0"
},
"message": {
"account_number": "3",
"chain_id": "injective-777",
"fee": {
"amount": [
{
"amount": "160000000",
"denom": "inj"
}
],
"gas": "400000"
},
"memo": "",
"msgs": [
{
"type": "exchange/MsgCancelSpotOrder",
"value": {
"cid": "order-123",
"market_id": "0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0",
"order_hash": "0x3b1bcc8ab01e1e0f1f2cf9de5f44267bed6368fabd62adbcf3826a207340194e",
"sender": "inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku",
"subaccount_id": "0xaf79152ac5df276d9a8e1e2e22822f9713474902000000000000000000000000"
}
}
],
"sequence": "0",
"timeout_height": "999999999"
}
}`
type prepareEip712Payload struct {
// Specify chainID for the target tx
ChainID uint64AsString `json:"chainId"`
// Specify Ethereum address of a signer
SignerAddress string `json:"signerAddress"`
// Sequence number of the transaction signer
Sequence uint64AsString `json:"sequence"`
// Account number of the transaction signer
AccountNumber uint64AsString `json:"accountNumber"`
// Textual memo information to attach with tx
Memo string `json:"memo"`
// Block height until which the transaction is valid.
TimeoutHeight uint64AsString `json:"timeoutHeight"`
// Transaction fee details.
Fee *cosmosTxFee `json:"fee"`
// List of Cosmos proto3-encoded Msgs to include in a single tx
Msgs []json.RawMessage `json:"msgs"`
// The wrapper of the EIP712 message, 'v1'/'v2' or 'V1'/'V2'
Eip712Wrapper string `json:"eip712Wrapper"`
}
type cosmosTxFee struct {
// Transaction gas price
Price []*cosmosCoin `json:"price"`
// Transaction gas limit
Gas uint64AsString `json:"gas"`
// Explicitly require fee delegation when set to true. Or don't care = false.
// Will be replaced by other flag in the next version.
DelegateFee bool `json:"delegateFee"`
}
type cosmosCoin struct {
// Coin denominator
Denom string `json:"denom"`
// Coin amount (big int)
Amount string `json:"amount"`
}
type uint64AsString uint64
// MarshalJSON encodes uint64AsString as a string.
func (u uint64AsString) MarshalJSON() ([]byte, error) {
return json.Marshal(strconv.FormatUint(uint64(u), 10))
}
// UnmarshalJSON decodes a JSON string into uint64AsString.
func (u *uint64AsString) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
v, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return err
}
*u = uint64AsString(v)
return nil
}
type feeDelegationOptions struct {
FeePayer cosmtypes.AccAddress
}