Skip to main content

dpp/identity/
signer.rs

1use crate::address_funds::AddressWitness;
2use crate::ProtocolError;
3use async_trait::async_trait;
4use platform_value::BinaryData;
5use std::fmt::Debug;
6use std::sync::Arc;
7
8// `#[async_trait]` is required: `Signer` is used dyn — stored as
9// `Arc<dyn Signer<_>>` in `VTableSigner` (rs-sdk-ffi/src/signer.rs).
10#[async_trait]
11pub trait Signer<K>: Send + Sync + Debug {
12    /// the public key bytes are only used to look up the private key
13    async fn sign(&self, key: &K, data: &[u8]) -> Result<BinaryData, ProtocolError>;
14
15    /// the public key bytes are only used to look up the private key
16    async fn sign_create_witness(
17        &self,
18        key: &K,
19        data: &[u8],
20    ) -> Result<AddressWitness, ProtocolError>;
21
22    /// do we have this identity public key in the signer?
23    fn can_sign_with(&self, key: &K) -> bool;
24}
25
26#[async_trait]
27impl<K, S> Signer<K> for Arc<S>
28where
29    K: Send + Sync,
30    S: Signer<K> + ?Sized + Send + Sync,
31{
32    async fn sign(&self, key: &K, data: &[u8]) -> Result<BinaryData, ProtocolError> {
33        (**self).sign(key, data).await
34    }
35
36    async fn sign_create_witness(
37        &self,
38        key: &K,
39        data: &[u8],
40    ) -> Result<AddressWitness, ProtocolError> {
41        (**self).sign_create_witness(key, data).await
42    }
43
44    fn can_sign_with(&self, key: &K) -> bool {
45        (**self).can_sign_with(key)
46    }
47}