diff --git a/src/api/dart_ffi.rs b/src/api/dart_ffi.rs index 0227a06..141bb3e 100644 --- a/src/api/dart_ffi.rs +++ b/src/api/dart_ffi.rs @@ -1,5 +1,5 @@ use crate::coinconfig::{init_coin, CoinConfig}; -use crate::{broadcast_tx, ChainError}; +use crate::{broadcast_tx, ChainError, Tx}; use allo_isolate::{ffi, IntoDart}; use android_logger::Config; use lazy_static::lazy_static; @@ -331,9 +331,9 @@ pub async unsafe extern "C" fn prepare_multi_payment( &recipients, use_transparent, anchor_offset, - ) - .await?; - Ok(tx) + ).await?; + let tx_str = serde_json::to_string(&tx)?; + Ok(tx_str) }; to_c_str(log_string(res.await)) } @@ -346,8 +346,9 @@ pub async unsafe extern "C" fn sign(tx_filename: *mut c_char, port: i64) -> *mut let mut file = std::fs::File::open(&tx_filename.to_string())?; let mut s = String::new(); file.read_to_string(&mut s)?; + let tx: Tx = serde_json::from_str(&s)?; let raw_tx = crate::api::payment::sign_only_multi_payment( - &s, + &tx, Box::new(move |progress| { report_progress(progress, port); }), diff --git a/src/api/payment.rs b/src/api/payment.rs index c8943ad..0f8b481 100644 --- a/src/api/payment.rs +++ b/src/api/payment.rs @@ -80,18 +80,18 @@ pub async fn build_only_multi_payment( recipients: &[RecipientMemo], use_transparent: bool, anchor_offset: u32, -) -> anyhow::Result { +) -> anyhow::Result { let (tx, _) = prepare_multi_payment(last_height, recipients, use_transparent, anchor_offset).await?; - let tx_str = serde_json::to_string(&tx)?; - Ok(tx_str) + // let tx_str = serde_json::to_string(&tx)?; + Ok(tx) } pub async fn sign_only_multi_payment( - tx_string: &str, + tx: &Tx, progress_callback: PaymentProgressCallback, ) -> anyhow::Result> { - let tx = serde_json::from_str::(tx_string)?; + // let tx = serde_json::from_str::(tx_string)?; let raw_tx = sign(&tx, progress_callback)?; Ok(raw_tx) } diff --git a/src/main/rpc.rs b/src/main/rpc.rs index a716787..982faa5 100644 --- a/src/main/rpc.rs +++ b/src/main/rpc.rs @@ -11,7 +11,7 @@ use std::collections::HashMap; use thiserror::Error; use warp_api_ffi::api::payment::{Recipient, RecipientMemo}; use warp_api_ffi::api::payment_uri::PaymentURI; -use warp_api_ffi::{AccountRec, CoinConfig, TxRec}; +use warp_api_ffi::{AccountRec, CoinConfig, Tx, TxRec}; #[derive(Debug, Error)] pub enum Error { @@ -60,7 +60,6 @@ async fn main() -> anyhow::Result<()> { .mount( "/", routes![ - set_lwd, set_active, new_account, list_accounts, @@ -73,6 +72,8 @@ async fn main() -> anyhow::Result<()> { get_tx_history, pay, mark_synced, + create_offline_tx, + sign_offline_tx, broadcast_tx, new_diversified_address, make_payment_uri, @@ -86,11 +87,6 @@ async fn main() -> anyhow::Result<()> { Ok(()) } -#[post("/set_lwd?&")] -pub fn set_lwd(coin: u8, lwd_url: String) { - warp_api_ffi::set_coin_lwd_url(coin, &lwd_url); -} - #[post("/set_active?&")] pub fn set_active(coin: u8, id_account: u32) { warp_api_ffi::set_active_account(coin, id_account); @@ -179,6 +175,33 @@ pub fn get_balance() -> Result { Ok(balance.to_string()) } +#[post("/create_offline_tx", data = "")] +pub async fn create_offline_tx(payment: Json) -> Result, Error> { + let c = CoinConfig::get_active(); + let latest = warp_api_ffi::api::sync::get_latest_height().await?; + let from = { + let db = c.db()?; + db.get_address(c.id_account)? + }; + let recipients: Vec<_> = payment + .recipients + .iter() + .map(|p| RecipientMemo::from_recipient(&from, p)) + .collect(); + let tx = warp_api_ffi::api::payment::build_only_multi_payment(latest, &recipients, false, payment.confirmations).await?; + Ok(Json(tx)) +} + +#[post("/sign_offline_tx", data = "")] +pub async fn sign_offline_tx(tx: Json, config: &State) -> Result { + if !config.allow_send { + Err(anyhow!("Payment API not enabled").into()) + } else { + let tx_hex = warp_api_ffi::api::payment::sign_only_multi_payment(&tx, Box::new(|_| {})).await?; + Ok(hex::encode(tx_hex)) + } +} + #[post("/pay", data = "")] pub async fn pay(payment: Json, config: &State) -> Result { if !config.allow_send {