nostr_double_ratchet/
ids.rs1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
5pub struct UnixSeconds(pub u64);
6
7#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub struct OwnerPubkey([u8; 32]);
9
10#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub struct DevicePubkey([u8; 32]);
12
13impl UnixSeconds {
14 pub fn get(self) -> u64 {
15 self.0
16 }
17}
18
19impl OwnerPubkey {
20 pub fn from_bytes(bytes: [u8; 32]) -> Self {
21 Self(bytes)
22 }
23
24 pub fn to_bytes(self) -> [u8; 32] {
25 self.0
26 }
27
28 pub fn to_hex(self) -> String {
29 hex::encode(self.0)
30 }
31
32 pub fn to_nostr(self) -> Result<nostr::PublicKey, crate::Error> {
33 nostr::PublicKey::from_slice(&self.0).map_err(|e| crate::Error::Parse(e.to_string()))
34 }
35}
36
37impl DevicePubkey {
38 pub fn from_bytes(bytes: [u8; 32]) -> Self {
39 Self(bytes)
40 }
41
42 pub fn from_secret_bytes(secret_key_bytes: [u8; 32]) -> Result<Self, crate::Error> {
43 let secret_key = nostr::SecretKey::from_slice(&secret_key_bytes)
44 .map_err(|e| crate::Error::Parse(e.to_string()))?;
45 let public_key = nostr::Keys::new(secret_key).public_key();
46 Ok(Self(public_key.to_bytes()))
47 }
48
49 pub fn to_bytes(self) -> [u8; 32] {
50 self.0
51 }
52
53 pub fn to_hex(self) -> String {
54 hex::encode(self.0)
55 }
56
57 pub(crate) fn from_nostr(pubkey: nostr::PublicKey) -> Self {
58 Self(pubkey.to_bytes())
59 }
60
61 pub fn to_nostr(self) -> Result<nostr::PublicKey, crate::Error> {
62 nostr::PublicKey::from_slice(&self.0).map_err(|e| crate::Error::Parse(e.to_string()))
63 }
64}
65
66impl fmt::Debug for OwnerPubkey {
67 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68 write!(f, "OwnerPubkey({})", hex::encode(self.0))
69 }
70}
71
72impl fmt::Debug for DevicePubkey {
73 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74 write!(f, "DevicePubkey({})", hex::encode(self.0))
75 }
76}
77
78impl fmt::Display for OwnerPubkey {
79 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80 f.write_str(&hex::encode(self.0))
81 }
82}
83
84impl fmt::Display for DevicePubkey {
85 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86 f.write_str(&hex::encode(self.0))
87 }
88}
89
90impl Serialize for OwnerPubkey {
91 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
92 where
93 S: serde::Serializer,
94 {
95 serializer.serialize_str(&hex::encode(self.0))
96 }
97}
98
99impl<'de> Deserialize<'de> for OwnerPubkey {
100 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
101 where
102 D: serde::Deserializer<'de>,
103 {
104 let value = String::deserialize(deserializer)?;
105 parse_hex_pubkey(&value)
106 .map(Self)
107 .map_err(serde::de::Error::custom)
108 }
109}
110
111impl Serialize for DevicePubkey {
112 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
113 where
114 S: serde::Serializer,
115 {
116 serializer.serialize_str(&hex::encode(self.0))
117 }
118}
119
120impl<'de> Deserialize<'de> for DevicePubkey {
121 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
122 where
123 D: serde::Deserializer<'de>,
124 {
125 let value = String::deserialize(deserializer)?;
126 parse_hex_pubkey(&value)
127 .map(Self)
128 .map_err(serde::de::Error::custom)
129 }
130}
131
132pub(crate) fn parse_hex_pubkey(value: &str) -> Result<[u8; 32], String> {
133 let bytes = hex::decode(value).map_err(|e| e.to_string())?;
134 <[u8; 32]>::try_from(bytes.as_slice()).map_err(|_| "expected 32-byte public key".to_string())
135}
136
137pub(crate) fn owner_pubkey_from_device_pubkey(device_pubkey: DevicePubkey) -> OwnerPubkey {
138 OwnerPubkey::from_bytes(device_pubkey.to_bytes())
139}