/
validatorregistration.go
152 lines (132 loc) · 4.38 KB
/
validatorregistration.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
// Copyright © 2022 Attestant Limited.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package v1
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"github.com/attestantio/go-eth2-client/spec/bellatrix"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/goccy/go-yaml"
"github.com/pkg/errors"
)
// ValidatorRegistration represents a ValidatorRegistrationV1.
type ValidatorRegistration struct {
FeeRecipient bellatrix.ExecutionAddress `ssz-size:"20"`
GasLimit uint64
Timestamp time.Time
Pubkey phase0.BLSPubKey `ssz-size:"48"`
}
// validatorRegistrationJSON is the spec representation of the struct.
type validatorRegistrationJSON struct {
FeeRecipient string `json:"fee_recipient"`
GasLimit string `json:"gas_limit"`
Timestamp string `json:"timestamp"`
Pubkey string `json:"pubkey"`
}
// validatorRegistrationYAML is the spec representation of the struct.
type validatorRegistrationYAML struct {
FeeRecipient string `yaml:"fee_recipient"`
GasLimit uint64 `yaml:"gas_limit"`
Timestamp uint64 `yaml:"timestamp"`
Pubkey string `yaml:"pubkey"`
}
// MarshalJSON implements json.Marshaler.
func (v *ValidatorRegistration) MarshalJSON() ([]byte, error) {
return json.Marshal(&validatorRegistrationJSON{
FeeRecipient: v.FeeRecipient.String(),
GasLimit: strconv.FormatUint(v.GasLimit, 10),
Timestamp: strconv.FormatInt(v.Timestamp.Unix(), 10),
Pubkey: fmt.Sprintf("%#x", v.Pubkey),
})
}
// UnmarshalJSON implements json.Unmarshaler.
func (v *ValidatorRegistration) UnmarshalJSON(input []byte) error {
var data validatorRegistrationJSON
if err := json.Unmarshal(input, &data); err != nil {
return errors.Wrap(err, "invalid JSON")
}
return v.unpack(&data)
}
func (v *ValidatorRegistration) unpack(data *validatorRegistrationJSON) error {
if data.FeeRecipient == "" {
return errors.New("fee recipient missing")
}
feeRecipient, err := hex.DecodeString(strings.TrimPrefix(data.FeeRecipient, "0x"))
if err != nil {
return errors.Wrap(err, "invalid value for fee recipient")
}
copy(v.FeeRecipient[:], feeRecipient)
if data.GasLimit == "" {
return errors.New("gas limit missing")
}
gasLimit, err := strconv.ParseUint(data.GasLimit, 10, 64)
if err != nil {
return errors.Wrap(err, "invalid value for gas limit")
}
v.GasLimit = gasLimit
if data.Timestamp == "" {
return errors.New("timestamp missing")
}
timestamp, err := strconv.ParseInt(data.Timestamp, 10, 64)
if err != nil {
return errors.Wrap(err, "invalid value for timestamp")
}
v.Timestamp = time.Unix(timestamp, 0)
if data.Pubkey == "" {
return errors.New("public key missing")
}
pubKey, err := hex.DecodeString(strings.TrimPrefix(data.Pubkey, "0x"))
if err != nil {
return errors.Wrap(err, "invalid value for public key")
}
if len(pubKey) != publicKeyLength {
return errors.New("incorrect length for public key")
}
copy(v.Pubkey[:], pubKey)
return nil
}
// MarshalYAML implements yaml.Marshaler.
func (v *ValidatorRegistration) MarshalYAML() ([]byte, error) {
yamlBytes, err := yaml.MarshalWithOptions(&validatorRegistrationYAML{
FeeRecipient: v.FeeRecipient.String(),
GasLimit: v.GasLimit,
Timestamp: uint64(v.Timestamp.Unix()),
Pubkey: fmt.Sprintf("%#x", v.Pubkey),
}, yaml.Flow(true))
if err != nil {
return nil, err
}
return bytes.ReplaceAll(yamlBytes, []byte(`"`), []byte(`'`)), nil
}
// UnmarshalYAML implements yaml.Unmarshaler.
func (v *ValidatorRegistration) UnmarshalYAML(input []byte) error {
// We unmarshal to the JSON struct to save on duplicate code.
var data validatorRegistrationJSON
if err := yaml.Unmarshal(input, &data); err != nil {
return err
}
return v.unpack(&data)
}
// String returns a string version of the structure.
func (v *ValidatorRegistration) String() string {
data, err := yaml.Marshal(v)
if err != nil {
return fmt.Sprintf("ERR: %v", err)
}
return string(data)
}