2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2021-10-04 19:08:31 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2022-10-18 18:37:17 +00:00
|
|
|
# NOTE: This module has been used in both `beacon_node` and `validator_client`,
|
|
|
|
# please keep imports clear of `rest_utils` or any other module which imports
|
|
|
|
# beacon node's specific networking code.
|
|
|
|
|
2022-10-24 20:32:52 +00:00
|
|
|
import std/[tables, strutils, uri]
|
2021-10-04 19:08:31 +00:00
|
|
|
import chronos, chronicles, confutils,
|
2024-01-16 22:37:14 +00:00
|
|
|
results, stew/[base10, io2], blscurve, presto
|
2021-10-04 19:08:31 +00:00
|
|
|
import ".."/spec/[keystore, crypto]
|
2021-12-22 12:37:31 +00:00
|
|
|
import ".."/spec/eth2_apis/rest_keymanager_types
|
2022-08-19 10:30:07 +00:00
|
|
|
import ".."/validators/[slashing_protection, keystore_management,
|
2022-10-17 13:42:43 +00:00
|
|
|
validator_pool]
|
2022-10-18 18:37:17 +00:00
|
|
|
import ".."/rpc/rest_constants
|
2021-10-04 19:08:31 +00:00
|
|
|
|
2022-10-18 18:37:17 +00:00
|
|
|
export rest_constants, results
|
2021-10-04 19:08:31 +00:00
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
func validateKeymanagerApiQueries*(key: string, value: string): int =
|
|
|
|
# There are no queries to validate
|
|
|
|
return 0
|
|
|
|
|
2023-06-14 06:46:01 +00:00
|
|
|
proc listLocalValidators*(validatorPool: ValidatorPool): seq[KeystoreInfo] {.
|
2023-08-25 09:29:07 +00:00
|
|
|
raises: [].} =
|
2021-12-22 12:37:31 +00:00
|
|
|
var validators: seq[KeystoreInfo]
|
2022-08-19 10:30:07 +00:00
|
|
|
for item in validatorPool:
|
2022-02-07 20:36:09 +00:00
|
|
|
if item.kind == ValidatorKind.Local:
|
|
|
|
validators.add KeystoreInfo(
|
|
|
|
validating_pubkey: item.pubkey,
|
|
|
|
derivation_path: string(item.data.path),
|
|
|
|
readonly: false
|
|
|
|
)
|
|
|
|
validators
|
2021-10-04 19:08:31 +00:00
|
|
|
|
2023-06-14 06:46:01 +00:00
|
|
|
proc listRemoteValidators*(
|
|
|
|
validatorPool: ValidatorPool): seq[RemoteKeystoreInfo] {.
|
2023-08-25 09:29:07 +00:00
|
|
|
raises: [].} =
|
2022-02-07 20:36:09 +00:00
|
|
|
var validators: seq[RemoteKeystoreInfo]
|
2022-08-19 10:30:07 +00:00
|
|
|
for item in validatorPool:
|
2022-05-10 00:32:12 +00:00
|
|
|
if item.kind == ValidatorKind.Remote and item.data.remotes.len == 1:
|
2022-02-07 20:36:09 +00:00
|
|
|
validators.add RemoteKeystoreInfo(
|
|
|
|
pubkey: item.pubkey,
|
2022-05-10 00:32:12 +00:00
|
|
|
url: HttpHostUri(item.data.remotes[0].url)
|
|
|
|
)
|
|
|
|
validators
|
|
|
|
|
2023-06-14 06:46:01 +00:00
|
|
|
proc listRemoteDistributedValidators*(
|
|
|
|
validatorPool: ValidatorPool): seq[DistributedKeystoreInfo] {.
|
2023-08-25 09:29:07 +00:00
|
|
|
raises: [].} =
|
2022-05-10 00:32:12 +00:00
|
|
|
var validators: seq[DistributedKeystoreInfo]
|
2022-08-19 10:30:07 +00:00
|
|
|
for item in validatorPool:
|
2022-05-10 00:32:12 +00:00
|
|
|
if item.kind == ValidatorKind.Remote and item.data.remotes.len > 1:
|
|
|
|
validators.add DistributedKeystoreInfo(
|
|
|
|
pubkey: item.pubkey,
|
|
|
|
remotes: item.data.remotes
|
2022-02-07 20:36:09 +00:00
|
|
|
)
|
2021-12-22 12:37:31 +00:00
|
|
|
validators
|
|
|
|
|
2022-07-13 14:45:04 +00:00
|
|
|
proc keymanagerApiError(status: HttpCode, msg: string): RestApiResponse =
|
|
|
|
let data =
|
|
|
|
block:
|
|
|
|
var default: string
|
|
|
|
try:
|
|
|
|
var stream = memoryOutput()
|
|
|
|
var writer = JsonWriter[RestJson].init(stream)
|
|
|
|
writer.beginRecord()
|
|
|
|
writer.writeField("message", msg)
|
|
|
|
writer.endRecord()
|
|
|
|
stream.getOutput(string)
|
|
|
|
except SerializationError:
|
|
|
|
default
|
|
|
|
except IOError:
|
|
|
|
default
|
|
|
|
RestApiResponse.error(status, data, "application/json")
|
|
|
|
|
2023-06-14 06:46:01 +00:00
|
|
|
proc checkAuthorization*(
|
|
|
|
request: HttpRequestRef,
|
|
|
|
host: KeymanagerHost): Result[void, AuthorizationError] =
|
2021-12-22 12:37:31 +00:00
|
|
|
let authorizations = request.headers.getList("authorization")
|
|
|
|
if authorizations.len > 0:
|
|
|
|
for authHeader in authorizations:
|
|
|
|
let parts = authHeader.split(' ', maxsplit = 1)
|
|
|
|
if parts.len == 2 and parts[0] == "Bearer":
|
2022-08-19 10:30:07 +00:00
|
|
|
if parts[1] == host.keymanagerToken:
|
2021-12-22 12:37:31 +00:00
|
|
|
return ok()
|
2021-10-04 19:08:31 +00:00
|
|
|
else:
|
2021-12-22 12:37:31 +00:00
|
|
|
return err incorrectToken
|
|
|
|
return err missingBearerScheme
|
2021-10-04 19:08:31 +00:00
|
|
|
else:
|
2021-12-22 12:37:31 +00:00
|
|
|
return err noAuthorizationHeader
|
|
|
|
|
2022-07-13 14:45:04 +00:00
|
|
|
proc authErrorResponse(error: AuthorizationError): RestApiResponse =
|
|
|
|
let status = case error:
|
|
|
|
of missingBearerScheme, noAuthorizationHeader:
|
|
|
|
Http401
|
|
|
|
of incorrectToken:
|
|
|
|
Http403
|
|
|
|
|
|
|
|
keymanagerApiError(status, InvalidAuthorizationError)
|
|
|
|
|
2022-02-07 20:36:09 +00:00
|
|
|
proc validateUri*(url: string): Result[Uri, cstring] =
|
|
|
|
let surl = parseUri(url)
|
|
|
|
if surl.scheme notin ["http", "https"]:
|
|
|
|
return err("Incorrect URL scheme")
|
|
|
|
if len(surl.hostname) == 0:
|
|
|
|
return err("Empty URL hostname")
|
|
|
|
ok(surl)
|
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
proc handleRemoveValidatorReq(host: KeymanagerHost,
|
|
|
|
key: ValidatorPubKey): RemoteKeystoreStatus =
|
|
|
|
let res = removeValidator(host.validatorPool[],
|
|
|
|
host.validatorsDir, host.secretsDir,
|
2022-05-10 00:32:12 +00:00
|
|
|
key, KeystoreKind.Remote)
|
|
|
|
if res.isOk:
|
|
|
|
case res.value()
|
|
|
|
of RemoveValidatorStatus.deleted:
|
|
|
|
return RemoteKeystoreStatus(status: KeystoreStatus.deleted)
|
|
|
|
of RemoveValidatorStatus.notFound:
|
|
|
|
return RemoteKeystoreStatus(status: KeystoreStatus.notFound)
|
|
|
|
else:
|
|
|
|
return RemoteKeystoreStatus(status: KeystoreStatus.error,
|
2023-09-27 14:45:33 +00:00
|
|
|
message: Opt.some($res.error()))
|
2022-05-10 00:32:12 +00:00
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
proc handleAddRemoteValidatorReq(host: KeymanagerHost,
|
|
|
|
keystore: RemoteKeystore): RequestItemStatus =
|
|
|
|
let res = importKeystore(host.validatorPool[], host.validatorsDir, keystore)
|
|
|
|
if res.isOk:
|
2023-05-17 04:56:37 +00:00
|
|
|
host.addValidator(
|
|
|
|
res.get(), host.getValidatorWithdrawalAddress(keystore.pubkey))
|
2022-12-09 16:05:55 +00:00
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
RequestItemStatus(status: $KeystoreStatus.imported)
|
|
|
|
else:
|
2022-05-10 00:32:12 +00:00
|
|
|
case res.error().status
|
|
|
|
of AddValidatorStatus.failed:
|
2022-08-19 10:30:07 +00:00
|
|
|
RequestItemStatus(status: $KeystoreStatus.error,
|
|
|
|
message: $res.error().message)
|
2022-05-10 00:32:12 +00:00
|
|
|
of AddValidatorStatus.existingArtifacts:
|
2022-08-19 10:30:07 +00:00
|
|
|
RequestItemStatus(status: $KeystoreStatus.duplicate)
|
2022-05-10 00:32:12 +00:00
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
proc installKeymanagerHandlers*(router: var RestRouter, host: KeymanagerHost) =
|
2021-12-22 12:37:31 +00:00
|
|
|
# https://ethereum.github.io/keymanager-APIs/#/Keymanager/ListKeys
|
2024-01-20 16:06:28 +00:00
|
|
|
router.api2(MethodGet, "/eth/v1/keystores") do () -> RestApiResponse:
|
2022-08-19 10:30:07 +00:00
|
|
|
let authStatus = checkAuthorization(request, host)
|
2021-12-22 12:37:31 +00:00
|
|
|
if authStatus.isErr():
|
2022-07-13 14:45:04 +00:00
|
|
|
return authErrorResponse authStatus.error
|
2022-08-19 10:30:07 +00:00
|
|
|
let response = GetKeystoresResponse(
|
|
|
|
data: listLocalValidators(host.validatorPool[]))
|
2024-01-20 16:06:28 +00:00
|
|
|
RestApiResponse.jsonResponsePlain(response)
|
2021-12-22 12:37:31 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/keymanager-APIs/#/Keymanager/ImportKeystores
|
2024-01-20 16:06:28 +00:00
|
|
|
router.api2(MethodPost, "/eth/v1/keystores") do (
|
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
2022-08-19 10:30:07 +00:00
|
|
|
let authStatus = checkAuthorization(request, host)
|
2021-12-22 12:37:31 +00:00
|
|
|
if authStatus.isErr():
|
2022-07-13 14:45:04 +00:00
|
|
|
return authErrorResponse authStatus.error
|
2021-12-22 12:37:31 +00:00
|
|
|
let request =
|
2021-10-04 19:08:31 +00:00
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
2022-07-13 14:45:04 +00:00
|
|
|
return keymanagerApiError(Http404, EmptyRequestBodyError)
|
2021-12-22 12:37:31 +00:00
|
|
|
let dres = decodeBody(KeystoresAndSlashingProtection, contentBody.get())
|
2021-10-04 19:08:31 +00:00
|
|
|
if dres.isErr():
|
2022-07-13 14:45:04 +00:00
|
|
|
return keymanagerApiError(Http400, InvalidKeystoreObjects)
|
2021-10-04 19:08:31 +00:00
|
|
|
dres.get()
|
|
|
|
|
2022-02-07 20:36:09 +00:00
|
|
|
if request.slashing_protection.isSome():
|
|
|
|
let slashing_protection = request.slashing_protection.get()
|
2024-01-20 16:06:28 +00:00
|
|
|
let nodeSPDIR =
|
|
|
|
try:
|
|
|
|
toSPDIR(host.validatorPool[].slashingProtection)
|
|
|
|
except IOError as exc:
|
|
|
|
return keymanagerApiError(
|
|
|
|
Http500, "Internal server error; " & $exc.msg)
|
2022-02-07 20:36:09 +00:00
|
|
|
if nodeSPDIR.metadata.genesis_validators_root.Eth2Digest !=
|
|
|
|
slashing_protection.metadata.genesis_validators_root.Eth2Digest:
|
2022-07-13 14:45:04 +00:00
|
|
|
return keymanagerApiError(Http400,
|
2022-02-07 20:36:09 +00:00
|
|
|
"The slashing protection database and imported file refer to " &
|
|
|
|
"different blockchains.")
|
2024-01-20 16:06:28 +00:00
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
inclSPDIR(host.validatorPool[].slashingProtection,
|
|
|
|
slashing_protection)
|
|
|
|
except SerializationError as exc:
|
|
|
|
return keymanagerApiError(
|
|
|
|
Http500, "Internal server error; Failed to import slashing " &
|
|
|
|
"protection data, reason: " &
|
|
|
|
exc.formatMsg("slashing_protection"))
|
|
|
|
except IOError as exc:
|
|
|
|
return keymanagerApiError(
|
|
|
|
Http500, "Internal server error; Failed to import slashing " &
|
|
|
|
"protection data, reason: " & $exc.msg)
|
2022-02-07 20:36:09 +00:00
|
|
|
if res == siFailure:
|
2022-07-13 14:45:04 +00:00
|
|
|
return keymanagerApiError(Http500,
|
2022-02-07 20:36:09 +00:00
|
|
|
"Internal server error; Failed to import slashing protection data")
|
2021-12-22 12:37:31 +00:00
|
|
|
|
2022-02-07 20:36:09 +00:00
|
|
|
var response: PostKeystoresResponse
|
2021-12-22 12:37:31 +00:00
|
|
|
|
2022-05-10 10:03:40 +00:00
|
|
|
for index, item in request.keystores:
|
2022-08-19 10:30:07 +00:00
|
|
|
let res = importKeystore(host.validatorPool[], host.rng[],
|
|
|
|
host.validatorsDir, host.secretsDir,
|
2023-09-21 18:07:02 +00:00
|
|
|
item, request.passwords[index],
|
|
|
|
host.keystoreCache)
|
2021-10-04 19:08:31 +00:00
|
|
|
if res.isErr():
|
2022-02-07 20:36:09 +00:00
|
|
|
let failure = res.error()
|
|
|
|
case failure.status
|
|
|
|
of AddValidatorStatus.failed:
|
|
|
|
response.data.add(
|
|
|
|
RequestItemStatus(status: $KeystoreStatus.error,
|
|
|
|
message: failure.message))
|
|
|
|
of AddValidatorStatus.existingArtifacts:
|
|
|
|
response.data.add(
|
|
|
|
RequestItemStatus(status: $KeystoreStatus.duplicate))
|
2021-12-22 12:37:31 +00:00
|
|
|
else:
|
2023-05-17 04:56:37 +00:00
|
|
|
host.addValidator(
|
|
|
|
res.get(), host.getValidatorWithdrawalAddress(res.get.pubkey))
|
2022-02-07 20:36:09 +00:00
|
|
|
response.data.add(
|
|
|
|
RequestItemStatus(status: $KeystoreStatus.imported))
|
2021-10-04 19:08:31 +00:00
|
|
|
|
2024-01-20 16:06:28 +00:00
|
|
|
RestApiResponse.jsonResponsePlain(response)
|
2021-12-22 12:37:31 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/keymanager-APIs/#/Keymanager/DeleteKeys
|
2024-01-20 16:06:28 +00:00
|
|
|
router.api2(MethodDelete, "/eth/v1/keystores") do (
|
2021-12-22 12:37:31 +00:00
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
2022-08-19 10:30:07 +00:00
|
|
|
let authStatus = checkAuthorization(request, host)
|
2021-12-22 12:37:31 +00:00
|
|
|
if authStatus.isErr():
|
2022-07-13 14:45:04 +00:00
|
|
|
return authErrorResponse authStatus.error
|
2021-10-04 19:08:31 +00:00
|
|
|
let keys =
|
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
2022-07-13 14:45:04 +00:00
|
|
|
return keymanagerApiError(Http404, EmptyRequestBodyError)
|
2021-12-22 12:37:31 +00:00
|
|
|
let dres = decodeBody(DeleteKeystoresBody, contentBody.get())
|
2021-10-04 19:08:31 +00:00
|
|
|
if dres.isErr():
|
2022-07-13 14:45:04 +00:00
|
|
|
return keymanagerApiError(Http400, InvalidValidatorPublicKey)
|
2021-12-22 12:37:31 +00:00
|
|
|
dres.get().pubkeys
|
2021-10-04 19:08:31 +00:00
|
|
|
|
2021-12-22 12:37:31 +00:00
|
|
|
var
|
|
|
|
response: DeleteKeystoresResponse
|
2024-01-20 16:06:28 +00:00
|
|
|
nodeSPDIR =
|
|
|
|
try:
|
|
|
|
toSPDIR(host.validatorPool[].slashingProtection)
|
|
|
|
except IOError as exc:
|
|
|
|
return keymanagerApiError(
|
|
|
|
Http500, "Internal server error; " & $exc.msg)
|
2021-12-22 12:37:31 +00:00
|
|
|
# Hash table to keep the removal status of all keys form request
|
|
|
|
keysAndDeleteStatus = initTable[PubKeyBytes, RequestItemStatus]()
|
2022-08-19 10:30:07 +00:00
|
|
|
responseSPDIR: SPDIR
|
2021-10-04 19:08:31 +00:00
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
responseSPDIR.metadata = nodeSPDIR.metadata
|
2021-10-04 19:08:31 +00:00
|
|
|
|
2022-05-10 10:03:40 +00:00
|
|
|
for index, key in keys:
|
2021-12-22 12:37:31 +00:00
|
|
|
let
|
2022-08-19 10:30:07 +00:00
|
|
|
res = removeValidator(host.validatorPool[],
|
|
|
|
host.validatorsDir, host.secretsDir,
|
|
|
|
key, KeystoreKind.Local)
|
2021-12-22 12:37:31 +00:00
|
|
|
pubkey = key.blob.PubKey0x.PubKeyBytes
|
|
|
|
|
|
|
|
if res.isOk:
|
|
|
|
case res.value()
|
|
|
|
of RemoveValidatorStatus.deleted:
|
2022-07-01 21:52:23 +00:00
|
|
|
keysAndDeleteStatus[pubkey] =
|
|
|
|
RequestItemStatus(status: $KeystoreStatus.deleted)
|
2021-12-22 12:37:31 +00:00
|
|
|
|
2022-02-07 20:36:09 +00:00
|
|
|
# At first all keys with status missing directory after removal receive
|
|
|
|
# status 'not_found'
|
|
|
|
of RemoveValidatorStatus.notFound:
|
2022-07-01 21:52:23 +00:00
|
|
|
keysAndDeleteStatus[pubkey] =
|
|
|
|
RequestItemStatus(status: $KeystoreStatus.notFound)
|
2021-12-22 12:37:31 +00:00
|
|
|
else:
|
2022-07-01 21:52:23 +00:00
|
|
|
keysAndDeleteStatus[pubkey] =
|
|
|
|
RequestItemStatus(status: $KeystoreStatus.error,
|
|
|
|
message: $res.error())
|
2021-10-04 19:08:31 +00:00
|
|
|
|
2021-12-22 12:37:31 +00:00
|
|
|
# If we discover slashing protection data for a validator that was not
|
|
|
|
# found, this means the validator was active in the past, so we must
|
|
|
|
# respond with `not_active`:
|
|
|
|
for validator in nodeSPDIR.data:
|
2022-08-19 10:30:07 +00:00
|
|
|
keysAndDeleteStatus.withValue(validator.pubkey.PubKeyBytes,
|
|
|
|
foundKeystore) do:
|
|
|
|
responseSPDIR.data.add(validator)
|
2021-10-04 19:08:31 +00:00
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
if foundKeystore.status == $KeystoreStatus.notFound:
|
|
|
|
foundKeystore.status = $KeystoreStatus.notActive
|
2021-12-22 12:37:31 +00:00
|
|
|
|
2022-05-10 10:03:40 +00:00
|
|
|
for index, key in keys:
|
2024-01-20 16:06:28 +00:00
|
|
|
response.data.add(
|
|
|
|
keysAndDeleteStatus.getOrDefault(key.blob.PubKey0x.PubKeyBytes))
|
2021-12-22 12:37:31 +00:00
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
response.slashing_protection = RestJson.encode(responseSPDIR)
|
|
|
|
|
2024-01-20 16:06:28 +00:00
|
|
|
RestApiResponse.jsonResponsePlain(response)
|
2021-10-04 19:08:31 +00:00
|
|
|
|
2022-02-07 20:36:09 +00:00
|
|
|
# https://ethereum.github.io/keymanager-APIs/#/Remote%20Key%20Manager/ListRemoteKeys
|
2024-01-20 16:06:28 +00:00
|
|
|
router.api2(MethodGet, "/eth/v1/remotekeys") do () -> RestApiResponse:
|
2022-08-19 10:30:07 +00:00
|
|
|
let authStatus = checkAuthorization(request, host)
|
2022-02-07 20:36:09 +00:00
|
|
|
if authStatus.isErr():
|
2022-07-13 14:45:04 +00:00
|
|
|
return authErrorResponse authStatus.error
|
2022-08-19 10:30:07 +00:00
|
|
|
let response = GetRemoteKeystoresResponse(
|
|
|
|
data: listRemoteValidators(host.validatorPool[]))
|
2024-01-20 16:06:28 +00:00
|
|
|
RestApiResponse.jsonResponsePlain(response)
|
2022-02-07 20:36:09 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/keymanager-APIs/#/Remote%20Key%20Manager/ImportRemoteKeys
|
2024-01-20 16:06:28 +00:00
|
|
|
router.api2(MethodPost, "/eth/v1/remotekeys") do (
|
2022-02-07 20:36:09 +00:00
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
2022-08-19 10:30:07 +00:00
|
|
|
let authStatus = checkAuthorization(request, host)
|
2022-02-07 20:36:09 +00:00
|
|
|
if authStatus.isErr():
|
2022-07-13 14:45:04 +00:00
|
|
|
return authErrorResponse authStatus.error
|
2022-02-07 20:36:09 +00:00
|
|
|
let keys =
|
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
2022-07-13 14:45:04 +00:00
|
|
|
return keymanagerApiError(Http404, EmptyRequestBodyError)
|
2022-02-07 20:36:09 +00:00
|
|
|
let dres = decodeBody(ImportRemoteKeystoresBody, contentBody.get())
|
|
|
|
if dres.isErr():
|
2022-07-13 14:45:04 +00:00
|
|
|
return keymanagerApiError(Http400, InvalidKeystoreObjects)
|
2022-02-07 20:36:09 +00:00
|
|
|
dres.get().remote_keys
|
|
|
|
|
|
|
|
var response: PostKeystoresResponse
|
|
|
|
|
2022-05-10 10:03:40 +00:00
|
|
|
for index, key in keys:
|
2022-05-10 00:32:12 +00:00
|
|
|
let
|
|
|
|
remoteInfo = RemoteSignerInfo(
|
|
|
|
url: key.url,
|
|
|
|
pubkey: key.pubkey,
|
|
|
|
id: 0)
|
|
|
|
keystore = RemoteKeystore(
|
|
|
|
version: 1'u64, remoteType: RemoteSignerType.Web3Signer,
|
|
|
|
pubkey: key.pubkey, remotes: @[remoteInfo])
|
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
response.data.add handleAddRemoteValidatorReq(host, keystore)
|
2022-02-07 20:36:09 +00:00
|
|
|
|
2024-01-20 16:06:28 +00:00
|
|
|
RestApiResponse.jsonResponsePlain(response)
|
2022-02-07 20:36:09 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/keymanager-APIs/#/Remote%20Key%20Manager/DeleteRemoteKeys
|
2024-01-20 16:06:28 +00:00
|
|
|
router.api2(MethodDelete, "/eth/v1/remotekeys") do (
|
2022-02-07 20:36:09 +00:00
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
2022-08-19 10:30:07 +00:00
|
|
|
let authStatus = checkAuthorization(request, host)
|
2022-02-07 20:36:09 +00:00
|
|
|
if authStatus.isErr():
|
2022-07-13 14:45:04 +00:00
|
|
|
return authErrorResponse authStatus.error
|
2022-02-07 20:36:09 +00:00
|
|
|
let keys =
|
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
2022-07-13 14:45:04 +00:00
|
|
|
return keymanagerApiError(Http404, EmptyRequestBodyError)
|
2022-02-07 20:36:09 +00:00
|
|
|
let dres = decodeBody(DeleteKeystoresBody, contentBody.get())
|
|
|
|
if dres.isErr():
|
2022-07-13 14:45:04 +00:00
|
|
|
return keymanagerApiError(Http400, InvalidValidatorPublicKey)
|
2022-02-07 20:36:09 +00:00
|
|
|
dres.get().pubkeys
|
|
|
|
|
2022-05-10 00:32:12 +00:00
|
|
|
var response: DeleteRemoteKeystoresResponse
|
2022-05-17 13:50:49 +00:00
|
|
|
for index, key in keys:
|
2022-08-19 10:30:07 +00:00
|
|
|
response.data.add handleRemoveValidatorReq(host, key)
|
2024-01-20 16:06:28 +00:00
|
|
|
RestApiResponse.jsonResponsePlain(response)
|
2022-05-10 00:32:12 +00:00
|
|
|
|
2022-07-13 14:45:04 +00:00
|
|
|
# https://ethereum.github.io/keymanager-APIs/#/Fee%20Recipient/ListFeeRecipient
|
2024-01-20 16:06:28 +00:00
|
|
|
router.api2(MethodGet, "/eth/v1/validator/{pubkey}/feerecipient") do (
|
2022-07-13 14:45:04 +00:00
|
|
|
pubkey: ValidatorPubKey) -> RestApiResponse:
|
2022-08-19 10:30:07 +00:00
|
|
|
let authStatus = checkAuthorization(request, host)
|
2022-07-13 14:45:04 +00:00
|
|
|
if authStatus.isErr():
|
|
|
|
return authErrorResponse authStatus.error
|
|
|
|
let
|
|
|
|
pubkey = pubkey.valueOr:
|
|
|
|
return keymanagerApiError(Http400, InvalidValidatorPublicKey)
|
2023-05-17 04:56:37 +00:00
|
|
|
perValidatorDefaultFeeRecipient = getPerValidatorDefaultFeeRecipient(
|
|
|
|
host.defaultFeeRecipient,
|
|
|
|
host.getValidatorWithdrawalAddress(pubkey))
|
|
|
|
ethaddress = host.getSuggestedFeeRecipient(
|
|
|
|
pubkey, perValidatorDefaultFeeRecipient)
|
2022-07-13 14:45:04 +00:00
|
|
|
|
2024-01-20 16:06:28 +00:00
|
|
|
if ethaddress.isOk:
|
2022-07-13 14:45:04 +00:00
|
|
|
RestApiResponse.jsonResponse(ListFeeRecipientResponse(
|
|
|
|
pubkey: pubkey,
|
|
|
|
ethaddress: ethaddress.get))
|
|
|
|
else:
|
|
|
|
case ethaddress.error
|
|
|
|
of noSuchValidator:
|
|
|
|
keymanagerApiError(Http404, "No matching validator found")
|
2023-02-15 15:10:31 +00:00
|
|
|
of malformedConfigFile:
|
2022-07-13 14:45:04 +00:00
|
|
|
keymanagerApiError(Http500, "Error reading fee recipient file")
|
|
|
|
|
|
|
|
# https://ethereum.github.io/keymanager-APIs/#/Fee%20Recipient/SetFeeRecipient
|
2024-01-20 16:06:28 +00:00
|
|
|
router.api2(MethodPost, "/eth/v1/validator/{pubkey}/feerecipient") do (
|
|
|
|
pubkey: ValidatorPubKey,
|
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
2022-08-19 10:30:07 +00:00
|
|
|
let authStatus = checkAuthorization(request, host)
|
2022-07-13 14:45:04 +00:00
|
|
|
if authStatus.isErr():
|
|
|
|
return authErrorResponse authStatus.error
|
|
|
|
let
|
2024-01-20 16:06:28 +00:00
|
|
|
pubkey = pubkey.valueOr:
|
2022-07-13 14:45:04 +00:00
|
|
|
return keymanagerApiError(Http400, InvalidValidatorPublicKey)
|
|
|
|
feeRecipientReq =
|
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
|
|
|
return keymanagerApiError(Http400, InvalidFeeRecipientRequestError)
|
|
|
|
let dres = decodeBody(SetFeeRecipientRequest, contentBody.get())
|
|
|
|
if dres.isErr():
|
|
|
|
return keymanagerApiError(Http400, InvalidFeeRecipientRequestError)
|
|
|
|
dres.get()
|
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
status = host.setFeeRecipient(pubkey, feeRecipientReq.ethaddress)
|
2022-07-13 14:45:04 +00:00
|
|
|
|
2024-01-20 16:06:28 +00:00
|
|
|
if status.isOk:
|
2022-07-13 14:45:04 +00:00
|
|
|
RestApiResponse.response("", Http202, "text/plain")
|
|
|
|
else:
|
|
|
|
keymanagerApiError(
|
|
|
|
Http500, "Failed to set fee recipient: " & status.error)
|
|
|
|
|
|
|
|
# https://ethereum.github.io/keymanager-APIs/#/Fee%20Recipient/DeleteFeeRecipient
|
2024-01-20 16:06:28 +00:00
|
|
|
router.api2(MethodDelete, "/eth/v1/validator/{pubkey}/feerecipient") do (
|
2022-07-13 14:45:04 +00:00
|
|
|
pubkey: ValidatorPubKey) -> RestApiResponse:
|
2022-08-19 10:30:07 +00:00
|
|
|
let authStatus = checkAuthorization(request, host)
|
2022-07-13 14:45:04 +00:00
|
|
|
if authStatus.isErr():
|
|
|
|
return authErrorResponse authStatus.error
|
|
|
|
let
|
|
|
|
pubkey = pubkey.valueOr:
|
|
|
|
return keymanagerApiError(Http400, InvalidValidatorPublicKey)
|
2022-08-19 10:30:07 +00:00
|
|
|
res = host.removeFeeRecipientFile(pubkey)
|
2022-07-13 14:45:04 +00:00
|
|
|
|
2024-01-20 16:06:28 +00:00
|
|
|
if res.isOk:
|
2022-07-13 14:45:04 +00:00
|
|
|
RestApiResponse.response("", Http204, "text/plain")
|
|
|
|
else:
|
|
|
|
keymanagerApiError(
|
|
|
|
Http500, "Failed to remove fee recipient file: " & res.error)
|
|
|
|
|
2023-02-15 15:10:31 +00:00
|
|
|
# https://ethereum.github.io/keymanager-APIs/#/Gas%20Limit/getGasLimit
|
2024-01-20 16:06:28 +00:00
|
|
|
router.api2(MethodGet, "/eth/v1/validator/{pubkey}/gas_limit") do (
|
|
|
|
pubkey: ValidatorPubKey) -> RestApiResponse:
|
2023-02-15 15:10:31 +00:00
|
|
|
let authStatus = checkAuthorization(request, host)
|
|
|
|
if authStatus.isErr():
|
|
|
|
return authErrorResponse authStatus.error
|
|
|
|
|
|
|
|
let
|
|
|
|
pubkey = pubkey.valueOr:
|
|
|
|
return keymanagerApiError(Http400, InvalidValidatorPublicKey)
|
|
|
|
gasLimit = host.getSuggestedGasLimit(pubkey)
|
|
|
|
|
2024-01-20 16:06:28 +00:00
|
|
|
if gasLimit.isOk:
|
2023-02-15 15:10:31 +00:00
|
|
|
RestApiResponse.jsonResponse(GetValidatorGasLimitResponse(
|
|
|
|
pubkey: pubkey,
|
|
|
|
gas_limit: gasLimit.get))
|
|
|
|
else:
|
|
|
|
case gasLimit.error
|
|
|
|
of noSuchValidator:
|
|
|
|
keymanagerApiError(Http404, "No matching validator found")
|
|
|
|
of malformedConfigFile:
|
|
|
|
keymanagerApiError(Http500, "Error reading gas limit file")
|
|
|
|
|
|
|
|
# https://ethereum.github.io/keymanager-APIs/#/Gas%20Limit/setGasLimit
|
2024-01-20 16:06:28 +00:00
|
|
|
router.api2(MethodPost, "/eth/v1/validator/{pubkey}/gas_limit") do (
|
|
|
|
pubkey: ValidatorPubKey,
|
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
2023-02-15 15:10:31 +00:00
|
|
|
let authStatus = checkAuthorization(request, host)
|
|
|
|
if authStatus.isErr():
|
|
|
|
return authErrorResponse authStatus.error
|
|
|
|
let
|
|
|
|
pubkey = pubkey.valueOr:
|
|
|
|
return keymanagerApiError(Http400, InvalidValidatorPublicKey)
|
|
|
|
gasLimitReq =
|
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
|
|
|
return keymanagerApiError(Http400, InvalidGasLimitRequestError)
|
|
|
|
let dres = decodeBody(SetGasLimitRequest, contentBody.get())
|
|
|
|
if dres.isErr():
|
|
|
|
return keymanagerApiError(Http400, InvalidGasLimitRequestError)
|
|
|
|
dres.get()
|
|
|
|
|
|
|
|
status = host.setGasLimit(pubkey, gasLimitReq.gas_limit)
|
|
|
|
|
2024-01-20 16:06:28 +00:00
|
|
|
if status.isOk:
|
2023-02-15 15:10:31 +00:00
|
|
|
RestApiResponse.response("", Http202, "text/plain")
|
|
|
|
else:
|
|
|
|
keymanagerApiError(
|
|
|
|
Http500, "Failed to set gas limit: " & status.error)
|
|
|
|
|
|
|
|
# https://ethereum.github.io/keymanager-APIs/#/Gas%20Limit/deleteGasLimit
|
2024-01-20 16:06:28 +00:00
|
|
|
router.api2(MethodDelete, "/eth/v1/validator/{pubkey}/gas_limit") do (
|
|
|
|
pubkey: ValidatorPubKey) -> RestApiResponse:
|
2023-02-15 15:10:31 +00:00
|
|
|
let authStatus = checkAuthorization(request, host)
|
|
|
|
if authStatus.isErr():
|
|
|
|
return authErrorResponse authStatus.error
|
|
|
|
let
|
|
|
|
pubkey = pubkey.valueOr:
|
|
|
|
return keymanagerApiError(Http400, InvalidValidatorPublicKey)
|
|
|
|
res = host.removeGasLimitFile(pubkey)
|
|
|
|
|
2024-01-20 16:06:28 +00:00
|
|
|
if res.isOk:
|
2023-02-15 15:10:31 +00:00
|
|
|
RestApiResponse.response("", Http204, "text/plain")
|
|
|
|
else:
|
|
|
|
keymanagerApiError(
|
|
|
|
Http500, "Failed to remove gas limit file: " & res.error)
|
|
|
|
|
2022-05-10 00:32:12 +00:00
|
|
|
# TODO: These URLs will be changed once we submit a proposal for
|
2022-09-13 11:52:26 +00:00
|
|
|
# /eth/v2/remotekeys that supports distributed keys.
|
2024-01-20 16:06:28 +00:00
|
|
|
router.api2(MethodGet, "/eth/v1/remotekeys/distributed") do (
|
|
|
|
) -> RestApiResponse:
|
2022-08-19 10:30:07 +00:00
|
|
|
let authStatus = checkAuthorization(request, host)
|
2022-05-10 00:32:12 +00:00
|
|
|
if authStatus.isErr():
|
2022-07-13 14:45:04 +00:00
|
|
|
return authErrorResponse authStatus.error
|
2022-08-19 10:30:07 +00:00
|
|
|
let response = GetDistributedKeystoresResponse(
|
|
|
|
data: listRemoteDistributedValidators(host.validatorPool[]))
|
2024-01-20 16:06:28 +00:00
|
|
|
RestApiResponse.jsonResponsePlain(response)
|
2022-05-10 00:32:12 +00:00
|
|
|
|
|
|
|
# TODO: These URLs will be changed once we submit a proposal for
|
2022-09-13 11:52:26 +00:00
|
|
|
# /eth/v2/remotekeys that supports distributed keys.
|
2024-01-20 16:06:28 +00:00
|
|
|
router.api2(MethodPost, "/eth/v1/remotekeys/distributed") do (
|
2022-05-10 00:32:12 +00:00
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
2022-08-19 10:30:07 +00:00
|
|
|
let authStatus = checkAuthorization(request, host)
|
2022-05-10 00:32:12 +00:00
|
|
|
if authStatus.isErr():
|
2022-07-13 14:45:04 +00:00
|
|
|
return authErrorResponse authStatus.error
|
2022-05-10 00:32:12 +00:00
|
|
|
let keys =
|
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
2022-07-13 14:45:04 +00:00
|
|
|
return keymanagerApiError(Http404, EmptyRequestBodyError)
|
2022-05-10 00:32:12 +00:00
|
|
|
let dres = decodeBody(ImportDistributedKeystoresBody, contentBody.get())
|
|
|
|
if dres.isErr():
|
2022-07-13 14:45:04 +00:00
|
|
|
return keymanagerApiError(Http400, InvalidKeystoreObjects)
|
2022-05-10 00:32:12 +00:00
|
|
|
dres.get.remote_keys
|
|
|
|
|
|
|
|
var response: PostKeystoresResponse
|
|
|
|
|
2022-05-10 10:03:40 +00:00
|
|
|
for index, key in keys:
|
2022-05-10 00:32:12 +00:00
|
|
|
let keystore = RemoteKeystore(
|
|
|
|
version: 2'u64,
|
|
|
|
remoteType: RemoteSignerType.Web3Signer,
|
|
|
|
pubkey: key.pubkey,
|
|
|
|
remotes: key.remotes,
|
|
|
|
threshold: uint32 key.threshold
|
|
|
|
)
|
2022-08-19 10:30:07 +00:00
|
|
|
response.data.add handleAddRemoteValidatorReq(host, keystore)
|
2022-05-10 00:32:12 +00:00
|
|
|
|
2024-01-20 16:06:28 +00:00
|
|
|
RestApiResponse.jsonResponsePlain(response)
|
2022-05-10 00:32:12 +00:00
|
|
|
|
2024-01-20 16:06:28 +00:00
|
|
|
router.api2(MethodDelete, "/eth/v1/remotekeys/distributed") do (
|
2022-05-10 00:32:12 +00:00
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
2022-08-19 10:30:07 +00:00
|
|
|
let authStatus = checkAuthorization(request, host)
|
2022-05-10 00:32:12 +00:00
|
|
|
if authStatus.isErr():
|
2022-07-13 14:45:04 +00:00
|
|
|
return authErrorResponse authStatus.error
|
2022-05-10 00:32:12 +00:00
|
|
|
let keys =
|
2022-02-07 20:36:09 +00:00
|
|
|
block:
|
2022-05-10 00:32:12 +00:00
|
|
|
if contentBody.isNone():
|
2022-07-13 14:45:04 +00:00
|
|
|
return keymanagerApiError(Http404, EmptyRequestBodyError)
|
2022-05-10 00:32:12 +00:00
|
|
|
let dres = decodeBody(DeleteKeystoresBody, contentBody.get())
|
|
|
|
if dres.isErr():
|
2022-07-13 14:45:04 +00:00
|
|
|
return keymanagerApiError(Http400, InvalidValidatorPublicKey)
|
2022-05-10 00:32:12 +00:00
|
|
|
dres.get.pubkeys
|
|
|
|
|
|
|
|
var response: DeleteRemoteKeystoresResponse
|
2022-05-10 10:03:40 +00:00
|
|
|
for index, key in keys:
|
2022-08-19 10:30:07 +00:00
|
|
|
response.data.add handleRemoveValidatorReq(host, key)
|
2022-02-07 20:36:09 +00:00
|
|
|
|
2024-01-20 16:06:28 +00:00
|
|
|
RestApiResponse.jsonResponsePlain(response)
|
2023-06-14 06:46:01 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/keymanager-APIs/?urls.primaryName=dev#/Voluntary%20Exit/signVoluntaryExit
|
2024-01-20 16:06:28 +00:00
|
|
|
router.api2(MethodPost, "/eth/v1/validator/{pubkey}/voluntary_exit") do (
|
2023-06-14 06:46:01 +00:00
|
|
|
pubkey: ValidatorPubKey, epoch: Option[Epoch],
|
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
|
|
|
|
|
|
|
let authStatus = checkAuthorization(request, host)
|
|
|
|
if authStatus.isErr():
|
|
|
|
return authErrorResponse(authStatus.error)
|
|
|
|
|
|
|
|
let
|
|
|
|
qpubkey = pubkey.valueOr:
|
|
|
|
return keymanagerApiError(Http400, InvalidValidatorPublicKey)
|
|
|
|
qepoch =
|
|
|
|
if epoch.isSome():
|
|
|
|
let res = epoch.get()
|
|
|
|
if res.isErr():
|
|
|
|
return keymanagerApiError(Http400, InvalidEpochValueError)
|
|
|
|
res.get()
|
|
|
|
else:
|
|
|
|
host.getBeaconTimeFn().slotOrZero().epoch()
|
|
|
|
validator =
|
|
|
|
block:
|
|
|
|
let res = host.validatorPool[].getValidator(qpubkey).valueOr:
|
|
|
|
return keymanagerApiError(Http404, ValidatorNotFoundError)
|
|
|
|
if res.index.isNone():
|
|
|
|
return keymanagerApiError(Http404, ValidatorIndexMissingError)
|
|
|
|
res
|
|
|
|
voluntaryExit =
|
|
|
|
VoluntaryExit(epoch: qepoch,
|
|
|
|
validator_index: uint64(validator.index.get()))
|
|
|
|
fork = host.getForkFn(qepoch).valueOr:
|
|
|
|
return keymanagerApiError(Http500, FailedToObtainForkError)
|
|
|
|
signature =
|
|
|
|
try:
|
|
|
|
let res = await validator.getValidatorExitSignature(
|
|
|
|
fork, host.getGenesisFn(), voluntaryExit)
|
|
|
|
if res.isErr():
|
|
|
|
return keymanagerApiError(Http500, res.error())
|
|
|
|
res.get()
|
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
|
|
|
except CatchableError as exc:
|
|
|
|
error "An unexpected error occurred while signing validator exit",
|
|
|
|
err_name = exc.name, err_msg = exc.msg
|
|
|
|
return keymanagerApiError(Http500, $exc.msg)
|
|
|
|
response = SignedVoluntaryExit(
|
|
|
|
message: voluntaryExit,
|
|
|
|
signature: signature
|
|
|
|
)
|
2024-01-20 16:06:28 +00:00
|
|
|
RestApiResponse.jsonResponse(response)
|