-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
messages.go
82 lines (73 loc) · 1.9 KB
/
messages.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
package s4
import (
"bytes"
"math/big"
ubig "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big"
"github.com/smartcontractkit/chainlink/v2/core/services/s4"
"github.com/ethereum/go-ethereum/common"
"google.golang.org/protobuf/proto"
)
func MarshalQuery(rows []*SnapshotRow, addressRange *s4.AddressRange) ([]byte, error) {
rr := &Query{
AddressRange: &AddressRange{
MinAddress: addressRange.MinAddress.Bytes(),
MaxAddress: addressRange.MaxAddress.Bytes(),
},
Rows: rows,
}
return proto.Marshal(rr)
}
func UnmarshalQuery(data []byte) ([]*SnapshotRow, *s4.AddressRange, error) {
addressRange := s4.NewFullAddressRange()
query := &Query{}
if err := proto.Unmarshal(data, query); err != nil {
return nil, nil, err
}
if query.Rows == nil {
query.Rows = make([]*SnapshotRow, 0)
}
if query.AddressRange != nil {
addressRange = &s4.AddressRange{
MinAddress: UnmarshalAddress(query.AddressRange.MinAddress),
MaxAddress: UnmarshalAddress(query.AddressRange.MaxAddress),
}
}
return query.Rows, addressRange, nil
}
func MarshalRows(rows []*Row) ([]byte, error) {
rr := &Rows{
Rows: rows,
}
return proto.Marshal(rr)
}
func UnmarshalRows(data []byte) ([]*Row, error) {
rows := &Rows{}
if err := proto.Unmarshal(data, rows); err != nil {
return nil, err
}
if rows.Rows == nil {
rows.Rows = make([]*Row, 0)
}
return rows.Rows, nil
}
func UnmarshalAddress(address []byte) *ubig.Big {
return ubig.New(new(big.Int).SetBytes(address))
}
func (row *Row) VerifySignature() error {
address := common.BytesToAddress(row.Address)
e := &s4.Envelope{
Address: address.Bytes(),
SlotID: uint(row.Slotid),
Payload: row.Payload,
Version: row.Version,
Expiration: row.Expiration,
}
signer, err := e.GetSignerAddress(row.Signature)
if err != nil {
return err
}
if !bytes.Equal(signer.Bytes(), address.Bytes()) {
return s4.ErrWrongSignature
}
return nil
}