-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.rs
41 lines (35 loc) · 1.34 KB
/
key.rs
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
//! Module provides cryptographic utilities and types related to
//! the user keys.
//!
use crate::chain_crypto as crypto;
use crate::chain_crypto::SecretKey;
use rand_core::{CryptoRng, RngCore};
#[derive(Clone)]
pub enum EitherEd25519SecretKey {
Extended(crypto::SecretKey<crypto::Ed25519Extended>),
Normal(crypto::SecretKey<crypto::Ed25519>),
}
impl EitherEd25519SecretKey {
pub fn generate<R: RngCore + CryptoRng>(rng: R) -> Self {
EitherEd25519SecretKey::Extended(SecretKey::generate(rng))
}
pub fn to_public(&self) -> crypto::PublicKey<crypto::Ed25519> {
match self {
EitherEd25519SecretKey::Extended(sk) => sk.to_public(),
EitherEd25519SecretKey::Normal(sk) => sk.to_public(),
}
}
pub fn sign<T: AsRef<[u8]>>(&self, dat: &T) -> crypto::Signature<T, crypto::Ed25519> {
match self {
EitherEd25519SecretKey::Extended(sk) => sk.sign(dat),
EitherEd25519SecretKey::Normal(sk) => sk.sign(dat),
}
}
pub fn sign_slice<T: ?Sized>(&self, dat: &[u8]) -> crypto::Signature<T, crypto::Ed25519> {
match self {
EitherEd25519SecretKey::Extended(sk) => sk.sign_slice(dat),
EitherEd25519SecretKey::Normal(sk) => sk.sign_slice(dat),
}
}
}
pub type Ed25519Signature<T> = crypto::Signature<T, crypto::Ed25519>;