diff --git a/packages/cli/src/cmds/validator/keymanager/impl.ts b/packages/cli/src/cmds/validator/keymanager/impl.ts index 8816f964a8c..24bf653250e 100644 --- a/packages/cli/src/cmds/validator/keymanager/impl.ts +++ b/packages/cli/src/cmds/validator/keymanager/impl.ts @@ -42,11 +42,13 @@ export class KeymanagerApi implements Api { } async listFeeRecipient({pubkey}: {pubkey: PubkeyHex}): ReturnType { + this.assertValidKnownPubkey(pubkey); return {data: {pubkey, ethaddress: this.validator.validatorStore.getFeeRecipient(pubkey)}}; } async setFeeRecipient({pubkey, ethaddress}: FeeRecipientData): ReturnType { this.checkIfProposerWriteEnabled(); + this.assertValidKnownPubkey(pubkey); this.validator.validatorStore.setFeeRecipient(pubkey, parseFeeRecipient(ethaddress)); this.persistedKeysBackend.writeProposerConfig(pubkey, this.validator.validatorStore.getProposerConfig(pubkey)); return {status: 202}; @@ -54,12 +56,14 @@ export class KeymanagerApi implements Api { async deleteFeeRecipient({pubkey}: {pubkey: PubkeyHex}): ReturnType { this.checkIfProposerWriteEnabled(); + this.assertValidKnownPubkey(pubkey); this.validator.validatorStore.deleteFeeRecipient(pubkey); this.persistedKeysBackend.writeProposerConfig(pubkey, this.validator.validatorStore.getProposerConfig(pubkey)); return {status: 204}; } async getGraffiti({pubkey}: {pubkey: PubkeyHex}): ReturnType { + this.assertValidKnownPubkey(pubkey); const graffiti = this.validator.validatorStore.getGraffiti(pubkey); if (graffiti === undefined) { throw new ApiError(404, `No graffiti for pubkey ${pubkey}`); @@ -69,6 +73,7 @@ export class KeymanagerApi implements Api { async setGraffiti({pubkey, graffiti}: GraffitiData): ReturnType { this.checkIfProposerWriteEnabled(); + this.assertValidKnownPubkey(pubkey); this.validator.validatorStore.setGraffiti(pubkey, graffiti); this.persistedKeysBackend.writeProposerConfig(pubkey, this.validator.validatorStore.getProposerConfig(pubkey)); return {status: 202}; @@ -76,18 +81,21 @@ export class KeymanagerApi implements Api { async deleteGraffiti({pubkey}: {pubkey: PubkeyHex}): ReturnType { this.checkIfProposerWriteEnabled(); + this.assertValidKnownPubkey(pubkey); this.validator.validatorStore.deleteGraffiti(pubkey); this.persistedKeysBackend.writeProposerConfig(pubkey, this.validator.validatorStore.getProposerConfig(pubkey)); return {status: 204}; } async getGasLimit({pubkey}: {pubkey: PubkeyHex}): ReturnType { + this.assertValidKnownPubkey(pubkey); const gasLimit = this.validator.validatorStore.getGasLimit(pubkey); return {data: {pubkey, gasLimit}}; } async setGasLimit({pubkey, gasLimit}: GasLimitData): ReturnType { this.checkIfProposerWriteEnabled(); + this.assertValidKnownPubkey(pubkey); this.validator.validatorStore.setGasLimit(pubkey, gasLimit); this.persistedKeysBackend.writeProposerConfig(pubkey, this.validator.validatorStore.getProposerConfig(pubkey)); return {status: 202}; @@ -95,6 +103,7 @@ export class KeymanagerApi implements Api { async deleteGasLimit({pubkey}: {pubkey: PubkeyHex}): ReturnType { this.checkIfProposerWriteEnabled(); + this.assertValidKnownPubkey(pubkey); this.validator.validatorStore.deleteGasLimit(pubkey); this.persistedKeysBackend.writeProposerConfig(pubkey, this.validator.validatorStore.getProposerConfig(pubkey)); return {status: 204}; @@ -344,6 +353,7 @@ export class KeymanagerApi implements Api { } async getBuilderBoostFactor({pubkey}: {pubkey: PubkeyHex}): ReturnType { + this.assertValidKnownPubkey(pubkey); const builderBoostFactor = this.validator.validatorStore.getBuilderBoostFactor(pubkey); return {data: {pubkey, builderBoostFactor}}; } @@ -353,6 +363,7 @@ export class KeymanagerApi implements Api { builderBoostFactor, }: BuilderBoostFactorData): ReturnType { this.checkIfProposerWriteEnabled(); + this.assertValidKnownPubkey(pubkey); this.validator.validatorStore.setBuilderBoostFactor(pubkey, builderBoostFactor); this.persistedKeysBackend.writeProposerConfig(pubkey, this.validator.validatorStore.getProposerConfig(pubkey)); return {status: 202}; @@ -366,6 +377,8 @@ export class KeymanagerApi implements Api { } async getProposerConfig({pubkey}: {pubkey: PubkeyHex}): ReturnType { + this.assertValidKnownPubkey(pubkey); + const config = this.validator.validatorStore.getProposerConfig(pubkey); const data: ProposerConfigResponse = { @@ -383,10 +396,18 @@ export class KeymanagerApi implements Api { } async signVoluntaryExit({pubkey, epoch}: {pubkey: PubkeyHex; epoch?: Epoch}): ReturnType { + this.assertValidKnownPubkey(pubkey); + return {data: await this.validator.signVoluntaryExit(pubkey, epoch)}; + } + + private assertValidKnownPubkey(pubkey: PubkeyHex): void { if (!isValidatePubkeyHex(pubkey)) { throw new ApiError(400, `Invalid pubkey ${pubkey}`); } - return {data: await this.validator.signVoluntaryExit(pubkey, epoch)}; + + if (!this.validator.validatorStore.hasVotingPubkey(pubkey)) { + throw new ApiError(404, `Validator pubkey ${pubkey} not known`); + } } } diff --git a/packages/validator/src/services/validatorStore.ts b/packages/validator/src/services/validatorStore.ts index 0c66f576ffa..e80c0b291ef 100644 --- a/packages/validator/src/services/validatorStore.ts +++ b/packages/validator/src/services/validatorStore.ts @@ -251,7 +251,11 @@ export class ValidatorStore { } getGraffiti(pubkeyHex: PubkeyHex): string | undefined { - return this.validators.get(pubkeyHex)?.graffiti ?? this.defaultProposerConfig.graffiti; + const validatorData = this.validators.get(pubkeyHex); + if (validatorData === undefined) { + throw Error(`Validator pubkey ${pubkeyHex} not known`); + } + return validatorData.graffiti ?? this.defaultProposerConfig.graffiti; } setGraffiti(pubkeyHex: PubkeyHex, graffiti: string): void {