refac: change awsSigner to own kmsclient (#1922)

This commit is contained in:
Jared Tokuz 2022-12-02 21:06:06 -06:00 committed by GitHub
parent f886d379a4
commit a88d2d03e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 13 deletions

View File

@ -22,7 +22,7 @@ use utils::{apply_eip155, rsig_to_ethsig, verifying_key_to_address};
/// The AWS Signer passes signing requests to the cloud service. AWS KMS keys /// The AWS Signer passes signing requests to the cloud service. AWS KMS keys
/// are identified by a UUID, the `key_id`. /// are identified by a UUID, the `key_id`.
/// ///
/// Because the public key is unknwon, we retrieve it on instantiation of the /// Because the public key is unknown, we retrieve it on instantiation of the
/// signer. This means that the new function is `async` and must be called /// signer. This means that the new function is `async` and must be called
/// within some runtime. /// within some runtime.
/// ///
@ -44,15 +44,15 @@ use utils::{apply_eip155, rsig_to_ethsig, verifying_key_to_address};
/// let sig = signer.sign_message(H256::zero()).await?; /// let sig = signer.sign_message(H256::zero()).await?;
/// ``` /// ```
#[derive(Clone)] #[derive(Clone)]
pub struct AwsSigner<'a> { pub struct AwsSigner {
kms: &'a rusoto_kms::KmsClient, kms: KmsClient,
chain_id: u64, chain_id: u64,
key_id: String, key_id: String,
pubkey: VerifyingKey, pubkey: VerifyingKey,
address: Address, address: Address,
} }
impl<'a> std::fmt::Debug for AwsSigner<'a> { impl std::fmt::Debug for AwsSigner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AwsSigner") f.debug_struct("AwsSigner")
.field("key_id", &self.key_id) .field("key_id", &self.key_id)
@ -63,7 +63,7 @@ impl<'a> std::fmt::Debug for AwsSigner<'a> {
} }
} }
impl<'a> std::fmt::Display for AwsSigner<'a> { impl std::fmt::Display for AwsSigner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!( write!(
f, f,
@ -146,21 +146,21 @@ where
resp resp
} }
impl<'a> AwsSigner<'a> { impl AwsSigner {
/// Instantiate a new signer from an existing `KmsClient` and Key ID. /// Instantiate a new signer from an existing `KmsClient` and Key ID.
/// ///
/// This function retrieves the public key from AWS and calculates the /// This function retrieves the public key from AWS and calculates the
/// Etheruem address. It is therefore `async`. /// Etheruem address. It is therefore `async`.
#[instrument(err, skip(kms, key_id, chain_id), fields(key_id = %key_id.as_ref()))] #[instrument(err, skip(kms, key_id, chain_id), fields(key_id = %key_id.as_ref()))]
pub async fn new<T>( pub async fn new<T>(
kms: &'a KmsClient, kms: KmsClient,
key_id: T, key_id: T,
chain_id: u64, chain_id: u64,
) -> Result<AwsSigner<'a>, AwsSignerError> ) -> Result<AwsSigner, AwsSignerError>
where where
T: AsRef<str>, T: AsRef<str>,
{ {
let pubkey = request_get_pubkey(kms, &key_id).await.map(utils::decode_pubkey)??; let pubkey = request_get_pubkey(&kms, &key_id).await.map(utils::decode_pubkey)??;
let address = verifying_key_to_address(&pubkey); let address = verifying_key_to_address(&pubkey);
debug!( debug!(
@ -177,7 +177,7 @@ impl<'a> AwsSigner<'a> {
where where
T: AsRef<str>, T: AsRef<str>,
{ {
request_get_pubkey(self.kms, key_id).await.map(utils::decode_pubkey)? request_get_pubkey(&self.kms, key_id).await.map(utils::decode_pubkey)?
} }
/// Fetch the pubkey associated with this signer's key ID /// Fetch the pubkey associated with this signer's key ID
@ -194,7 +194,7 @@ impl<'a> AwsSigner<'a> {
where where
T: AsRef<str>, T: AsRef<str>,
{ {
request_sign_digest(self.kms, key_id, digest).await.map(utils::decode_signature)? request_sign_digest(&self.kms, key_id, digest).await.map(utils::decode_signature)?
} }
/// Sign a digest with this signer's key /// Sign a digest with this signer's key
@ -221,7 +221,7 @@ impl<'a> AwsSigner<'a> {
} }
#[async_trait::async_trait] #[async_trait::async_trait]
impl<'a> super::Signer for AwsSigner<'a> { impl super::Signer for AwsSigner {
type Error = AwsSignerError; type Error = AwsSignerError;
#[instrument(err, skip(message))] #[instrument(err, skip(message))]
@ -320,7 +320,7 @@ mod tests {
}; };
setup_tracing(); setup_tracing();
let client = env_client(); let client = env_client();
let signer = AwsSigner::new(&client, key_id, chain_id).await.unwrap(); let signer = AwsSigner::new(client, key_id, chain_id).await.unwrap();
let message = vec![0, 1, 2, 3]; let message = vec![0, 1, 2, 3];