niko_trust_gui/examples/send_2fa.rs
Niko Marmeladkov 6278321873
niko_trust_gui: TCE protocol client with iced GUI
- Byte-exact TCE codec (encoder/decoder, canonical numbers, vectors-tested)
- Ed25519 identities with bech32m trust1… addresses
- Relay client: challenge/auth handshake, request feed, responses,
  transparent re-auth on 401
- iced GUI (Material-dark, Solana-style palette): tab shell, approval
  prompts with native toasts on Linux, address book, history, settings,
  password change
- Portable single-file vault (identity + book + history + settings)
- Live 2FA roundtrip examples (send_2fa, await_2fa)
2026-08-22 23:20:41 +03:00

59 lines
2.1 KiB
Rust

//! Send a live approval request ("2FA authentication") to a recipient.
//!
//! Usage:
//! cargo run --example send_2fa -- [relay] <recipient-address> [action] [message]
//!
//! Defaults: relay = https://trust.n1ko.dev, action = "2fa".
use niko_trust_gui::address::Address;
use niko_trust_gui::protocol::{encode_approval_request, ApprovalRequest};
use niko_trust_gui::relay::Relay;
use niko_trust_gui::signer::Signer;
use niko_trust_gui::tce::NONCE_SIZE;
fn main() {
let mut args = std::env::args().skip(1);
let first = args.next().unwrap_or_default();
let (base, addr_s) = if first.starts_with("trust1") {
("https://trust.n1ko.dev".to_string(), first)
} else {
let a = args.next().unwrap_or_else(|| {
eprintln!("usage: send_2fa [relay] <recipient-address> [action] [message]");
std::process::exit(2);
});
(first.trim_end_matches('/').to_string(), a)
};
let action = args.next().unwrap_or_else(|| "2fa".to_string());
let message = args
.next()
.unwrap_or_else(|| "Confirm sign-in (2FA)".to_string());
let recipient = Address::parse(&addr_s).expect("invalid recipient address");
let sender = Signer::generate().expect("generate sender key");
let created = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("clock")
.as_secs();
let mut nonce = [0u8; NONCE_SIZE];
getrandom::fill(&mut nonce).expect("rng");
let req = ApprovalRequest {
sender: sender.pubkey(),
recipient: *recipient.pubkey(),
action,
payload: Default::default(),
message,
created_at: created,
expires_at: created + niko_trust_gui::tce::MAX_APPROVAL_LIFETIME,
nonce,
};
let tce = encode_approval_request(&req).expect("encode request");
let sig = sender.sign(&tce);
let agent = Relay::auth(&base, &sender, "*").expect("sender auth");
let id = agent.store(&tce, &sig).expect("store request");
println!("request stored: object id {}", id);
println!("recipient: {}", recipient);
}