[verified] Add F4 onion locator rotate with signed loc frames.
Identity key stays put. Typing ROTATE publishes a new Arti HS, hard-cuts the old nickname, persists self.onion, and pushes loc to reachable friends.
This commit is contained in:
parent
a2dfe3b2b8
commit
07aa97711c
8 changed files with 743 additions and 87 deletions
164
src/tui.rs
164
src/tui.rs
|
|
@ -1,4 +1,5 @@
|
|||
use std::io;
|
||||
use std::sync::Arc;
|
||||
|
||||
use ratatui::crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
|
||||
use ratatui::layout::{Constraint, Layout};
|
||||
|
|
@ -6,15 +7,69 @@ use ratatui::style::{Color, Style};
|
|||
use ratatui::widgets::{Block, Borders, List, ListItem, Paragraph, Wrap};
|
||||
use ratatui::{DefaultTerminal, Frame};
|
||||
|
||||
use crate::node::Node;
|
||||
use crate::qr::{self, QrPayload};
|
||||
use crate::store::{Friend, Store};
|
||||
use crate::store::Friend;
|
||||
|
||||
pub fn fingerprint_mismatch_banner() -> &'static str {
|
||||
"fingerprint mismatch"
|
||||
}
|
||||
|
||||
pub fn run(store: Store) -> Result<(), String> {
|
||||
let mut app = App::new(store)?;
|
||||
pub fn rotate_screen_text() -> &'static str {
|
||||
"Rotate onion address?\n\
|
||||
Your identity key stays the same.\n\
|
||||
Online friends get a signed location update.\n\
|
||||
Offline friends CANNOT find you until they rescan your QR.\n\
|
||||
Type ROTATE to confirm Esc to cancel"
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum RotateDecision {
|
||||
Pending,
|
||||
Confirm,
|
||||
Cancel,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct RotatePrompt {
|
||||
buf: String,
|
||||
}
|
||||
|
||||
impl RotatePrompt {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn on_esc(&self) -> RotateDecision {
|
||||
RotateDecision::Cancel
|
||||
}
|
||||
|
||||
pub fn on_char(&mut self, c: char) -> RotateDecision {
|
||||
if c == '\n' {
|
||||
return RotateDecision::Pending;
|
||||
}
|
||||
if c == '\u{8}' {
|
||||
self.buf.pop();
|
||||
return RotateDecision::Pending;
|
||||
}
|
||||
if !c.is_ascii_alphabetic() {
|
||||
return RotateDecision::Pending;
|
||||
}
|
||||
self.buf.push(c);
|
||||
if self.buf == "ROTATE" {
|
||||
RotateDecision::Confirm
|
||||
} else {
|
||||
RotateDecision::Pending
|
||||
}
|
||||
}
|
||||
|
||||
pub fn typed(&self) -> &str {
|
||||
&self.buf
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(node: Arc<Node>, rt: tokio::runtime::Handle) -> Result<(), String> {
|
||||
let mut app = App::new(node, rt)?;
|
||||
let mut terminal = ratatui::init();
|
||||
let result = app.run(&mut terminal);
|
||||
ratatui::restore();
|
||||
|
|
@ -22,15 +77,15 @@ pub fn run(store: Store) -> Result<(), String> {
|
|||
}
|
||||
|
||||
struct App {
|
||||
store: Store,
|
||||
node: Arc<Node>,
|
||||
rt: tokio::runtime::Handle,
|
||||
screen: Screen,
|
||||
friends: Vec<Friend>,
|
||||
selected: usize,
|
||||
me_fp: String,
|
||||
me_onion: String,
|
||||
identity_sk: Vec<u8>,
|
||||
prekey_pk: Vec<u8>,
|
||||
alert: Option<String>,
|
||||
status_note: Option<String>,
|
||||
}
|
||||
|
||||
enum Screen {
|
||||
|
|
@ -38,23 +93,22 @@ enum Screen {
|
|||
Share { art: String, payload: String },
|
||||
Paste { buf: String, err: Option<String> },
|
||||
Approve { payload: QrPayload },
|
||||
Rotate,
|
||||
Rotate { prompt: RotatePrompt },
|
||||
}
|
||||
|
||||
impl App {
|
||||
fn new(store: Store) -> Result<Self, String> {
|
||||
let me = store.self_identity().map_err(|e| e.to_string())?;
|
||||
let friends = store.list_friends().map_err(|e| e.to_string())?;
|
||||
fn new(node: Arc<Node>, rt: tokio::runtime::Handle) -> Result<Self, String> {
|
||||
let friends = node.list_friends()?;
|
||||
Ok(Self {
|
||||
store,
|
||||
me_fp: to_hex(&node.identity_pk()),
|
||||
me_onion: node.onion(),
|
||||
node,
|
||||
rt,
|
||||
screen: Screen::Main,
|
||||
friends,
|
||||
selected: 0,
|
||||
me_fp: to_hex(&me.identity_pk),
|
||||
me_onion: me.onion,
|
||||
identity_sk: me.identity_sk,
|
||||
prekey_pk: me.prekey_pk,
|
||||
alert: None,
|
||||
status_note: None,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -92,7 +146,11 @@ impl App {
|
|||
err: None,
|
||||
};
|
||||
}
|
||||
KeyCode::F(4) => self.screen = Screen::Rotate,
|
||||
KeyCode::F(4) => {
|
||||
self.screen = Screen::Rotate {
|
||||
prompt: RotatePrompt::new(),
|
||||
};
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Screen::Share { .. } => {
|
||||
|
|
@ -124,23 +182,43 @@ impl App {
|
|||
KeyCode::Char('n') | KeyCode::Esc => self.screen = Screen::Main,
|
||||
_ => {}
|
||||
},
|
||||
Screen::Rotate => {
|
||||
if matches!(key.code, KeyCode::Esc | KeyCode::Enter) {
|
||||
self.screen = Screen::Main;
|
||||
Screen::Rotate { prompt } => match key.code {
|
||||
KeyCode::Esc => self.screen = Screen::Main,
|
||||
KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
|
||||
if prompt.on_char(c) == RotateDecision::Confirm {
|
||||
self.confirm_rotate()?;
|
||||
}
|
||||
}
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
prompt.on_char('\u{8}');
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn confirm_rotate(&mut self) -> Result<(), String> {
|
||||
let node = Arc::clone(&self.node);
|
||||
match self.rt.block_on(node.rotate()) {
|
||||
Ok(outcome) => {
|
||||
self.me_onion = node.onion();
|
||||
self.status_note = Some(format!(
|
||||
"rotated · notified {}/{} friends",
|
||||
outcome.notified, outcome.friends
|
||||
));
|
||||
self.open_share()
|
||||
}
|
||||
Err(e) => {
|
||||
self.alert = Some(e);
|
||||
self.screen = Screen::Main;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn open_share(&mut self) -> Result<(), String> {
|
||||
let onion = if self.me_onion.is_empty() {
|
||||
"unset"
|
||||
} else {
|
||||
self.me_onion.as_str()
|
||||
};
|
||||
let payload =
|
||||
qr::encode(&self.identity_sk, onion, &self.prekey_pk).map_err(|e| e.to_string())?;
|
||||
let payload = self.node.qr_payload()?;
|
||||
let art = qr::render_unicode(&payload).map_err(|e| e.to_string())?;
|
||||
self.screen = Screen::Share { art, payload };
|
||||
Ok(())
|
||||
|
|
@ -155,11 +233,7 @@ impl App {
|
|||
};
|
||||
}
|
||||
Ok(payload) => {
|
||||
let known = self
|
||||
.store
|
||||
.get_friend(&payload.pubkey)
|
||||
.map_err(|e| e.to_string())?
|
||||
.is_some();
|
||||
let known = self.node.get_friend(&payload.pubkey)?.is_some();
|
||||
if known {
|
||||
self.accept_friend(&payload)?;
|
||||
} else {
|
||||
|
|
@ -171,19 +245,14 @@ impl App {
|
|||
}
|
||||
|
||||
fn accept_friend(&mut self, payload: &QrPayload) -> Result<(), String> {
|
||||
self.store
|
||||
.upsert_friend(&payload.pubkey, &payload.onion, None)
|
||||
.map_err(|e| e.to_string())?;
|
||||
self.store
|
||||
.set_friend_prekey(&payload.pubkey, &payload.signed_prekey)
|
||||
.map_err(|e| e.to_string())?;
|
||||
self.node.add_friend_payload(payload)?;
|
||||
self.reload_friends()?;
|
||||
self.screen = Screen::Main;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reload_friends(&mut self) -> Result<(), String> {
|
||||
self.friends = self.store.list_friends().map_err(|e| e.to_string())?;
|
||||
self.friends = self.node.list_friends()?;
|
||||
if self.friends.is_empty() {
|
||||
self.selected = 0;
|
||||
} else {
|
||||
|
|
@ -198,7 +267,7 @@ impl App {
|
|||
Screen::Share { art, payload } => draw_share(f, art, payload),
|
||||
Screen::Paste { buf, err } => draw_paste(f, buf, err.as_deref()),
|
||||
Screen::Approve { payload } => draw_approve(f, payload),
|
||||
Screen::Rotate => draw_rotate(f),
|
||||
Screen::Rotate { prompt } => draw_rotate(f, prompt.typed()),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -250,8 +319,13 @@ impl App {
|
|||
} else {
|
||||
self.me_onion.as_str()
|
||||
};
|
||||
let note = self
|
||||
.status_note
|
||||
.as_deref()
|
||||
.map(|s| format!("{s} | "))
|
||||
.unwrap_or_default();
|
||||
let status = format!(
|
||||
"TOR: offline | me: {} | onion: {} F2 share F3 paste F4 rotate Ctrl-Q quit",
|
||||
"{note}TOR: up | me: {} | onion: {} F2 share F3 paste F4 rotate Ctrl-Q quit",
|
||||
truncate_fp(&self.me_fp),
|
||||
onion
|
||||
);
|
||||
|
|
@ -293,12 +367,8 @@ fn draw_approve(f: &mut Frame, payload: &QrPayload) {
|
|||
);
|
||||
}
|
||||
|
||||
fn draw_rotate(f: &mut Frame) {
|
||||
let body = "Rotate onion address?\n\
|
||||
Your identity key stays the same.\n\
|
||||
Online friends get a signed location update.\n\
|
||||
Offline friends CANNOT find you until they rescan your QR.\n\n\
|
||||
M4 will rotate the HS. Esc / Enter to go back (no change).";
|
||||
fn draw_rotate(f: &mut Frame, typed: &str) {
|
||||
let body = format!("{}\n\n{typed}", rotate_screen_text());
|
||||
f.render_widget(
|
||||
Paragraph::new(body)
|
||||
.wrap(Wrap { trim: false })
|
||||
|
|
|
|||
Loading…
Reference in a new issue