-
Notifications
You must be signed in to change notification settings - Fork 30
/
translate.go
118 lines (100 loc) · 3.29 KB
/
translate.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package types
import (
"github.com/omni-network/omni/lib/xchain"
"github.com/ethereum/go-ethereum/common"
)
// AttestationsToProto converts a slice of xchain.Attestations to a slice of protobuf Attestations.
func AttestationsToProto(atts []xchain.Attestation) []*Attestation {
resp := make([]*Attestation, 0, len(atts))
for _, att := range atts {
resp = append(resp, AttestationToProto(att))
}
return resp
}
// AttestationsFromProto converts a slice of protobuf Attestations to a slice of xchain.Attestations.
func AttestationsFromProto(atts []*Attestation) ([]xchain.Attestation, error) {
resp := make([]xchain.Attestation, 0, len(atts))
for _, attpb := range atts {
att, err := AttestationFromProto(attpb)
if err != nil {
return nil, err
}
resp = append(resp, att)
}
return resp, nil
}
// AttestationToProto converts a xchain.Attestation to a protobuf Attestation.
func AttestationToProto(att xchain.Attestation) *Attestation {
sigs := make([]*SigTuple, 0, len(att.Signatures))
for _, sig := range att.Signatures {
sigs = append(sigs, SigToProto(sig))
}
return &Attestation{
BlockHeader: BlockHeaderToProto(att.BlockHeader),
ValidatorSetId: att.ValidatorSetID,
AttestationRoot: att.AttestationRoot[:],
Signatures: sigs,
}
}
// AttestationFromProto converts a protobuf Attestation to a xchain.Attestation.
func AttestationFromProto(att *Attestation) (xchain.Attestation, error) {
if err := att.Verify(); err != nil {
return xchain.Attestation{}, err
}
header, err := BlockHeaderFromProto(att.GetBlockHeader())
if err != nil {
return xchain.Attestation{}, err
}
sigs := make([]xchain.SigTuple, 0, len(att.GetSignatures()))
for _, sigpb := range att.GetSignatures() {
sig, err := SigFromProto(sigpb)
if err != nil {
return xchain.Attestation{}, err
}
sigs = append(sigs, sig)
}
return xchain.Attestation{
BlockHeader: header,
ValidatorSetID: att.ValidatorSetId,
AttestationRoot: common.BytesToHash(att.GetAttestationRoot()),
Signatures: sigs,
}, nil
}
// SigFromProto converts a protobuf SigTuple to a xchain.SigTuple.
func SigFromProto(sig *SigTuple) (xchain.SigTuple, error) {
if err := sig.Verify(); err != nil {
return xchain.SigTuple{}, err
}
return xchain.SigTuple{
ValidatorAddress: common.Address(sig.GetValidatorAddress()),
Signature: xchain.Signature65(sig.GetSignature()),
}, nil
}
// SigToProto converts a xchain.SigTuple to a protobuf SigTuple.
func SigToProto(sig xchain.SigTuple) *SigTuple {
return &SigTuple{
ValidatorAddress: sig.ValidatorAddress[:],
Signature: sig.Signature[:],
}
}
// BlockHeaderToProto converts a xchain.BlockHeader to a protobuf BlockHeader.
func BlockHeaderToProto(header xchain.BlockHeader) *BlockHeader {
return &BlockHeader{
ChainId: header.SourceChainID,
Offset: header.BlockOffset,
Height: header.BlockHeight,
Hash: header.BlockHash[:],
}
}
// BlockHeaderFromProto converts a protobuf BlockHeader to a xchain.BlockHeader.
func BlockHeaderFromProto(header *BlockHeader) (xchain.BlockHeader, error) {
if err := header.Verify(); err != nil {
return xchain.BlockHeader{}, err
}
return xchain.BlockHeader{
SourceChainID: header.GetChainId(),
BlockOffset: header.GetOffset(),
BlockHeight: header.GetHeight(),
BlockHash: common.Hash(header.GetHash()),
}, nil
}