Skip to content

Commit

Permalink
use bitnames to resolve encryption pubkeys, bump version number
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash-L2L committed Mar 4, 2024
1 parent 654d670 commit d96d94f
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 76 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "plain_bitnames_app"
version = "0.5.5"
version = "0.5.6"
edition = "2021"
authors = [ "Ash Manning <[email protected]>" ]

Expand All @@ -16,6 +16,7 @@ tokio = { version = "1.29.1", features = ["process", "rt-multi-thread"] }
anyhow = { version = "1.0.72", features = ["backtrace"] }
bincode = "1.3.3"
blake3 = "1.4.1"
borsh = "1.3.0"
clap = { version = "4.3.19", features = ["derive"] }
ctrlc = "3.4.0"
dirs = "5.0.1"
Expand Down
11 changes: 7 additions & 4 deletions app/gui/activity/bitname_explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ impl BitNameExplorer {
.map(|(bitname_id, bitname_data)| {
{
ui.horizontal(|ui| {
ui.monospace_selectable_singleline(format!(
"BitName ID: {}",
hex::encode(bitname_id.0)
)) | crate::gui::bitname_explorer::show_bitname_data(
ui.monospace_selectable_singleline(
true,
format!(
"BitName ID: {}",
hex::encode(bitname_id.0)
),
) | crate::gui::bitname_explorer::show_bitname_data(
ui,
&bitname_data,
)
Expand Down
40 changes: 24 additions & 16 deletions app/gui/bitname_explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,45 @@ pub fn show_bitname_data(
let paymail_fee = paymail_fee
.map_or("Not set".to_owned(), |paymail_fee| paymail_fee.to_string());
ui.horizontal(|ui| {
ui.monospace_selectable_singleline(format!("Commitment: {commitment}"))
ui.monospace_selectable_singleline(
true,
format!("Commitment: {commitment}"),
)
})
.join()
| ui.horizontal(|ui| {
ui.monospace_selectable_singleline(format!(
"IPv4 Address: {ipv4_addr}"
))
ui.monospace_selectable_singleline(
false,
format!("IPv4 Address: {ipv4_addr}"),
)
})
.join()
| ui.horizontal(|ui| {
ui.monospace_selectable_singleline(format!(
"IPv6 Address: {ipv6_addr}"
))
ui.monospace_selectable_singleline(
false,
format!("IPv6 Address: {ipv6_addr}"),
)
})
.join()
| ui.horizontal(|ui| {
ui.monospace_selectable_singleline(format!(
"Encryption Pubkey: {encryption_pubkey}"
))
ui.monospace_selectable_singleline(
true,
format!("Encryption Pubkey: {encryption_pubkey}"),
)
})
.join()
| ui.horizontal(|ui| {
ui.monospace_selectable_singleline(format!(
"Signing Pubkey: {signing_pubkey}"
))
ui.monospace_selectable_singleline(
true,
format!("Signing Pubkey: {signing_pubkey}"),
)
})
.join()
| ui.horizontal(|ui| {
ui.monospace_selectable_singleline(format!(
"Paymail fee: {paymail_fee}"
))
ui.monospace_selectable_singleline(
false,
format!("Paymail fee: {paymail_fee}"),
)
})
.join()
}
Expand Down
61 changes: 35 additions & 26 deletions app/gui/coins/my_bitnames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,18 @@ impl MyBitnames {
let txid = hex::encode(txid.0);
let commitment = hex::encode(commitment);
ui.vertical(|ui| {
ui.monospace_selectable_singleline(format!(
"plaintext name: {plaintext_name}"
));
ui.monospace_selectable_singleline(format!(
"txid: {txid}"
));
ui.monospace_selectable_singleline(format!(
"commitment: {commitment}"
));
ui.monospace_selectable_singleline(
true,
format!("plaintext name: {plaintext_name}"),
);
ui.monospace_selectable_singleline(
true,
format!("txid: {txid}"),
);
ui.monospace_selectable_singleline(
true,
format!("commitment: {commitment}"),
);
});
ui.end_row()
}
Expand All @@ -74,12 +77,14 @@ impl MyBitnames {
let txid = hex::encode(txid.0);
let commitment = hex::encode(commitment);
ui.vertical(|ui| {
ui.monospace_selectable_singleline(format!(
"txid: {txid}"
));
ui.monospace_selectable_singleline(format!(
"commitment: {commitment}"
));
ui.monospace_selectable_singleline(
true,
format!("txid: {txid}"),
);
ui.monospace_selectable_singleline(
true,
format!("commitment: {commitment}"),
);
});
ui.end_row()
}
Expand Down Expand Up @@ -122,21 +127,25 @@ impl MyBitnames {
.show(ui, |ui| {
for (bitname, plaintext_name) in known_name_bitnames {
ui.vertical(|ui| {
ui.monospace_selectable_singleline(format!(
"plaintext name: {plaintext_name}"
));
ui.monospace_selectable_singleline(format!(
"bitname: {}",
hex::encode(bitname.0)
));
ui.monospace_selectable_singleline(
true,
format!("plaintext name: {plaintext_name}"),
);
ui.monospace_selectable_singleline(
true,
format!(
"bitname: {}",
hex::encode(bitname.0)
),
);
});
ui.end_row()
}
for bitname in unknown_name_bitnames {
ui.monospace_selectable_singleline(format!(
"bitname: {}",
hex::encode(bitname.0)
));
ui.monospace_selectable_singleline(
true,
format!("bitname: {}", hex::encode(bitname.0)),
);
ui.end_row()
}
});
Expand Down
55 changes: 37 additions & 18 deletions app/gui/encrypt_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ use eframe::egui;
use libes::key::conversion::PublicKeyFrom;
use plain_bitnames::types::EncryptionPubKey;

use super::util::{Ecies, InnerResponseExt, UiExt};
use super::util::{borsh_deserialize_hex, Ecies, InnerResponseExt, UiExt};
use crate::app::App;

#[derive(Debug)]
pub struct EncryptMessage {
receiver_pubkey_string: String,
// none if not yet set, otherwise result of parsing receiver pubkey
// pubkey or BitName
receiver_input: String,
// none if not yet set, otherwise result of parsing/resolving receiver pubkey
receiver_pubkey: Option<anyhow::Result<EncryptionPubKey>>,
plaintext: String,
// none if not yet computed, otherwise result of attempting to encrypt
Expand All @@ -19,35 +20,53 @@ pub struct EncryptMessage {
impl EncryptMessage {
pub fn new() -> Self {
Self {
receiver_pubkey_string: String::new(),
receiver_input: String::new(),
receiver_pubkey: None,
plaintext: String::new(),
ciphertext: None,
}
}

fn show_error(ui: &mut egui::Ui, error: &anyhow::Error) {
ui.monospace_selectable_singleline(false, "Error: ");
ui.horizontal_wrapped(|ui| {
ui.monospace("Error: ");
ui.code(format!("{error}"));
ui.monospace_selectable_multiline(format!("{error:#}"));
});
}

pub fn show(&mut self, _app: &mut App, ui: &mut egui::Ui) {
pub fn show(&mut self, app: &mut App, ui: &mut egui::Ui) {
ui.heading("Encrypt Message");
let receiver_pubkey_response = ui
let receiver_input_response = ui
.horizontal(|ui| {
ui.monospace("Receiver's Encryption Pubkey (Bech32m): ")
| ui.add(egui::TextEdit::singleline(
&mut self.receiver_pubkey_string,
))
ui.monospace(
"Receiver's BitName or Encryption Pubkey (Bech32m): ",
) | ui.add(egui::TextEdit::singleline(&mut self.receiver_input))
})
.join();
if receiver_pubkey_response.changed() {
self.receiver_pubkey = Some(
EncryptionPubKey::bech32m_decode(&self.receiver_pubkey_string)
.map_err(anyhow::Error::new),
);
if receiver_input_response.changed() {
let receiver_pubkey: anyhow::Result<EncryptionPubKey> = {
if let Ok(bitname) = borsh_deserialize_hex(&self.receiver_input)
{
app.node
.get_current_bitname_data(&bitname)
.map_err(anyhow::Error::from)
.and_then(|bitname_data| {
bitname_data.encryption_pubkey.ok_or(
anyhow::anyhow!(
"No encryption pubkey exists for this BitName"
),
)
})
} else {
EncryptionPubKey::bech32m_decode(&self.receiver_input)
.map_err(|_| {
anyhow::anyhow!(
"Failed to parse BitName or Encryption Pubkey"
)
})
}
};
self.receiver_pubkey = Some(receiver_pubkey);
}
let plaintext_response = ui
.horizontal_wrapped(|ui| {
Expand All @@ -67,7 +86,7 @@ impl EncryptMessage {
Some(Ok(receiver_pubkey)) => receiver_pubkey,
};
// regenerate ciphertext if possible
if receiver_pubkey_response.changed() || plaintext_response.changed() {
if receiver_input_response.changed() || plaintext_response.changed() {
let receiver_pubkey =
libes::key::X25519::pk_from(receiver_pubkey.0);
self.ciphertext = Some(
Expand Down
25 changes: 23 additions & 2 deletions app/gui/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::borrow::Borrow;

use borsh::BorshDeserialize;
use eframe::egui::{self, InnerResponse, Response, Ui};
use libes::{auth::HmacSha256, enc::Aes256Gcm, key::X25519};

Expand All @@ -13,7 +14,11 @@ pub trait InnerResponseExt {

/// extension trait for egui::Ui
pub trait UiExt {
fn monospace_selectable_singleline<Text>(&mut self, text: Text) -> Response
fn monospace_selectable_singleline<Text>(
&mut self,
clip_text: bool,
text: Text,
) -> Response
where
Text: Borrow<str>;

Expand All @@ -38,14 +43,19 @@ impl InnerResponseExt for InnerResponse<Option<Response>> {
}

impl UiExt for Ui {
fn monospace_selectable_singleline<Text>(&mut self, text: Text) -> Response
fn monospace_selectable_singleline<Text>(
&mut self,
clip_text: bool,
text: Text,
) -> Response
where
Text: Borrow<str>,
{
use egui::{TextEdit, TextStyle, Widget};
let mut text: &str = text.borrow();
TextEdit::singleline(&mut text)
.font(TextStyle::Monospace)
.clip_text(clip_text)
.ui(self)
}

Expand All @@ -60,3 +70,14 @@ impl UiExt for Ui {
.ui(self)
}
}

pub fn borsh_deserialize_hex<T>(hex: impl AsRef<[u8]>) -> anyhow::Result<T>
where
T: BorshDeserialize,
{
match hex::decode(hex) {
Ok(bytes) => borsh::BorshDeserialize::try_from_slice(&bytes)
.map_err(anyhow::Error::new),
Err(err) => Err(anyhow::Error::new(err)),
}
}
1 change: 1 addition & 0 deletions app/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fn main() -> anyhow::Result<()> {
let config = cli.get_config()?;
let () = set_tracing_subscriber(config.log_level);
let app = app::App::new(&config)?;

// spawn rpc server
app.runtime.spawn({
let app = app.clone();
Expand Down
2 changes: 1 addition & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "plain_bitnames"
version = "0.5.5"
version = "0.5.6"
edition = "2021"
authors = [ "Ash Manning <[email protected]>" ]

Expand Down
Loading

0 comments on commit d96d94f

Please sign in to comment.