Use `Profile` in client

Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
Alexis Sellier 2022-09-23 20:21:21 +02:00
parent 51e18c27fc
commit 116eaa79e6
No known key found for this signature in database
4 changed files with 13 additions and 30 deletions

View File

@ -1,14 +1,12 @@
use std::net; use std::net;
use std::path::Path;
use crossbeam_channel as chan; use crossbeam_channel as chan;
use nakamoto_net::{LocalTime, Reactor}; use nakamoto_net::{LocalTime, Reactor};
use crate::clock::RefClock; use crate::clock::RefClock;
use crate::collections::HashMap; use crate::collections::HashMap;
use crate::crypto::Signer; use crate::profile::Profile;
use crate::service; use crate::service;
use crate::storage::git::Storage;
use crate::transport::Transport; use crate::transport::Transport;
use crate::wire::Wire; use crate::wire::Wire;
@ -45,10 +43,9 @@ impl Default for Config {
} }
} }
pub struct Client<R: Reactor, G: Signer> { pub struct Client<R: Reactor> {
reactor: R, reactor: R,
storage: Storage, profile: Profile,
signer: G,
handle: chan::Sender<service::Command>, handle: chan::Sender<service::Command>,
commands: chan::Receiver<service::Command>, commands: chan::Receiver<service::Command>,
@ -57,18 +54,16 @@ pub struct Client<R: Reactor, G: Signer> {
events: Events, events: Events,
} }
impl<R: Reactor, G: Signer> Client<R, G> { impl<R: Reactor> Client<R> {
pub fn new<P: AsRef<Path>>(path: P, signer: G) -> Result<Self, nakamoto_net::error::Error> { pub fn new(profile: Profile) -> Result<Self, nakamoto_net::error::Error> {
let (handle, commands) = chan::unbounded::<service::Command>(); let (handle, commands) = chan::unbounded::<service::Command>();
let (shutdown, shutdown_recv) = chan::bounded(1); let (shutdown, shutdown_recv) = chan::bounded(1);
let (listening_send, listening) = chan::bounded(1); let (listening_send, listening) = chan::bounded(1);
let reactor = R::new(shutdown_recv, listening_send)?; let reactor = R::new(shutdown_recv, listening_send)?;
let storage = Storage::open(path)?;
let events = Events {}; let events = Events {};
Ok(Self { Ok(Self {
storage, profile,
signer,
reactor, reactor,
handle, handle,
commands, commands,
@ -82,8 +77,8 @@ impl<R: Reactor, G: Signer> Client<R, G> {
let network = config.service.network; let network = config.service.network;
let rng = fastrand::Rng::new(); let rng = fastrand::Rng::new();
let time = LocalTime::now(); let time = LocalTime::now();
let storage = self.storage; let storage = self.profile.storage;
let signer = self.signer; let signer = self.profile.signer;
let addresses = HashMap::with_hasher(rng.clone().into()); let addresses = HashMap::with_hasher(rng.clone().into());
log::info!("Initializing client ({:?})..", network); log::info!("Initializing client ({:?})..", network);

View File

@ -13,7 +13,7 @@ pub mod transport;
pub mod wire; pub mod wire;
pub use nakamoto_net::{Io, Link, LocalDuration, LocalTime}; pub use nakamoto_net::{Io, Link, LocalDuration, LocalTime};
pub use radicle::{collections, crypto, git, hash, identity, rad, storage}; pub use radicle::{collections, crypto, git, hash, identity, profile, rad, storage};
pub mod prelude { pub mod prelude {
pub use crate::clock::Timestamp; pub use crate::clock::Timestamp;

View File

@ -1,27 +1,13 @@
use std::path::Path;
use std::thread; use std::thread;
use std::{env, net}; use std::{env, net};
use radicle_node::crypto::{PublicKey, Signature, Signer};
use radicle_node::{client, control}; use radicle_node::{client, control};
type Reactor = nakamoto_net_poll::Reactor<net::TcpStream>; type Reactor = nakamoto_net_poll::Reactor<net::TcpStream>;
struct FailingSigner {}
impl Signer for FailingSigner {
fn public_key(&self) -> &PublicKey {
panic!("Failing signer always fails!");
}
fn sign(&self, _msg: &[u8]) -> Signature {
panic!("Failing signer always fails!");
}
}
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let signer = FailingSigner {}; let profile = radicle::Profile::load()?;
let client = client::Client::<Reactor, _>::new(Path::new("."), signer)?; let client = client::Client::<Reactor>::new(profile)?;
let handle = client.handle(); let handle = client.handle();
let config = client::Config::default(); let config = client::Config::default();
let socket = env::var("RAD_SOCKET").unwrap_or_else(|_| control::DEFAULT_SOCKET_NAME.to_owned()); let socket = env::var("RAD_SOCKET").unwrap_or_else(|_| control::DEFAULT_SOCKET_NAME.to_owned());

View File

@ -5,6 +5,7 @@ use crate::crypto::{KeyPair, PublicKey, SecretKey, Signature, Signer};
use crate::keystore::UnsafeKeystore; use crate::keystore::UnsafeKeystore;
use crate::storage::git::Storage; use crate::storage::git::Storage;
#[derive(Debug)]
pub struct UnsafeSigner { pub struct UnsafeSigner {
public: PublicKey, public: PublicKey,
secret: SecretKey, secret: SecretKey,
@ -20,6 +21,7 @@ impl Signer for UnsafeSigner {
} }
} }
#[derive(Debug)]
pub struct Profile { pub struct Profile {
pub home: PathBuf, pub home: PathBuf,
pub signer: UnsafeSigner, pub signer: UnsafeSigner,