-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy patheth2signeddata.go
More file actions
181 lines (134 loc) · 5.35 KB
/
eth2signeddata.go
File metadata and controls
181 lines (134 loc) · 5.35 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright © 2022-2026 Obol Labs Inc. Licensed under the terms of a Business Source License 1.1
package core
import (
"context"
eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/obolnetwork/charon/app/errors"
"github.com/obolnetwork/charon/app/eth2wrap"
"github.com/obolnetwork/charon/eth2util"
"github.com/obolnetwork/charon/eth2util/signing"
"github.com/obolnetwork/charon/tbls"
)
var (
_ Eth2SignedData = VersionedSignedProposal{}
_ Eth2SignedData = Attestation{}
_ Eth2SignedData = VersionedAttestation{}
_ Eth2SignedData = SignedVoluntaryExit{}
_ Eth2SignedData = VersionedSignedValidatorRegistration{}
_ Eth2SignedData = SignedRandao{}
_ Eth2SignedData = BeaconCommitteeSelection{}
_ Eth2SignedData = SignedAggregateAndProof{}
_ Eth2SignedData = VersionedSignedAggregateAndProof{}
_ Eth2SignedData = SignedSyncMessage{}
_ Eth2SignedData = SignedSyncContributionAndProof{}
_ Eth2SignedData = SyncCommitteeSelection{}
)
// VerifyEth2SignedData verifies signature associated with the given Eth2SignedData.
func VerifyEth2SignedData(ctx context.Context, eth2Cl eth2wrap.Client, data Eth2SignedData, pubkey tbls.PublicKey) error {
epoch, err := data.Epoch(ctx, eth2Cl)
if err != nil {
return err
}
sigRoot, err := data.MessageRoot()
if err != nil {
return err
}
return signing.Verify(ctx, eth2Cl, data.DomainName(), epoch, sigRoot, data.Signature().ToETH2(), pubkey)
}
// Implement Eth2SignedData for VersionedSignedProposal.
func (VersionedSignedProposal) DomainName() signing.DomainName {
return signing.DomainBeaconProposer
}
func (p VersionedSignedProposal) Epoch(ctx context.Context, eth2Cl eth2wrap.Client) (eth2p0.Epoch, error) {
slot, err := p.Slot()
if err != nil {
return 0, err
}
return eth2util.EpochFromSlot(ctx, eth2Cl, slot)
}
// Implement Eth2SignedData for Attestation.
func (Attestation) DomainName() signing.DomainName {
return signing.DomainBeaconAttester
}
func (a Attestation) Epoch(_ context.Context, _ eth2wrap.Client) (eth2p0.Epoch, error) {
return a.Data.Target.Epoch, nil
}
// Implement Eth2SignedData for VersionedAttestation.
func (VersionedAttestation) DomainName() signing.DomainName {
return signing.DomainBeaconAttester
}
func (a VersionedAttestation) Epoch(_ context.Context, _ eth2wrap.Client) (eth2p0.Epoch, error) {
data, err := a.Data()
if err != nil {
return 0, errors.Wrap(err, "get attestation data")
}
return data.Target.Epoch, nil
}
// Implement Eth2SignedData for SignedVoluntaryExit.
func (SignedVoluntaryExit) DomainName() signing.DomainName {
return signing.DomainExit
}
func (e SignedVoluntaryExit) Epoch(_ context.Context, _ eth2wrap.Client) (eth2p0.Epoch, error) {
return e.Message.Epoch, nil
}
// Implement Eth2SignedData for VersionedSignedValidatorRegistration.
func (VersionedSignedValidatorRegistration) DomainName() signing.DomainName {
return signing.DomainApplicationBuilder
}
func (VersionedSignedValidatorRegistration) Epoch(context.Context, eth2wrap.Client) (eth2p0.Epoch, error) {
// Always use epoch 0 for DomainApplicationBuilder.
return 0, nil
}
// Implement Eth2SignedData for SignedRandao.
func (SignedRandao) DomainName() signing.DomainName {
return signing.DomainRandao
}
func (s SignedRandao) Epoch(_ context.Context, _ eth2wrap.Client) (eth2p0.Epoch, error) {
return s.SignedEpoch.Epoch, nil
}
// Implement Eth2SignedData for BeaconCommitteeSelection.
func (BeaconCommitteeSelection) DomainName() signing.DomainName {
return signing.DomainSelectionProof
}
func (s BeaconCommitteeSelection) Epoch(ctx context.Context, eth2Cl eth2wrap.Client) (eth2p0.Epoch, error) {
return eth2util.EpochFromSlot(ctx, eth2Cl, s.Slot)
}
// Implement Eth2SignedData for SignedAggregateAndProof.
func (SignedAggregateAndProof) DomainName() signing.DomainName {
return signing.DomainAggregateAndProof
}
func (s SignedAggregateAndProof) Epoch(ctx context.Context, eth2Cl eth2wrap.Client) (eth2p0.Epoch, error) {
return eth2util.EpochFromSlot(ctx, eth2Cl, s.Message.Aggregate.Data.Slot)
}
// Implement Eth2SignedData for SignedAggregateAndProof.
func (VersionedSignedAggregateAndProof) DomainName() signing.DomainName {
return signing.DomainAggregateAndProof
}
func (ap VersionedSignedAggregateAndProof) Epoch(ctx context.Context, eth2Cl eth2wrap.Client) (eth2p0.Epoch, error) {
slot, err := ap.Slot()
if err != nil {
return 0, err
}
return eth2util.EpochFromSlot(ctx, eth2Cl, slot)
}
// Implement Eth2SignedData for SignedSyncMessage.
func (SignedSyncMessage) DomainName() signing.DomainName {
return signing.DomainSyncCommittee
}
func (s SignedSyncMessage) Epoch(ctx context.Context, eth2Cl eth2wrap.Client) (eth2p0.Epoch, error) {
return eth2util.EpochFromSlot(ctx, eth2Cl, s.Slot)
}
// Implement Eth2SignedData for SignedSyncContributionAndProof.
func (SignedSyncContributionAndProof) DomainName() signing.DomainName {
return signing.DomainContributionAndProof
}
func (s SignedSyncContributionAndProof) Epoch(ctx context.Context, eth2Cl eth2wrap.Client) (eth2p0.Epoch, error) {
return eth2util.EpochFromSlot(ctx, eth2Cl, s.Message.Contribution.Slot)
}
// Implement Eth2SignedData for SyncCommitteeSelection.
func (SyncCommitteeSelection) DomainName() signing.DomainName {
return signing.DomainSyncCommitteeSelectionProof
}
func (s SyncCommitteeSelection) Epoch(ctx context.Context, eth2Cl eth2wrap.Client) (eth2p0.Epoch, error) {
return eth2util.EpochFromSlot(ctx, eth2Cl, s.Slot)
}