Skip to content

Commit

Permalink
Fix status as sender (#674)
Browse files Browse the repository at this point in the history
* Fix status as sender

* Update mobilecoind-json

* Clarify case where tx_public_key did not land but key_images did

* Add test for key image already spent and remove deprecated test_client

* Clippy

* Add field names to hex decode error
  • Loading branch information
sugargoat authored Jan 10, 2021
1 parent b7d3356 commit 0dfb34e
Show file tree
Hide file tree
Showing 13 changed files with 364 additions and 1,105 deletions.
6 changes: 0 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -429,12 +429,6 @@ jobs:
--exclude mc-sgx-ias-types \
--exclude mc-sgx-report-cache-untrusted \
--exclude mc-sgx-urts-sys
- run:
name: Cargo check mobilecoind/testnet-client
command: |
cargo check --frozen --target "$HOST_TARGET_TRIPLE" \
-p mc-mobilecoind --bin mobilecoind \
-p mc-testnet-client --bin mc-testnet-client
- check-dirty-git
- when:
condition: { equal: [ << pipeline.git.branch >>, master ] }
Expand Down
144 changes: 8 additions & 136 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ members = [
"sgx/urts-sys",
"test-vectors/account-keys",
"test-vectors/b58-encodings",
"testnet-client",
"transaction/core",
"transaction/core/test-utils",
"transaction/std",
Expand Down
25 changes: 7 additions & 18 deletions mobilecoind-json/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#![feature(proc_macro_hygiene, decl_macro)]

use grpcio::ChannelBuilder;
use mc_api::external::{CompressedRistretto, KeyImage, PublicAddress, RistrettoPrivate};
use mc_api::external::{CompressedRistretto, PublicAddress, RistrettoPrivate};
use mc_common::logger::{create_app_logger, log, o};
use mc_mobilecoind_api::{mobilecoind_api_grpc::MobilecoindApiClient, MobilecoindUri};
use mc_mobilecoind_json::data_types::*;
Expand Down Expand Up @@ -530,28 +530,17 @@ fn submit_tx(
}

/// Checks the status of a transfer given a key image and tombstone block
#[post("/tx/status-as-sender", format = "json", data = "<receipt>")]
#[post("/tx/status-as-sender", format = "json", data = "<submit_response>")]
fn check_transfer_status(
state: rocket::State<State>,
receipt: Json<JsonSendPaymentResponse>,
submit_response: Json<JsonSubmitTxResponse>,
) -> Result<Json<JsonStatusResponse>, String> {
let mut sender_receipt = mc_mobilecoind_api::SenderTxReceipt::new();
let mut key_images = Vec::new();
for key_image_hex in &receipt.sender_tx_receipt.key_images {
key_images.push(KeyImage::from(
hex::decode(&key_image_hex).map_err(|err| format!("{}", err))?,
))
}

sender_receipt.set_key_image_list(RepeatedField::from_vec(key_images));
sender_receipt.set_tombstone(receipt.sender_tx_receipt.tombstone);

let mut req = mc_mobilecoind_api::GetTxStatusAsSenderRequest::new();
req.set_receipt(sender_receipt);

let resp = state
.mobilecoind_api_client
.get_tx_status_as_sender(&req)
.get_tx_status_as_sender(
&mc_mobilecoind_api::SubmitTxResponse::try_from(&submit_response.0)
.map_err(|err| format!("Could not convert JsonSubmitTxResponse: {}", err))?,
)
.map_err(|err| format!("Failed getting status: {}", err))?;

Ok(Json(JsonStatusResponse::from(&resp)))
Expand Down
75 changes: 75 additions & 0 deletions mobilecoind-json/src/data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,63 @@ impl From<&mc_mobilecoind_api::SubmitTxResponse> for JsonSubmitTxResponse {
}
}

impl TryFrom<&JsonSubmitTxResponse> for mc_mobilecoind_api::SubmitTxResponse {
type Error = String;

fn try_from(src: &JsonSubmitTxResponse) -> Result<Self, String> {
let mut sender_receipt = mc_mobilecoind_api::SenderTxReceipt::new();

let key_images: Vec<KeyImage> = src
.sender_tx_receipt
.key_images
.iter()
.map(|k| {
hex::decode(&k).map(KeyImage::from).map_err(|err| {
format!(
"Failed to decode hex for sender_tx_receipt.key_images: {}",
err
)
})
})
.collect::<Result<Vec<KeyImage>, String>>()?;

sender_receipt.set_key_image_list(RepeatedField::from_vec(key_images));
sender_receipt.set_tombstone(src.sender_tx_receipt.tombstone);

let mut receiver_receipts = Vec::new();
for r in src.receiver_tx_receipt_list.iter() {
let mut receiver_receipt = mc_mobilecoind_api::ReceiverTxReceipt::new();
receiver_receipt.set_recipient(
PublicAddress::try_from(&r.recipient)
.map_err(|err| format!("Failed to convert recipient: {}", err))?,
);
let mut pubkey = mc_api::external::CompressedRistretto::new();
pubkey.set_data(
hex::decode(&r.tx_public_key)
.map_err(|err| format!("Failed to decode hex for tx_public_key: {}", err))?,
);
receiver_receipt.set_tx_public_key(pubkey);
receiver_receipt.set_tx_out_hash(
hex::decode(&r.tx_out_hash)
.map_err(|err| format!("Failed to decode hex for tx_out_hash: {}", err))?,
);
receiver_receipt.set_tombstone(r.tombstone);
receiver_receipt.set_confirmation_number(
hex::decode(&r.confirmation_number).map_err(|err| {
format!("Failed to decode hex for confirmation_number: {}", err)
})?,
);
receiver_receipts.push(receiver_receipt);
}

let mut resp = mc_mobilecoind_api::SubmitTxResponse::new();
resp.set_sender_tx_receipt(sender_receipt);
resp.set_receiver_tx_receipt_list(RepeatedField::from_vec(receiver_receipts));

Ok(resp)
}
}

#[derive(Serialize, Default, Debug)]
pub struct JsonStatusResponse {
pub status: String,
Expand All @@ -1016,6 +1073,15 @@ impl From<&mc_mobilecoind_api::GetTxStatusAsSenderResponse> for JsonStatusRespon
mc_mobilecoind_api::TxStatus::Verified => "verified",
mc_mobilecoind_api::TxStatus::TombstoneBlockExceeded => "failed",
mc_mobilecoind_api::TxStatus::InvalidConfirmationNumber => "invalid_confirmation",
mc_mobilecoind_api::TxStatus::PublicKeysInDifferentBlocks => {
"public_keys_in_different_blocks"
}
mc_mobilecoind_api::TxStatus::TransactionFailureKeyImageBlockMismatch => {
"transaction_failure_key_image_block_mismatch"
}
mc_mobilecoind_api::TxStatus::TransactionFailureKeyImageAlreadySpent => {
"transaction_failure_key_image_already_spent"
}
};

Self {
Expand All @@ -1031,6 +1097,15 @@ impl From<&mc_mobilecoind_api::GetTxStatusAsReceiverResponse> for JsonStatusResp
mc_mobilecoind_api::TxStatus::Verified => "verified",
mc_mobilecoind_api::TxStatus::TombstoneBlockExceeded => "failed",
mc_mobilecoind_api::TxStatus::InvalidConfirmationNumber => "invalid_confirmation",
mc_mobilecoind_api::TxStatus::PublicKeysInDifferentBlocks => {
"public_keys_in_different_blocks"
}
mc_mobilecoind_api::TxStatus::TransactionFailureKeyImageBlockMismatch => {
"transaction_failure_key_image_block_mismatch"
}
mc_mobilecoind_api::TxStatus::TransactionFailureKeyImageAlreadySpent => {
"transaction_failure_key_image_already_spent"
}
};

Self {
Expand Down
Loading

0 comments on commit 0dfb34e

Please sign in to comment.