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
use crate::{Error, PersistentSafetyStorage};
use diem_crypto::{
ed25519::{Ed25519PrivateKey, Ed25519PublicKey, Ed25519Signature},
hash::CryptoHash,
};
use diem_global_constants::CONSENSUS_KEY;
use diem_types::{account_address::AccountAddress, validator_signer::ValidatorSigner};
use serde::Serialize;
pub enum ConfigurableValidatorSigner {
Signer(ValidatorSigner),
Handle(ValidatorHandle),
}
impl ConfigurableValidatorSigner {
pub fn new_signer(author: AccountAddress, consensus_key: Ed25519PrivateKey) -> Self {
let signer = ValidatorSigner::new(author, consensus_key);
ConfigurableValidatorSigner::Signer(signer)
}
pub fn new_handle(author: AccountAddress, key_version: Ed25519PublicKey) -> Self {
let handle = ValidatorHandle::new(author, key_version);
ConfigurableValidatorSigner::Handle(handle)
}
pub fn author(&self) -> AccountAddress {
match self {
ConfigurableValidatorSigner::Signer(signer) => signer.author(),
ConfigurableValidatorSigner::Handle(handle) => handle.author(),
}
}
pub fn public_key(&self) -> Ed25519PublicKey {
match self {
ConfigurableValidatorSigner::Signer(signer) => signer.public_key(),
ConfigurableValidatorSigner::Handle(handle) => handle.key_version(),
}
}
pub fn sign<T: Serialize + CryptoHash>(
&self,
message: &T,
storage: &PersistentSafetyStorage,
) -> Result<Ed25519Signature, Error> {
match self {
ConfigurableValidatorSigner::Signer(signer) => Ok(signer.sign(message)),
ConfigurableValidatorSigner::Handle(handle) => handle.sign(message, storage),
}
}
}
pub struct ValidatorHandle {
author: AccountAddress,
key_version: Ed25519PublicKey,
}
impl ValidatorHandle {
pub fn new(author: AccountAddress, key_version: Ed25519PublicKey) -> Self {
ValidatorHandle {
author,
key_version,
}
}
pub fn author(&self) -> AccountAddress {
self.author
}
pub fn key_version(&self) -> Ed25519PublicKey {
self.key_version.clone()
}
pub fn sign<T: Serialize + CryptoHash>(
&self,
message: &T,
storage: &PersistentSafetyStorage,
) -> Result<Ed25519Signature, Error> {
storage.sign(CONSENSUS_KEY.into(), self.key_version(), message)
}
}