/
partial_sig_message.go
127 lines (105 loc) · 3.7 KB
/
partial_sig_message.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
119
120
121
122
123
124
125
126
127
package types
import (
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/pkg/errors"
)
type PartialSigMsgType uint64
const (
// PostConsensusPartialSig is a partial signature over a decided duty (attestation data, block, etc)
PostConsensusPartialSig PartialSigMsgType = iota
// RandaoPartialSig is a partial signature over randao reveal
RandaoPartialSig
// SelectionProofPartialSig is a partial signature for aggregator selection proof
SelectionProofPartialSig
// ContributionProofs is the partial selection proofs for sync committee contributions (it's an array of sigs)
ContributionProofs
// ValidatorRegistrationPartialSig is a partial signature over a ValidatorRegistration object
ValidatorRegistrationPartialSig
// VoluntaryExitPartialSig is a partial signature over a VoluntaryExit object
VoluntaryExitPartialSig
)
type PartialSignatureMessages struct {
Type PartialSigMsgType
Slot phase0.Slot
Messages []*PartialSignatureMessage `ssz-max:"13"`
}
// Encode returns a msg encoded bytes or error
func (msgs *PartialSignatureMessages) Encode() ([]byte, error) {
return msgs.MarshalSSZ()
}
// Decode returns error if decoding failed
func (msgs *PartialSignatureMessages) Decode(data []byte) error {
return msgs.UnmarshalSSZ(data)
}
// GetRoot returns the root used for signing and verification
func (msgs PartialSignatureMessages) GetRoot() ([32]byte, error) {
return msgs.HashTreeRoot()
}
func (msgs PartialSignatureMessages) Validate() error {
if len(msgs.Messages) == 0 {
return errors.New("no PartialSignatureMessages messages")
}
for _, m := range msgs.Messages {
if err := m.Validate(); err != nil {
return errors.Wrap(err, "message invalid")
}
}
return nil
}
// PartialSignatureMessage is a msg for partial Beacon chain related signatures (like partial attestation, block, randao sigs)
type PartialSignatureMessage struct {
PartialSignature Signature `ssz-size:"96"` // The Beacon chain partial Signature for a duty
SigningRoot [32]byte `ssz-size:"32"` // the root signed in PartialSignature
Signer OperatorID
}
// Encode returns a msg encoded bytes or error
func (pcsm *PartialSignatureMessage) Encode() ([]byte, error) {
return pcsm.MarshalSSZ()
}
// Decode returns error if decoding failed
func (pcsm *PartialSignatureMessage) Decode(data []byte) error {
return pcsm.UnmarshalSSZ(data)
}
func (pcsm *PartialSignatureMessage) GetRoot() ([32]byte, error) {
return pcsm.HashTreeRoot()
}
func (pcsm *PartialSignatureMessage) Validate() error {
if pcsm.Signer == 0 {
return errors.New("signer ID 0 not allowed")
}
return nil
}
// SignedPartialSignatureMessage is an operator's signature over PartialSignatureMessage
type SignedPartialSignatureMessage struct {
Message PartialSignatureMessages
Signature Signature `ssz-size:"96"`
Signer OperatorID
}
// Encode returns a msg encoded bytes or error
func (spcsm *SignedPartialSignatureMessage) Encode() ([]byte, error) {
return spcsm.MarshalSSZ()
}
// Decode returns error if decoding failed
func (spcsm *SignedPartialSignatureMessage) Decode(data []byte) error {
return spcsm.UnmarshalSSZ(data)
}
func (spcsm *SignedPartialSignatureMessage) GetSignature() Signature {
return spcsm.Signature
}
func (spcsm *SignedPartialSignatureMessage) GetSigners() []OperatorID {
return []OperatorID{spcsm.Signer}
}
func (spcsm *SignedPartialSignatureMessage) GetRoot() ([32]byte, error) {
return spcsm.Message.GetRoot()
}
func (spcsm *SignedPartialSignatureMessage) Validate() error {
if spcsm.Signer == 0 {
return errors.New("signer ID 0 not allowed")
}
for _, msg := range spcsm.Message.Messages {
if spcsm.Signer != msg.Signer {
return errors.New("inconsistent signers")
}
}
return spcsm.Message.Validate()
}