utility_crypto/
signer.rs

1use crate::key_conversion::convert_secret_key;
2use crate::key_file::KeyFile;
3use crate::{KeyType, PublicKey, SecretKey, Signature};
4use near_account_id::AccountId;
5use std::io;
6use std::path::Path;
7use std::sync::Arc;
8
9/// Generic signer trait, that can sign with some subset of supported curves.
10pub trait Signer: Sync + Send {
11    fn public_key(&self) -> PublicKey;
12    fn sign(&self, data: &[u8]) -> Signature;
13
14    fn verify(&self, data: &[u8], signature: &Signature) -> bool {
15        signature.verify(data, &self.public_key())
16    }
17
18    fn compute_vrf_with_proof(&self, _data: &[u8]) -> (crate::vrf::Value, crate::vrf::Proof);
19
20    /// Used by test infrastructure, only implement if make sense for testing otherwise raise `unimplemented`.
21    fn write_to_file(&self, _path: &Path) -> io::Result<()> {
22        unimplemented!();
23    }
24}
25
26// Signer that returns empty signature. Used for transaction testing.
27pub struct EmptySigner {}
28
29impl Signer for EmptySigner {
30    fn public_key(&self) -> PublicKey {
31        PublicKey::empty(KeyType::ED25519)
32    }
33
34    fn sign(&self, _data: &[u8]) -> Signature {
35        Signature::empty(KeyType::ED25519)
36    }
37
38    fn compute_vrf_with_proof(&self, _data: &[u8]) -> (crate::vrf::Value, crate::vrf::Proof) {
39        unimplemented!()
40    }
41}
42
43/// Signer that keeps secret key in memory.
44#[derive(Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
45pub struct InMemorySigner {
46    pub account_id: AccountId,
47    pub public_key: PublicKey,
48    pub secret_key: SecretKey,
49}
50
51impl InMemorySigner {
52    pub fn from_seed(account_id: AccountId, key_type: KeyType, seed: &str) -> Self {
53        let secret_key = SecretKey::from_seed(key_type, seed);
54        Self { account_id, public_key: secret_key.public_key(), secret_key }
55    }
56
57    pub fn from_secret_key(account_id: AccountId, secret_key: SecretKey) -> Self {
58        Self { account_id, public_key: secret_key.public_key(), secret_key }
59    }
60
61    pub fn from_file(path: &Path) -> io::Result<Self> {
62        KeyFile::from_file(path).map(Self::from)
63    }
64}
65
66impl Signer for InMemorySigner {
67    fn public_key(&self) -> PublicKey {
68        self.public_key.clone()
69    }
70
71    fn sign(&self, data: &[u8]) -> Signature {
72        self.secret_key.sign(data)
73    }
74
75    fn compute_vrf_with_proof(&self, data: &[u8]) -> (crate::vrf::Value, crate::vrf::Proof) {
76        let secret_key = convert_secret_key(self.secret_key.unwrap_as_ed25519());
77        secret_key.compute_vrf_with_proof(&data)
78    }
79
80    fn write_to_file(&self, path: &Path) -> io::Result<()> {
81        KeyFile::from(self).write_to_file(path)
82    }
83}
84
85impl From<KeyFile> for InMemorySigner {
86    fn from(key_file: KeyFile) -> Self {
87        Self {
88            account_id: key_file.account_id,
89            public_key: key_file.public_key,
90            secret_key: key_file.secret_key,
91        }
92    }
93}
94
95impl From<&InMemorySigner> for KeyFile {
96    fn from(signer: &InMemorySigner) -> KeyFile {
97        KeyFile {
98            account_id: signer.account_id.clone(),
99            public_key: signer.public_key.clone(),
100            secret_key: signer.secret_key.clone(),
101        }
102    }
103}
104
105impl From<Arc<InMemorySigner>> for KeyFile {
106    fn from(signer: Arc<InMemorySigner>) -> KeyFile {
107        KeyFile {
108            account_id: signer.account_id.clone(),
109            public_key: signer.public_key.clone(),
110            secret_key: signer.secret_key.clone(),
111        }
112    }
113}