Skip to content

Commit

Permalink
refactor RawTransactionError
Browse files Browse the repository at this point in the history
  • Loading branch information
dimxy committed Aug 24, 2023
1 parent d678aba commit 6b11193
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
31 changes: 21 additions & 10 deletions mm2src/coins/lp_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
Expand Down Expand Up @@ -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<String>,
/// 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<Vec<PrevTxns>>,
pub sig_type: Option<String>,
pub branch_id: Option<u32>,
// TODO: add if needed:
// pub sighash_type: Option<String>, 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<u32>, 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,
}

Expand Down Expand Up @@ -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<dyn Future<Item = String, Error = String> + 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<dyn Future<Item = (), Error = String> + Send>;
Expand Down
9 changes: 5 additions & 4 deletions mm2src/coins/utxo/utxo_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -2502,12 +2502,12 @@ pub async fn sign_raw_tx<T: AsRef<UtxoCoinFields> + 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 {
Expand Down Expand Up @@ -2543,6 +2543,7 @@ pub async fn sign_raw_tx<T: AsRef<UtxoCoinFields> + 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);
Expand All @@ -2555,7 +2556,7 @@ pub async fn sign_raw_tx<T: AsRef<UtxoCoinFields> + 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")))?;
Expand Down

0 comments on commit 6b11193

Please sign in to comment.