2019-02-14 01:09:18 +00:00
|
|
|
use super::{PublicKey, SecretKey};
|
2019-03-30 03:27:37 +00:00
|
|
|
use std::fmt;
|
2019-03-28 04:50:57 +00:00
|
|
|
use std::hash::{Hash, Hasher};
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2020-05-19 01:23:08 +00:00
|
|
|
#[derive(Clone)]
|
2019-02-14 01:09:18 +00:00
|
|
|
pub struct Keypair {
|
|
|
|
pub sk: SecretKey,
|
|
|
|
pub pk: PublicKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Keypair {
|
|
|
|
/// Instantiate a Keypair using SecretKey::random().
|
|
|
|
pub fn random() -> Self {
|
|
|
|
let sk = SecretKey::random();
|
|
|
|
let pk = PublicKey::from_secret_key(&sk);
|
|
|
|
Keypair { sk, pk }
|
|
|
|
}
|
2019-03-12 10:56:45 +00:00
|
|
|
|
|
|
|
pub fn identifier(&self) -> String {
|
|
|
|
self.pk.concatenated_hex_id()
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
2019-03-28 04:50:57 +00:00
|
|
|
|
2019-11-10 23:28:22 +00:00
|
|
|
#[allow(clippy::derive_hash_xor_eq)]
|
2019-03-28 04:50:57 +00:00
|
|
|
impl Hash for Keypair {
|
|
|
|
/// Note: this is distinct from consensus serialization, it will produce a different hash.
|
|
|
|
///
|
|
|
|
/// This method uses the uncompressed bytes, which are much faster to obtain than the
|
|
|
|
/// compressed bytes required for consensus serialization.
|
|
|
|
///
|
|
|
|
/// Use `ssz::Encode` to obtain the bytes required for consensus hashing.
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
self.pk.as_uncompressed_bytes().hash(state)
|
|
|
|
}
|
|
|
|
}
|
2019-03-30 03:27:37 +00:00
|
|
|
|
|
|
|
impl fmt::Display for Keypair {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.pk)
|
|
|
|
}
|
|
|
|
}
|