Skip to content

Commit be94fc0

Browse files
committed
Impelment vrf hash generator to EngineSigner
1 parent eb00b95 commit be94fc0

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

core/src/account_provider.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use ckey::{public_to_address, Address, Error as KeyError, Generator, KeyPair, Pa
2525
use ckeystore::accounts_dir::MemoryDirectory;
2626
use ckeystore::{DecryptedAccount, Error as KeystoreError, KeyStore, SecretStore, SimpleSecretStore};
2727
use parking_lot::RwLock;
28+
use vrf::openssl::Error as VRFError;
2829

2930
/// Type of unlock.
3031
#[derive(Clone, PartialEq)]
@@ -56,6 +57,8 @@ pub enum Error {
5657
KeyError(KeyError),
5758
/// Keystore error.
5859
KeystoreError(KeystoreError),
60+
/// VRF error,
61+
VRFError(VRFError),
5962
}
6063

6164
impl From<KeyError> for Error {
@@ -70,13 +73,20 @@ impl From<KeystoreError> for Error {
7073
}
7174
}
7275

76+
impl From<VRFError> for Error {
77+
fn from(e: VRFError) -> Self {
78+
Error::VRFError(e)
79+
}
80+
}
81+
7382
impl fmt::Display for Error {
7483
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
7584
match self {
7685
Error::NotUnlocked => write!(f, "Account is locked"),
7786
Error::NotFound => write!(f, "Account does not exist"),
7887
Error::KeyError(e) => write!(f, "{}", e),
7988
Error::KeystoreError(e) => write!(f, "{}", e),
89+
Error::VRFError(e) => write!(f, "{}", e),
8090
}
8191
}
8292
}

core/src/consensus/signer.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use std::sync::Arc;
1919
use ckey::{Address, Public, SchnorrSignature, Signature};
2020
use ckeystore::DecryptedAccount;
2121
use primitives::H256;
22+
use vrf::openssl::ECVRF;
2223

2324
use crate::account_provider::{AccountProvider, Error as AccountProviderError};
2425

@@ -77,6 +78,19 @@ impl EngineSigner {
7778
Ok(result)
7879
}
7980

81+
/// Generate a vrf random hash.
82+
pub fn vrf_hash(&self, hash: H256, vrf_inst: &mut ECVRF) -> Result<Vec<u8>, AccountProviderError> {
83+
Ok(match &self.decrypted_account {
84+
Some(account) => account.vrf_hash(&hash, vrf_inst)?,
85+
None => {
86+
let address = self.signer.map(|(address, _)| address).unwrap_or_default();
87+
self.account_provider
88+
.get_unlocked_account(&address)
89+
.and_then(|account| account.vrf_hash(&hash, vrf_inst).map_err(From::from))?
90+
}
91+
})
92+
}
93+
8094
/// Sign a message hash with ECDSA.
8195
pub fn sign_ecdsa(&self, hash: H256) -> Result<Signature, AccountProviderError> {
8296
let address = self.signer.map(|(address, _public)| address).unwrap_or_else(Default::default);

0 commit comments

Comments
 (0)