summaryrefslogtreecommitdiffstats
path: root/lib/src/profile/device.rs
blob: daeab21b0c8fcb9f45ea1235d576ecfe96bafdf3 (plain)
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
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)
    }
}