From e3661f3b94176345b47f6eff26fbe2925c19f0ce Mon Sep 17 00:00:00 2001 From: Artem Pikulin Date: Sat, 12 Dec 2020 23:40:37 +0700 Subject: [PATCH] Add convert_utxo_address RPC call. --- mm2src/coins/lp_coins.rs | 27 +++++++++++++++++++++++++++ mm2src/coins/utxo.rs | 2 +- mm2src/rpc.rs | 6 ++++-- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/mm2src/coins/lp_coins.rs b/mm2src/coins/lp_coins.rs index 8ca5a518cc..4288a3b4d0 100644 --- a/mm2src/coins/lp_coins.rs +++ b/mm2src/coins/lp_coins.rs @@ -1146,3 +1146,30 @@ pub fn update_coins_config(mut config: Json) -> Result { Ok(config) } + +#[derive(Deserialize)] +struct ConvertUtxoAddressReq { + address: String, + to_coin: String, +} + +pub async fn convert_utxo_address(ctx: MmArc, req: Json) -> Result>, String> { + let req: ConvertUtxoAddressReq = try_s!(json::from_value(req)); + let mut addr: utxo::Address = try_s!(req.address.parse()); + let coin = match lp_coinfindᔃ(&ctx, &req.to_coin).await { + Ok(Some(c)) => c, + _ => return ERR!("Coin {} is not activated", req.to_coin), + }; + let coin = match coin { + MmCoinEnum::UtxoCoin(utxo) => utxo, + _ => return ERR!("Coin {} is not utxo", req.to_coin), + }; + addr.prefix = coin.as_ref().my_address.prefix; + addr.t_addr_prefix = coin.as_ref().my_address.t_addr_prefix; + addr.checksum_type = coin.as_ref().my_address.checksum_type; + + let response = try_s!(json::to_vec(&json!({ + "result": addr.to_string(), + }))); + Ok(try_s!(Response::builder().body(response))) +} diff --git a/mm2src/coins/utxo.rs b/mm2src/coins/utxo.rs index dd50473d08..b54afb5fcf 100644 --- a/mm2src/coins/utxo.rs +++ b/mm2src/coins/utxo.rs @@ -45,7 +45,7 @@ use futures::lock::{Mutex as AsyncMutex, MutexGuard as AsyncMutexGuard}; use futures::stream::StreamExt; use futures01::Future; use keys::bytes::Bytes; -use keys::{Address, KeyPair, Private, Public, Secret}; +pub use keys::{Address, KeyPair, Private, Public, Secret}; #[cfg(test)] use mocktopus::macros::*; use num_traits::ToPrimitive; use primitives::hash::{H256, H264, H512}; diff --git a/mm2src/rpc.rs b/mm2src/rpc.rs index 79a4dc3774..3d38133843 100644 --- a/mm2src/rpc.rs +++ b/mm2src/rpc.rs @@ -19,8 +19,9 @@ #![cfg_attr(not(feature = "native"), allow(unused_imports))] #![cfg_attr(not(feature = "native"), allow(dead_code))] -use coins::{convert_address, get_enabled_coins, get_trade_fee, kmd_rewards_info, my_tx_history, send_raw_transaction, - set_required_confirmations, set_requires_notarization, show_priv_key, validate_address, withdraw}; +use coins::{convert_address, convert_utxo_address, get_enabled_coins, get_trade_fee, kmd_rewards_info, my_tx_history, + send_raw_transaction, set_required_confirmations, set_requires_notarization, show_priv_key, + validate_address, withdraw}; use common::mm_ctx::MmArc; #[cfg(feature = "native")] use common::wio::{CORE, CPUPOOL}; use common::{err_to_rpc_json_string, err_tp_rpc_json, HyRes}; @@ -121,6 +122,7 @@ pub fn dispatcher(req: Json, ctx: MmArc) -> DispatcherRes { "cancel_order" => hyres(cancel_order(ctx, req)), "coins_needed_for_kick_start" => hyres(coins_needed_for_kick_start(ctx)), "convertaddress" => hyres(convert_address(ctx, req)), + "convert_utxo_address" => hyres(convert_utxo_address(ctx, req)), "disable_coin" => hyres(disable_coin(ctx, req)), "electrum" => hyres(electrum(ctx, req)), "enable" => hyres(enable(ctx, req)),