forked from flashbots/mev-boost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelay_entry.go
49 lines (40 loc) · 1.18 KB
/
relay_entry.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
package server
import (
"net/url"
"strings"
"github.com/flashbots/go-boost-utils/types"
)
// RelayEntry represents a relay that mev-boost connects to.
type RelayEntry struct {
PublicKey types.PublicKey
URL *url.URL
}
func (r *RelayEntry) String() string {
return r.URL.String()
}
// GetURI returns the full request URI with scheme, host, path and args.
func (r *RelayEntry) GetURI(path string) string {
u2 := *r.URL
u2.User = nil
u2.Path = path
return u2.String()
}
// NewRelayEntry creates a new instance based on an input string
// relayURL can be IP@PORT, PUBKEY@IP:PORT, https://IP, etc.
func NewRelayEntry(relayURL string) (entry RelayEntry, err error) {
// Add protocol scheme prefix if it does not exist.
if !strings.HasPrefix(relayURL, "http") {
relayURL = "http://" + relayURL
}
// Parse the provided relay's URL and save the parsed URL in the RelayEntry.
entry.URL, err = url.ParseRequestURI(relayURL)
if err != nil {
return entry, err
}
// Extract the relay's public key from the parsed URL.
if entry.URL.User.Username() == "" {
return entry, ErrMissingRelayPubkey
}
err = entry.PublicKey.UnmarshalText([]byte(entry.URL.User.Username()))
return entry, err
}