use crate::types::AccountId;
use near_crypto::{InMemorySigner, KeyType, PublicKey, Signature, Signer};
use std::fmt::Debug;
use std::path::Path;
use std::sync::Arc;
#[derive(Clone, Debug, PartialEq)]
pub enum ValidatorSigner {
Empty(EmptyValidatorSigner),
InMemory(InMemoryValidatorSigner),
}
impl ValidatorSigner {
pub fn validator_id(&self) -> &AccountId {
match self {
ValidatorSigner::Empty(signer) => signer.validator_id(),
ValidatorSigner::InMemory(signer) => signer.validator_id(),
}
}
pub fn public_key(&self) -> PublicKey {
match self {
ValidatorSigner::Empty(signer) => signer.public_key(),
ValidatorSigner::InMemory(signer) => signer.public_key(),
}
}
pub fn sign_bytes(&self, data: &[u8]) -> Signature {
match self {
ValidatorSigner::Empty(signer) => signer.noop_signature(),
ValidatorSigner::InMemory(signer) => signer.sign_bytes(data),
}
}
pub fn compute_vrf_with_proof(
&self,
data: &[u8],
) -> (near_crypto::vrf::Value, near_crypto::vrf::Proof) {
match self {
ValidatorSigner::Empty(_) => unimplemented!(),
ValidatorSigner::InMemory(signer) => signer.compute_vrf_with_proof(data),
}
}
pub fn write_to_file(&self, path: &Path) -> std::io::Result<()> {
match self {
ValidatorSigner::Empty(_) => unimplemented!(),
ValidatorSigner::InMemory(signer) => signer.write_to_file(path),
}
}
}
impl From<EmptyValidatorSigner> for ValidatorSigner {
fn from(signer: EmptyValidatorSigner) -> Self {
ValidatorSigner::Empty(signer)
}
}
#[derive(smart_default::SmartDefault, Clone, Debug, PartialEq)]
pub struct EmptyValidatorSigner {
#[default("test".parse().unwrap())]
account_id: AccountId,
}
impl EmptyValidatorSigner {
pub fn new(account_id: AccountId) -> ValidatorSigner {
ValidatorSigner::Empty(Self { account_id })
}
fn validator_id(&self) -> &AccountId {
&self.account_id
}
fn public_key(&self) -> PublicKey {
PublicKey::empty(KeyType::ED25519)
}
fn noop_signature(&self) -> Signature {
Signature::default()
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct InMemoryValidatorSigner {
account_id: AccountId,
signer: Arc<Signer>,
}
impl InMemoryValidatorSigner {
#[cfg(feature = "rand")]
pub fn from_random(account_id: AccountId, key_type: KeyType) -> ValidatorSigner {
let signer = Arc::new(InMemorySigner::from_random(account_id.clone(), key_type).into());
ValidatorSigner::InMemory(Self { account_id, signer })
}
#[cfg(feature = "rand")]
pub fn from_seed(account_id: AccountId, key_type: KeyType, seed: &str) -> ValidatorSigner {
let signer = Arc::new(InMemorySigner::from_seed(account_id.clone(), key_type, seed));
ValidatorSigner::InMemory(Self { account_id, signer })
}
pub fn public_key(&self) -> PublicKey {
self.signer.public_key()
}
pub fn from_signer(signer: Signer) -> ValidatorSigner {
ValidatorSigner::InMemory(Self {
account_id: signer.get_account_id(),
signer: Arc::new(signer),
})
}
pub fn from_file(path: &Path) -> std::io::Result<ValidatorSigner> {
let signer = InMemorySigner::from_file(path)?;
Ok(Self::from_signer(signer))
}
pub fn validator_id(&self) -> &AccountId {
&self.account_id
}
fn sign_bytes(&self, bytes: &[u8]) -> Signature {
self.signer.sign(bytes)
}
fn compute_vrf_with_proof(
&self,
data: &[u8],
) -> (near_crypto::vrf::Value, near_crypto::vrf::Proof) {
self.signer.compute_vrf_with_proof(data)
}
fn write_to_file(&self, path: &Path) -> std::io::Result<()> {
self.signer.write_to_file(path)
}
}