diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-12-07 15:10:54 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-12-10 15:45:03 +0100 |
commit | a38a0babea989e979cb2bf7bdcb05c6c7aa0dfe9 (patch) | |
tree | 2086b5f362f6e69f9630a70c249e1366e9dc2c4e | |
parent | aa5c1f79dcdfae35f10835ea43bac6f451d7149d (diff) |
Add support for storing other known devices in Profile
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | lib/src/profile/device.rs | 31 | ||||
-rw-r--r-- | lib/src/profile/mod.rs | 1 | ||||
-rw-r--r-- | lib/src/profile/state.rs | 25 |
3 files changed, 55 insertions, 2 deletions
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<u8>, +} + +impl TryFrom<Device> for DeviceSaveable { + type Error = anyhow::Error; + + fn try_from(device: Device) -> Result<Self> { + Ok(DeviceSaveable { device_id: device.device_id.encode().to_vec() }) + } +} + +impl TryInto<Device> for DeviceSaveable { + type Error = anyhow::Error; + + fn try_into(self) -> Result<Device> { + 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<Device>, } 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<Vec<u8>>, profile_name: String, keypair: Vec<u8>, + other_devices: Vec<DeviceSaveable> } 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::<Result<Vec<_>>>()? + }, }) } @@ -127,6 +142,12 @@ impl TryInto<ProfileState> 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::<Result<Vec<_>>>()? + }, }) } } |