Skip to content

Commit a306f23

Browse files
committed
Consolidated similar bodies for signing handlers
1 parent 228cbec commit a306f23

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
lines changed

crates/signer/src/service.rs

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use std::{
55
time::{Duration, Instant},
66
};
77

8-
use alloy::{primitives::Address, rpc::types::beacon::BlsPublicKey};
8+
use alloy::{
9+
primitives::{Address, B256},
10+
rpc::types::beacon::BlsPublicKey,
11+
};
912
use axum::{
1013
extract::{ConnectInfo, Request, State},
1114
http::StatusCode,
@@ -304,36 +307,16 @@ async fn handle_request_signature_bls(
304307
Json(request): Json<SignConsensusRequest>,
305308
) -> Result<impl IntoResponse, SignerModuleError> {
306309
let req_id = Uuid::new_v4();
307-
let Some(signing_id) = state.jwts.read().get(&module_id).map(|m| m.signing_id) else {
308-
error!(event = "bls_request_signature", ?module_id, ?req_id, "Module signing ID not found");
309-
return Err(SignerModuleError::RequestError("Module signing ID not found".to_string()));
310-
};
311310
debug!(event = "bls_request_signature", ?module_id, %request, ?req_id, "New request");
312-
313-
match &*state.manager.read().await {
314-
SigningManager::Local(local_manager) => {
315-
local_manager
316-
.sign_consensus(&request.pubkey, &request.object_root, Some(&signing_id))
317-
.await
318-
}
319-
SigningManager::Dirk(dirk_manager) => {
320-
dirk_manager
321-
.request_consensus_signature(
322-
&request.pubkey,
323-
&request.object_root,
324-
Some(&signing_id),
325-
)
326-
.await
327-
}
328-
}
329-
.map(|sig| {
330-
Json(BlsSignResponse::new(request.pubkey, request.object_root, signing_id, sig))
331-
.into_response()
332-
})
333-
.map_err(|err| {
334-
error!(event = "request_signature", ?module_id, ?req_id, "{err}");
335-
err
336-
})
311+
handle_request_signature_bls_impl(
312+
&module_id,
313+
&state,
314+
&req_id,
315+
false,
316+
&request.pubkey,
317+
&request.object_root,
318+
)
319+
.await
337320
}
338321

339322
/// Validates a BLS key signature request using a proxy key and returns the
@@ -344,7 +327,28 @@ async fn handle_request_signature_proxy_bls(
344327
Json(request): Json<SignProxyRequest<BlsPublicKey>>,
345328
) -> Result<impl IntoResponse, SignerModuleError> {
346329
let req_id = Uuid::new_v4();
347-
let Some(signing_id) = state.jwts.read().get(&module_id).map(|m| m.signing_id) else {
330+
debug!(event = "proxy_bls_request_signature", ?module_id, %request, ?req_id, "New request");
331+
handle_request_signature_bls_impl(
332+
&module_id,
333+
&state,
334+
&req_id,
335+
true,
336+
&request.proxy,
337+
&request.object_root,
338+
)
339+
.await
340+
}
341+
342+
/// Implementation for handling a BLS signature request
343+
async fn handle_request_signature_bls_impl(
344+
module_id: &ModuleId,
345+
state: &SigningState,
346+
req_id: &Uuid,
347+
is_proxy: bool,
348+
signing_pubkey: &BlsPublicKey,
349+
object_root: &B256,
350+
) -> Result<impl IntoResponse, SignerModuleError> {
351+
let Some(signing_id) = state.jwts.read().get(module_id).map(|m| m.signing_id) else {
348352
error!(
349353
event = "proxy_bls_request_signature",
350354
?module_id,
@@ -353,23 +357,23 @@ async fn handle_request_signature_proxy_bls(
353357
);
354358
return Err(SignerModuleError::RequestError("Module signing ID not found".to_string()));
355359
};
356-
debug!(event = "proxy_bls_request_signature", ?module_id, %request, ?req_id, "New request");
357360

358361
match &*state.manager.read().await {
359362
SigningManager::Local(local_manager) => {
360-
local_manager
361-
.sign_proxy_bls(&request.proxy, &request.object_root, Some(&signing_id))
362-
.await
363+
if is_proxy {
364+
local_manager.sign_proxy_bls(signing_pubkey, object_root, Some(&signing_id)).await
365+
} else {
366+
local_manager.sign_consensus(signing_pubkey, object_root, Some(&signing_id)).await
367+
}
363368
}
364369
SigningManager::Dirk(dirk_manager) => {
365370
dirk_manager
366-
.request_proxy_signature(&request.proxy, &request.object_root, Some(&signing_id))
371+
.request_proxy_signature(signing_pubkey, object_root, Some(&signing_id))
367372
.await
368373
}
369374
}
370375
.map(|sig| {
371-
Json(BlsSignResponse::new(request.proxy, request.object_root, signing_id, sig))
372-
.into_response()
376+
Json(BlsSignResponse::new(*signing_pubkey, *object_root, signing_id, sig)).into_response()
373377
})
374378
.map_err(|err| {
375379
error!(event = "request_signature", ?module_id, ?req_id, "{err}");

0 commit comments

Comments
 (0)