From a38a0babea989e979cb2bf7bdcb05c6c7aa0dfe9 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 7 Dec 2021 15:10:54 +0100 Subject: Add support for storing other known devices in Profile Signed-off-by: Matthias Beyer --- lib/src/profile/device.rs | 31 +++++++++++++++++++++++++++++++ lib/src/profile/mod.rs | 1 + lib/src/profile/state.rs | 25 +++++++++++++++++++++++-- 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 lib/src/profile/device.rs diff --git a/lib/src/profile/device.rs b/lib/src/profile/device.rs new file mode 100644 index 0000000..daeab21 --- /dev/null +++ b/lib/src/profile/device.rs @@ -0,0 +1,31 @@ +use std::convert::TryFrom; +use std::convert::TryInto; +use anyhow::Result; + +#[derive(Clone, Debug)] +pub struct Device { + device_id: libp2p::identity::ed25519::PublicKey, +} + +#[derive(Debug, serde::Serialize, serde::Deserialize)] +pub struct DeviceSaveable { + device_id: Vec, +} + +impl TryFrom for DeviceSaveable { + type Error = anyhow::Error; + + fn try_from(device: Device) -> Result { + Ok(DeviceSaveable { device_id: device.device_id.encode().to_vec() }) + } +} + +impl TryInto for DeviceSaveable { + type Error = anyhow::Error; + + fn try_into(self) -> Result { + libp2p::identity::ed25519::PublicKey::decode(&self.device_id) + .map(|device_id| Device { device_id }) + .map_err(anyhow::Error::from) + } +} diff --git a/lib/src/profile/mod.rs b/lib/src/profile/mod.rs index 050d53d..d3e0712 100644 --- a/lib/src/profile/mod.rs +++ b/lib/src/profile/mod.rs @@ -8,6 +8,7 @@ use crate::client::Client; use crate::config::Config; use crate::ipfs_client::IpfsClient; +mod device; mod state; use state::*; diff --git a/lib/src/profile/state.rs b/lib/src/profile/state.rs index 33f0bd6..921a2bf 100644 --- a/lib/src/profile/state.rs +++ b/lib/src/profile/state.rs @@ -6,6 +6,9 @@ use anyhow::Context; use anyhow::Result; use tokio::io::AsyncWriteExt; +use crate::profile::device::Device; +use crate::profile::device::DeviceSaveable; + #[derive(Debug)] pub struct StateDir(PathBuf); @@ -39,6 +42,9 @@ pub struct ProfileState { #[getset(get = "pub")] keypair: libp2p::identity::Keypair, + + #[getset(get = "pub")] + other_devices: Vec, } impl ProfileState { @@ -46,7 +52,8 @@ impl ProfileState { Self { profile_head: None, profile_name, - keypair + keypair, + other_devices: vec![], } } @@ -67,6 +74,7 @@ pub(super) struct ProfileStateSaveable { profile_head: Option>, profile_name: String, keypair: Vec, + other_devices: Vec } impl ProfileStateSaveable { @@ -77,7 +85,14 @@ impl ProfileStateSaveable { keypair: match s.keypair { libp2p::identity::Keypair::Ed25519(ref kp) => Vec::from(kp.encode()), _ => anyhow::bail!("Only keypair type ed25519 supported"), - } + }, + other_devices: { + s.other_devices + .iter() + .cloned() + .map(DeviceSaveable::try_from) + .collect::>>()? + }, }) } @@ -127,6 +142,12 @@ impl TryInto for ProfileStateSaveable { let kp = libp2p::identity::ed25519::Keypair::decode(&mut self.keypair)?; libp2p::identity::Keypair::Ed25519(kp) }, + other_devices: { + self.other_devices + .into_iter() + .map(DeviceSaveable::try_into) + .collect::>>()? + }, }) } } -- cgit v1.2.3