Skip to content

Commit af13089

Browse files
jclapisltitanbManuelBilbao
authored
Split request_signature into separate paths that return JSON (#350)
Co-authored-by: eltitanb <[email protected]> Co-authored-by: ltitanb <[email protected]> Co-authored-by: Manuel Iñaki Bilbao <[email protected]>
1 parent 498eed9 commit af13089

File tree

15 files changed

+637
-216
lines changed

15 files changed

+637
-216
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ cb-signer = { path = "crates/signer" }
3434
cipher = "0.4"
3535
clap = { version = "4.5.4", features = ["derive", "env"] }
3636
color-eyre = "0.6.3"
37+
const_format = "0.2.34"
3738
ctr = "0.9.2"
3839
derive_more = { version = "2.0.1", features = ["deref", "display", "from", "into"] }
3940
docker-compose-types = "0.16.0"

api/signer-api.yml

Lines changed: 326 additions & 47 deletions
Large diffs are not rendered by default.

crates/common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ bimap.workspace = true
1515
blst.workspace = true
1616
bytes.workspace = true
1717
cipher.workspace = true
18+
const_format.workspace = true
1819
ctr.workspace = true
1920
derive_more.workspace = true
2021
docker-image.workspace = true

crates/common/src/commit/client.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
use std::time::{Duration, Instant};
22

3-
use alloy::{primitives::Address, rpc::types::beacon::BlsSignature};
3+
use alloy::primitives::Address;
44
use eyre::WrapErr;
55
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION};
6-
use serde::Deserialize;
6+
use serde::{Deserialize, Serialize};
77
use url::Url;
88

99
use super::{
10-
constants::{GENERATE_PROXY_KEY_PATH, GET_PUBKEYS_PATH, REQUEST_SIGNATURE_PATH},
10+
constants::{GENERATE_PROXY_KEY_PATH, GET_PUBKEYS_PATH},
1111
error::SignerClientError,
1212
request::{
1313
EncryptionScheme, GenerateProxyRequest, GetPubkeysResponse, ProxyId, SignConsensusRequest,
14-
SignProxyRequest, SignRequest, SignedProxyDelegation,
14+
SignProxyRequest, SignedProxyDelegation,
1515
},
1616
};
1717
use crate::{
18+
commit::{
19+
constants::{
20+
REQUEST_SIGNATURE_BLS_PATH, REQUEST_SIGNATURE_PROXY_BLS_PATH,
21+
REQUEST_SIGNATURE_PROXY_ECDSA_PATH,
22+
},
23+
response::{BlsSignResponse, EcdsaSignResponse},
24+
},
1825
constants::SIGNER_JWT_EXPIRATION,
19-
signer::{BlsPublicKey, EcdsaSignature},
26+
signer::BlsPublicKey,
2027
types::{Jwt, ModuleId},
2128
utils::create_jwt,
2229
DEFAULT_REQUEST_TIMEOUT,
@@ -99,13 +106,18 @@ impl SignerClient {
99106
}
100107

101108
/// Send a signature request
102-
async fn request_signature<T>(&mut self, request: &SignRequest) -> Result<T, SignerClientError>
109+
async fn request_signature<Q, T>(
110+
&mut self,
111+
route: &str,
112+
request: &Q,
113+
) -> Result<T, SignerClientError>
103114
where
115+
Q: Serialize,
104116
T: for<'de> Deserialize<'de>,
105117
{
106118
self.refresh_jwt()?;
107119

108-
let url = self.url.join(REQUEST_SIGNATURE_PATH)?;
120+
let url = self.url.join(route)?;
109121
let res = self.client.post(url).json(&request).send().await?;
110122

111123
let status = res.status();
@@ -126,22 +138,22 @@ impl SignerClient {
126138
pub async fn request_consensus_signature(
127139
&mut self,
128140
request: SignConsensusRequest,
129-
) -> Result<BlsSignature, SignerClientError> {
130-
self.request_signature(&request.into()).await
141+
) -> Result<BlsSignResponse, SignerClientError> {
142+
self.request_signature(REQUEST_SIGNATURE_BLS_PATH, &request).await
131143
}
132144

133145
pub async fn request_proxy_signature_ecdsa(
134146
&mut self,
135147
request: SignProxyRequest<Address>,
136-
) -> Result<EcdsaSignature, SignerClientError> {
137-
self.request_signature(&request.into()).await
148+
) -> Result<EcdsaSignResponse, SignerClientError> {
149+
self.request_signature(REQUEST_SIGNATURE_PROXY_ECDSA_PATH, &request).await
138150
}
139151

140152
pub async fn request_proxy_signature_bls(
141153
&mut self,
142154
request: SignProxyRequest<BlsPublicKey>,
143-
) -> Result<BlsSignature, SignerClientError> {
144-
self.request_signature(&request.into()).await
155+
) -> Result<BlsSignResponse, SignerClientError> {
156+
self.request_signature(REQUEST_SIGNATURE_PROXY_BLS_PATH, &request).await
145157
}
146158

147159
async fn generate_proxy_key<T>(

crates/common/src/commit/constants.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
use const_format::concatcp;
2+
13
pub const GET_PUBKEYS_PATH: &str = "/signer/v1/get_pubkeys";
2-
pub const REQUEST_SIGNATURE_PATH: &str = "/signer/v1/request_signature";
4+
pub const REQUEST_SIGNATURE_BASE_PATH: &str = "/signer/v1/request_signature";
5+
pub const REQUEST_SIGNATURE_BLS_PATH: &str = concatcp!(REQUEST_SIGNATURE_BASE_PATH, "/bls");
6+
pub const REQUEST_SIGNATURE_PROXY_BLS_PATH: &str =
7+
concatcp!(REQUEST_SIGNATURE_BASE_PATH, "/proxy-bls");
8+
pub const REQUEST_SIGNATURE_PROXY_ECDSA_PATH: &str =
9+
concatcp!(REQUEST_SIGNATURE_BASE_PATH, "/proxy-ecdsa");
310
pub const GENERATE_PROXY_KEY_PATH: &str = "/signer/v1/generate_proxy_key";
411
pub const STATUS_PATH: &str = "/status";
512
pub const RELOAD_PATH: &str = "/reload";

crates/common/src/commit/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ pub mod client;
22
pub mod constants;
33
pub mod error;
44
pub mod request;
5+
pub mod response;

crates/common/src/commit/request.rs

Lines changed: 33 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use alloy::{
99
primitives::{aliases::B32, Address, B256},
1010
rpc::types::beacon::BlsSignature,
1111
};
12-
use derive_more::derive::From;
1312
use serde::{Deserialize, Deserializer, Serialize};
1413
use tree_hash::TreeHash;
1514
use tree_hash_derive::TreeHash;
@@ -74,40 +73,6 @@ impl<T: ProxyId> fmt::Display for SignedProxyDelegation<T> {
7473
}
7574
}
7675

77-
// TODO(David): This struct shouldn't be visible to module authors
78-
#[derive(Debug, Clone, Serialize, Deserialize, From)]
79-
#[serde(tag = "type", rename_all = "snake_case")]
80-
pub enum SignRequest {
81-
Consensus(SignConsensusRequest),
82-
ProxyBls(SignProxyRequest<BlsPublicKey>),
83-
ProxyEcdsa(SignProxyRequest<Address>),
84-
}
85-
86-
impl Display for SignRequest {
87-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88-
match self {
89-
SignRequest::Consensus(req) => write!(
90-
f,
91-
"Consensus(pubkey: {}, object_root: {})",
92-
req.pubkey,
93-
hex::encode_prefixed(req.object_root)
94-
),
95-
SignRequest::ProxyBls(req) => write!(
96-
f,
97-
"BLS(proxy: {}, object_root: {})",
98-
req.proxy,
99-
hex::encode_prefixed(req.object_root)
100-
),
101-
SignRequest::ProxyEcdsa(req) => write!(
102-
f,
103-
"ECDSA(proxy: {}, object_root: {})",
104-
req.proxy,
105-
hex::encode_prefixed(req.object_root)
106-
),
107-
}
108-
}
109-
}
110-
11176
#[derive(Debug, Clone, Serialize, Deserialize)]
11277
pub struct SignConsensusRequest {
11378
pub pubkey: BlsPublicKey,
@@ -132,6 +97,17 @@ impl SignConsensusRequest {
13297
}
13398
}
13499

100+
impl Display for SignConsensusRequest {
101+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102+
write!(
103+
f,
104+
"Consensus(pubkey: {}, object_root: {})",
105+
self.pubkey,
106+
hex::encode_prefixed(self.object_root)
107+
)
108+
}
109+
}
110+
135111
#[derive(Debug, Clone, Serialize, Deserialize)]
136112
pub struct SignProxyRequest<T: ProxyId> {
137113
pub proxy: T,
@@ -156,6 +132,28 @@ impl<T: ProxyId> SignProxyRequest<T> {
156132
}
157133
}
158134

135+
impl Display for SignProxyRequest<BlsPublicKey> {
136+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
137+
write!(
138+
f,
139+
"BLS(proxy: {}, object_root: {})",
140+
self.proxy,
141+
hex::encode_prefixed(self.object_root)
142+
)
143+
}
144+
}
145+
146+
impl Display for SignProxyRequest<Address> {
147+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
148+
write!(
149+
f,
150+
"ECDSA(proxy: {}, object_root: {})",
151+
self.proxy,
152+
hex::encode_prefixed(self.object_root)
153+
)
154+
}
155+
}
156+
159157
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
160158
pub enum EncryptionScheme {
161159
#[serde(rename = "bls")]
@@ -249,36 +247,6 @@ mod tests {
249247
use super::*;
250248
use crate::signer::EcdsaSignature;
251249

252-
#[test]
253-
fn test_decode_request_signature() {
254-
let data = r#"{
255-
"type": "consensus",
256-
"pubkey": "0xa3366b54f28e4bf1461926a3c70cdb0ec432b5c92554ecaae3742d33fb33873990cbed1761c68020e6d3c14d30a22050",
257-
"object_root": "0x5c89913beafa0472168e0ec05e349b4ceb9985d25ab9fa8de53a60208c85b3a5"
258-
}"#;
259-
260-
let request: SignRequest = serde_json::from_str(data).unwrap();
261-
assert!(matches!(request, SignRequest::Consensus(..)));
262-
263-
let data = r#"{
264-
"type": "proxy_bls",
265-
"proxy": "0xa3366b54f28e4bf1461926a3c70cdb0ec432b5c92554ecaae3742d33fb33873990cbed1761c68020e6d3c14d30a22050",
266-
"object_root": "0x5c89913beafa0472168e0ec05e349b4ceb9985d25ab9fa8de53a60208c85b3a5"
267-
}"#;
268-
269-
let request: SignRequest = serde_json::from_str(data).unwrap();
270-
assert!(matches!(request, SignRequest::ProxyBls(..)));
271-
272-
let data = r#"{
273-
"type": "proxy_ecdsa",
274-
"proxy": "0x4ca9939a8311a7cab3dde201b70157285fa81a9d",
275-
"object_root": "0x5c89913beafa0472168e0ec05e349b4ceb9985d25ab9fa8de53a60208c85b3a5"
276-
}"#;
277-
278-
let request: SignRequest = serde_json::from_str(data).unwrap();
279-
assert!(matches!(request, SignRequest::ProxyEcdsa(..)));
280-
}
281-
282250
#[test]
283251
fn test_decode_response_signature() {
284252
let data = r#""0xa3ffa9241f78279f1af04644cb8c79c2d8f02bcf0e28e2f186f6dcccac0a869c2be441fda50f0dea895cfce2e53f0989a3ffa9241f78279f1af04644cb8c79c2d8f02bcf0e28e2f186f6dcccac0a869c2be441fda50f0dea895cfce2e53f0989""#;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use alloy::{
2+
primitives::{Address, B256},
3+
rpc::types::beacon::BlsSignature,
4+
};
5+
use serde::{Deserialize, Serialize};
6+
7+
use crate::signer::{BlsPublicKey, EcdsaSignature};
8+
9+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
10+
pub struct BlsSignResponse {
11+
pub pubkey: BlsPublicKey,
12+
pub object_root: B256,
13+
pub module_signing_id: B256,
14+
pub signature: BlsSignature,
15+
}
16+
17+
impl BlsSignResponse {
18+
pub fn new(
19+
pubkey: BlsPublicKey,
20+
object_root: B256,
21+
module_signing_id: B256,
22+
signature: BlsSignature,
23+
) -> Self {
24+
Self { pubkey, object_root, module_signing_id, signature }
25+
}
26+
}
27+
28+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
29+
pub struct EcdsaSignResponse {
30+
pub address: Address,
31+
pub object_root: B256,
32+
pub module_signing_id: B256,
33+
pub signature: EcdsaSignature,
34+
}
35+
36+
impl EcdsaSignResponse {
37+
pub fn new(
38+
address: Address,
39+
object_root: B256,
40+
module_signing_id: B256,
41+
signature: EcdsaSignature,
42+
) -> Self {
43+
Self { address, object_root, module_signing_id, signature }
44+
}
45+
}

crates/signer/src/constants.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
pub const GET_PUBKEYS_ENDPOINT_TAG: &str = "get_pubkeys";
22
pub const GENERATE_PROXY_KEY_ENDPOINT_TAG: &str = "generate_proxy_key";
3-
pub const REQUEST_SIGNATURE_ENDPOINT_TAG: &str = "request_signature";
3+
pub const REQUEST_SIGNATURE_BLS_ENDPOINT_TAG: &str = "request_signature_bls";
4+
pub const REQUEST_SIGNATURE_PROXY_BLS_ENDPOINT_TAG: &str = "request_signature_proxy_bls";
5+
pub const REQUEST_SIGNATURE_PROXY_ECDSA_ENDPOINT_TAG: &str = "request_signature_proxy_ecdsa";

0 commit comments

Comments
 (0)