|
| 1 | +package entrypoint |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "math/big" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/ethereum/go-ethereum/common" |
| 12 | + "github.com/ethereum/go-ethereum/common/hexutil" |
| 13 | + "github.com/ethereum/go-ethereum/ethclient" |
| 14 | + "github.com/stackup-wallet/stackup-bundler/pkg/userop" |
| 15 | +) |
| 16 | + |
| 17 | +type HashLookupResult struct { |
| 18 | + UserOperation *userop.UserOperation `json:"userOperation"` |
| 19 | + EntryPoint string `json:"entryPoint"` |
| 20 | + BlockNumber *big.Int `json:"blockNumber"` |
| 21 | + BlockHash common.Hash `json:"blockHash"` |
| 22 | + TransactionHash common.Hash `json:"transactionHash"` |
| 23 | +} |
| 24 | + |
| 25 | +// GetUserOperationByHash filters the EntryPoint contract for UserOperationEvents and returns the |
| 26 | +// corresponding UserOp from a given userOpHash. |
| 27 | +func GetUserOperationByHash( |
| 28 | + eth *ethclient.Client, |
| 29 | + userOpHash string, |
| 30 | + entryPoint common.Address, |
| 31 | + chainID *big.Int, |
| 32 | +) (*HashLookupResult, error) { |
| 33 | + it, err := filterUserOperationEvent(eth, userOpHash, entryPoint) |
| 34 | + if err != nil { |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + |
| 38 | + if it.Next() { |
| 39 | + receipt, err := eth.TransactionReceipt(context.Background(), it.Event.Raw.TxHash) |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + tx, isPending, err := eth.TransactionByHash(context.Background(), it.Event.Raw.TxHash) |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } else if isPending { |
| 47 | + //lint:ignore ST1005 This needs to match the bundler test spec. |
| 48 | + return nil, errors.New("Missing/invalid userOpHash") |
| 49 | + } |
| 50 | + |
| 51 | + hex := hexutil.Encode(tx.Data()) |
| 52 | + if strings.HasPrefix(hex, handleOpsSelector) { |
| 53 | + data := common.Hex2Bytes(hex[len(handleOpsSelector):]) |
| 54 | + args, err := handleOpsMethod.Inputs.Unpack(data) |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + if len(args) != 2 { |
| 59 | + return nil, fmt.Errorf( |
| 60 | + "handleOps: invalid input length: expected 2, got %d", |
| 61 | + len(args), |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + // TODO: Find better way to convert this |
| 66 | + ops, ok := args[0].([]struct { |
| 67 | + Sender common.Address `json:"sender"` |
| 68 | + Nonce *big.Int `json:"nonce"` |
| 69 | + InitCode []uint8 `json:"initCode"` |
| 70 | + CallData []uint8 `json:"callData"` |
| 71 | + CallGasLimit *big.Int `json:"callGasLimit"` |
| 72 | + VerificationGasLimit *big.Int `json:"verificationGasLimit"` |
| 73 | + PreVerificationGas *big.Int `json:"preVerificationGas"` |
| 74 | + MaxFeePerGas *big.Int `json:"maxFeePerGas"` |
| 75 | + MaxPriorityFeePerGas *big.Int `json:"maxPriorityFeePerGas"` |
| 76 | + PaymasterAndData []uint8 `json:"paymasterAndData"` |
| 77 | + Signature []uint8 `json:"signature"` |
| 78 | + }) |
| 79 | + if !ok { |
| 80 | + return nil, errors.New("handleOps: cannot assert type: ops is not of type []struct{...}") |
| 81 | + } |
| 82 | + |
| 83 | + for _, abiOp := range ops { |
| 84 | + data, err := json.Marshal(abiOp) |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + |
| 89 | + var op userop.UserOperation |
| 90 | + if err = json.Unmarshal(data, &op); err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + |
| 94 | + if op.GetUserOpHash(entryPoint, chainID).String() == userOpHash { |
| 95 | + return &HashLookupResult{ |
| 96 | + UserOperation: &op, |
| 97 | + EntryPoint: entryPoint.String(), |
| 98 | + BlockNumber: receipt.BlockNumber, |
| 99 | + BlockHash: receipt.BlockHash, |
| 100 | + TransactionHash: it.Event.Raw.TxHash, |
| 101 | + }, nil |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + //lint:ignore ST1005 This needs to match the bundler test spec. |
| 109 | + return nil, errors.New("Missing/invalid userOpHash") |
| 110 | +} |
0 commit comments