package shared import ( "strconv" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/prysmaticlabs/prysm/v4/consensus-types/primitives" eth "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1" ) type Attestation struct { AggregationBits string `json:"aggregation_bits" validate:"required,hexadecimal"` Data *AttestationData `json:"data" validate:"required"` Signature string `json:"signature" validate:"required,hexadecimal"` } type AttestationData struct { Slot string `json:"slot" validate:"required,number,gte=0"` CommitteeIndex string `json:"index" validate:"required,number,gte=0"` BeaconBlockRoot string `json:"beacon_block_root" validate:"required,hexadecimal"` Source *Checkpoint `json:"source" validate:"required"` Target *Checkpoint `json:"target" validate:"required"` } type Checkpoint struct { Epoch string `json:"epoch" validate:"required,number,gte=0"` Root string `json:"root" validate:"required,hexadecimal"` } type SignedContributionAndProof struct { Message *ContributionAndProof `json:"message" validate:"required"` Signature string `json:"signature" validate:"required,hexadecimal"` } type ContributionAndProof struct { AggregatorIndex string `json:"aggregator_index" validate:"required,number,gte=0"` Contribution *SyncCommitteeContribution `json:"contribution" validate:"required"` SelectionProof string `json:"selection_proof" validate:"required,hexadecimal"` } type SyncCommitteeContribution struct { Slot string `json:"slot" validate:"required,number,gte=0"` BeaconBlockRoot string `json:"beacon_block_root" hex:"true" validate:"required,hexadecimal"` SubcommitteeIndex string `json:"subcommittee_index" validate:"required,number,gte=0"` AggregationBits string `json:"aggregation_bits" hex:"true" validate:"required,hexadecimal"` Signature string `json:"signature" hex:"true" validate:"required,hexadecimal"` } type SignedAggregateAttestationAndProof struct { Message *AggregateAttestationAndProof `json:"message" validate:"required"` Signature string `json:"signature" validate:"required,hexadecimal"` } type AggregateAttestationAndProof struct { AggregatorIndex string `json:"aggregator_index" validate:"required,number,gte=0"` Aggregate *Attestation `json:"aggregate" validate:"required"` SelectionProof string `json:"selection_proof" validate:"required,hexadecimal"` } func (s *SignedContributionAndProof) ToConsensus() (*eth.SignedContributionAndProof, error) { msg, err := s.Message.ToConsensus() if err != nil { return nil, NewDecodeError(err, "Message") } sig, err := hexutil.Decode(s.Signature) if err != nil { return nil, NewDecodeError(err, "Signature") } return ð.SignedContributionAndProof{ Message: msg, Signature: sig, }, nil } func (c *ContributionAndProof) ToConsensus() (*eth.ContributionAndProof, error) { contribution, err := c.Contribution.ToConsensus() if err != nil { return nil, NewDecodeError(err, "Contribution") } aggregatorIndex, err := strconv.ParseUint(c.AggregatorIndex, 10, 64) if err != nil { return nil, NewDecodeError(err, "AggregatorIndex") } selectionProof, err := hexutil.Decode(c.SelectionProof) if err != nil { return nil, NewDecodeError(err, "SelectionProof") } return ð.ContributionAndProof{ AggregatorIndex: primitives.ValidatorIndex(aggregatorIndex), Contribution: contribution, SelectionProof: selectionProof, }, nil } func (s *SyncCommitteeContribution) ToConsensus() (*eth.SyncCommitteeContribution, error) { slot, err := strconv.ParseUint(s.Slot, 10, 64) if err != nil { return nil, NewDecodeError(err, "Slot") } bbRoot, err := hexutil.Decode(s.BeaconBlockRoot) if err != nil { return nil, NewDecodeError(err, "BeaconBlockRoot") } subcommitteeIndex, err := strconv.ParseUint(s.SubcommitteeIndex, 10, 64) if err != nil { return nil, NewDecodeError(err, "SubcommitteeIndex") } aggBits, err := hexutil.Decode(s.AggregationBits) if err != nil { return nil, NewDecodeError(err, "AggregationBits") } sig, err := hexutil.Decode(s.Signature) if err != nil { return nil, NewDecodeError(err, "Signature") } return ð.SyncCommitteeContribution{ Slot: primitives.Slot(slot), BlockRoot: bbRoot, SubcommitteeIndex: subcommitteeIndex, AggregationBits: aggBits, Signature: sig, }, nil } func (s *SignedAggregateAttestationAndProof) ToConsensus() (*eth.SignedAggregateAttestationAndProof, error) { msg, err := s.Message.ToConsensus() if err != nil { return nil, NewDecodeError(err, "Message") } sig, err := hexutil.Decode(s.Signature) if err != nil { return nil, NewDecodeError(err, "Signature") } return ð.SignedAggregateAttestationAndProof{ Message: msg, Signature: sig, }, nil } func (a *AggregateAttestationAndProof) ToConsensus() (*eth.AggregateAttestationAndProof, error) { aggIndex, err := strconv.ParseUint(a.AggregatorIndex, 10, 64) if err != nil { return nil, NewDecodeError(err, "AggregatorIndex") } agg, err := a.Aggregate.ToConsensus() if err != nil { return nil, NewDecodeError(err, "Aggregate") } proof, err := hexutil.Decode(a.SelectionProof) if err != nil { return nil, NewDecodeError(err, "SelectionProof") } return ð.AggregateAttestationAndProof{ AggregatorIndex: primitives.ValidatorIndex(aggIndex), Aggregate: agg, SelectionProof: proof, }, nil } func (a *Attestation) ToConsensus() (*eth.Attestation, error) { aggBits, err := hexutil.Decode(a.AggregationBits) if err != nil { return nil, NewDecodeError(err, "AggregationBits") } data, err := a.Data.ToConsensus() if err != nil { return nil, NewDecodeError(err, "Data") } sig, err := hexutil.Decode(a.Signature) if err != nil { return nil, NewDecodeError(err, "Signature") } return ð.Attestation{ AggregationBits: aggBits, Data: data, Signature: sig, }, nil } func (a *AttestationData) ToConsensus() (*eth.AttestationData, error) { slot, err := strconv.ParseUint(a.Slot, 10, 64) if err != nil { return nil, NewDecodeError(err, "Slot") } committeeIndex, err := strconv.ParseUint(a.CommitteeIndex, 10, 64) if err != nil { return nil, NewDecodeError(err, "CommitteeIndex") } bbRoot, err := hexutil.Decode(a.BeaconBlockRoot) if err != nil { return nil, NewDecodeError(err, "BeaconBlockRoot") } source, err := a.Source.ToConsensus() if err != nil { return nil, NewDecodeError(err, "Source") } target, err := a.Target.ToConsensus() if err != nil { return nil, NewDecodeError(err, "Target") } return ð.AttestationData{ Slot: primitives.Slot(slot), CommitteeIndex: primitives.CommitteeIndex(committeeIndex), BeaconBlockRoot: bbRoot, Source: source, Target: target, }, nil } func (c *Checkpoint) ToConsensus() (*eth.Checkpoint, error) { epoch, err := strconv.ParseUint(c.Epoch, 10, 64) if err != nil { return nil, NewDecodeError(err, "Epoch") } root, err := hexutil.Decode(c.Root) if err != nil { return nil, NewDecodeError(err, "Root") } return ð.Checkpoint{ Epoch: primitives.Epoch(epoch), Root: root, }, nil }