-
Notifications
You must be signed in to change notification settings - Fork 206
/
relay_exchange.go
70 lines (61 loc) · 1.67 KB
/
relay_exchange.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 types
import (
"strings"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/utils/sigs"
)
// RelayExchange consists a relay request and its corresponding response
type RelayExchange struct {
Request RelayRequest
Reply RelayReply
}
func NewRelayExchange(req RelayRequest, res RelayReply) RelayExchange {
return RelayExchange{Request: req, Reply: res}
}
func (re RelayExchange) GetSignature() []byte {
return re.Reply.Sig
}
func (re RelayExchange) DataToSign() []byte {
re.Reply.Sig = nil
var metadataBytes []byte
for _, metadata := range re.Reply.GetMetadata() {
data, err := metadata.Marshal()
if err != nil {
utils.LavaFormatError("metadata can't be marshaled to bytes", err)
}
metadataBytes = append(metadataBytes, data...)
}
// we remove the salt from the signature because it can be different
re.Request.RelayData.Salt = nil
msgParts := [][]byte{
re.Reply.GetData(),
[]byte(re.Request.RelayData.String()),
metadataBytes,
}
return sigs.Join(msgParts)
}
func (re RelayExchange) HashRounds() int {
return 2
}
func (rp RelayPrivateData) GetContentHashData() []byte {
var metadataBytes []byte
for _, metadataEntry := range rp.Metadata {
metadataBytes = append(metadataBytes, []byte(metadataEntry.Name+metadataEntry.Value)...)
}
requestBlockBytes := sigs.EncodeUint64(uint64(rp.RequestBlock))
seenBlockBytes := sigs.EncodeUint64(uint64(rp.SeenBlock))
msgParts := [][]byte{
metadataBytes,
[]byte(strings.Join(rp.Extensions, "")),
[]byte(rp.Addon),
[]byte(rp.ApiInterface),
[]byte(rp.ConnectionType),
[]byte(rp.ApiUrl),
rp.Data,
requestBlockBytes,
seenBlockBytes,
rp.Salt,
}
msgData := sigs.Join(msgParts)
return msgData
}