kona_sources/signer/
mod.rs

1//! Signer utilities for the Kona node.
2//!
3//! We currently support two types of block signers:
4//!
5//! 1. A local block signer that is used to sign blocks with a locally available private key.
6//! 2. A remote block signer that is used to sign blocks with a remote private key.
7
8use alloy_primitives::{Address, ChainId};
9use alloy_signer::{Signature, SignerSync};
10use derive_more::From;
11use op_alloy_rpc_types_engine::PayloadHash;
12use std::fmt::Debug;
13
14mod remote;
15pub use remote::{
16    CertificateError, ClientCert, RemoteSigner, RemoteSignerError, RemoteSignerHandler,
17    RemoteSignerStartError,
18};
19
20/// A builder for a block signer.
21#[derive(Debug, Clone, From)]
22pub enum BlockSigner {
23    /// A local block signer that is used to sign blocks with a locally available private key.
24    Local(#[from] alloy_signer_local::PrivateKeySigner),
25    /// A remote block signer that is used to sign blocks with a remote private key.
26    Remote(#[from] RemoteSigner),
27}
28
29/// A handler for a block signer.
30#[derive(Debug)]
31pub enum BlockSignerHandler {
32    /// A local block signer that is used to sign blocks with a locally available private key.
33    Local(alloy_signer_local::PrivateKeySigner),
34    /// A remote block signer that is used to sign blocks with a remote private key.
35    Remote(RemoteSignerHandler),
36}
37
38/// Errors that can occur when starting a block signer.
39#[derive(Debug, thiserror::Error)]
40pub enum BlockSignerStartError {
41    /// An error that can occur when signing a block with a local signer.
42    #[error(transparent)]
43    Local(#[from] alloy_signer::Error),
44    /// An error that can occur when signing a block with a remote signer.
45    #[error(transparent)]
46    Remote(#[from] RemoteSignerStartError),
47}
48
49/// Errors that can occur when signing a block.
50#[derive(Debug, thiserror::Error)]
51pub enum BlockSignerError {
52    /// An error that can occur when signing a block with a local signer.
53    #[error(transparent)]
54    Local(#[from] alloy_signer::Error),
55    /// An error that can occur when signing a block with a remote signer.
56    #[error(transparent)]
57    Remote(#[from] RemoteSignerError),
58}
59
60impl BlockSigner {
61    /// Starts a block signer.
62    pub async fn start(self) -> Result<BlockSignerHandler, BlockSignerStartError> {
63        match self {
64            Self::Local(signer) => Ok(BlockSignerHandler::Local(signer)),
65            Self::Remote(signer) => Ok(BlockSignerHandler::Remote(signer.start().await?)),
66        }
67    }
68}
69
70impl BlockSignerHandler {
71    /// Signs a payload with the signer.
72    pub async fn sign_block(
73        &self,
74        payload_hash: PayloadHash,
75        chain_id: ChainId,
76        sender_address: Address,
77    ) -> Result<Signature, BlockSignerError> {
78        let signature = match self {
79            Self::Local(signer) => {
80                signer.sign_hash_sync(&payload_hash.signature_message(chain_id))?
81            }
82            Self::Remote(signer) => {
83                signer.sign_block_v1(payload_hash, chain_id, sender_address).await?
84            }
85        };
86
87        Ok(signature)
88    }
89}