1extern crate alloc;
6
7use crate::{pubkey::PubKeyError, seckey::SecKeyError, CurvePoint, PubKey, ScalarField, SecKey};
8use alloc::{string::String, vec::Vec};
9use core::fmt;
10use rand::{self, CryptoRng, RngCore};
11use thiserror::Error;
12
13#[derive(Error, Debug, Clone, PartialEq, Eq)]
15pub enum KeypairError {
16 #[error(transparent)]
18 SecretKey(#[from] SecKeyError),
19 #[error(transparent)]
21 PublicKey(#[from] PubKeyError),
22 #[error("point not on curve")]
24 NonCurvePoint,
25}
26pub type Result<T> = core::result::Result<T, KeypairError>;
28
29#[derive(Clone, PartialEq, Eq)]
31pub struct Keypair {
32 pub secret: SecKey,
34 pub public: PubKey,
36}
37
38impl Keypair {
39 pub fn from_parts_unsafe(secret: ScalarField, public: CurvePoint) -> Self {
42 Self {
43 secret: SecKey::new(secret),
44 public: PubKey::from_point_unsafe(public),
45 }
46 }
47
48 pub fn from_secret_key(secret_key: SecKey) -> Result<Self> {
50 let public = PubKey::from_secret_key(secret_key.clone())?;
51
52 Ok(Self::from_parts_unsafe(
54 secret_key.into_scalar(),
55 public.into_point(),
56 ))
57 }
58
59 pub fn rand(rng: &mut (impl RngCore + CryptoRng)) -> Result<Self> {
61 let sec_key: SecKey = SecKey::rand(rng);
62 Keypair::from_secret_key(sec_key)
63 }
64
65 pub fn from_bytes(secret_bytes: &[u8]) -> Result<Self> {
71 let secret = SecKey::from_bytes(secret_bytes)?;
72 Keypair::from_secret_key(secret)
73 }
74
75 pub fn from_hex(secret_hex: &str) -> Result<Self> {
81 let secret = SecKey::from_hex(secret_hex)?;
82 Keypair::from_secret_key(secret)
83 }
84
85 pub fn get_address(self) -> String {
87 self.public.into_address()
88 }
89
90 pub fn to_bytes(&self) -> Vec<u8> {
92 self.secret.to_bytes()
93 }
94
95 pub fn to_hex(&self) -> String {
97 hex::encode(self.to_bytes())
98 }
99}
100
101impl fmt::Debug for Keypair {
102 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103 write!(f, "{:?}", self.public)
105 }
106}
107
108impl fmt::Display for Keypair {
109 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110 write!(f, "{}", self.public)
112 }
113}