-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcontact.rs
174 lines (147 loc) · 5.16 KB
/
contact.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use std::error::Error;
use eyre::{eyre, OptionExt};
use frostd::PublicKey;
use serde::{Deserialize, Serialize};
use crate::{args::Command, config::Config};
/// A FROST contact, which critically has the public key required to
/// send and receive encrypted and authenticated messages to them.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Contact {
/// Format version. Only 0 supported for now. It is an Option since
/// we don't want the version when writing it to the config file.
pub version: Option<u8>,
/// Name of the contact.
pub name: String,
/// Public key of the contact.
pub pubkey: PublicKey,
}
impl Contact {
/// Returns a human-readable summary of the contact; used when it is
/// printed to the terminal.
pub fn as_human_readable_summary(&self) -> String {
format!(
"Name: {}\nPublic Key: {}\n",
self.name,
hex::encode(&self.pubkey.0)
)
}
/// Returns the contact encoded as a text string, with Bech32.
pub fn as_text(&self) -> Result<String, Box<dyn Error>> {
let bytes = postcard::to_allocvec(self)?;
let hrp = bech32::Hrp::parse("zffrost").expect("valid hrp");
Ok(bech32::encode::<bech32::Bech32m>(hrp, &bytes)?)
}
/// Creates a Contact from the given encoded text string.
pub fn from_text(s: &str) -> Result<Self, Box<dyn Error>> {
let (hrp, bytes) = bech32::decode(s)?;
if hrp.as_str() != "zffrost" {
return Err(eyre!("invalid contact format").into());
}
let contact: Contact = postcard::from_bytes(&bytes)?;
if contact.version != Some(0) {
return Err(eyre!("invalid contact version").into());
}
Ok(contact)
}
}
/// Import a contact into the user's address book, in the config file.
pub(crate) fn import(args: &Command) -> Result<(), Box<dyn Error>> {
let Command::Import {
contact: text_contact,
config,
} = (*args).clone()
else {
panic!("invalid Command");
};
let mut config = Config::read(config)?;
let mut contact = Contact::from_text(&text_contact)?;
if config.contact.contains_key(&contact.name) {
return Err(eyre!(
"contact with name {} already exists. Either remove the existing \
one, or ask the sender to change their display name when exporting",
&contact.name
)
.into());
}
if config.contact.values().any(|c| c.pubkey == contact.pubkey) {
return Err(eyre!(
"pubkey {} already registered for {}",
hex::encode(&contact.pubkey.0),
&contact.name,
)
.into());
}
if config.communication_key.as_ref().map(|c| &c.pubkey) == Some(&contact.pubkey) {
return Err(eyre!(
"pubkey {} already registered for yourself",
hex::encode(&contact.pubkey.0)
)
.into());
}
// We don't want the version when writing to the config file.
contact.version = None;
config.contact.insert(contact.name.clone(), contact.clone());
eprintln!("Imported this contact:");
eprint!("{}", contact.as_human_readable_summary());
config.write()?;
Ok(())
}
/// Export a contact from the user's address book in the config file.
pub(crate) fn export(args: &Command) -> Result<(), Box<dyn Error>> {
let Command::Export { name, config } = (*args).clone() else {
panic!("invalid Command");
};
let config = Config::read(config)?;
// Build the contact to export.
let contact = Contact {
version: Some(0),
name,
pubkey: config
.communication_key
.ok_or(eyre!("pubkey not generated yet"))?
.pubkey,
};
eprintln!("Exporting this information:");
eprint!("{}", contact.as_human_readable_summary());
eprintln!(
"Check if contains the expected information. If it does, copy the following \
contact string and send to other participants you want to use FROST with:"
);
eprintln!("{}", contact.as_text()?);
Ok(())
}
/// List the contacts in the address book in the config file.
pub(crate) fn list(args: &Command) -> Result<(), Box<dyn Error>> {
let Command::Contacts { config } = (*args).clone() else {
panic!("invalid Command");
};
let config = Config::read(config)?;
for contact in config.contact.values() {
eprint!("{}", contact.as_human_readable_summary());
eprintln!("{}", contact.as_text()?);
eprintln!();
}
Ok(())
}
/// Remove a contact from the user's address book in the config file.
pub(crate) fn remove(args: &Command) -> Result<(), Box<dyn Error>> {
let Command::RemoveContact { config, pubkey } = (*args).clone() else {
panic!("invalid Command");
};
let mut config = Config::read(config)?;
let name = config
.contact
.iter()
.find_map(|(name, c)| {
if hex::encode(&c.pubkey.0) == pubkey {
Some(name.clone())
} else {
None
}
})
.clone()
.ok_or_eyre("contact not found")?;
config.contact.remove(&name);
config.write()?;
Ok(())
}