pub struct LocalHost { /* private fields */ }Expand description
LocalHost is a host for which this instance of the program is responsible.
It holds the private key for the host, which should never be transmitted over the wire to any other peer.
It is serializable because you may want to re-use a host after a program restart, to inform other peers that yes, you are still the same host, after all.
Implementations§
source§impl LocalHost
impl LocalHost
sourcepub fn new(options: Option<LocalHostOptions>) -> Result<LocalHost, ParseError>
pub fn new(options: Option<LocalHostOptions>) -> Result<LocalHost, ParseError>
Create a new local host.
Parameters typically come from a UI or config, user specifying what is the name of the host and how it can be reached.
Examples
use confitul::LocalHost;
let lh = LocalHost::new(None).unwrap();
print!("{}", lh);sourcepub fn sign_msg(&self, msg: &[u8]) -> Vec<u8> ⓘ
pub fn sign_msg(&self, msg: &[u8]) -> Vec<u8> ⓘ
Sign a message.
Anything sent over the wire should be signed. Only local hosts can sign, by design, as the only host you can trust for this is yourself.
Examples
use confitul::LocalHost;
use confitul::Host;
let local_host = LocalHost::new(None).unwrap();
let msg = "a message".as_bytes();
let sig = local_host.sign_msg(msg);
assert!(matches!(local_host.verify_msg(msg, &sig), Ok(())));
assert!(matches!(local_host.verify_msg(msg, msg), Err(_)));sourcepub fn update_name(&mut self, name: &str)
pub fn update_name(&mut self, name: &str)
Update local host name.
A special call is needed for this as the signature depends on the name, so this function ensure that the signature is updated after the name is modified.
Examples
use confitul::LocalHost;
use confitul::Host;
let mut local_host = LocalHost::new(None).unwrap();
local_host.update_name("another test");sourcepub fn update_description(&mut self, description: &str)
pub fn update_description(&mut self, description: &str)
Update local host description.
A special call is needed for this as the signature depends on the description, so this function ensure that the signature is updated after the description is modified.
Examples
use confitul::LocalHost;
use confitul::Host;
let mut local_host = LocalHost::new(None).unwrap();
local_host.update_description("another test");sourcepub fn update_urls(&mut self, urls: &Vec<String>) -> Result<(), ParseError>
pub fn update_urls(&mut self, urls: &Vec<String>) -> Result<(), ParseError>
Update local host URLs.
A special call is needed for this as the signature depends on the URLs, so this function ensure that the signature is updated after the URLs are modified.
Examples
use confitul::LocalHost;
use confitul::Host;
use url::Url;
let mut local_host = LocalHost::new(None).unwrap();
local_host.update_urls(&vec![String::from("https://a-location"), String::from("https://another-location")]).unwrap();Examples found in repository?
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
pub fn new(options: Option<LocalHostOptions>) -> Result<LocalHost, ParseError> {
let mut csprng = OsRng {};
let keypair: Keypair = Keypair::generate(&mut csprng);
let real_options = options.unwrap_or(LocalHostOptions::default());
let mut local_host = LocalHost {
info: HostInfo {
id: HostId::new(&keypair),
name: real_options.name,
description: real_options.description,
urls: Vec::new(),
sig: None,
},
keypair: keypair,
};
local_host.update_urls(&real_options.urls)?; // update_urls will update the sig
Ok(local_host)
}