Skip to content

Commit 6b6233a

Browse files
authored
fix(pkg/userop): correctly implement json.Unmarshaller iface (#458)
1 parent f4862e7 commit 6b6233a

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

pkg/userop/userop.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -151,58 +151,56 @@ func (op UserOperation) MarshalJSON() ([]byte, error) {
151151
}
152152

153153
// UnmarshalJSON decodes a JSON encoding into a UserOperation.
154-
func UnmarshalJSON(data []byte) (*UserOperation, error) {
154+
func (op *UserOperation) UnmarshalJSON(data []byte) error {
155155
var uoDTO UserOperationDTO
156156
if err := json.Unmarshal(data, &uoDTO); err != nil {
157-
return nil, err
157+
return err
158158
}
159159

160-
var op UserOperation
161160
var err error
162161
op.Sender = common.HexToAddress(uoDTO.Sender)
163-
fmt.Printf("raw.Nonce: %s\n", uoDTO.Nonce)
164162
if nonceBI, ok := big.NewInt(0).SetString(uoDTO.Nonce, 0); !ok {
165-
return nil, fmt.Errorf("invalid nonce: %w", err)
163+
return fmt.Errorf("invalid nonce: %w", err)
166164
} else {
167165
op.Nonce = decimal.NewFromBigInt(nonceBI, 0)
168166
}
169167
if op.InitCode, err = hexutil.Decode(uoDTO.InitCode); err != nil {
170-
return nil, fmt.Errorf("invalid initCode: %w", err)
168+
return fmt.Errorf("invalid initCode: %w", err)
171169
}
172170
if op.CallData, err = hexutil.Decode(uoDTO.CallData); err != nil {
173-
return nil, fmt.Errorf("invalid callData: %w", err)
171+
return fmt.Errorf("invalid callData: %w", err)
174172
}
175173
if callGasLimitBI, ok := new(big.Int).SetString(uoDTO.CallGasLimit, 0); !ok {
176-
return nil, fmt.Errorf("invalid callGasLimit: %w", err)
174+
return fmt.Errorf("invalid callGasLimit: %w", err)
177175
} else {
178176
op.CallGasLimit = decimal.NewFromBigInt(callGasLimitBI, 0)
179177
}
180178
if verificationGasLimitBI, ok := new(big.Int).SetString(uoDTO.VerificationGasLimit, 0); !ok {
181-
return nil, fmt.Errorf("invalid verificationGasLimit: %w", err)
179+
return fmt.Errorf("invalid verificationGasLimit: %w", err)
182180
} else {
183181
op.VerificationGasLimit = decimal.NewFromBigInt(verificationGasLimitBI, 0)
184182
}
185183
if preVerificationGasBI, ok := new(big.Int).SetString(uoDTO.PreVerificationGas, 0); !ok {
186-
return nil, fmt.Errorf("invalid preVerificationGas: %w", err)
184+
return fmt.Errorf("invalid preVerificationGas: %w", err)
187185
} else {
188186
op.PreVerificationGas = decimal.NewFromBigInt(preVerificationGasBI, 0)
189187
}
190188
if maxFeePerGasBI, ok := new(big.Int).SetString(uoDTO.MaxFeePerGas, 0); !ok {
191-
return nil, fmt.Errorf("invalid maxFeePerGas: %w", err)
189+
return fmt.Errorf("invalid maxFeePerGas: %w", err)
192190
} else {
193191
op.MaxFeePerGas = decimal.NewFromBigInt(maxFeePerGasBI, 0)
194192
}
195193
if maxPriorityFeePerGasBI, ok := new(big.Int).SetString(uoDTO.MaxPriorityFeePerGas, 0); !ok {
196-
return nil, fmt.Errorf("invalid maxPriorityFeePerGas: %w", err)
194+
return fmt.Errorf("invalid maxPriorityFeePerGas: %w", err)
197195
} else {
198196
op.MaxPriorityFeePerGas = decimal.NewFromBigInt(maxPriorityFeePerGasBI, 0)
199197
}
200198
if op.PaymasterAndData, err = hexutil.Decode(uoDTO.PaymasterAndData); err != nil {
201-
return nil, fmt.Errorf("invalid paymasterAndData: %w", err)
199+
return fmt.Errorf("invalid paymasterAndData: %w", err)
202200
}
203201
if op.Signature, err = hexutil.Decode(uoDTO.Signature); err != nil {
204-
return nil, fmt.Errorf("invalid signature: %w", err)
202+
return fmt.Errorf("invalid signature: %w", err)
205203
}
206204

207-
return &op, nil
205+
return nil
208206
}

pkg/userop/userop_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ func TestUnmarshal(t *testing.T) {
7676
userOpBytes, err := json.Marshal(userOpDTO)
7777
require.NoError(t, err)
7878

79-
userOp, err := UnmarshalJSON(userOpBytes)
79+
var userOp UserOperation
80+
err = json.Unmarshal(userOpBytes, &userOp)
8081
require.NoError(t, err)
8182
assert.Equal(t, userOpDTO.Sender, userOp.Sender.String())
8283
assert.Equal(t, userOpDTO.Nonce, "0x"+userOp.Nonce.BigInt().Text(16))

0 commit comments

Comments
 (0)