Skip to content

Commit 755b2b5

Browse files
committed
Save delegation signatures on local store
1 parent 5cd313e commit 755b2b5

4 files changed

Lines changed: 59 additions & 12 deletions

File tree

crates/common/src/config/signer.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ pub enum SignerType {
6666
/// Whether to unlock the accounts in case they are locked
6767
#[serde(default)]
6868
unlock: bool,
69+
/// How to store proxy keys
70+
store: Option<ProxyStore>,
6971
},
7072
}
7173

@@ -118,6 +120,7 @@ impl StartSignerConfig {
118120
ca_cert_path,
119121
server_domain,
120122
unlock,
123+
store,
121124
..
122125
} => {
123126
let cert_path = load_env_var(DIRK_CERT_ENV).map(PathBuf::from).unwrap_or(cert_path);
@@ -132,7 +135,7 @@ impl StartSignerConfig {
132135
server_port,
133136
jwts,
134137
loader: None,
135-
store: None,
138+
store,
136139
dirk: Some(DirkConfig {
137140
url,
138141
wallets,

crates/common/src/signer/store.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,32 @@ impl ProxyStore {
153153
Ok(())
154154
}
155155

156+
pub fn store_proxy_bls_delegation(
157+
&self,
158+
module_id: &ModuleId,
159+
delegation: &SignedProxyDelegation<BlsPublicKey>,
160+
) -> eyre::Result<()> {
161+
let base_path = match self {
162+
ProxyStore::File { proxy_dir } => proxy_dir,
163+
ProxyStore::ERC2335 { keys_path, .. } => keys_path,
164+
};
165+
let file_path = base_path
166+
.join("delegations")
167+
.join(module_id.to_string())
168+
.join("bls")
169+
.join(delegation.message.proxy.to_string());
170+
let content = serde_json::to_vec(&delegation)?;
171+
172+
if let Some(parent) = file_path.parent() {
173+
create_dir_all(parent)?;
174+
}
175+
176+
let mut file = std::fs::File::create(file_path)?;
177+
file.write_all(content.as_ref())?;
178+
179+
Ok(())
180+
}
181+
156182
#[allow(clippy::type_complexity)]
157183
pub fn load_proxies(
158184
&self,

crates/signer/src/manager/dirk.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ use cb_common::{
66
config::DirkConfig,
77
constants::COMMIT_BOOST_DOMAIN,
88
signature::compute_domain,
9-
signer::{BlsPublicKey, BlsSignature},
9+
signer::{BlsPublicKey, BlsSignature, ProxyStore},
1010
types::{Chain, ModuleId},
1111
};
1212
use rand::Rng;
1313
use tonic::transport::{Channel, ClientTlsConfig};
1414
use tracing::info;
15+
use tree_hash::TreeHash;
1516

1617
use crate::proto::v1::{
1718
account_manager_client::AccountManagerClient, lister_client::ListerClient,
@@ -26,6 +27,7 @@ pub struct DirkManager {
2627
wallets: Vec<String>,
2728
unlock: bool,
2829
secrets_path: PathBuf,
30+
proxy_store: Option<ProxyStore>,
2931
}
3032

3133
impl DirkManager {
@@ -54,9 +56,14 @@ impl DirkManager {
5456
wallets: config.wallets,
5557
unlock: config.unlock,
5658
secrets_path: config.secrets_path,
59+
proxy_store: None,
5760
})
5861
}
5962

63+
pub fn with_proxy_store(self, proxy_store: ProxyStore) -> eyre::Result<Self> {
64+
Ok(Self { proxy_store: Some(proxy_store), ..self })
65+
}
66+
6067
async fn get_all_accounts(&self) -> eyre::Result<Vec<DirkAccount>> {
6168
let mut client = ListerClient::new(self.channel.clone());
6269
let pubkeys_request =
@@ -248,10 +255,16 @@ impl DirkManager {
248255

249256
self.unlock_account(account_name, new_password).await?;
250257

251-
Ok(SignedProxyDelegation {
252-
message: ProxyDelegation { delegator: consensus_pubkey, proxy: proxy_key },
253-
signature: BlsSignature::ZERO,
254-
})
258+
let message = ProxyDelegation { delegator: consensus_pubkey, proxy: proxy_key };
259+
let signature =
260+
self.request_signature(consensus_pubkey, message.tree_hash_root().0).await?;
261+
let delegation = SignedProxyDelegation { message, signature };
262+
263+
if let Some(store) = &self.proxy_store {
264+
store.store_proxy_bls_delegation(&module_id, &delegation)?;
265+
}
266+
267+
Ok(delegation)
255268
}
256269

257270
pub async fn request_signature(

crates/signer/src/service.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,17 @@ impl SigningService {
5959
let module_ids: Vec<String> = config.jwts.left_values().cloned().map(Into::into).collect();
6060

6161
let state = match config.dirk {
62-
Some(dirk) => SigningState {
63-
manager: SigningManager::Dirk(
64-
DirkManager::new_from_config(config.chain, dirk).await?,
65-
),
66-
jwts: config.jwts.into(),
67-
},
62+
Some(dirk) => {
63+
let mut dirk_manager = DirkManager::new_from_config(config.chain, dirk).await?;
64+
if let Some(store) = config.store {
65+
dirk_manager = dirk_manager.with_proxy_store(store.init_from_env()?)?;
66+
}
67+
68+
SigningState {
69+
manager: SigningManager::Dirk(dirk_manager),
70+
jwts: config.jwts.into(),
71+
}
72+
}
6873
None => {
6974
let proxy_store = if let Some(store) = config.store {
7075
Some(store.init_from_env()?)

0 commit comments

Comments
 (0)