keypair.rs (3767B)
1 use nostr::nips::nip49::EncryptedSecretKey; 2 use serde::Deserialize; 3 use serde::Serialize; 4 5 use crate::Pubkey; 6 use crate::SecretKey; 7 8 #[derive(Debug, Eq, PartialEq, Clone)] 9 pub struct Keypair { 10 pub pubkey: Pubkey, 11 pub secret_key: Option<SecretKey>, 12 } 13 14 impl Keypair { 15 pub fn from_secret(secret_key: SecretKey) -> Self { 16 let cloned_secret_key = secret_key.clone(); 17 let nostr_keys = nostr::Keys::new(secret_key); 18 Keypair { 19 pubkey: Pubkey::new(nostr_keys.public_key().to_bytes()), 20 secret_key: Some(cloned_secret_key), 21 } 22 } 23 24 pub fn new(pubkey: Pubkey, secret_key: Option<SecretKey>) -> Self { 25 Keypair { pubkey, secret_key } 26 } 27 28 pub fn only_pubkey(pubkey: Pubkey) -> Self { 29 Keypair { 30 pubkey, 31 secret_key: None, 32 } 33 } 34 35 pub fn to_full(&self) -> Option<FilledKeypair<'_>> { 36 self.secret_key.as_ref().map(|secret_key| FilledKeypair { 37 pubkey: &self.pubkey, 38 secret_key, 39 }) 40 } 41 } 42 43 #[derive(Debug, Eq, PartialEq, Clone)] 44 pub struct FullKeypair { 45 pub pubkey: Pubkey, 46 pub secret_key: SecretKey, 47 } 48 49 #[derive(Debug, Eq, PartialEq, Clone, Copy)] 50 pub struct FilledKeypair<'a> { 51 pub pubkey: &'a Pubkey, 52 pub secret_key: &'a SecretKey, 53 } 54 55 impl<'a> FilledKeypair<'a> { 56 pub fn new(pubkey: &'a Pubkey, secret_key: &'a SecretKey) -> Self { 57 FilledKeypair { pubkey, secret_key } 58 } 59 60 pub fn to_full(&self) -> FullKeypair { 61 FullKeypair { 62 pubkey: self.pubkey.to_owned(), 63 secret_key: self.secret_key.to_owned(), 64 } 65 } 66 } 67 68 impl FullKeypair { 69 pub fn new(pubkey: Pubkey, secret_key: SecretKey) -> Self { 70 FullKeypair { pubkey, secret_key } 71 } 72 73 pub fn to_filled(&self) -> FilledKeypair<'_> { 74 FilledKeypair::new(&self.pubkey, &self.secret_key) 75 } 76 77 pub fn generate() -> Self { 78 let mut rng = nostr::secp256k1::rand::rngs::OsRng; 79 let (secret_key, _) = &nostr::SECP256K1.generate_keypair(&mut rng); 80 let (xopk, _) = secret_key.x_only_public_key(&nostr::SECP256K1); 81 let secret_key = nostr::SecretKey::from(*secret_key); 82 FullKeypair { 83 pubkey: Pubkey::new(xopk.serialize()), 84 secret_key, 85 } 86 } 87 88 pub fn to_keypair(self) -> Keypair { 89 Keypair { 90 pubkey: self.pubkey, 91 secret_key: Some(self.secret_key), 92 } 93 } 94 } 95 96 impl std::fmt::Display for Keypair { 97 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 98 write!( 99 f, 100 "Keypair:\n\tpublic: {}\n\tsecret: {}", 101 self.pubkey, 102 match self.secret_key { 103 Some(_) => "Some(<hidden>)", 104 None => "None", 105 } 106 ) 107 } 108 } 109 110 impl std::fmt::Display for FullKeypair { 111 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 112 write!(f, "Keypair:\n\tpublic: {}\n\tsecret: <hidden>", self.pubkey) 113 } 114 } 115 116 #[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] 117 pub struct SerializableKeypair { 118 pub pubkey: Pubkey, 119 pub encrypted_secret_key: Option<EncryptedSecretKey>, 120 } 121 122 impl SerializableKeypair { 123 pub fn from_keypair(kp: &Keypair, pass: &str, log_n: u8) -> Self { 124 Self { 125 pubkey: kp.pubkey, 126 encrypted_secret_key: kp.secret_key.clone().and_then(|s| { 127 EncryptedSecretKey::new(&s, pass, log_n, nostr::nips::nip49::KeySecurity::Weak).ok() 128 }), 129 } 130 } 131 132 pub fn to_keypair(&self, pass: &str) -> Keypair { 133 Keypair::new( 134 self.pubkey, 135 self.encrypted_secret_key 136 .and_then(|e| e.to_secret_key(pass).ok()), 137 ) 138 } 139 }