Implement VC API (#1657)

## Issue Addressed

NA

## Proposed Changes

- Implements a HTTP API for the validator client.
- Creates EIP-2335 keystores with an empty `description` field, instead of a missing `description` field. Adds option to set name.
- Be more graceful with setups without any validators (yet)
    - Remove an error log when there are no validators.
    - Create the `validator` dir if it doesn't exist.
- Allow building a `ValidatorDir` without a withdrawal keystore (required for the API method where we only post a voting keystore).
- Add optional `description` field to `validator_definitions.yml`

## TODO

- [x] Signature header, as per https://github.com/sigp/lighthouse/issues/1269#issuecomment-649879855
- [x] Return validator descriptions
- [x] Return deposit data
- [x] Respect the mnemonic offset
- [x] Check that mnemonic can derive returned keys
- [x] Be strict about non-localhost
- [x] Allow graceful start without any validators (+ create validator dir)
- [x] Docs final pass
- [x] Swap to EIP-2335 description field. 
- [x] Fix Zerioze TODO in VC api types.
- [x] Zeroize secp256k1 key

## Endpoints

- [x] `GET /lighthouse/version`
- [x] `GET /lighthouse/health`
- [x] `GET /lighthouse/validators` 
- [x] `POST /lighthouse/validators/hd`
- [x] `POST /lighthouse/validators/keystore`
- [x] `PATCH /lighthouse/validators/:validator_pubkey`
- [ ] ~~`POST /lighthouse/validators/:validator_pubkey/exit/:epoch`~~ Future works


## Additional Info

TBC
This commit is contained in:
Paul Hauner
2020-10-02 09:42:19 +00:00
parent 1d278aaa83
commit 6ea3bc5e52
43 changed files with 2882 additions and 172 deletions

View File

@@ -81,6 +81,7 @@ pub struct KeystoreBuilder<'a> {
cipher: Cipher,
uuid: Uuid,
path: String,
description: String,
}
impl<'a> KeystoreBuilder<'a> {
@@ -105,10 +106,17 @@ impl<'a> KeystoreBuilder<'a> {
cipher: Cipher::Aes128Ctr(Aes128Ctr { iv }),
uuid: Uuid::new_v4(),
path,
description: "".to_string(),
})
}
}
/// Build the keystore with a specific description instead of an empty string.
pub fn description(mut self, description: String) -> Self {
self.description = description;
self
}
/// Build the keystore using the supplied `kdf` instead of `crate::default_kdf`.
pub fn kdf(mut self, kdf: Kdf) -> Self {
self.kdf = kdf;
@@ -124,6 +132,7 @@ impl<'a> KeystoreBuilder<'a> {
self.cipher,
self.uuid,
self.path,
self.description,
)
}
}
@@ -147,6 +156,7 @@ impl Keystore {
cipher: Cipher,
uuid: Uuid,
path: String,
description: String,
) -> Result<Self, Error> {
let secret: ZeroizeHash = keypair.sk.serialize();
@@ -175,7 +185,7 @@ impl Keystore {
path: Some(path),
pubkey: keypair.pk.to_hex_string()[2..].to_string(),
version: Version::four(),
description: None,
description: Some(description),
name: None,
},
})
@@ -228,6 +238,18 @@ impl Keystore {
&self.json.pubkey
}
/// Returns the description for the keystore, if the field is present.
pub fn description(&self) -> Option<&str> {
self.json.description.as_deref()
}
/// Sets the description for the keystore.
///
/// Note: this does not save the keystore to disk.
pub fn set_description(&mut self, description: String) {
self.json.description = Some(description)
}
/// Returns the pubkey for the keystore, parsed as a `PublicKey` if it parses.
pub fn public_key(&self) -> Option<PublicKey> {
serde_json::from_str(&format!("\"0x{}\"", &self.json.pubkey)).ok()

View File

@@ -215,6 +215,23 @@ impl Wallet {
self.json.nextaccount
}
/// Sets the value of the JSON wallet `nextaccount` field.
///
/// This will be the index of the next wallet generated with `Self::next_validator`.
///
/// ## Errors
///
/// Returns `Err(())` if `nextaccount` is less than `self.nextaccount()` without mutating
/// `self`. This is to protect against duplicate validator generation.
pub fn set_nextaccount(&mut self, nextaccount: u32) -> Result<(), ()> {
if nextaccount >= self.nextaccount() {
self.json.nextaccount = nextaccount;
Ok(())
} else {
Err(())
}
}
/// Returns the value of the JSON wallet `name` field.
pub fn name(&self) -> &str {
&self.json.name