Skip to content

Commit 87a98c2

Browse files
committed
Remote key manager:
- Ignore duplicate keys during import instead of returning an error. - Fix incorrect listed remote key pubkey attribute key.
1 parent b39ce29 commit 87a98c2

File tree

5 files changed

+35
-32
lines changed

5 files changed

+35
-32
lines changed

keymanager/src/keystores.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use eip_2335::Keystore;
1010
use futures::lock::{MappedMutexGuard, Mutex, MutexGuard};
1111
use itertools::Itertools as _;
1212
use log::{info, warn};
13+
use serde::Serialize;
1314
use signer::{KeyOrigin, Signer};
1415
use slashing_protection::{interchange_format::InterchangeFormat, SlashingProtector};
1516
use std_ext::ArcExt as _;
@@ -20,7 +21,7 @@ use uuid::Uuid;
2021
use validator_key_cache::ValidatorKeyCache;
2122
use zeroize::Zeroizing;
2223

23-
use crate::misc::{Error, OperationStatus, Status, ValidatingPubkey};
24+
use crate::misc::{Error, OperationStatus, Status};
2425

2526
const KEYSTORE_STORAGE_FILE: &str = "keystores.json";
2627

@@ -48,6 +49,12 @@ impl PersistenceConfig {
4849
}
4950
}
5051

52+
#[derive(Debug, PartialEq, Eq, Serialize)]
53+
pub struct ValidatingPubkey {
54+
pub validating_pubkey: PublicKeyBytes,
55+
pub readonly: bool,
56+
}
57+
5158
pub struct KeystoreManager {
5259
signer: Arc<RwLock<Signer>>,
5360
slashing_protector: Arc<Mutex<SlashingProtector>>,
@@ -250,7 +257,6 @@ impl KeystoreManager {
250257
.keys_with_origin()
251258
.map(|(pubkey, origin)| ValidatingPubkey {
252259
validating_pubkey: pubkey,
253-
url: None,
254260
readonly: match origin {
255261
KeyOrigin::KeymanagerAPI => false,
256262
KeyOrigin::LocalFileSystem | KeyOrigin::Web3Signer => true,
@@ -614,7 +620,6 @@ mod tests {
614620
manager.list_validating_pubkeys().await,
615621
vec![ValidatingPubkey {
616622
validating_pubkey: expected_pubkey,
617-
url: None,
618623
readonly: false
619624
}],
620625
);
@@ -646,7 +651,6 @@ mod tests {
646651
manager.list_validating_pubkeys().await,
647652
vec![ValidatingPubkey {
648653
validating_pubkey: expected_pubkey,
649-
url: None,
650654
readonly: false
651655
}],
652656
);
@@ -750,7 +754,6 @@ mod tests {
750754
manager.list_validating_pubkeys().await,
751755
vec![ValidatingPubkey {
752756
validating_pubkey: expected_pubkey,
753-
url: None,
754757
readonly: false
755758
}],
756759
);
@@ -782,7 +785,6 @@ mod tests {
782785
manager.list_validating_pubkeys().await,
783786
vec![ValidatingPubkey {
784787
validating_pubkey: expected_pubkey,
785-
url: None,
786788
readonly: false
787789
}],
788790
);

keymanager/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
pub use keystores::{load_key_storage, load_key_storage_password};
2-
pub use misc::{OperationStatus as KeymanagerOperationStatus, ValidatingPubkey};
3-
pub use remote_keys::RemoteKey;
1+
pub use keystores::{load_key_storage, load_key_storage_password, ValidatingPubkey};
2+
pub use misc::OperationStatus as KeymanagerOperationStatus;
3+
pub use remote_keys::{ListedRemoteKey, RemoteKey};
44

55
pub use crate::proposer_configs::ProposerConfigs;
66

keymanager/src/misc.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use anyhow::Error as AnyhowError;
2-
use bls::PublicKeyBytes;
32
use serde::Serialize;
43
use thiserror::Error;
54
use types::phase0::primitives::H256;
@@ -28,6 +27,7 @@ pub enum Error {
2827
#[serde(rename_all = "snake_case")]
2928
pub enum Status {
3029
Deleted,
30+
Duplicate,
3131
Error,
3232
Imported,
3333
}
@@ -64,11 +64,3 @@ impl From<Status> for OperationStatus {
6464
}
6565
}
6666
}
67-
68-
#[derive(Debug, PartialEq, Eq, Serialize)]
69-
pub struct ValidatingPubkey {
70-
pub validating_pubkey: PublicKeyBytes,
71-
#[serde(skip_serializing_if = "Option::is_none")]
72-
pub url: Option<String>,
73-
pub readonly: bool,
74-
}

keymanager/src/remote_keys.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@ use anyhow::{anyhow, Result};
44
use bls::PublicKeyBytes;
55
use futures::lock::Mutex;
66
use reqwest::Url;
7-
use serde::Deserialize;
7+
use serde::{Deserialize, Serialize};
88
use signer::{KeyOrigin, Signer};
99
use slashing_protection::SlashingProtector;
1010
use tokio::sync::RwLock;
1111

12-
use crate::misc::{Error, OperationStatus, Status, ValidatingPubkey};
12+
use crate::misc::{Error, OperationStatus, Status};
13+
14+
#[derive(Debug, PartialEq, Eq, Serialize)]
15+
pub struct ListedRemoteKey {
16+
pub pubkey: PublicKeyBytes,
17+
pub url: String,
18+
pub readonly: bool,
19+
}
1320

1421
#[derive(Debug, PartialEq, Eq, Deserialize)]
1522
pub struct RemoteKey {
@@ -71,7 +78,7 @@ impl RemoteKeyManager {
7178
imported_pubkeys.push(pubkey);
7279
Status::Imported.into()
7380
} else {
74-
Error::Duplicate.into()
81+
Status::Duplicate.into()
7582
}
7683
}
7784
Err(error) => anyhow!(error).into(),
@@ -88,14 +95,14 @@ impl RemoteKeyManager {
8895
Ok(statuses)
8996
}
9097

91-
pub async fn list(&self) -> Vec<ValidatingPubkey> {
98+
pub async fn list(&self) -> Vec<ListedRemoteKey> {
9299
self.signer
93100
.read()
94101
.await
95102
.web3signer_keys()
96-
.map(|(pubkey, url)| ValidatingPubkey {
97-
validating_pubkey: pubkey,
98-
url: Some(url.to_string()),
103+
.map(|(pubkey, url)| ListedRemoteKey {
104+
pubkey,
105+
url: url.to_string(),
99106
readonly: false,
100107
})
101108
.collect()
@@ -169,8 +176,8 @@ mod tests {
169176
message: None,
170177
},
171178
OperationStatus {
172-
status: Status::Error,
173-
message: Some("key already exists".into()),
179+
status: Status::Duplicate,
180+
message: None,
174181
},
175182
]
176183
);
@@ -182,9 +189,9 @@ mod tests {
182189

183190
assert_eq!(
184191
manager.list().await,
185-
[ValidatingPubkey {
186-
validating_pubkey: PUBKEY_REMOTE,
187-
url: Some("https://www.example.com/".into()),
192+
[ListedRemoteKey {
193+
pubkey: PUBKEY_REMOTE,
194+
url: "https://www.example.com/".into(),
188195
readonly: false,
189196
}],
190197
);

validator/src/api.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ use jwt_simple::{
2828
claims::{JWTClaims, NoCustomClaims},
2929
reexports::coarsetime::Clock,
3030
};
31-
use keymanager::{KeyManager, KeymanagerOperationStatus, RemoteKey, ValidatingPubkey};
31+
use keymanager::{
32+
KeyManager, KeymanagerOperationStatus, ListedRemoteKey, RemoteKey, ValidatingPubkey,
33+
};
3234
use log::{debug, info};
3335
use serde::{de::DeserializeOwned, Deserialize, Serialize};
3436
use signer::{Signer, SigningMessage};
@@ -546,7 +548,7 @@ async fn keymanager_delete_keystores(
546548
/// `GET /eth/v1/remotekeys`
547549
async fn keymanager_list_remote_keys(
548550
State(keymanager): State<Arc<KeyManager>>,
549-
) -> Result<EthResponse<Vec<ValidatingPubkey>>, Error> {
551+
) -> Result<EthResponse<Vec<ListedRemoteKey>>, Error> {
550552
let remote_keys = keymanager.remote_keys().list().await;
551553

552554
Ok(EthResponse::json(remote_keys))

0 commit comments

Comments
 (0)