Skip to content

Commit

Permalink
add encode/decode for duty randao parsigneddata (#282)
Browse files Browse the repository at this point in the history
Adds encode and decode functions for Randao ParSignedData to encode.go and its unit test.
  • Loading branch information
dB2510 authored Mar 25, 2022
1 parent a24c31c commit 20a0b61
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
28 changes: 26 additions & 2 deletions core/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func EncodeAttestationParSignedData(att *eth2p0.Attestation, shareIdx int) (ParS
}, nil
}

// DecodeAttestationParSignedData returns the attestation as an encoded ParSignedData.
// DecodeAttestationParSignedData returns the attestation from the encoded ParSignedData.
func DecodeAttestationParSignedData(data ParSignedData) (*eth2p0.Attestation, error) {
att := new(eth2p0.Attestation)
err := json.Unmarshal(data.Data, att)
Expand All @@ -103,7 +103,7 @@ func EncodeAttestationAggSignedData(att *eth2p0.Attestation) (AggSignedData, err
}, nil
}

// DecodeAttestationAggSignedData returns the attestation as an encoded AggSignedData.
// DecodeAttestationAggSignedData returns the attestation from the encoded AggSignedData.
func DecodeAttestationAggSignedData(data AggSignedData) (*eth2p0.Attestation, error) {
att := new(eth2p0.Attestation)
err := json.Unmarshal(data.Data, att)
Expand All @@ -113,3 +113,27 @@ func DecodeAttestationAggSignedData(data AggSignedData) (*eth2p0.Attestation, er

return att, nil
}

// EncodeRandaoParSignedData returns the RANDAO reveal as an encoded ParSignedData.
func EncodeRandaoParSignedData(randao *eth2p0.BLSSignature, shareIdx int) (ParSignedData, error) {
data, err := json.Marshal(randao)
if err != nil {
return ParSignedData{}, errors.Wrap(err, "marshal randao reveal")
}

return ParSignedData{
Data: data,
ShareIdx: shareIdx,
}, nil
}

// DecodeRandaoParSignedData returns the RANDAO reveal from the encoded ParSignedData as BLS signature.
func DecodeRandaoParSignedData(data ParSignedData) (*eth2p0.BLSSignature, error) {
randao := new(eth2p0.BLSSignature)
err := json.Unmarshal(data.Data, randao)
if err != nil {
return nil, errors.Wrap(err, "unmarshal randao reveal")
}

return randao, nil
}
16 changes: 16 additions & 0 deletions core/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,19 @@ func TestEncodeAttesterAggSignedData(t *testing.T) {
require.Equal(t, att1, att2)
require.Equal(t, data1, data2)
}

func TestEncodeRandaoParSignedData(t *testing.T) {
randao1 := testutil.RandomSignature()

data1, err := core.EncodeRandaoParSignedData(&randao1, 1)
require.NoError(t, err)

randao2, err := core.DecodeRandaoParSignedData(data1)
require.NoError(t, err)

data2, err := core.EncodeRandaoParSignedData(randao2, 1)
require.NoError(t, err)

require.Equal(t, randao1, *randao2)
require.Equal(t, data1, data2)
}

0 comments on commit 20a0b61

Please sign in to comment.