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