2022-01-05 13:41:39 +00:00
|
|
|
# Copyright (c) 2021-2022 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-02-07 20:36:09 +00:00
|
|
|
import std/[tables, os, strutils, uri]
|
2021-10-04 19:08:31 +00:00
|
|
|
import chronos, chronicles, confutils,
|
2022-06-21 08:29:16 +00:00
|
|
|
stew/[base10, results, io2], blscurve
|
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,
|
|
|
|
validator_pool, validator_duties]
|
|
|
|
import ".."/rpc/rest_utils
|
2021-10-04 19:08:31 +00:00
|
|
|
|
2022-02-07 20:36:09 +00:00
|
|
|
export rest_utils, 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
|
|
|
|
|
|
|
|
proc listLocalValidators*(validatorPool: ValidatorPool): seq[KeystoreInfo]
|
2022-05-10 00:32:12 +00:00
|
|
|
{.raises: [Defect].} =
|
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
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
proc listRemoteValidators*(validatorPool: ValidatorPool): seq[RemoteKeystoreInfo]
|
2022-05-10 00:32:12 +00:00
|
|
|
{.raises: [Defect].} =
|
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
|
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
proc listRemoteDistributedValidators*(validatorPool: ValidatorPool): seq[DistributedKeystoreInfo]
|
2022-05-10 00:32:12 +00:00
|
|
|
{.raises: [Defect].} =
|
|
|
|
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 defstrings: seq[string]
|
|
|
|
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")
|
|
|
|
|
2021-12-22 12:37:31 +00:00
|
|
|
proc checkAuthorization*(request: HttpRequestRef,
|
2022-08-19 10:30:07 +00:00
|
|
|
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,
|
|
|
|
message: some($res.error()))
|
|
|
|
|
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:
|
|
|
|
let
|
|
|
|
slot = host.getBeaconTimeFn().slotOrZero
|
|
|
|
validatorIdx = host.getValidatorIdx(keystore.pubkey)
|
2022-09-17 05:30:07 +00:00
|
|
|
feeRecipient = host.getSuggestedFeeRecipient(keystore.pubkey).valueOr(
|
|
|
|
host.defaultFeeRecipient)
|
|
|
|
host.validatorPool[].addRemoteValidator(
|
|
|
|
res.get, validatorIdx, feeRecipient, slot)
|
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
|
2022-09-13 11:52:26 +00:00
|
|
|
router.api(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[]))
|
2021-12-22 12:37:31 +00:00
|
|
|
return RestApiResponse.jsonResponsePlain(response)
|
|
|
|
|
|
|
|
# https://ethereum.github.io/keymanager-APIs/#/Keymanager/ImportKeystores
|
2022-09-13 11:52:26 +00:00
|
|
|
router.api(MethodPost, "/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-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()
|
2022-08-19 10:30:07 +00:00
|
|
|
let nodeSPDIR = toSPDIR(host.validatorPool[].slashingProtection)
|
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.")
|
2022-08-19 10:30:07 +00:00
|
|
|
let res = inclSPDIR(host.validatorPool[].slashingProtection,
|
2022-02-07 20:36:09 +00:00
|
|
|
slashing_protection)
|
|
|
|
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,
|
|
|
|
item, request.passwords[index])
|
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:
|
2022-08-19 10:30:07 +00:00
|
|
|
host.addLocalValidator(res.get())
|
2022-02-07 20:36:09 +00:00
|
|
|
response.data.add(
|
|
|
|
RequestItemStatus(status: $KeystoreStatus.imported))
|
2021-10-04 19:08:31 +00:00
|
|
|
|
2021-12-22 12:37:31 +00:00
|
|
|
return RestApiResponse.jsonResponsePlain(response)
|
|
|
|
|
|
|
|
# https://ethereum.github.io/keymanager-APIs/#/Keymanager/DeleteKeys
|
2022-09-13 11:52:26 +00:00
|
|
|
router.api(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
|
2022-08-19 10:30:07 +00:00
|
|
|
nodeSPDIR = toSPDIR(host.validatorPool[].slashingProtection)
|
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:
|
2021-12-22 12:37:31 +00:00
|
|
|
response.data.add(keysAndDeleteStatus[key.blob.PubKey0x.PubKeyBytes])
|
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
response.slashing_protection = RestJson.encode(responseSPDIR)
|
|
|
|
|
2021-12-22 12:37:31 +00:00
|
|
|
return 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
|
2022-09-13 11:52:26 +00:00
|
|
|
router.api(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[]))
|
2022-02-07 20:36:09 +00:00
|
|
|
return RestApiResponse.jsonResponsePlain(response)
|
|
|
|
|
|
|
|
# https://ethereum.github.io/keymanager-APIs/#/Remote%20Key%20Manager/ImportRemoteKeys
|
2022-09-13 11:52:26 +00:00
|
|
|
router.api(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
|
|
|
|
|
|
|
return RestApiResponse.jsonResponsePlain(response)
|
|
|
|
|
|
|
|
# https://ethereum.github.io/keymanager-APIs/#/Remote%20Key%20Manager/DeleteRemoteKeys
|
2022-09-13 11:52:26 +00:00
|
|
|
router.api(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)
|
2022-05-10 00:32:12 +00:00
|
|
|
return RestApiResponse.jsonResponsePlain(response)
|
|
|
|
|
2022-07-13 14:45:04 +00:00
|
|
|
# https://ethereum.github.io/keymanager-APIs/#/Fee%20Recipient/ListFeeRecipient
|
2022-09-13 11:52:26 +00:00
|
|
|
router.api(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)
|
2022-08-19 10:30:07 +00:00
|
|
|
ethaddress = host.getSuggestedFeeRecipient(pubkey)
|
2022-07-13 14:45:04 +00:00
|
|
|
|
|
|
|
return if ethaddress.isOk:
|
|
|
|
RestApiResponse.jsonResponse(ListFeeRecipientResponse(
|
|
|
|
pubkey: pubkey,
|
|
|
|
ethaddress: ethaddress.get))
|
|
|
|
else:
|
|
|
|
case ethaddress.error
|
|
|
|
of noSuchValidator:
|
|
|
|
keymanagerApiError(Http404, "No matching validator found")
|
|
|
|
of invalidFeeRecipientFile:
|
|
|
|
keymanagerApiError(Http500, "Error reading fee recipient file")
|
|
|
|
|
|
|
|
# https://ethereum.github.io/keymanager-APIs/#/Fee%20Recipient/SetFeeRecipient
|
2022-09-13 11:52:26 +00:00
|
|
|
router.api(MethodPost, "/eth/v1/validator/{pubkey}/feerecipient") do (
|
2022-07-13 14:45:04 +00:00
|
|
|
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
|
|
|
|
pubkey= pubkey.valueOr:
|
|
|
|
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
|
|
|
|
|
|
|
return if status.isOk:
|
|
|
|
RestApiResponse.response("", Http202, "text/plain")
|
|
|
|
else:
|
|
|
|
keymanagerApiError(
|
|
|
|
Http500, "Failed to set fee recipient: " & status.error)
|
|
|
|
|
|
|
|
# https://ethereum.github.io/keymanager-APIs/#/Fee%20Recipient/DeleteFeeRecipient
|
2022-09-13 11:52:26 +00:00
|
|
|
router.api(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
|
|
|
|
|
|
|
return if res.isOk:
|
|
|
|
RestApiResponse.response("", Http204, "text/plain")
|
|
|
|
else:
|
|
|
|
keymanagerApiError(
|
|
|
|
Http500, "Failed to remove fee recipient 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.
|
|
|
|
router.api(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[]))
|
2022-05-10 00:32:12 +00:00
|
|
|
return RestApiResponse.jsonResponsePlain(response)
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
router.api(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
|
|
|
|
|
|
|
return RestApiResponse.jsonResponsePlain(response)
|
|
|
|
|
2022-09-13 11:52:26 +00:00
|
|
|
router.api(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
|
|
|
|
|
|
|
return RestApiResponse.jsonResponsePlain(response)
|