2022-12-06 12:27:26 +00:00
|
|
|
package beacon_api
|
|
|
|
|
|
|
|
import (
|
2023-01-06 03:32:13 +00:00
|
|
|
"context"
|
2022-12-06 12:27:26 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
|
|
|
|
)
|
|
|
|
|
2023-01-06 03:32:13 +00:00
|
|
|
func (c beaconApiValidatorClient) validatorIndex(ctx context.Context, in *ethpb.ValidatorIndexRequest) (*ethpb.ValidatorIndexResponse, error) {
|
2022-12-06 12:27:26 +00:00
|
|
|
stringPubKey := hexutil.Encode(in.PublicKey)
|
|
|
|
|
2023-01-06 03:32:13 +00:00
|
|
|
stateValidator, err := c.stateValidatorsProvider.GetStateValidators(ctx, []string{stringPubKey}, nil, nil)
|
2022-12-06 12:27:26 +00:00
|
|
|
if err != nil {
|
2022-12-14 12:58:36 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to get state validator")
|
2022-12-06 12:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(stateValidator.Data) == 0 {
|
|
|
|
return nil, errors.Errorf("could not find validator index for public key `%s`", stringPubKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
stringValidatorIndex := stateValidator.Data[0].Index
|
|
|
|
|
|
|
|
index, err := strconv.ParseUint(stringValidatorIndex, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to parse validator index")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ðpb.ValidatorIndexResponse{Index: types.ValidatorIndex(index)}, nil
|
|
|
|
}
|