updating wip

This commit is contained in:
Lorenzo Tucci 2022-11-19 11:26:25 +01:00 committed by binarybaron
parent 22deb6b47e
commit da3307d4d6
5 changed files with 151 additions and 61 deletions

View File

@ -33,6 +33,10 @@ use uuid::Uuid;
use crate::protocol::Database;
use crate::env::{Config, Mainnet, Testnet};
use crate::fs::system_data_dir;
use serde_json::json;
use std::str::FromStr;
use tokio::task;
pub struct Request {
pub params: Params,
@ -52,36 +56,128 @@ pub struct Params {
pub struct Init {
db: Arc<dyn Database + Send + Sync>,
bitcoin_wallet: Option<bitcoin::Wallet>,
monero_wallet: Option<(monero::Wallet, monero::WalletRpcProcess)>,
pub bitcoin_wallet: Option<bitcoin::Wallet>,
monero_wallet: Option<monero::Wallet>,
tor_socks5_port: Option<u16>,
namespace: XmrBtcNamespace,
server_handle: Option<HttpServerHandle>,
debug: bool,
json: bool,
is_testnet: bool,
//server_handle: Option<task::JoinHandle<()>>,
server_address: Option<SocketAddr>,
pub seed: Option<Seed>,
pub debug: bool,
pub json: bool,
pub is_testnet: bool,
}
impl Request {
pub async fn call(&self, api_init: &Init) -> Result<()> {
match self.cmd {
Command::BuyXmr => { }
Command::History => {
pub async fn call(&self, api_init: &Init) -> Result<serde_json::Value> {
let result = match self.cmd {
Command::BuyXmr => {
json!({
"empty": "true"
})
}
Command::History => {
let swaps = api_init.db.all().await?;
let mut vec: Vec<(Uuid, String)> = Vec::new();
for (swap_id, state) in swaps {
let state: BobState = state.try_into()?;
vec.push((swap_id, state.to_string()));
}
json!({
"swaps": vec
})
}
Command::Config => {
json!({
"empty": "true"
})
}
Command::WithdrawBtc => {
json!({
"empty": "true"
})
}
Command::Config => { }
Command::WithdrawBtc => { }
Command::StartDaemon => {
let addr2 = "127.0.0.1:1234".parse()?;
let server_handle = {
if let Some(addr) = api_init.server_address {
let (_addr, handle) = rpc::run_server(addr, api_init).await?;
Some(handle)
} else {
let (_addr, handle) = rpc::run_server(addr2, api_init).await?;
Some(handle)
}
};
json!({
"empty": "true"
})
}
Command::Balance => {
let debug = api_init.debug;
let json = api_init.json;
let is_testnet = api_init.is_testnet;
let bitcoin_balance = api_init.bitcoin_wallet
.as_ref().unwrap().balance().await?;
tracing::info!(
balance = %bitcoin_balance,
"Checked Bitcoin balance",
);
json!({
"balance": bitcoin_balance.as_sat()
})
}
Command::Resume => { }
Command::Cancel => { }
Command::Refund => { }
Command::ListSellers => { }
Command::ExportBitcoinWallet => { }
Command::MoneroRecovery => { }
}
Ok(())
Command::Resume => {
json!({
"empty": "true"
})
}
Command::Cancel => {
json!({
"empty": "true"
})
}
Command::Refund => {
json!({
"empty": "true"
})
}
Command::ListSellers => {
let rendezvous_point = self.params.rendezvous_point.clone().unwrap();
let rendezvous_node_peer_id = rendezvous_point
.extract_peer_id()
.context("Rendezvous node address must contain peer ID")?;
let identity = api_init.seed.as_ref().unwrap().derive_libp2p_identity();
let sellers = list_sellers(
rendezvous_node_peer_id,
rendezvous_point,
api_init.namespace,
api_init.tor_socks5_port.unwrap(),
identity,
)
.await?;
json!({
"empty": "true"
})
}
Command::ExportBitcoinWallet => {
json!({
"empty": "true"
})
}
Command::MoneroRecovery => {
json!({
"empty": "true"
})
}
};
Ok(result)
}
}
impl Init {
@ -108,14 +204,7 @@ impl Init {
let seed = Seed::from_file_or_generate(data_dir.as_path())
.context("Failed to read seed in file")?;
let server_handle = {
if let Some(addr) = server_address {
let (_addr, handle) = rpc::run_server(addr).await?;
Some(handle)
} else {
None
}
};
let tor_socks5_port = {
if let Some(tor) = tor {
@ -125,6 +214,8 @@ impl Init {
}
};
cli::tracing::init(debug, json, data_dir.join("logs"), None)?;
let init = Init {
bitcoin_wallet: Some(init_bitcoin_wallet(
bitcoin_electrum_rpc_url,
@ -140,14 +231,15 @@ impl Init {
monero_daemon_address,
env_config,
)
.await?),
.await?.0),
tor_socks5_port: tor_socks5_port,
namespace: XmrBtcNamespace::from_is_testnet(is_testnet),
db: open_db(data_dir.join("sqlite")).await?,
seed: Some(seed),
debug,
json,
is_testnet,
server_handle,
server_address,
};
@ -171,6 +263,7 @@ impl Init {
None
}
};
cli::tracing::init(debug, json, data_dir.join("logs"), None)?;
let init = Init {
bitcoin_wallet: None,
@ -178,10 +271,11 @@ impl Init {
tor_socks5_port,
namespace: XmrBtcNamespace::from_is_testnet(is_testnet),
db: open_db(data_dir.join("sqlite")).await?,
seed: None,
debug,
json,
is_testnet,
server_handle: None,
server_address: None,
};
Ok(init)
}
@ -211,6 +305,8 @@ impl Init {
}
};
cli::tracing::init(debug, json, data_dir.join("logs"), None)?;
let init = Init {
bitcoin_wallet: Some(init_bitcoin_wallet(
bitcoin_electrum_rpc_url,
@ -224,14 +320,16 @@ impl Init {
tor_socks5_port,
namespace: XmrBtcNamespace::from_is_testnet(is_testnet),
db: open_db(data_dir.join("sqlite")).await?,
seed: Some(seed),
debug,
json,
is_testnet,
server_handle: None,
server_address: None,
};
Ok(init)
}
}
async fn init_bitcoin_wallet(

View File

@ -19,18 +19,19 @@ use swap::common::check_latest_version;
#[tokio::main]
async fn main() -> Result<()> {
// let api = match parse_args_and_apply_defaults(env::args_os()).await? {
// ParseResult::InternalApi(api) => *api,
// ParseResult::PrintAndExitZero { message } => {
// println!("{}", message);
// std::process::exit(0);
// }
// };
let (api_init, request) = match parse_args_and_apply_defaults(env::args_os()).await? {
ParseResult::Init(api_init, request) => (api_init, request),
ParseResult::PrintAndExitZero { message } => {
println!("{}", message);
std::process::exit(0);
}
};
if let Err(e) = check_latest_version(env!("CARGO_PKG_VERSION")).await {
eprintln!("{}", e);
}
//api.call().await?;
let result = request.call(&api_init).await?;
println!("{}", result);
Ok(())
}

View File

@ -143,7 +143,7 @@ where
).await?;
let request = Request {
params: Params::default(),
cmd: Command::Config,
cmd: Command::Balance,
};
(init, request)
}

View File

@ -1,6 +1,7 @@
use std::net::SocketAddr;
use jsonrpsee::http_server::{RpcModule, HttpServerBuilder, HttpServerHandle};
use thiserror::Error;
use crate::api::{Init};
pub mod methods;
@ -10,11 +11,11 @@ pub enum Error {
ExampleError,
}
pub async fn run_server(server_address: SocketAddr) -> anyhow::Result<(SocketAddr, HttpServerHandle)> {
pub async fn run_server(server_address: SocketAddr, api_init: &Init) -> anyhow::Result<(SocketAddr, HttpServerHandle)> {
let server = HttpServerBuilder::default().build(server_address).await?;
let mut modules = RpcModule::new(());
{
modules.merge(methods::register_modules())
modules.merge(methods::register_modules(&api_init))
.unwrap()
}

View File

@ -1,5 +1,5 @@
use jsonrpsee::http_server::{RpcModule};
use crate::api::{Request, Params};
use crate::api::{Request, Init, Params};
use crate::env::{Config, GetConfig, Testnet};
use crate::fs::system_data_dir;
use url::Url;
@ -7,9 +7,10 @@ use crate::cli::command::{Command, Options};
use std::str::FromStr;
use crate::cli::command::{DEFAULT_ELECTRUM_RPC_URL_TESTNET, DEFAULT_BITCOIN_CONFIRMATION_TARGET_TESTNET};
use crate::rpc::Error;
use crate::{bitcoin, cli, monero};
pub fn register_modules() -> RpcModule<()> {
pub fn register_modules(api_init: &Init) -> RpcModule<()> {
let mut module = RpcModule::new(());
module
.register_async_method("get_bitcoin_balance", |_, _| async {
@ -21,22 +22,11 @@ pub fn register_modules() -> RpcModule<()> {
}
async fn get_bitcoin_balance() -> anyhow::Result<(), Error> {
// let api = InternalApi {
// opts: Options {
// env_config: Testnet::get_config(),
// debug: false,
// json: true,
// data_dir: system_data_dir().unwrap().join("cli")
//
// },
// params: Params {
// bitcoin_electrum_rpc_url: Some(Url::from_str(DEFAULT_ELECTRUM_RPC_URL_TESTNET).unwrap()),
// bitcoin_target_block: Some(DEFAULT_BITCOIN_CONFIRMATION_TARGET_TESTNET),
// ..Default::default()
// },
// cmd: Command::Balance,
// };
// api.call().await;
let request = Request {
params: Params::default(),
cmd: Command::Balance,
};
// request.call(api_init).await;
Ok(())
}