mostro_client/cli.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
pub mod add_invoice;
pub mod conversation_key;
pub mod get_dm;
pub mod list_disputes;
pub mod list_orders;
pub mod new_order;
pub mod rate_user;
pub mod send_dm;
pub mod send_msg;
pub mod take_buy;
pub mod take_dispute;
pub mod take_sell;
use crate::cli::add_invoice::execute_add_invoice;
use crate::cli::conversation_key::execute_conversation_key;
use crate::cli::get_dm::execute_get_dm;
use crate::cli::list_disputes::execute_list_disputes;
use crate::cli::list_orders::execute_list_orders;
use crate::cli::new_order::execute_new_order;
use crate::cli::rate_user::execute_rate_user;
use crate::cli::send_dm::execute_send_dm;
use crate::cli::send_msg::execute_send_msg;
use crate::cli::take_buy::execute_take_buy;
use crate::cli::take_dispute::execute_take_dispute;
use crate::cli::take_sell::execute_take_sell;
use crate::db::{connect, User};
use crate::util;
use anyhow::{Error, Result};
use clap::{Parser, Subcommand};
use nostr_sdk::prelude::*;
use std::{
env::{set_var, var},
str::FromStr,
};
use uuid::Uuid;
#[derive(Parser)]
#[command(
name = "mostro-cli",
about = "A simple CLI to use Mostro P2P",
author,
help_template = "\
{before-help}{name} 🧌
{about-with-newline}
{author-with-newline}
{usage-heading} {usage}
{all-args}{after-help}
",
version
)]
#[command(propagate_version = true)]
#[command(arg_required_else_help(true))]
pub struct Cli {
#[command(subcommand)]
pub command: Option<Commands>,
#[arg(short, long)]
pub verbose: bool,
#[arg(short, long)]
pub nsec: Option<String>,
#[arg(short, long)]
pub mostropubkey: Option<String>,
#[arg(short, long)]
pub relays: Option<String>,
#[arg(short, long)]
pub pow: Option<String>,
}
#[derive(Subcommand, Clone)]
#[clap(rename_all = "lower")]
pub enum Commands {
/// Requests open orders from Mostro pubkey
ListOrders {
/// Status of the order
#[arg(short, long)]
status: Option<String>,
/// Currency selected
#[arg(short, long)]
currency: Option<String>,
/// Choose an order kind
#[arg(short, long)]
kind: Option<String>,
},
/// Create a new buy/sell order on Mostro
NewOrder {
/// Choose an order kind
#[arg(short, long)]
kind: String,
/// Sats amount - leave empty for market price
#[arg(short, long)]
#[clap(default_value_t = 0)]
amount: i64,
/// Currency selected
#[arg(short = 'c', long)]
fiat_code: String,
/// Fiat amount
#[arg(short, long)]
#[clap(value_parser=check_fiat_range)]
fiat_amount: (i64, Option<i64>),
/// Payment method
#[arg(short = 'm', long)]
payment_method: String,
/// Premium on price
#[arg(short, long)]
#[clap(default_value_t = 0)]
premium: i64,
/// Invoice string
#[arg(short, long)]
invoice: Option<String>,
/// Expiration time of a pending Order, in days
#[arg(short, long)]
#[clap(default_value_t = 0)]
expiration_days: i64,
},
/// Take a sell order from a Mostro pubkey
TakeSell {
/// Order id
#[arg(short, long)]
order_id: Uuid,
/// Invoice string
#[arg(short, long)]
invoice: Option<String>,
/// Amount of fiat to buy
#[arg(short, long)]
amount: Option<u32>,
},
/// Take a buy order from a Mostro pubkey
TakeBuy {
/// Order id
#[arg(short, long)]
order_id: Uuid,
/// Amount of fiat to sell
#[arg(short, long)]
amount: Option<u32>,
},
/// Buyer add a new invoice to receive the payment
AddInvoice {
/// Order id
#[arg(short, long)]
order_id: Uuid,
/// Invoice string
#[arg(short, long)]
invoice: String,
},
/// Get the latest direct messages
GetDm {
/// Since time of the messages in minutes
#[arg(short, long)]
#[clap(default_value_t = 30)]
since: i64,
/// If true, get messages from counterparty, otherwise from Mostro
#[arg(short)]
from_user: bool,
},
/// Send direct message to a user
SendDm {
/// Pubkey of the counterpart
#[arg(short, long)]
pubkey: String,
/// Message to send
#[arg(short, long)]
message: String,
},
/// Send fiat sent message to confirm payment to other user
FiatSent {
/// Order id
#[arg(short, long)]
order_id: Uuid,
},
/// Settle the hold invoice and pay to buyer.
Release {
/// Order id
#[arg(short, long)]
order_id: Uuid,
},
/// Cancel a pending order
Cancel {
/// Order id
#[arg(short, long)]
order_id: Uuid,
},
/// Rate counterpart after a successful trade
Rate {
/// Order id
#[arg(short, long)]
order_id: Uuid,
/// Rating from 1 to 5
#[arg(short, long)]
rating: u8,
},
/// Start a dispute
Dispute {
/// Order id
#[arg(short, long)]
order_id: Uuid,
},
/// Cancel an order (only admin)
AdmCancel {
/// Order id
#[arg(short, long)]
order_id: Uuid,
},
/// Settle a seller's hold invoice (only admin)
AdmSettle {
/// Order id
#[arg(short, long)]
order_id: Uuid,
},
/// Requests open disputes from Mostro pubkey
AdmListDisputes {},
/// Add a new dispute's solver (only admin)
AdmAddSolver {
/// npubkey
#[arg(short, long)]
npubkey: String,
},
/// Admin or solver take a Pending dispute (only admin)
AdmTakeDispute {
/// Dispute id
#[arg(short, long)]
dispute_id: Uuid,
},
/// Get the conversation key for direct messaging with a user
ConversationKey {
/// Pubkey of the counterpart
#[arg(short, long)]
pubkey: String,
},
}
// Check range with two values value
fn check_fiat_range(s: &str) -> Result<(i64, Option<i64>)> {
if s.contains('-') {
let min: i64;
let max: i64;
// Get values from CLI
let values: Vec<&str> = s.split('-').collect();
// Check if more than two values
if values.len() > 2 {
return Err(Error::msg("Wrong amount syntax"));
};
// Get ranged command
if let Err(e) = values[0].parse::<i64>() {
return Err(e.into());
} else {
min = values[0].parse().unwrap();
}
if let Err(e) = values[1].parse::<i64>() {
return Err(e.into());
} else {
max = values[1].parse().unwrap();
}
// Check min below max
if min >= max {
return Err(Error::msg("Range of values must be 100-200 for example..."));
};
Ok((min, Some(max)))
} else {
match s.parse::<i64>() {
Ok(s) => Ok((s, None)),
Err(e) => Err(e.into()),
}
}
}
pub async fn run() -> Result<()> {
let cli = Cli::parse();
// Init logger
if cli.verbose {
set_var("RUST_LOG", "info");
pretty_env_logger::init();
}
if cli.mostropubkey.is_some() {
set_var("MOSTRO_PUBKEY", cli.mostropubkey.unwrap());
}
let pubkey = var("MOSTRO_PUBKEY").expect("$MOSTRO_PUBKEY env var needs to be set");
if cli.nsec.is_some() {
set_var("NSEC_PRIVKEY", cli.nsec.unwrap());
}
if cli.relays.is_some() {
set_var("RELAYS", cli.relays.unwrap());
}
if cli.pow.is_some() {
set_var("POW", cli.pow.unwrap());
}
let pool = connect().await?;
let identity_keys = User::get_identity_keys(&pool)
.await
.map_err(|e| anyhow::anyhow!("Failed to get identity keys: {}", e))?;
let (trade_keys, trade_index) = User::get_next_trade_keys(&pool)
.await
.map_err(|e| anyhow::anyhow!("Failed to get trade keys: {}", e))?;
// Mostro pubkey
let mostro_key = PublicKey::from_str(&pubkey)?;
// Call function to connect to relays
let client = util::connect_nostr().await?;
if let Some(cmd) = cli.command {
match &cmd {
Commands::ConversationKey { pubkey } => {
execute_conversation_key(&trade_keys, PublicKey::from_str(pubkey)?).await?
}
Commands::ListOrders {
status,
currency,
kind,
} => execute_list_orders(kind, currency, status, mostro_key, &client).await?,
Commands::TakeSell {
order_id,
invoice,
amount,
} => {
execute_take_sell(
order_id,
invoice,
*amount,
&identity_keys,
&trade_keys,
trade_index,
mostro_key,
&client,
)
.await?
}
Commands::TakeBuy { order_id, amount } => {
execute_take_buy(
order_id,
*amount,
&identity_keys,
&trade_keys,
trade_index,
mostro_key,
&client,
)
.await?
}
Commands::AddInvoice { order_id, invoice } => {
execute_add_invoice(
order_id,
invoice,
&identity_keys,
&trade_keys,
mostro_key,
&client,
)
.await?
}
Commands::GetDm { since, from_user } => {
execute_get_dm(since, &trade_keys, &client, *from_user).await?
}
Commands::FiatSent { order_id }
| Commands::Release { order_id }
| Commands::Dispute { order_id }
| Commands::AdmCancel { order_id }
| Commands::AdmSettle { order_id }
| Commands::Cancel { order_id } => {
execute_send_msg(
cmd.clone(),
Some(*order_id),
Some(&identity_keys),
&trade_keys,
mostro_key,
&client,
None,
)
.await?
}
Commands::AdmAddSolver { npubkey } => {
execute_send_msg(
cmd.clone(),
None,
Some(&identity_keys),
&trade_keys,
mostro_key,
&client,
Some(npubkey),
)
.await?
}
Commands::NewOrder {
kind,
fiat_code,
amount,
fiat_amount,
payment_method,
premium,
invoice,
expiration_days,
} => {
execute_new_order(
kind,
fiat_code,
fiat_amount,
amount,
payment_method,
premium,
invoice,
&identity_keys,
&trade_keys,
trade_index,
mostro_key,
&client,
expiration_days,
)
.await?
}
Commands::Rate { order_id, rating } => {
execute_rate_user(
order_id,
rating,
&identity_keys,
&trade_keys,
mostro_key,
&client,
)
.await?;
}
Commands::AdmTakeDispute { dispute_id } => {
execute_take_dispute(dispute_id, &identity_keys, &trade_keys, mostro_key, &client)
.await?
}
Commands::AdmListDisputes {} => execute_list_disputes(mostro_key, &client).await?,
Commands::SendDm { pubkey, message } => {
let pubkey = PublicKey::from_str(pubkey)?;
execute_send_dm(&trade_keys, pubkey, &client, message).await?
}
};
}
println!("Bye Bye!");
Ok(())
}