fix(x/staking): Verify the pubkey type when rotating (#20714)

This commit is contained in:
Facundo Medica
2024-06-21 08:41:58 +00:00
committed by GitHub
parent a6407f411e
commit 207b30262f
2 changed files with 123 additions and 11 deletions
+22
View File
@@ -651,6 +651,28 @@ func (k msgServer) RotateConsPubKey(ctx context.Context, msg *types.MsgRotateCon
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidType, "expecting cryptotypes.PubKey, got %T", cv)
}
// check if the new public key type is valid
paramsRes := consensusv1.QueryParamsResponse{}
if err := k.QueryRouterService.InvokeTyped(ctx, &consensusv1.QueryParamsRequest{}, &paramsRes); err != nil {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "failed to query consensus params: %s", err)
}
if paramsRes.Params.Validator != nil {
pkType := pk.Type()
if !slices.Contains(paramsRes.Params.Validator.PubKeyTypes, pkType) {
return nil, errorsmod.Wrapf(
types.ErrValidatorPubKeyTypeNotSupported,
"got: %s, expected: %s", pk.Type(), paramsRes.Params.Validator.PubKeyTypes,
)
}
if pkType == sdk.PubKeyEd25519Type && len(pk.Bytes()) != ed25519.PubKeySize {
return nil, errorsmod.Wrapf(
types.ErrConsensusPubKeyLenInvalid,
"got: %d, expected: %d", len(pk.Bytes()), ed25519.PubKeySize,
)
}
}
err = k.checkConsKeyAlreadyUsed(ctx, pk)
if err != nil {
return nil, err