From 6b11193f4523d70f167cd342faa47cdcaf61ffcd Mon Sep 17 00:00:00 2001 From: dimxy Date: Thu, 24 Aug 2023 14:06:25 +0500 Subject: [PATCH] refactor RawTransactionError --- mm2src/coins/lp_coins.rs | 31 +++++++++++++++++++++---------- mm2src/coins/utxo/utxo_common.rs | 9 +++++---- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/mm2src/coins/lp_coins.rs b/mm2src/coins/lp_coins.rs index 36e6c39d6d..058aeb3205 100644 --- a/mm2src/coins/lp_coins.rs +++ b/mm2src/coins/lp_coins.rs @@ -353,17 +353,16 @@ pub enum RawTransactionError { impl HttpStatusCode for RawTransactionError { fn status_code(&self) -> StatusCode { match self { - RawTransactionError::Transport(_) | RawTransactionError::InternalError(_) => { - StatusCode::INTERNAL_SERVER_ERROR - }, + RawTransactionError::Transport(_) + | RawTransactionError::InternalError(_) + | RawTransactionError::SigningError(_) => StatusCode::INTERNAL_SERVER_ERROR, RawTransactionError::NoSuchCoin { .. } | RawTransactionError::InvalidHashError(_) | RawTransactionError::HashNotExist(_) | RawTransactionError::DecodeError(_) | RawTransactionError::InvalidParam(_) - | RawTransactionError::NonExistentPrevOutputError(_) - | RawTransactionError::SigningError(_) - | RawTransactionError::NotImplemented { .. } => StatusCode::BAD_REQUEST, + | RawTransactionError::NonExistentPrevOutputError(_) => StatusCode::BAD_REQUEST, + RawTransactionError::NotImplemented { .. } => StatusCode::NOT_IMPLEMENTED, } } } @@ -420,27 +419,39 @@ pub struct RawTransactionRes { pub tx_hex: BytesJson, } +/// Previous utxo transaction data for signing #[derive(Clone, Debug, Deserialize)] pub struct PrevTxns { + /// transaction hash pub tx_hash: String, + /// transaction output index pub index: u32, + /// transaction output script pub key pub script_pub_key: String, + /// redeem script for P2SH script pubkey pub redeem_script: Option, + /// transaction output amount pub amount: BigDecimal, } +/// RPC request with unsigned utxo transaction and params for signing #[derive(Clone, Debug, Deserialize)] pub struct SignRawTransactionRequest { + /// coin ticker name pub coin: String, + /// unsigned utxo transaction in hex pub tx_hex: String, + /// optional data of previous transactions referred by unsigned transaction inputs pub prev_txns: Option>, - pub sig_type: Option, - pub branch_id: Option, + // TODO: add if needed: + // pub sighash_type: Option, optional signature hash type, one of values: NONE, SINGLE, ALL, NONE|ANYONECANPAY, SINGLE|ANYONECANPAY, ALL|ANYONECANPAY (if not set 'ALL' is used) + // pub branch_id: Option, zcash or komodo optional consensus branch id, used for signing transactions ahead of current height } +/// RPC response with signed utxo transaction #[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] pub struct SignRawTransactionResponse { - /// Raw bytes of signed transaction in hexadecimal string which is the response from the signrawtransaction request + /// Raw bytes of signed transaction in hexadecimal string which is the response from the sign_raw_transaction request pub tx_hex: BytesJson, } @@ -1072,7 +1083,7 @@ pub trait MarketCoinOps { /// Receives raw transaction bytes as input and returns tx hash in hexadecimal format fn send_raw_tx_bytes(&self, tx: &[u8]) -> Box + Send>; - /// Signs raw transaction in hexadecimal format as input and returns signed transaction in hexadecimal format + /// Signs raw utxo transaction in hexadecimal format as input and returns signed transaction in hexadecimal format async fn sign_raw_tx(&self, args: &SignRawTransactionRequest) -> SignRawTransactionResult; fn wait_for_confirmations(&self, input: ConfirmPaymentInput) -> Box + Send>; diff --git a/mm2src/coins/utxo/utxo_common.rs b/mm2src/coins/utxo/utxo_common.rs index 7447f680a7..4bd32e999d 100644 --- a/mm2src/coins/utxo/utxo_common.rs +++ b/mm2src/coins/utxo/utxo_common.rs @@ -32,7 +32,7 @@ use chain::constants::SEQUENCE_FINAL; use chain::{OutPoint, TransactionInput, TransactionOutput}; use common::executor::Timer; use common::jsonrpc_client::JsonRpcErrorType; -use common::log::{error, warn}; +use common::log::{error, warn, debug}; use crypto::{Bip32DerPathOps, Bip44Chain, RpcDerivationPath, StandardHDPath, StandardHDPathError}; use futures::compat::Future01CompatExt; use futures::future::{FutureExt, TryFutureExt}; @@ -2502,12 +2502,12 @@ pub async fn sign_raw_tx + UtxoTxGenerationOps>( if prev_script.is_none() { // get first previous utxo script assuming all are the same let script_bytes = hex::decode(prev_utxo.clone().script_pub_key) - .map_to_mm(|e| RawTransactionError::InvalidParam(e.to_string()))?; + .map_to_mm(|e| RawTransactionError::DecodeError(e.to_string()))?; prev_script = Some(Script::from(script_bytes)); } let prev_hash = hex::decode(prev_utxo.tx_hash.as_bytes()) - .map_err(|e| RawTransactionError::InvalidParam(e.to_string()))?; + .map_to_mm(|e| RawTransactionError::DecodeError(e.to_string()))?; unspents.push(UnspentInfo { outpoint: OutPoint { @@ -2543,6 +2543,7 @@ pub async fn sign_raw_tx + UtxoTxGenerationOps>( }; } + // TODO: use zeroise for privkey let key_pair = coin.as_ref().priv_key_policy.key_pair_or_err().unwrap(); let mut input_signer_incomplete = TransactionInputSigner::from(tx); @@ -2555,7 +2556,7 @@ pub async fn sign_raw_tx + UtxoTxGenerationOps>( .build_unchecked() .await .map_err(|e| RawTransactionError::InvalidParam(e.to_string()))?; - log!("Unsigned tx = {:?} for signing", unsigned); + debug!("Unsigned tx = {:?} for signing", unsigned); let prev_script = prev_script .ok_or_else(|| RawTransactionError::NonExistentPrevOutputError(String::from("no previous script")))?;