Skip to content

Commit 46ec92b

Browse files
committed
Merge branch 'signer-json-api' into augment-sign-requests
2 parents 3345ef0 + dc90028 commit 46ec92b

File tree

2 files changed

+109
-156
lines changed

2 files changed

+109
-156
lines changed

crates/common/src/commit/response.rs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use std::{fmt, fmt::Display};
2-
31
use alloy::{
4-
hex,
52
primitives::{Address, B256, U256},
63
rpc::types::beacon::BlsSignature,
74
};
@@ -32,21 +29,6 @@ impl BlsSignResponse {
3229
}
3330
}
3431

35-
impl Display for BlsSignResponse {
36-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37-
write!(
38-
f,
39-
"BLS Signature(pubkey: {}, object_root: {}, module_id: {}, nonce: {}, chain_id: {} signature: {})",
40-
self.pubkey,
41-
hex::encode_prefixed(self.object_root),
42-
self.module_signing_id,
43-
self.nonce,
44-
self.chain_id,
45-
hex::encode_prefixed(self.signature)
46-
)
47-
}
48-
}
49-
5032
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
5133
pub struct EcdsaSignResponse {
5234
pub address: Address,
@@ -69,18 +51,3 @@ impl EcdsaSignResponse {
6951
Self { address, object_root, module_signing_id, nonce, chain_id, signature }
7052
}
7153
}
72-
73-
impl Display for EcdsaSignResponse {
74-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75-
write!(
76-
f,
77-
"ECDSA Signature(address: {}, object_root: {}, module_id: {}, nonce: {}, chain_id: {} signature: 0x{})",
78-
self.address,
79-
hex::encode_prefixed(self.object_root),
80-
self.module_signing_id,
81-
self.nonce,
82-
self.chain_id,
83-
self.signature
84-
)
85-
}
86-
}

crates/signer/src/service.rs

Lines changed: 109 additions & 123 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, U256},
10+
rpc::types::beacon::BlsPublicKey,
11+
};
912
use axum::{
1013
extract::{ConnectInfo, Request, State},
1114
http::StatusCode,
@@ -304,57 +307,17 @@ 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-
let manager = state.manager.read().await;
314-
let res = match &*manager {
315-
SigningManager::Local(local_manager) => local_manager
316-
.sign_consensus(
317-
&request.pubkey,
318-
&request.object_root,
319-
Some(&SignatureRequestInfo { module_signing_id: signing_id, nonce: request.nonce }),
320-
)
321-
.await
322-
.map(|sig| {
323-
Json(BlsSignResponse::new(
324-
request.pubkey,
325-
request.object_root,
326-
signing_id,
327-
request.nonce,
328-
local_manager.get_chain().id(),
329-
sig,
330-
))
331-
.into_response()
332-
}),
333-
SigningManager::Dirk(dirk_manager) => dirk_manager
334-
.request_consensus_signature(
335-
&request.pubkey,
336-
&request.object_root,
337-
Some(&SignatureRequestInfo { module_signing_id: signing_id, nonce: request.nonce }),
338-
)
339-
.await
340-
.map(|sig| {
341-
Json(BlsSignResponse::new(
342-
request.pubkey,
343-
request.object_root,
344-
signing_id,
345-
request.nonce,
346-
dirk_manager.get_chain().id(),
347-
sig,
348-
))
349-
.into_response()
350-
}),
351-
};
352-
353-
if let Err(err) = &res {
354-
error!(event = "request_signature", ?module_id, ?req_id, "{err}");
355-
}
356-
357-
res
311+
handle_request_signature_bls_impl(
312+
&module_id,
313+
&state,
314+
&req_id,
315+
false,
316+
&request.pubkey,
317+
&request.object_root,
318+
request.nonce,
319+
)
320+
.await
358321
}
359322

360323
/// Validates a BLS key signature request using a proxy key and returns the
@@ -365,7 +328,30 @@ async fn handle_request_signature_proxy_bls(
365328
Json(request): Json<SignProxyRequest<BlsPublicKey>>,
366329
) -> Result<impl IntoResponse, SignerModuleError> {
367330
let req_id = Uuid::new_v4();
368-
let Some(signing_id) = state.jwts.read().get(&module_id).map(|m| m.signing_id) else {
331+
debug!(event = "proxy_bls_request_signature", ?module_id, %request, ?req_id, "New request");
332+
handle_request_signature_bls_impl(
333+
&module_id,
334+
&state,
335+
&req_id,
336+
true,
337+
&request.proxy,
338+
&request.object_root,
339+
request.nonce,
340+
)
341+
.await
342+
}
343+
344+
/// Implementation for handling a BLS signature request
345+
async fn handle_request_signature_bls_impl(
346+
module_id: &ModuleId,
347+
state: &SigningState,
348+
req_id: &Uuid,
349+
is_proxy: bool,
350+
signing_pubkey: &BlsPublicKey,
351+
object_root: &B256,
352+
nonce: u64,
353+
) -> Result<impl IntoResponse, SignerModuleError> {
354+
let Some(signing_id) = state.jwts.read().get(module_id).map(|m| m.signing_id) else {
369355
error!(
370356
event = "proxy_bls_request_signature",
371357
?module_id,
@@ -374,53 +360,48 @@ async fn handle_request_signature_proxy_bls(
374360
);
375361
return Err(SignerModuleError::RequestError("Module signing ID not found".to_string()));
376362
};
377-
debug!(event = "proxy_bls_request_signature", ?module_id, %request, ?req_id, "New request");
378363

379-
let manager = state.manager.read().await;
380-
let res = match &*manager {
381-
SigningManager::Local(local_manager) => local_manager
382-
.sign_proxy_bls(
383-
&request.proxy,
384-
&request.object_root,
385-
Some(&SignatureRequestInfo { module_signing_id: signing_id, nonce: request.nonce }),
386-
)
387-
.await
388-
.map(|sig| {
389-
Json(BlsSignResponse::new(
390-
request.proxy,
391-
request.object_root,
392-
signing_id,
393-
request.nonce,
394-
local_manager.get_chain().id(),
395-
sig,
396-
))
397-
.into_response()
398-
}),
399-
SigningManager::Dirk(dirk_manager) => dirk_manager
400-
.request_proxy_signature(
401-
&request.proxy,
402-
&request.object_root,
403-
Some(&SignatureRequestInfo { module_signing_id: signing_id, nonce: request.nonce }),
404-
)
405-
.await
406-
.map(|sig| {
407-
Json(BlsSignResponse::new(
408-
request.proxy,
409-
request.object_root,
410-
signing_id,
411-
request.nonce,
412-
dirk_manager.get_chain().id(),
413-
sig,
414-
))
415-
.into_response()
416-
}),
417-
};
418-
419-
if let Err(err) = &res {
420-
error!(event = "request_signature", ?module_id, ?req_id, "{err}");
364+
let chain_id: U256;
365+
match &*state.manager.read().await {
366+
SigningManager::Local(local_manager) => {
367+
chain_id = local_manager.get_chain().id();
368+
if is_proxy {
369+
local_manager
370+
.sign_proxy_bls(
371+
signing_pubkey,
372+
object_root,
373+
Some(&SignatureRequestInfo { module_signing_id: signing_id, nonce }),
374+
)
375+
.await
376+
} else {
377+
local_manager
378+
.sign_consensus(
379+
signing_pubkey,
380+
object_root,
381+
Some(&SignatureRequestInfo { module_signing_id: signing_id, nonce }),
382+
)
383+
.await
384+
}
385+
}
386+
SigningManager::Dirk(dirk_manager) => {
387+
chain_id = dirk_manager.get_chain().id();
388+
dirk_manager
389+
.request_proxy_signature(
390+
signing_pubkey,
391+
object_root,
392+
Some(&SignatureRequestInfo { module_signing_id: signing_id, nonce }),
393+
)
394+
.await
395+
}
421396
}
422-
423-
res
397+
.map(|sig| {
398+
Json(BlsSignResponse::new(*signing_pubkey, *object_root, signing_id, nonce, chain_id, sig))
399+
.into_response()
400+
})
401+
.map_err(|err| {
402+
error!(event = "request_signature", ?module_id, ?req_id, "{err}");
403+
err
404+
})
424405
}
425406

426407
/// Validates an ECDSA key signature request using a proxy key and returns the
@@ -442,27 +423,23 @@ async fn handle_request_signature_proxy_ecdsa(
442423
};
443424
debug!(event = "proxy_ecdsa_request_signature", ?module_id, %request, ?req_id, "New request");
444425

445-
let manager = state.manager.read().await;
446-
let res = match &*manager {
447-
SigningManager::Local(local_manager) => local_manager
448-
.sign_proxy_ecdsa(
449-
&request.proxy,
450-
&request.object_root,
451-
Some(&SignatureRequestInfo { module_signing_id: signing_id, nonce: request.nonce }),
452-
)
453-
.await
454-
.map(|sig| {
455-
Json(EcdsaSignResponse::new(
456-
request.proxy,
457-
request.object_root,
458-
signing_id,
459-
request.nonce,
460-
local_manager.get_chain().id(),
461-
sig,
462-
))
463-
.into_response()
464-
}),
426+
let chain_id: U256;
427+
match &*state.manager.read().await {
428+
SigningManager::Local(local_manager) => {
429+
chain_id = local_manager.get_chain().id();
430+
local_manager
431+
.sign_proxy_ecdsa(
432+
&request.proxy,
433+
&request.object_root,
434+
Some(&SignatureRequestInfo {
435+
module_signing_id: signing_id,
436+
nonce: request.nonce,
437+
}),
438+
)
439+
.await
440+
}
465441
SigningManager::Dirk(_) => {
442+
chain_id = U256::ZERO; // Dirk does not support ECDSA proxy signing
466443
error!(
467444
event = "request_signature",
468445
?module_id,
@@ -471,13 +448,22 @@ async fn handle_request_signature_proxy_ecdsa(
471448
);
472449
Err(SignerModuleError::DirkNotSupported)
473450
}
474-
};
475-
476-
if let Err(err) = &res {
477-
error!(event = "request_signature", ?module_id, ?req_id, "{err}");
478451
}
479-
480-
res
452+
.map(|sig| {
453+
Json(EcdsaSignResponse::new(
454+
request.proxy,
455+
request.object_root,
456+
signing_id,
457+
request.nonce,
458+
chain_id,
459+
sig,
460+
))
461+
.into_response()
462+
})
463+
.map_err(|err| {
464+
error!(event = "request_signature", ?module_id, ?req_id, "{err}");
465+
err
466+
})
481467
}
482468

483469
async fn handle_generate_proxy(

0 commit comments

Comments
 (0)