-
Notifications
You must be signed in to change notification settings - Fork 86
/
share_opt.go
76 lines (65 loc) · 2.6 KB
/
share_opt.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
package validator
import (
"encoding/hex"
spectypes "github.com/bloxapp/ssv-spec/types"
"github.com/herumi/bls-eth-go-binary/bls"
"github.com/pkg/errors"
"github.com/bloxapp/ssv/protocol/v1/blockchain/beacon"
)
// ShareOptions - used to load validator share from config
type ShareOptions struct {
NodeID uint64 `yaml:"NodeID" env:"NodeID" env-description:"Local share node ID"`
PublicKey string `yaml:"PublicKey" env:"LOCAL_NODE_ID" env-description:"Local validator public key"`
ShareKey string `yaml:"ShareKey" env:"LOCAL_SHARE_KEY" env-description:"Local share key"`
Committee map[string]int `yaml:"Committee" env:"LOCAL_COMMITTEE" env-description:"Local validator committee array"`
OwnerAddress string `yaml:"OwnerAddress" env:"LOCAL_OWNER_ADDRESS" env-description:"Local validator owner address"`
Operators []string `yaml:"Operators" env:"LOCAL_OPERATORS" env-description:"Local validator selected operators"`
OperatorIds []int `yaml:"OperatorIds" env:"LOCAL_OPERATOR_IDS" env-description:"Local validator selected operator ids"`
}
// ToShare creates a Share instance from ShareOptions
func (options *ShareOptions) ToShare() (*beacon.Share, error) {
var err error
if len(options.PublicKey) > 0 && len(options.ShareKey) > 0 && len(options.Committee) > 0 &&
len(options.OwnerAddress) > 0 && len(options.Operators) > 0 && len(options.OperatorIds) > 0 {
validatorPk := &bls.PublicKey{}
if err = validatorPk.DeserializeHexStr(options.PublicKey); err != nil {
return nil, errors.Wrap(err, "failed to decode validator key")
}
_getBytesFromHex := func(str string) []byte {
val, e := hex.DecodeString(str)
if e != nil {
err = errors.Wrap(err, "failed to decode committee")
}
return val
}
ibftCommittee := make(map[spectypes.OperatorID]*beacon.Node)
for pk, id := range options.Committee {
ibftCommittee[spectypes.OperatorID(id)] = &beacon.Node{
IbftID: uint64(id),
Pk: _getBytesFromHex(pk),
}
}
var operators [][]byte
for _, op := range options.Operators {
operators = append(operators, []byte(op))
}
var operatorIds []uint64
for _, opID := range options.OperatorIds {
operatorIds = append(operatorIds, uint64(opID))
}
if err != nil {
return nil, err
}
share := beacon.Share{
NodeID: spectypes.OperatorID(options.NodeID),
Metadata: nil,
PublicKey: validatorPk,
Committee: ibftCommittee,
OwnerAddress: options.OwnerAddress,
Operators: operators,
OperatorIds: operatorIds,
}
return &share, nil
}
return nil, errors.New("empty or invalid share")
}