-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuserops.go
More file actions
233 lines (209 loc) · 8.9 KB
/
userops.go
File metadata and controls
233 lines (209 loc) · 8.9 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
// Package model provides common data structures shared among blndgs projects.
// This file has been copied from the Bundler project.
// It is included in the model package to avoid introducing a cyclical dependency
// between the Model and Bundler projects and accommodate Bundler<->Solver communication.
// Any modifications made to the Bundler file should be reflected here as well to maintain consistency.
//
// Note: In the future, this file may move here as the single source of truth.
package model
import (
"encoding/json"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
)
var (
address, _ = abi.NewType("address", "", nil)
uint256, _ = abi.NewType("uint256", "", nil)
bytes32, _ = abi.NewType("bytes32", "", nil)
// UserOpPrimitives is the primitive ABI types for each UserOperation field.
UserOpPrimitives = []abi.ArgumentMarshaling{
{Name: "sender", InternalType: "Sender", Type: "address"},
{Name: "nonce", InternalType: "Nonce", Type: "uint256"},
{Name: "initCode", InternalType: "InitCode", Type: "bytes"},
{Name: "callData", InternalType: "CallData", Type: "bytes"},
{Name: "callGasLimit", InternalType: "CallGasLimit", Type: "uint256"},
{Name: "verificationGasLimit", InternalType: "VerificationGasLimit", Type: "uint256"},
{Name: "preVerificationGas", InternalType: "PreVerificationGas", Type: "uint256"},
{Name: "maxFeePerGas", InternalType: "MaxFeePerGas", Type: "uint256"},
{Name: "maxPriorityFeePerGas", InternalType: "MaxPriorityFeePerGas", Type: "uint256"},
{Name: "paymasterAndData", InternalType: "PaymasterAndData", Type: "bytes"},
{Name: "signature", InternalType: "Signature", Type: "bytes"},
}
// UserOpType is the ABI type of a UserOperation.
UserOpType, _ = abi.NewType("tuple", "op", UserOpPrimitives)
// UserOpArr is the ABI type for an array of UserOperations.
UserOpArr, _ = abi.NewType("tuple[]", "ops", UserOpPrimitives)
)
// UserOperation represents an EIP-4337 style transaction for a smart contract account.
type UserOperation struct {
Sender common.Address `json:"sender" mapstructure:"sender" validate:"required"`
Nonce *big.Int `json:"nonce" mapstructure:"nonce" validate:"required"`
InitCode []byte `json:"initCode" mapstructure:"initCode" validate:"required"`
CallData []byte `json:"callData" mapstructure:"callData" validate:"required"`
CallGasLimit *big.Int `json:"callGasLimit" mapstructure:"callGasLimit" validate:"required"`
VerificationGasLimit *big.Int `json:"verificationGasLimit" mapstructure:"verificationGasLimit" validate:"required"`
PreVerificationGas *big.Int `json:"preVerificationGas" mapstructure:"preVerificationGas" validate:"required"`
MaxFeePerGas *big.Int `json:"maxFeePerGas" mapstructure:"maxFeePerGas" validate:"required"`
MaxPriorityFeePerGas *big.Int `json:"maxPriorityFeePerGas" mapstructure:"maxPriorityFeePerGas" validate:"required"`
PaymasterAndData []byte `json:"paymasterAndData" mapstructure:"paymasterAndData" validate:"required"`
Signature []byte `json:"signature" mapstructure:"signature"`
}
// GetPaymaster returns the address portion of PaymasterAndData if applicable. Otherwise it returns the zero
// address.
func (op *UserOperation) GetPaymaster() common.Address {
if len(op.PaymasterAndData) < common.AddressLength {
return common.HexToAddress("0x")
}
return common.BytesToAddress(op.PaymasterAndData[:common.AddressLength])
}
// GetFactory returns the address portion of InitCode if applicable. Otherwise it returns the zero address.
func (op *UserOperation) GetFactory() common.Address {
if len(op.InitCode) < common.AddressLength {
return common.HexToAddress("0x")
}
return common.BytesToAddress(op.InitCode[:common.AddressLength])
}
// GetMaxGasAvailable returns the max amount of gas that can be consumed by this UserOperation.
func (op *UserOperation) GetMaxGasAvailable() *big.Int {
// TODO: Multiplier logic might change in v0.7
mul := big.NewInt(1)
paymaster := op.GetPaymaster()
if paymaster != common.HexToAddress("0x") {
mul = big.NewInt(3)
}
return big.NewInt(0).Add(
big.NewInt(0).Mul(op.VerificationGasLimit, mul),
big.NewInt(0).Add(op.PreVerificationGas, op.CallGasLimit),
)
}
// GetMaxPrefund returns the max amount of wei required to pay for gas fees by either the sender or
// paymaster.
func (op *UserOperation) GetMaxPrefund() *big.Int {
return big.NewInt(0).Mul(op.GetMaxGasAvailable(), op.MaxFeePerGas)
}
// GetDynamicGasPrice returns the effective gas price paid by the UserOperation given a basefee. If basefee is
// nil, it will assume a value of 0.
func (op *UserOperation) GetDynamicGasPrice(basefee *big.Int) *big.Int {
bf := basefee
if bf == nil {
bf = big.NewInt(0)
}
gp := big.NewInt(0).Add(bf, op.MaxPriorityFeePerGas)
if gp.Cmp(op.MaxFeePerGas) == 1 {
return op.MaxFeePerGas
}
return gp
}
// Pack returns a standard message of the userOp. This cannot be used to generate a userOpHash.
func (op *UserOperation) Pack() []byte {
args := abi.Arguments{
{Name: "UserOp", Type: UserOpType},
}
packed, _ := args.Pack(&struct {
Sender common.Address
Nonce *big.Int
InitCode []byte
CallData []byte
CallGasLimit *big.Int
VerificationGasLimit *big.Int
PreVerificationGas *big.Int
MaxFeePerGas *big.Int
MaxPriorityFeePerGas *big.Int
PaymasterAndData []byte
Signature []byte
}{
op.Sender,
op.Nonce,
op.InitCode,
op.CallData,
op.CallGasLimit,
op.VerificationGasLimit,
op.PreVerificationGas,
op.MaxFeePerGas,
op.MaxPriorityFeePerGas,
op.PaymasterAndData,
op.Signature,
})
enc := hexutil.Encode(packed)
enc = "0x" + enc[66:]
return (hexutil.MustDecode(enc))
}
// PackForSignature returns a minimal message of the userOp. This can be used to generate a userOpHash.
func (op *UserOperation) PackForSignature() []byte {
args := abi.Arguments{
{Name: "sender", Type: address},
{Name: "nonce", Type: uint256},
{Name: "hashInitCode", Type: bytes32},
{Name: "hashCallData", Type: bytes32},
{Name: "callGasLimit", Type: uint256},
{Name: "verificationGasLimit", Type: uint256},
{Name: "preVerificationGas", Type: uint256},
{Name: "maxFeePerGas", Type: uint256},
{Name: "maxPriorityFeePerGas", Type: uint256},
{Name: "hashPaymasterAndData", Type: bytes32},
}
packed, _ := args.Pack(
op.Sender,
op.Nonce,
crypto.Keccak256Hash(op.InitCode),
crypto.Keccak256Hash(op.CallData),
op.CallGasLimit,
op.VerificationGasLimit,
op.PreVerificationGas,
op.MaxFeePerGas,
op.MaxPriorityFeePerGas,
crypto.Keccak256Hash(op.PaymasterAndData),
)
return packed
}
// GetUserOpHash returns the hash of the userOp + entryPoint address + chainID.
func (op *UserOperation) GetUserOpHash(entryPoint common.Address, chainID *big.Int) common.Hash {
return crypto.Keccak256Hash(
crypto.Keccak256(op.PackForSignature()),
common.LeftPadBytes(entryPoint.Bytes(), 32),
common.LeftPadBytes(chainID.Bytes(), 32),
)
}
// MarshalJSON returns a JSON encoding of the UserOperation.
func (op *UserOperation) MarshalJSON() ([]byte, error) {
return json.Marshal(&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"`
}{
Sender: op.Sender.String(),
Nonce: hexutil.EncodeBig(op.Nonce),
InitCode: hexutil.Encode(op.InitCode),
CallData: hexutil.Encode(op.CallData),
CallGasLimit: hexutil.EncodeBig(op.CallGasLimit),
VerificationGasLimit: hexutil.EncodeBig(op.VerificationGasLimit),
PreVerificationGas: hexutil.EncodeBig(op.PreVerificationGas),
MaxFeePerGas: hexutil.EncodeBig(op.MaxFeePerGas),
MaxPriorityFeePerGas: hexutil.EncodeBig(op.MaxPriorityFeePerGas),
PaymasterAndData: hexutil.Encode(op.PaymasterAndData),
Signature: hexutil.Encode(op.Signature),
})
}
// ToMap returns the current UserOp struct as a map type.
func (op *UserOperation) ToMap() (map[string]any, error) {
data, err := op.MarshalJSON()
if err != nil {
return nil, err
}
var opData map[string]any
if err := json.Unmarshal(data, &opData); err != nil {
return nil, err
}
return opData, nil
}