-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuserops_ext.go
More file actions
638 lines (554 loc) · 20 KB
/
userops_ext.go
File metadata and controls
638 lines (554 loc) · 20 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
// Package model provides structures and methods for the communication between
// the Bundler and Solver.
// This file defines extensions to the UserOperation struct and methods for
// extracting and inserting Intent JSON from/to the CallData and Signature fields.
//
// The CallData field in a userOperation is expected to contain either the Intent JSON or
// the EVM instructions but not both.
// The Intent JSON is expected to be appended to the signature value within the Signature field
// when the Calldata field contains the EVM instructions.
// The Signature field is expected to contain only the signature when the userOperation is unsolved.
package model
import (
"bytes"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/goccy/go-json"
"google.golang.org/protobuf/encoding/protojson"
pb "github.com/blndgs/model/gen/go/proto/v1"
)
var EntrypointV06 = common.HexToAddress("0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789")
// BodyOfUserOps represents the request body for HTTP requests sent to the Solver.
// It contains slices of UserOperation and UserOperationExt, representing the primary
// data and its extended information required for processing by the Solver.
type BodyOfUserOps struct {
UserOps []*UserOperation `json:"user_ops" binding:"required,dive"`
UserOpsExt []UserOperationExt `json:"user_ops_ext" binding:"required,dive"`
}
// UserOperationExt extends the UserOperation with additional information necessary for
// processing by the Solver.This includes the original hash value of the UserOperation
// and its processing status.The sequence of UserOperationExt instances must correspond
// to the sequence in the UserOps slice.
type UserOperationExt struct {
OriginalHashValue string `json:"original_hash_value" mapstructure:"original_hash_value" validate:"required"`
ProcessingStatus pb.ProcessingStatus `json:"processing_status" mapstructure:"processing_status" validate:"required"`
}
// UnmarshalJSON makes sure we can support using strings instead of arbitrary
// numbers for the proto processing
func (u *UserOperationExt) UnmarshalJSON(data []byte) error {
aux := struct {
OriginalHashValue string `json:"original_hash_value"`
ProcessingStatus string `json:"processing_status"`
}{}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
u.ProcessingStatus = pb.ProcessingStatus(pb.ProcessingStatus_value[aux.ProcessingStatus])
u.OriginalHashValue = aux.OriginalHashValue
return nil
}
func (u *UserOperationExt) MarshalJSON() ([]byte, error) {
aux := struct {
OriginalHashValue string `json:"original_hash_value"`
ProcessingStatus string `json:"processing_status"`
}{
OriginalHashValue: u.OriginalHashValue,
ProcessingStatus: u.ProcessingStatus.String(),
}
return json.Marshal(&aux)
}
// UserOpSolvedStatus is an enum type that defines the possible states of a
// UserOperation's resolution.It indicates whether an operation is unsolved,
// solved, conventional, or in an unknown state.
type UserOpSolvedStatus int
const (
UnsolvedUserOp UserOpSolvedStatus = iota
UnsolvedAggregateUserOp // Unsolved Cross-chain userOp that contains 1 or more cross-chain unsolved userOps
SolvedUserOp // Intent Json values must be present
// ConventionalUserOp indicates that the UserOperation does not contain Intent JSON and
// must have a valid EVM calldata value.
ConventionalUserOp
UnSignedUserOp // UnSignedUserOp indicates that the UserOperation does not contain a signature value.
// UnknownUserOp indicates that the UserOperation's state is unknown or ambiguous.
UnknownUserOp
)
// userOperationError represents custom error types related to processing UserOperations.
// These errors include issues such as missing Intent, invalid JSON, or invalid CallData.
type userOperationError string
func (e userOperationError) Error() string {
return string(e)
}
// Define error constants
const (
ErrNoIntentFound userOperationError = "no Intent found"
ErrIntentInvalidJSON userOperationError = "invalid Intent JSON"
ErrNoSignatureValue userOperationError = "signature value is not found"
ErrNoCallData userOperationError = "no CallData found"
ErrInvalidCallData userOperationError = "invalid hex-encoded CallData"
ErrInvalidSignature userOperationError = "invalid hex-encoded signature"
ErrInvalidUserOp userOperationError = "ambiguous UserOperation solved state"
ErrDoubleIntentDef userOperationError = "intent JSON is set in both calldata and signature fields"
ErrUnsupportedIntentType userOperationError = "unsupported intent type"
ErrInvalidChainID userOperationError = "invalid chain ID"
)
type KernelSignaturePrefix int
const (
Prefix0 KernelSignaturePrefix = iota
Prefix1
Prefix2
)
var KernelSignaturePrefixValues = map[KernelSignaturePrefix][]byte{
Prefix0: []byte{0, 0, 0, 0},
Prefix1: []byte{0, 0, 0, 1},
Prefix2: []byte{0, 0, 0, 2},
}
const (
KernelSignatureLength = 69
SimpleSignatureLength = 65
)
// Validate checks the status of the UserOperation and returns
// its UserOpSolvedStatus. It determines if the operation is conventional,
// unsolved, or solved based on the presence and content of CallData and Signature.
//
// Returns:
// - UserOpSolvedStatus: The solved status of the UserOperation.
// - error: An error if there's an issue with the operation's state or contents.
func (op *UserOperation) Validate() (UserOpSolvedStatus, error) {
// Check for cross-chain operation
if op.IsCrossChainOperation() {
status, err := op.validateCrossChainOp()
if err != nil {
return UnknownUserOp, err
}
return status, nil
}
// Conventional userOp: empty CallData without signature value.
if len(op.CallData) == 0 && (len(op.Signature) == 0 || op.HasSignatureExact()) {
return ConventionalUserOp, nil
}
// Unsolved userOp: Check if CallData is a non-hex-encoded string
if _, callDataErr := hexutil.Decode(string(op.CallData)); callDataErr != nil {
// Not solved, check if there is a valid Intent JSON
_, validIntent := ExtractJSONFromField(string(op.CallData))
if validIntent && (op.HasSignatureExact() || len(op.Signature) == 0) {
// Valid intent JSON in CallData (Unsolved) and not defined again in Signature
return UnsolvedUserOp, nil
}
if validIntent && len(op.Signature) > KernelSignatureLength {
// Both unsolved (no CallData value) status and likely intent JSON in the Signature
return UnknownUserOp, ErrDoubleIntentDef
}
}
if !op.HasSignature() {
// Need a signature value for solved userOps
return SolvedUserOp, ErrNoSignatureValue
}
// Solved userOp: Intent JSON values may or may not be present in the Signature field
return SolvedUserOp, nil
}
func no0xPrefix(value []byte) bool {
return len(value) > 1 && (value[0] != '0' || value[1] != 'x')
}
// extractIntentJSON attempts to extract the Intent JSON from either the CallData
// or Signature field of a UserOperation. It first checks the CallData field. If
// the CallData field does not contain a valid JSON, the function then checks
// the Signature field. The Intent JSON is expected to be appended to the
// signature value within the Signature field. The signature has a fixed length
// of 132 characters with '0x' prefix.
// It also takes into account cross-chain calldata specific format which is
// prioritized and is in the following format:
//
// [2 bytes opType (0xFFFF)]
// [2 bytes length of intent JSON]
// [Intent JSON]
// [1 byte hash list length (N)]
// [Hash List Entry]
//
// Returns:
// - string: The extracted JSON string.
// - bool: A boolean indicating if a valid JSON was found.
//
// extractIntentJSON attempts to extract the Intent JSON from either the CallData
// or Signature field of a UserOperation.
func (op *UserOperation) extractIntentJSON() (string, bool) {
// Try parsing CallData as cross-chain data
crossChainData, err := ParseCrossChainData(op.CallData)
if err == nil {
intentJSON := string(crossChainData.IntentJSON)
if _, ok := ExtractJSONFromField(intentJSON); ok {
return intentJSON, true
}
} else if intentJSON, ok := ExtractJSONFromField(string(op.CallData)); ok {
return intentJSON, true
}
// Try parsing Signature as cross-chain data
signatureEndIndex := op.GetSignatureEndIdx()
if op.HasSignature() && signatureEndIndex < len(op.Signature) {
signatureData := op.Signature[signatureEndIndex:]
crossChainData, err := ParseCrossChainData(signatureData)
if err == nil {
intentJSON := string(crossChainData.IntentJSON)
if _, ok := ExtractJSONFromField(intentJSON); ok {
return intentJSON, true
}
} else if intentJSON, ok := ExtractJSONFromField(string(signatureData)); ok {
return intentJSON, true
}
}
return "", false
}
// ExtractJSONFromField tries to unmarshal the provided field data into an Intent
// struct. If successful, it assumes the field data is a valid JSON string of
// the Intent.
//
// Returns:
// - string: The JSON data if unmarshalling is successful.
// - bool: A boolean indicating if the unmarshalling was successful.
func ExtractJSONFromField(fieldData string) (string, bool) {
if fieldData != "" {
var intent pb.Intent
err := protojson.Unmarshal([]byte(fieldData), &intent)
if err != nil {
return "", false
}
if intent.String() == "" {
// intent is empty
return "", false
}
return fieldData, true
}
return "", false
}
// HasIntent checks if the CallData or signature field contains a valid Intent JSON that
// decodes successfully into an Intent struct.
func (op *UserOperation) HasIntent() bool {
_, hasIntent := op.extractIntentJSON()
return hasIntent
}
// HasSignature checks if the signature field contains a fixed length ECDSA
// hex-encoded signature value either a conventional (65 bytes) or a kernel
// with or without Intent.
func (op *UserOperation) HasSignature() bool {
// Valid signature does not have a '0x' prefix
if no0xPrefix(op.Signature) {
lenSig := len(op.Signature)
if lenSig == KernelSignatureLength {
// Check if it's a kernel signature
return sigHasKernelPrefix(op.Signature)
}
if lenSig > KernelSignatureLength && sigHasKernelPrefix(op.Signature) {
return true
}
// Check for conventional signature
if lenSig >= SimpleSignatureLength {
return true
}
}
return false
}
// GetSignatureEndIdx returns the end index of the signature value in the UserOperation's Signature field.
// Returns either of the 3 following values:
// - KernelSignatureLength: If the signature is a kernel signature with a prefix.
// - SimpleSignatureLength: If the signature is an ECDSA signature without a prefix.
// - 0: If the signature is a kernel signature without a prefix or as a fallback
func (op *UserOperation) GetSignatureEndIdx() int {
// valid signature does not have a '0x' prefix
if no0xPrefix(op.Signature) {
// chk kernel signature
lenSig := len(op.Signature)
if lenSig == KernelSignatureLength {
// cannot have a simple signature length fitting a kernel signature
if sigHasKernelPrefix(op.Signature) {
return KernelSignatureLength
} else {
// matching kernel signature length without a prefix
return 0
}
}
if lenSig > KernelSignatureLength && sigHasKernelPrefix(op.Signature) {
return KernelSignatureLength
}
// chk conventional signature
if lenSig >= SimpleSignatureLength {
return SimpleSignatureLength
}
}
return 0
}
// HasSignatureExact checks for an exact match of the signature length and the
// signature field contains a fixed length hex-encoded signature value either a
// conventional or a kernel without Intent.
func (op *UserOperation) HasSignatureExact() bool {
// Valid signature does not have a '0x' prefix
if no0xPrefix(op.Signature) {
lenSig := len(op.Signature)
if lenSig != KernelSignatureLength && lenSig != SimpleSignatureLength {
return false
}
if lenSig == KernelSignatureLength {
// Cannot have a simple signature length fitting a kernel signature length without a prefix
return sigHasKernelPrefix(op.Signature)
}
return true
}
return false
}
// GetIntentJSON returns the Intent JSON from the CallData or signature fields, if present.
func (op *UserOperation) GetIntentJSON() (string, error) {
intentJSON, hasIntent := op.extractIntentJSON()
if !hasIntent {
return "", ErrNoIntentFound
}
return intentJSON, nil
}
// GetIntent takes the Intent Type from the CallData or Signature field, decodes it into
// an Intent struct, and returns the struct.
func (op *UserOperation) GetIntent() (*pb.Intent, error) {
intentJSON, hasIntent := op.extractIntentJSON()
if !hasIntent {
return nil, ErrNoIntentFound
}
var intent pb.Intent
if err := protojson.Unmarshal([]byte(intentJSON), &intent); err != nil {
return nil, ErrIntentInvalidJSON
}
return &intent, nil
}
// SetIntent sets the Intent JSON in the appropriate field of the UserOperation
// based on the operation's solution state.
//
// If the UserOperation is unsolved, the Intent JSON is set in the CallData field.
// If the UserOperation is solved, the Intent JSON is appended to the Signature field.
func (op *UserOperation) SetIntent(intentJSON string) error {
if err := protojson.Unmarshal([]byte(intentJSON), new(pb.Intent)); err != nil {
return ErrIntentInvalidJSON
}
// These errors while useful do not really matter here in this context
// since we rely on the isCrossChain variable to determine if to set
// the cross chain item
//
// err could be like no intent json or not a cross chain operation
// so safe to ignore here.
isCrossChain, _ := op.IsCrossChainIntent()
if isCrossChain {
return op.setCrossChainIntent(intentJSON)
}
status, err := op.Validate()
if err != nil {
return err
}
if status == UnsolvedUserOp {
op.CallData = []byte(intentJSON)
} else {
op.Signature = append(op.GetSignatureValue(), []byte(intentJSON)...)
}
return nil
}
// GetSignatureValue retrieves the signature value from a UserOperation.
//
// This function supports three use cases:
//
// 1. No, or invalid signature value: It returns nil.
//
// 2. If the UserOperation has a Kernel signature (identified by a specific prefix),
// and the length of the signature is greater than or equal to the KernelSignatureLength,
// it returns the signature up to the KernelSignatureLength.
//
// 3. Treated as a fallback if the UserOperation has a sufficient length for a conventional signature,
// it returns the signature up to the SignatureLength.
//
// Otherwise, it returns nil.
func (op *UserOperation) GetSignatureValue() []byte {
if no0xPrefix(op.Signature) {
lenSig := len(op.Signature)
// sigHasKernelPrefix already checks this internally
// if lenSig >= KernelSignatureLength && sigHasKernelPrefix(op.Signature) {
if sigHasKernelPrefix(op.Signature) {
return op.Signature[:KernelSignatureLength]
}
if lenSig == KernelSignatureLength {
// Cannot have a simple signature length fitting a kernel signature length without a prefix
return nil
}
if lenSig >= SimpleSignatureLength {
return op.Signature[:SimpleSignatureLength]
}
}
return nil
}
// sigHasKernelPrefix checks if the provided signature has a Kernel prefix.
func sigHasKernelPrefix(signature []byte) bool {
if len(signature) < KernelSignatureLength {
return false
}
kernelPrefixes := [][]byte{
{0, 0, 0, 0},
{0, 0, 0, 1},
{0, 0, 0, 2},
}
for _, prefix := range kernelPrefixes {
if bytes.HasPrefix(signature, prefix) {
return true
}
}
return false
}
// SetEVMInstructions sets the EVM instructions in the CallData field of the
// UserOperation.
//
// If the operation is unsolved, it moves the Intent JSON to the Signature field
// if present, and then sets the provided EVM instructions in the CallData field.
func (op *UserOperation) SetEVMInstructions(callDataValue []byte) error {
if len(callDataValue) >= 2 && callDataValue[0] == '0' && callDataValue[1] == 'x' {
var err error
callDataValue, err = hexutil.Decode(string(callDataValue))
if err != nil {
return fmt.Errorf("invalid hex encoding of calldata: %w", err)
}
}
status, err := op.Validate()
if err != nil {
return err
}
if status == SolvedUserOp || status == ConventionalUserOp {
op.CallData = callDataValue
return nil
}
if !op.HasSignature() {
return ErrNoSignatureValue
}
// Append xData or Intent JSON to the Signature value if exists
if op.HasIntent() && IsCrossChainData(op.CallData, MinOpCount, MaxOpCount) {
op.Signature = append(op.GetSignatureValue(), op.CallData...)
} else if op.HasIntent() {
if _, ok := ExtractJSONFromField(string(op.CallData)); ok {
// Same chain Intent JSON in CallData, move JSON to Signature
op.Signature = append(op.GetSignatureValue(), []byte(op.CallData)...)
}
}
// Assign byte-level representation
op.CallData = callDataValue
return nil
}
// UnmarshalJSON does the reverse of the provided bundler custom
// JSON marshaler for a UserOperation.
func (op *UserOperation) UnmarshalJSON(data []byte) error {
aux := struct {
Sender string `json:"sender"`
Nonce string `json:"nonce"`
InitCode string `json:"initCode"`
CallData string `json:"callData"`
CallGasLimit string `json:"callGasLimit"`
VerificationGasLimit string `json:"verificationGasLimit"`
PreVerificationGas string `json:"preVerificationGas"`
MaxFeePerGas string `json:"maxFeePerGas"`
MaxPriorityFeePerGas string `json:"maxPriorityFeePerGas"`
PaymasterAndData string `json:"paymasterAndData"`
Signature string `json:"signature"`
}{}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
var err error
op.Sender = common.HexToAddress(aux.Sender)
op.Nonce, err = hexutil.DecodeBig(aux.Nonce)
if err != nil {
return err
}
op.InitCode, err = hexutil.Decode(aux.InitCode)
if err != nil {
return err
}
op.CallGasLimit, err = hexutil.DecodeBig(aux.CallGasLimit)
if err != nil {
return err
}
op.VerificationGasLimit, err = hexutil.DecodeBig(aux.VerificationGasLimit)
if err != nil {
return err
}
op.PreVerificationGas, err = hexutil.DecodeBig(aux.PreVerificationGas)
if err != nil {
return err
}
op.MaxFeePerGas, err = hexutil.DecodeBig(aux.MaxFeePerGas)
if err != nil {
return err
}
op.MaxPriorityFeePerGas, err = hexutil.DecodeBig(aux.MaxPriorityFeePerGas)
if err != nil {
return err
}
op.PaymasterAndData, err = hexutil.Decode(aux.PaymasterAndData)
if err != nil {
return err
}
op.Signature, err = hexutil.Decode(aux.Signature)
if err != nil {
return err
}
// Handle CallData (Intent JSON or hex-encoded data)
if intentJSON, ok := ExtractJSONFromField(aux.CallData); ok {
op.CallData = []byte(intentJSON)
} else {
op.CallData, err = hexutil.Decode(aux.CallData)
if err != nil {
return fmt.Errorf("invalid CallData: %w", err)
}
}
return nil
}
// String returns a string representation of the UserOperation.
func (op *UserOperation) String() string {
formatBytes := func(b []byte) string {
if len(b) == 0 {
return "0x" // default for empty byte slice
}
if len(b) >= 2 && b[0] == '0' && b[1] == 'x' {
return string(b)
}
return fmt.Sprintf("0x%x", b)
}
formatBigInt := func(b *big.Int) string {
if b == nil {
return "0x, 0" // Default for nil big.Int
}
return fmt.Sprintf("0x%x, %s", b, b.Text(10))
}
formatCallData := func(callDataBytes []byte) string {
// Directly return string if it's intended to be JSON (Intent)
if intentJSON, ok := op.extractIntentJSON(); ok {
return intentJSON
}
// Otherwise, encode as hex
return formatBytes(callDataBytes)
}
return fmt.Sprintf(
"UserOperation{\n"+
" Sender: %s\n"+
" Nonce: %s\n"+
" InitCode: %s\n"+
" CallData: %s\n"+
" CallGasLimit: %s\n"+
" VerificationGasLimit: %s\n"+
" PreVerificationGas: %s\n"+
" MaxFeePerGas: %s\n"+
" MaxPriorityFeePerGas: %s\n"+
" PaymasterAndData: %s\n"+
" Signature: %s\n"+
"}",
op.Sender.Hex(),
formatBigInt(op.Nonce),
formatBytes(op.InitCode),
formatCallData(op.CallData),
formatBigInt(op.CallGasLimit),
formatBigInt(op.VerificationGasLimit),
formatBigInt(op.PreVerificationGas),
formatBigInt(op.MaxFeePerGas),
formatBigInt(op.MaxPriorityFeePerGas),
formatBytes(op.PaymasterAndData),
formatBytes(op.Signature),
)
}