-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmiddleware.go
97 lines (87 loc) · 2.52 KB
/
middleware.go
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
package flashbots
import (
"bytes"
"context"
"crypto/ecdsa"
"errors"
"io"
"net/http"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rpc"
"github.com/lmittmann/w3"
)
// AuthTransport returns a http.RoundTripper that adds the
// 'X-Flashbots-Signature' header to every request.
func AuthTransport(privKey *ecdsa.PrivateKey) http.RoundTripper {
if privKey == nil {
return &authRoundTripper{}
}
return &authRoundTripper{privKey, crypto.PubkeyToAddress(privKey.PublicKey), http.DefaultTransport}
}
type authRoundTripper struct {
privKey *ecdsa.PrivateKey
addr common.Address
next http.RoundTripper
}
func (auth *authRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
if auth.privKey == nil {
return nil, errors.New("flashbots: key is nil")
}
if r.Body != nil {
// write request body to buffer and set buffer as new body
buf := bytes.NewBuffer(nil)
if _, err := io.Copy(buf, r.Body); err != nil {
return nil, err
}
r.Body.Close()
r.Body = io.NopCloser(buf)
// generate payload signature
sig, err := auth.sign(buf.Bytes())
if err != nil {
return nil, err
}
r.Header.Set("X-Flashbots-Signature", sig)
}
return auth.next.RoundTrip(r)
}
func (auth *authRoundTripper) sign(body []byte) (string, error) {
bodyHash := crypto.Keccak256(body)
sig, err := crypto.Sign(accounts.TextHash([]byte(hexutil.Encode(bodyHash))), auth.privKey)
if err != nil {
return "", err
}
return auth.addr.Hex() + ":" + hexutil.Encode(sig), nil
}
// Dial returns a new [w3.Client] connected to the URL rawurl that adds the
// 'X-Flashbots-Signature' to every request. An error is returned if the
// connection establishment fails.
//
// Use [w3.Dial] to connect to an RPC endpoint that does not require signed
// requests.
func Dial(rawurl string, prv *ecdsa.PrivateKey) (*w3.Client, error) {
rpcClient, err := rpc.DialOptions(
context.Background(),
rawurl,
rpc.WithHTTPClient(&http.Client{
Transport: AuthTransport(prv),
}),
)
if err != nil {
return nil, err
}
return w3.NewClient(rpcClient), nil
}
// MustDial is like [Dial] but panics if the connection establishment fails.
//
// Use [w3.MustDial] to connect to an RPC endpoint that does not require signed
// requests.
func MustDial(rawurl string, prv *ecdsa.PrivateKey) *w3.Client {
client, err := Dial(rawurl, prv)
if err != nil {
panic("flashbots: " + err.Error())
}
return client
}