Skip to main content

miden_validator/signers/
mod.rs

1mod kms;
2pub use kms::KmsSigner;
3use miden_node_utils::spawn::spawn_blocking_in_current_span;
4use miden_protocol::block::BlockHeader;
5use miden_protocol::crypto::dsa::ecdsa_k256_keccak::{PublicKey, Signature, SigningKey};
6
7// VALIDATOR SIGNER
8// =================================================================================================
9
10/// Signer that the Validator uses to sign blocks.
11pub enum ValidatorSigner {
12    Kms(KmsSigner),
13    Local(SigningKey),
14}
15
16impl ValidatorSigner {
17    /// Constructs a signer which uses an AWS KMS key for signing.
18    ///
19    /// See [`KmsSigner`] for details as to env var configuration and AWS IAM policies
20    /// required to use this functionality.
21    pub async fn new_kms(key_id: impl Into<String>) -> anyhow::Result<Self> {
22        let kms_signer = KmsSigner::new(key_id).await?;
23        Ok(Self::Kms(kms_signer))
24    }
25
26    /// Constructs a signer which uses a local secret key for signing.
27    pub fn new_local(secret_key: SigningKey) -> Self {
28        Self::Local(secret_key)
29    }
30
31    /// Returns the public key corresponding to the configured signer.
32    pub fn public_key(&self) -> PublicKey {
33        match self {
34            Self::Kms(signer) => signer.public_key(),
35            Self::Local(signer) => signer.public_key(),
36        }
37    }
38
39    /// Signs a block header using the configured signer.
40    pub async fn sign(&self, header: &BlockHeader) -> anyhow::Result<Signature> {
41        let commitment = header.commitment();
42        let signature = match self {
43            Self::Kms(signer) => signer.sign(commitment).await?,
44            Self::Local(signer) => spawn_blocking_in_current_span({
45                let signer = signer.clone();
46                move || signer.sign(commitment)
47            })
48            .await
49            .unwrap_or_else(|e| std::panic::resume_unwind(e.into_panic())),
50        };
51
52        Ok(signature)
53    }
54}