Skip to content

Commit

Permalink
feat: addressed review
Browse files Browse the repository at this point in the history
  • Loading branch information
merklefruit committed Oct 18, 2024
1 parent 1681883 commit 13de577
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion bolt-sidecar/src/config/constraint_signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct ConstraintSigningOpts {
/// Private key to use for signing constraint messages
#[clap(long, env = "BOLT_SIDECAR_CONSTRAINT_PRIVATE_KEY")]
pub constraint_private_key: Option<BlsSecretKeyWrapper>,
/// Socket address for the commit-boost sidecar
/// URL for the commit-boost sidecar
#[clap(long, env = "BOLT_SIDECAR_CB_SIGNER_URL", requires("commit_boost_jwt_hex"))]
pub commit_boost_signer_url: Option<Url>,
/// JWT in hexadecimal format for authenticating with the commit-boost service
Expand Down
36 changes: 25 additions & 11 deletions bolt-sidecar/src/signer/commit_boost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,15 @@ pub enum CommitBoostError {
#[error("failed to create signer client: {0}")]
SignerClientError(#[from] SignerClientError),
#[error("error in commit boost signer: {0}")]
Other(String),
Other(eyre::Report),
}

#[allow(unused)]
impl CommitBoostSigner {
/// Create a new [CommitBoostSigner] instance
pub fn new(signer_url: Url, jwt: &str) -> SignerResult<Self> {
let Some(hostname) = signer_url.host_str() else {
return Err(CommitBoostError::Other("Invalid signer host".to_string()).into());
};

let signer_server_address = format!("{}:{}", hostname, signer_url.port().unwrap_or(80));

let signer_client = match SignerClient::new(signer_server_address, jwt) {
Ok(client) => client,
Err(e) => return Err(CommitBoostError::Other(e.to_string()).into()),
};
let socket_addr = parse_address_from_url(signer_url).map_err(CommitBoostError::Other)?;
let signer_client = SignerClient::new(socket_addr, jwt).map_err(CommitBoostError::Other)?;

let client = Self {
signer_client,
Expand Down Expand Up @@ -167,12 +159,34 @@ impl SignerECDSA for CommitBoostSigner {
}
}

fn parse_address_from_url(url: Url) -> eyre::Result<String> {
let str = url.as_str();

// take the host out of the URL, e.g. "http://localhost:425" -> localhost:425
// and also "remotehost:2425" -> remotehost:2425
let without_base = url.as_str().split("://").last().unwrap_or(str);
let hostname = without_base.split(':').next().unwrap_or(without_base);
let port = without_base.split(':').last().ok_or_else(|| eyre::eyre!("No port found"))?;
let port = port.trim_end_matches('/');

Ok(format!("{}:{}", hostname, port))
}

#[cfg(test)]
mod test {
use super::*;
use rand::Rng;
use tracing::warn;

#[test]
fn test_url_parse_address() {
let url = Url::parse("http://localhost:8080").unwrap();
assert_eq!(parse_address_from_url(url).unwrap(), "localhost:8080");

let url = Url::parse("remotehost:2425").unwrap();
assert_eq!(parse_address_from_url(url).unwrap(), "remotehost:2425");
}

#[tokio::test]
async fn test_bls_commit_boost_signer() -> eyre::Result<()> {
let _ = dotenvy::dotenv();
Expand Down

0 comments on commit 13de577

Please sign in to comment.