mirror of
https://github.com/Blah-IM/blahrs.git
synced 2025-05-01 00:31:09 +00:00
feat(types): impl Debug,From<{&,}VerifyingKey> for PubKey
This commit is contained in:
parent
b93ff2882e
commit
dd940d5769
4 changed files with 33 additions and 15 deletions
|
@ -61,7 +61,7 @@ pub struct UserKey {
|
|||
pub act_key: PubKey,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct PubKey(#[serde(with = "hex::serde")] pub [u8; PUBLIC_KEY_LENGTH]);
|
||||
|
||||
|
@ -73,6 +73,12 @@ impl FromStr for PubKey {
|
|||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for PubKey {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_tuple("PubKey").field(&self.to_string()).finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for PubKey {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let mut buf = [0u8; PUBLIC_KEY_LENGTH * 2];
|
||||
|
@ -81,6 +87,18 @@ impl fmt::Display for PubKey {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<VerifyingKey> for PubKey {
|
||||
fn from(vk: VerifyingKey) -> Self {
|
||||
Self(vk.to_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&VerifyingKey> for PubKey {
|
||||
fn from(vk: &VerifyingKey) -> Self {
|
||||
Self(vk.to_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Signed<T> {
|
||||
|
@ -157,7 +175,7 @@ impl<T: Serialize> Signed<T> {
|
|||
payload,
|
||||
timestamp,
|
||||
user: UserKey {
|
||||
act_key: PubKey(act_key.verifying_key().to_bytes()),
|
||||
act_key: act_key.verifying_key().into(),
|
||||
id_key: id_key.clone(),
|
||||
},
|
||||
};
|
||||
|
@ -578,7 +596,7 @@ mod sql_impl {
|
|||
let rawkey = <[u8; PUBLIC_KEY_LENGTH]>::column_result(value)?;
|
||||
let key = VerifyingKey::from_bytes(&rawkey)
|
||||
.map_err(|err| FromSqlError::Other(format!("invalid pubkey: {err}").into()))?;
|
||||
Ok(PubKey(key.to_bytes()))
|
||||
Ok(key.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -635,7 +653,7 @@ mod tests {
|
|||
room: Id(42),
|
||||
}
|
||||
.sign_msg_with(
|
||||
&PubKey(id_key.verifying_key().to_bytes()),
|
||||
&id_key.verifying_key().into(),
|
||||
&act_key,
|
||||
timestamp,
|
||||
&mut fake_rng,
|
||||
|
|
|
@ -268,7 +268,7 @@ fn userkey_parser(s: &str) -> clap::error::Result<VerifyingKey> {
|
|||
impl User {
|
||||
async fn fetch_key(&self) -> Result<PubKey> {
|
||||
let rawkey = if let Some(key) = &self.key {
|
||||
return Ok(PubKey(key.to_bytes()));
|
||||
return Ok(key.into());
|
||||
} else if let Some(path) = &self.public_key_file {
|
||||
fs::read_to_string(path).context("failed to read key file")?
|
||||
} else if let Some(url) = &self.url {
|
||||
|
@ -336,7 +336,7 @@ fn main_id(cmd: IdCommand) -> Result<()> {
|
|||
id_url,
|
||||
} => {
|
||||
let id_key_priv = SigningKey::generate(&mut thread_rng());
|
||||
let id_key = PubKey(id_key_priv.verifying_key().to_bytes());
|
||||
let id_key = PubKey::from(id_key_priv.verifying_key());
|
||||
|
||||
let act_key_desc = UserActKeyDesc {
|
||||
act_key: id_key.clone(),
|
||||
|
@ -378,7 +378,7 @@ fn main_id(cmd: IdCommand) -> Result<()> {
|
|||
let mut id_desc = serde_json::from_str::<UserIdentityDesc>(&id_desc)
|
||||
.context("failed to parse desc_file")?;
|
||||
let id_key_priv = load_signing_key(&id_key_file)?;
|
||||
let id_key = PubKey(id_key_priv.verifying_key().to_bytes());
|
||||
let id_key = id_key_priv.verifying_key().into();
|
||||
// TODO: Dedup this check.
|
||||
ensure!(id_key == id_desc.id_key, "id_key mismatch with key file");
|
||||
ensure!(
|
||||
|
@ -416,7 +416,7 @@ fn main_id(cmd: IdCommand) -> Result<()> {
|
|||
let mut id_desc = serde_json::from_str::<UserIdentityDesc>(&id_desc)
|
||||
.context("failed to parse desc_file")?;
|
||||
let id_key_priv = load_signing_key(&id_key_file)?;
|
||||
let id_key = PubKey(id_key_priv.verifying_key().to_bytes());
|
||||
let id_key = id_key_priv.verifying_key().into();
|
||||
ensure!(id_key == id_desc.id_key, "id_key mismatch with key file");
|
||||
let exists = id_desc
|
||||
.act_keys
|
||||
|
@ -494,7 +494,7 @@ async fn main_api(api_url: Url, command: ApiCommand) -> Result<()> {
|
|||
title,
|
||||
})
|
||||
// FIXME: Same key.
|
||||
.sign_msg(&PubKey(key.to_bytes()), &key)
|
||||
.sign_msg(&key.verifying_key().into(), &key)
|
||||
.expect("serialization cannot fail");
|
||||
|
||||
let ret = client
|
||||
|
@ -518,7 +518,7 @@ async fn main_api(api_url: Url, command: ApiCommand) -> Result<()> {
|
|||
rich_text: RichText::from(text),
|
||||
}
|
||||
// FIXME: Same key.
|
||||
.sign_msg(&PubKey(key.to_bytes()), &key)
|
||||
.sign_msg(&key.verifying_key().into(), &key)
|
||||
.expect("serialization cannot fail");
|
||||
|
||||
let ret = client
|
||||
|
|
|
@ -12,9 +12,9 @@ fn bench_register_pow(c: &mut Criterion) {
|
|||
let rng = &mut thread_rng();
|
||||
|
||||
let id_key_priv = SigningKey::from_bytes(&[0x1A; 32]);
|
||||
let id_key = PubKey(id_key_priv.verifying_key().to_bytes());
|
||||
let id_key = PubKey::from(id_key_priv.verifying_key());
|
||||
let act_key_priv = SigningKey::from_bytes(&[0x2B; 32]);
|
||||
let act_key = PubKey(act_key_priv.verifying_key().to_bytes());
|
||||
let act_key = act_key_priv.verifying_key().into();
|
||||
let payload = UserRegisterPayload {
|
||||
id_key: id_key.clone(),
|
||||
server_url: "http://some.example.com".parse().unwrap(),
|
||||
|
|
|
@ -12,7 +12,7 @@ use axum::http::HeaderMap;
|
|||
use blah_types::identity::{IdUrl, UserActKeyDesc, UserIdentityDesc, UserProfile};
|
||||
use blah_types::{
|
||||
AuthPayload, ChatPayload, CreateGroup, CreatePeerChat, CreateRoomPayload, DeleteRoomPayload,
|
||||
Id, MemberPermission, PubKey, RichText, RoomAdminOp, RoomAdminPayload, RoomAttrs, RoomMetadata,
|
||||
Id, MemberPermission, RichText, RoomAdminOp, RoomAdminPayload, RoomAttrs, RoomMetadata,
|
||||
ServerPermission, SignExt, Signed, SignedChatMsg, UserKey, UserRegisterPayload, WithMsgId,
|
||||
X_BLAH_DIFFICULTY, X_BLAH_NONCE,
|
||||
};
|
||||
|
@ -68,8 +68,8 @@ impl User {
|
|||
let act_priv = SigningKey::from_bytes(&[b.to_ascii_lowercase(); 32]);
|
||||
Self {
|
||||
pubkeys: UserKey {
|
||||
id_key: PubKey(id_priv.verifying_key().to_bytes()),
|
||||
act_key: PubKey(act_priv.verifying_key().to_bytes()),
|
||||
id_key: id_priv.verifying_key().into(),
|
||||
act_key: act_priv.verifying_key().into(),
|
||||
},
|
||||
id_priv,
|
||||
act_priv,
|
||||
|
|
Loading…
Add table
Reference in a new issue