47 lines
1.4 KiB
Rust
47 lines
1.4 KiB
Rust
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
use crate::config::Config;
|
|
use tonic::{
|
|
metadata::{errors::InvalidMetadataValue, AsciiMetadataValue},
|
|
Request,
|
|
};
|
|
|
|
#[derive(thiserror::Error, Debug)]
|
|
pub enum Error {
|
|
#[error(transparent)]
|
|
ConfigError(#[from] crate::config::Error),
|
|
#[error(transparent)]
|
|
InternalError(#[from] InvalidMetadataValue),
|
|
}
|
|
|
|
pub fn block_on<F>(future: F) -> F::Output
|
|
where
|
|
F: std::future::Future,
|
|
{
|
|
tokio::runtime::Runtime::new().unwrap().block_on(future)
|
|
}
|
|
|
|
pub fn sign_request<T: std::fmt::Debug>(req: T) -> Result<Request<T>, Error> {
|
|
let pubkey = Config::get_detee_wallet()?;
|
|
let timestamp = chrono::Utc::now().to_rfc3339();
|
|
let signature = Config::try_sign_message(&format!("{timestamp}{req:?}"))?;
|
|
let timestamp: AsciiMetadataValue = timestamp.parse()?;
|
|
let pubkey: AsciiMetadataValue = pubkey.parse()?;
|
|
let signature: AsciiMetadataValue = signature.parse()?;
|
|
let mut req = Request::new(req);
|
|
req.metadata_mut().insert("timestamp", timestamp);
|
|
req.metadata_mut().insert("pubkey", pubkey);
|
|
req.metadata_mut().insert("request-signature", signature);
|
|
Ok(req)
|
|
}
|
|
|
|
pub fn shorten_string(my_string: &String) -> String {
|
|
if my_string.len() <= 8 {
|
|
my_string.to_string()
|
|
} else {
|
|
let first_part = &my_string[..8];
|
|
// let last_part = &my_string[my_string.len() - 4..];
|
|
format!("{}", first_part)
|
|
}
|
|
}
|