-
Notifications
You must be signed in to change notification settings - Fork 962
/
custom_mappers.go
393 lines (374 loc) · 13.8 KB
/
custom_mappers.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package v1
import (
"fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v5/network/forks"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/time/slots"
)
// MapForkInfo maps the eth2.ForkInfo proto to the Web3Signer spec.
func MapForkInfo(slot primitives.Slot, genesisValidatorsRoot []byte) (*ForkInfo, error) {
fork, err := forks.Fork(slots.ToEpoch(slot))
if err != nil {
return nil, errors.Wrap(err, "could not get fork info")
}
forkData := &Fork{
PreviousVersion: fork.PreviousVersion,
CurrentVersion: fork.CurrentVersion,
Epoch: fmt.Sprint(fork.Epoch),
}
return &ForkInfo{
Fork: forkData,
GenesisValidatorsRoot: genesisValidatorsRoot,
}, nil
}
// MapAggregateAndProof maps the eth2.AggregateAndProof proto to the Web3Signer spec.
func MapAggregateAndProof(from *ethpb.AggregateAttestationAndProof) (*AggregateAndProof, error) {
if from == nil {
return nil, fmt.Errorf("AggregateAttestationAndProof is nil")
}
aggregate, err := MapAttestation(from.Aggregate)
if err != nil {
return nil, err
}
return &AggregateAndProof{
AggregatorIndex: fmt.Sprint(from.AggregatorIndex),
Aggregate: aggregate,
SelectionProof: from.SelectionProof,
}, nil
}
// MapAttestation maps the eth2.Attestation proto to the Web3Signer spec.
func MapAttestation(attestation *ethpb.Attestation) (*Attestation, error) {
if attestation == nil {
return nil, fmt.Errorf("attestation is nil")
}
if attestation.AggregationBits == nil {
return nil, fmt.Errorf("aggregation bits in attestation is nil")
}
data, err := MapAttestationData(attestation.Data)
if err != nil {
return nil, err
}
return &Attestation{
AggregationBits: []byte(attestation.AggregationBits),
Data: data,
Signature: attestation.Signature,
}, nil
}
// MapAttestationData maps the eth2.AttestationData proto to the Web3Signer spec.
func MapAttestationData(data *ethpb.AttestationData) (*AttestationData, error) {
if data == nil {
return nil, fmt.Errorf("attestation data is nil")
}
source, err := MapCheckPoint(data.Source)
if err != nil {
return nil, errors.Wrap(err, "could not map source for attestation data")
}
target, err := MapCheckPoint(data.Target)
if err != nil {
return nil, errors.Wrap(err, "could not map target for attestation data")
}
return &AttestationData{
Slot: fmt.Sprint(data.Slot),
Index: fmt.Sprint(data.CommitteeIndex),
BeaconBlockRoot: data.BeaconBlockRoot,
Source: source,
Target: target,
}, nil
}
// MapCheckPoint maps the eth2.Checkpoint proto to the Web3Signer spec.
func MapCheckPoint(checkpoint *ethpb.Checkpoint) (*Checkpoint, error) {
if checkpoint == nil {
return nil, fmt.Errorf("checkpoint is nil")
}
return &Checkpoint{
Epoch: fmt.Sprint(checkpoint.Epoch),
Root: hexutil.Encode(checkpoint.Root),
}, nil
}
// MapBeaconBlockBody maps the eth2.BeaconBlockBody proto to the Web3Signer spec.
func MapBeaconBlockBody(body *ethpb.BeaconBlockBody) (*BeaconBlockBody, error) {
if body == nil {
return nil, fmt.Errorf("beacon block body is nil")
}
if body.Eth1Data == nil {
return nil, fmt.Errorf("eth1 data in Beacon Block Body is nil")
}
block := &BeaconBlockBody{
RandaoReveal: body.RandaoReveal,
Eth1Data: &Eth1Data{
DepositRoot: body.Eth1Data.DepositRoot,
DepositCount: fmt.Sprint(body.Eth1Data.DepositCount),
BlockHash: body.Eth1Data.BlockHash,
},
Graffiti: body.Graffiti,
ProposerSlashings: make([]*ProposerSlashing, len(body.ProposerSlashings)),
AttesterSlashings: make([]*AttesterSlashing, len(body.AttesterSlashings)),
Attestations: make([]*Attestation, len(body.Attestations)),
Deposits: make([]*Deposit, len(body.Deposits)),
VoluntaryExits: make([]*SignedVoluntaryExit, len(body.VoluntaryExits)),
}
for i, slashing := range body.ProposerSlashings {
slashing, err := MapProposerSlashing(slashing)
if err != nil {
return nil, fmt.Errorf("could not map proposer slashing at index %v: %v", i, err)
}
block.ProposerSlashings[i] = slashing
}
for i, slashing := range body.AttesterSlashings {
slashing, err := MapAttesterSlashing(slashing)
if err != nil {
return nil, fmt.Errorf("could not map attester slashing at index %v: %v", i, err)
}
block.AttesterSlashings[i] = slashing
}
for i, attestation := range body.Attestations {
attestation, err := MapAttestation(attestation)
if err != nil {
return nil, fmt.Errorf("could not map attestation at index %v: %v", i, err)
}
block.Attestations[i] = attestation
}
for i, Deposit := range body.Deposits {
deposit, err := MapDeposit(Deposit)
if err != nil {
return nil, fmt.Errorf("could not map deposit at index %v: %v", i, err)
}
block.Deposits[i] = deposit
}
for i, signedVoluntaryExit := range body.VoluntaryExits {
signedVoluntaryExit, err := MapSignedVoluntaryExit(signedVoluntaryExit)
if err != nil {
return nil, fmt.Errorf("could not map signed voluntary exit at index %v: %v", i, err)
}
block.VoluntaryExits[i] = signedVoluntaryExit
}
return block, nil
}
// MapProposerSlashing maps the eth2.ProposerSlashing proto to the Web3Signer spec.
func MapProposerSlashing(slashing *ethpb.ProposerSlashing) (*ProposerSlashing, error) {
if slashing == nil {
return nil, fmt.Errorf("proposer slashing is nil")
}
signedHeader1, err := MapSignedBeaconBlockHeader(slashing.Header_1)
if err != nil {
return nil, errors.Wrap(err, "could not map signed header 1")
}
signedHeader2, err := MapSignedBeaconBlockHeader(slashing.Header_2)
if err != nil {
return nil, errors.Wrap(err, "could not map signed header 2")
}
return &ProposerSlashing{
Signedheader1: signedHeader1,
Signedheader2: signedHeader2,
}, nil
}
// MapSignedBeaconBlockHeader maps the eth2.AttesterSlashing proto to the Web3Signer spec.
func MapSignedBeaconBlockHeader(signedHeader *ethpb.SignedBeaconBlockHeader) (*SignedBeaconBlockHeader, error) {
if signedHeader == nil {
return nil, fmt.Errorf("signed beacon block header is nil")
}
if signedHeader.Header == nil {
return nil, fmt.Errorf("signed beacon block header message is nil")
}
return &SignedBeaconBlockHeader{
Message: &BeaconBlockHeader{
Slot: fmt.Sprint(signedHeader.Header.Slot),
ProposerIndex: fmt.Sprint(signedHeader.Header.ProposerIndex),
ParentRoot: signedHeader.Header.ParentRoot,
StateRoot: signedHeader.Header.StateRoot,
BodyRoot: signedHeader.Header.BodyRoot,
},
Signature: signedHeader.Signature,
}, nil
}
// MapAttesterSlashing maps the eth2.AttesterSlashing proto to the Web3Signer spec.
func MapAttesterSlashing(slashing *ethpb.AttesterSlashing) (*AttesterSlashing, error) {
if slashing == nil {
return nil, fmt.Errorf("attester slashing is nil")
}
attestation1, err := MapIndexedAttestation(slashing.Attestation_1)
if err != nil {
return nil, errors.Wrap(err, "could not map attestation 1")
}
attestation2, err := MapIndexedAttestation(slashing.Attestation_2)
if err != nil {
return nil, errors.Wrap(err, "could not map attestation 2")
}
return &AttesterSlashing{
Attestation1: attestation1,
Attestation2: attestation2,
}, nil
}
// MapIndexedAttestation maps the eth2.IndexedAttestation proto to the Web3Signer spec.
func MapIndexedAttestation(attestation *ethpb.IndexedAttestation) (*IndexedAttestation, error) {
if attestation == nil {
return nil, fmt.Errorf("indexed attestation is nil")
}
attestingIndices := make([]string, len(attestation.AttestingIndices))
for i, indices := range attestation.AttestingIndices {
attestingIndices[i] = fmt.Sprint(indices)
}
attestationData, err := MapAttestationData(attestation.Data)
if err != nil {
return nil, errors.Wrap(err, "could not map attestation data to IndexedAttestation")
}
return &IndexedAttestation{
AttestingIndices: attestingIndices,
Data: attestationData,
Signature: attestation.Signature,
}, nil
}
// MapDeposit maps the eth2.Deposit proto to the Web3Signer spec.
func MapDeposit(deposit *ethpb.Deposit) (*Deposit, error) {
if deposit == nil {
return nil, fmt.Errorf("deposit is nil")
}
proof := make([]string, len(deposit.Proof))
for i, p := range deposit.Proof {
proof[i] = hexutil.Encode(p)
}
return &Deposit{
Proof: proof,
Data: &DepositData{
PublicKey: deposit.Data.PublicKey,
WithdrawalCredentials: deposit.Data.WithdrawalCredentials,
Amount: fmt.Sprint(deposit.Data.Amount),
Signature: deposit.Data.Signature,
},
}, nil
}
// MapSignedVoluntaryExit maps the eth2.SignedVoluntaryExit proto to the Web3Signer spec.
func MapSignedVoluntaryExit(signedVoluntaryExit *ethpb.SignedVoluntaryExit) (*SignedVoluntaryExit, error) {
if signedVoluntaryExit == nil {
return nil, fmt.Errorf("signed voluntary exit is nil")
}
if signedVoluntaryExit.Exit == nil {
return nil, fmt.Errorf("exit in signed voluntary exit is nil")
}
return &SignedVoluntaryExit{
Message: &VoluntaryExit{
Epoch: fmt.Sprint(signedVoluntaryExit.Exit.Epoch),
ValidatorIndex: fmt.Sprint(signedVoluntaryExit.Exit.ValidatorIndex),
},
Signature: signedVoluntaryExit.Signature,
}, nil
}
// MapBeaconBlockAltair maps the eth2.BeaconBlockAltair proto to the Web3Signer spec.
func MapBeaconBlockAltair(block *ethpb.BeaconBlockAltair) (*BeaconBlockAltair, error) {
if block == nil {
return nil, fmt.Errorf("beacon block altair is nil")
}
body, err := MapBeaconBlockBodyAltair(block.Body)
if err != nil {
return nil, errors.Wrap(err, "could not map beacon block body for altair")
}
return &BeaconBlockAltair{
Slot: fmt.Sprint(block.Slot),
ProposerIndex: fmt.Sprint(block.ProposerIndex),
ParentRoot: block.ParentRoot,
StateRoot: block.StateRoot,
Body: body,
}, nil
}
// MapBeaconBlockBodyAltair maps the eth2.BeaconBlockBodyAltair proto to the Web3Signer spec.
func MapBeaconBlockBodyAltair(body *ethpb.BeaconBlockBodyAltair) (*BeaconBlockBodyAltair, error) {
if body == nil {
return nil, fmt.Errorf("beacon block body altair is nil")
}
if body.SyncAggregate == nil {
return nil, fmt.Errorf("sync aggregate in beacon block body altair is nil")
}
if body.SyncAggregate.SyncCommitteeBits == nil {
return nil, fmt.Errorf("sync committee bits in sync aggregate in beacon block body altair is nil")
}
block := &BeaconBlockBodyAltair{
RandaoReveal: body.RandaoReveal,
Eth1Data: &Eth1Data{
DepositRoot: body.Eth1Data.DepositRoot,
DepositCount: fmt.Sprint(body.Eth1Data.DepositCount),
BlockHash: body.Eth1Data.BlockHash,
},
Graffiti: body.Graffiti,
ProposerSlashings: make([]*ProposerSlashing, len(body.ProposerSlashings)),
AttesterSlashings: make([]*AttesterSlashing, len(body.AttesterSlashings)),
Attestations: make([]*Attestation, len(body.Attestations)),
Deposits: make([]*Deposit, len(body.Deposits)),
VoluntaryExits: make([]*SignedVoluntaryExit, len(body.VoluntaryExits)),
SyncAggregate: &SyncAggregate{
SyncCommitteeBits: []byte(body.SyncAggregate.SyncCommitteeBits),
SyncCommitteeSignature: body.SyncAggregate.SyncCommitteeSignature,
},
}
for i, slashing := range body.ProposerSlashings {
proposer, err := MapProposerSlashing(slashing)
if err != nil {
return nil, fmt.Errorf("could not map proposer slashing at index %v: %v", i, err)
}
block.ProposerSlashings[i] = proposer
}
for i, slashing := range body.AttesterSlashings {
attesterSlashing, err := MapAttesterSlashing(slashing)
if err != nil {
return nil, fmt.Errorf("could not map attester slashing at index %v: %v", i, err)
}
block.AttesterSlashings[i] = attesterSlashing
}
for i, attestation := range body.Attestations {
attestation, err := MapAttestation(attestation)
if err != nil {
return nil, fmt.Errorf("could not map attestation at index %v: %v", i, err)
}
block.Attestations[i] = attestation
}
for i, deposit := range body.Deposits {
deposit, err := MapDeposit(deposit)
if err != nil {
return nil, fmt.Errorf("could not map deposit at index %v: %v", i, err)
}
block.Deposits[i] = deposit
}
for i, exit := range body.VoluntaryExits {
exit, err := MapSignedVoluntaryExit(exit)
if err != nil {
return nil, fmt.Errorf("could not map signed voluntary exit at index %v: %v", i, err)
}
block.VoluntaryExits[i] = exit
}
return block, nil
}
// MapSyncAggregatorSelectionData maps the eth2.SyncAggregatorSelectionData proto to the Web3Signer spec.
func MapSyncAggregatorSelectionData(data *ethpb.SyncAggregatorSelectionData) (*SyncAggregatorSelectionData, error) {
if data == nil {
return nil, fmt.Errorf("sync aggregator selection data is nil")
}
return &SyncAggregatorSelectionData{
Slot: fmt.Sprint(data.Slot),
SubcommitteeIndex: fmt.Sprint(data.SubcommitteeIndex),
}, nil
}
// MapContributionAndProof maps the eth2.ContributionAndProof proto to the Web3Signer spec.
func MapContributionAndProof(contribution *ethpb.ContributionAndProof) (*ContributionAndProof, error) {
if contribution == nil {
return nil, fmt.Errorf("contribution and proof is nil")
}
if contribution.Contribution == nil {
return nil, fmt.Errorf("contribution in ContributionAndProof is nil")
}
if contribution.Contribution.AggregationBits == nil {
return nil, fmt.Errorf("aggregation bits in ContributionAndProof is nil")
}
return &ContributionAndProof{
AggregatorIndex: fmt.Sprint(contribution.AggregatorIndex),
SelectionProof: contribution.SelectionProof,
Contribution: &SyncCommitteeContribution{
Slot: fmt.Sprint(contribution.Contribution.Slot),
BeaconBlockRoot: contribution.Contribution.BlockRoot,
SubcommitteeIndex: fmt.Sprint(contribution.Contribution.SubcommitteeIndex),
AggregationBits: []byte(contribution.Contribution.AggregationBits),
Signature: contribution.Contribution.Signature,
},
}, nil
}