-
Notifications
You must be signed in to change notification settings - Fork 4
/
relay_entry.go
70 lines (57 loc) · 1.76 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package relay
import (
"bytes"
"net/url"
"strings"
"github.com/attestantio/go-eth2-client/spec/phase0"
)
// The point-at-infinity is 48 zero bytes.
var pointAtInfinityPubkey = [48]byte{}
// RelayEntry represents a relay that mev-plus connects to.
type RelayEntry struct {
PublicKey phase0.BLSPubKey
SigningDomain phase0.Domain
URL *url.URL
}
func (r *RelayEntry) String() string {
return r.URL.String()
}
// GetURI returns the full request URI with scheme, host, path and args for the relay.
func (r *RelayEntry) GetURI(path string) string {
return GetURI(r.URL, path)
}
// 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
}
// Convert the username string to a public key.
entry.PublicKey, err = HexToPubkey(entry.URL.User.Username())
if err != nil {
return entry, err
}
// Check if the public key is valid.
if bytes.Equal(entry.PublicKey[:], pointAtInfinityPubkey[:]) {
return entry, ErrPointAtInfinityPubkey
}
return entry, nil
}
// RelayEntriesToStrings returns the string representation of a list of relay entries
func RelayEntriesToStrings(relays []RelayEntry) []string {
ret := make([]string, len(relays))
for i, entry := range relays {
ret[i] = entry.String()
}
return ret
}