|
| 1 | +package userop |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/ethereum/go-ethereum/common" |
| 12 | +) |
| 13 | + |
| 14 | +type rpcRequest struct { |
| 15 | + JSONRPC string `json:"jsonrpc"` |
| 16 | + ID uint64 `json:"id"` |
| 17 | + Method string `json:"method"` |
| 18 | + Params []any `json:"params"` |
| 19 | +} |
| 20 | + |
| 21 | +type rpcError struct { |
| 22 | + Code int `json:"code"` |
| 23 | + Message string `json:"message"` |
| 24 | + Data json.RawMessage `json:"data,omitempty"` |
| 25 | +} |
| 26 | + |
| 27 | +type rpcResponse struct { |
| 28 | + JSONRPC string `json:"jsonrpc"` |
| 29 | + ID uint64 `json:"id"` |
| 30 | + Result json.RawMessage `json:"result,omitempty"` |
| 31 | + Error *rpcError `json:"error,omitempty"` |
| 32 | +} |
| 33 | + |
| 34 | +// BundlerClient is a tiny JSON-RPC client for ERC-4337 calls. |
| 35 | +type BundlerClient struct { |
| 36 | + endpoint string |
| 37 | + httpClient *http.Client |
| 38 | +} |
| 39 | + |
| 40 | +// NewBundlerClient creates a client for eth_sendUserOperation requests. |
| 41 | +func NewBundlerClient(endpoint string) *BundlerClient { |
| 42 | + return &BundlerClient{ |
| 43 | + endpoint: endpoint, |
| 44 | + httpClient: &http.Client{ |
| 45 | + Timeout: 20 * time.Second, |
| 46 | + }, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// NewBundlerClientWithHTTPClient is useful for tests and custom transport wiring. |
| 51 | +func NewBundlerClientWithHTTPClient(endpoint string, httpClient *http.Client) *BundlerClient { |
| 52 | + if httpClient == nil { |
| 53 | + httpClient = &http.Client{Timeout: 20 * time.Second} |
| 54 | + } |
| 55 | + return &BundlerClient{ |
| 56 | + endpoint: endpoint, |
| 57 | + httpClient: httpClient, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// BuildSendUserOperationRequest returns JSON payload for eth_sendUserOperation. |
| 62 | +func BuildSendUserOperationRequest(op UserOperation, entryPoint common.Address) ([]byte, error) { |
| 63 | + if err := op.ValidateBasic(); err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + body := rpcRequest{ |
| 67 | + JSONRPC: "2.0", |
| 68 | + ID: 1, |
| 69 | + Method: "eth_sendUserOperation", |
| 70 | + Params: []any{op, entryPoint}, |
| 71 | + } |
| 72 | + enc, err := json.MarshalIndent(body, "", " ") |
| 73 | + if err != nil { |
| 74 | + return nil, fmt.Errorf("marshal rpc request: %w", err) |
| 75 | + } |
| 76 | + return enc, nil |
| 77 | +} |
| 78 | + |
| 79 | +// SendUserOperation sends one user operation and returns the userOp hash. |
| 80 | +func (c *BundlerClient) SendUserOperation(ctx context.Context, op UserOperation, entryPoint common.Address) (common.Hash, error) { |
| 81 | + payload, err := BuildSendUserOperationRequest(op, entryPoint) |
| 82 | + if err != nil { |
| 83 | + return common.Hash{}, err |
| 84 | + } |
| 85 | + |
| 86 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.endpoint, bytes.NewReader(payload)) |
| 87 | + if err != nil { |
| 88 | + return common.Hash{}, fmt.Errorf("create request: %w", err) |
| 89 | + } |
| 90 | + req.Header.Set("Content-Type", "application/json") |
| 91 | + |
| 92 | + resp, err := c.httpClient.Do(req) |
| 93 | + if err != nil { |
| 94 | + return common.Hash{}, fmt.Errorf("post request: %w", err) |
| 95 | + } |
| 96 | + defer resp.Body.Close() |
| 97 | + |
| 98 | + var rpcResp rpcResponse |
| 99 | + if err := json.NewDecoder(resp.Body).Decode(&rpcResp); err != nil { |
| 100 | + return common.Hash{}, fmt.Errorf("decode rpc response: %w", err) |
| 101 | + } |
| 102 | + if rpcResp.Error != nil { |
| 103 | + return common.Hash{}, fmt.Errorf("rpc error %d: %s", rpcResp.Error.Code, rpcResp.Error.Message) |
| 104 | + } |
| 105 | + var hash common.Hash |
| 106 | + if err := json.Unmarshal(rpcResp.Result, &hash); err != nil { |
| 107 | + return common.Hash{}, fmt.Errorf("decode result hash: %w", err) |
| 108 | + } |
| 109 | + return hash, nil |
| 110 | +} |
0 commit comments