Skip to content

Commit

Permalink
refactor: ♻️ mev: requestSignature
Browse files Browse the repository at this point in the history
Signed-off-by: thanhpp <[email protected]>
  • Loading branch information
thanhpp committed Jan 3, 2024
1 parent 7ae2e84 commit 267c4e6
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions pkg/mev/send_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ func SendBundle( // nolint: cyclop
return SendBundleResponse{}, fmt.Errorf("new http request error: %w", err)
}
if opts.flashbotSignKey != nil {
if err := signFlashbotRequest(opts.flashbotSignKey, httpReq, reqBody); err != nil {
signature, err := requestSignature(opts.flashbotSignKey, reqBody)
if err != nil {
return SendBundleResponse{}, fmt.Errorf("sign flashbot request error: %w", err)
}
httpReq.Header.Add(
"X-Flashbots-Signature",
fmt.Sprintf("%s:%s",
crypto.PubkeyToAddress(opts.flashbotSignKey.PublicKey), hexutil.Encode(signature)))
}
httpReq.Header.Add("Content-Type", "application/json")
httpReq.Header.Add("Accept", "application/json")
Expand Down Expand Up @@ -73,18 +78,14 @@ func SendBundle( // nolint: cyclop
return resp, nil
}

func signFlashbotRequest(key *ecdsa.PrivateKey, request *http.Request, body []byte) error {
func requestSignature(key *ecdsa.PrivateKey, body []byte) ([]byte, error) {
hashed := crypto.Keccak256Hash(body).Hex()
signature, err := crypto.Sign(accounts.TextHash([]byte(hashed)), key)
if err != nil {
return fmt.Errorf("sign crypto error: %w", err)
return nil, fmt.Errorf("sign crypto error: %w", err)
}

request.Header.Add(
"X-Flashbots-Signature",
fmt.Sprintf("%s:%s", crypto.PubkeyToAddress(key.PublicKey), hexutil.Encode(signature)))

return nil
return signature, nil
}

type sendBundleOpts struct {
Expand Down

0 comments on commit 267c4e6

Please sign in to comment.