Skip to content

Commit

Permalink
Address more review comments from @movekevin
Browse files Browse the repository at this point in the history
  • Loading branch information
alnoki committed May 25, 2023
1 parent a4bbabb commit a6d6be7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 37 deletions.
56 changes: 24 additions & 32 deletions crates/aptos/src/account/multisig_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use crate::common::{
CliCommand, CliError, CliTypedResult, EntryFunctionArguments, MultisigAccount,
MultisigAccountWithSequenceNumber, TransactionOptions, TransactionSummary,
},
utils::get_view_json_option_vec_ref,
utils::view_json_option_hex_as_bytes,
};
use aptos_cached_packages::aptos_stdlib;
use aptos_crypto::HashValue;
use aptos_rest_client::{
aptos_api_types::{HexEncodedBytes, ViewRequest, WriteResource, WriteSetChange},
aptos_api_types::{ViewRequest, WriteResource, WriteSetChange},
Transaction,
};
use aptos_types::{
Expand Down Expand Up @@ -183,41 +183,33 @@ impl CliCommand<serde_json::Value> for CheckTransaction {
],
})
.await?[0];
// Get reference to inner payload option from multisig transaction.
let multisig_payload_option_ref =
get_view_json_option_vec_ref(&multisig_transaction["payload"]);
// Get expected multisig transaction payload bytes from provided entry function.
let expected_multisig_transaction_payload_bytes =
to_bytes::<MultisigTransactionPayload>(&self.entry_function_args.try_into()?)?;
// If only storing payload hash on-chain, get expected hash bytes and actual hash hex:
let (expected_bytes, actual_value_hex_option_ref) =
if multisig_payload_option_ref.is_empty() {
(
HashValue::sha3_256_of(&expected_multisig_transaction_payload_bytes).to_vec(),
get_view_json_option_vec_ref(&multisig_transaction["payload_hash"]),
)
// If full payload stored on-chain, get expected payload bytes and actual payload hex:
} else {
(
expected_multisig_transaction_payload_bytes,
multisig_payload_option_ref,
)
};
// If expected bytes matches actual hex from view function:
if expected_bytes.eq(&actual_value_hex_option_ref[0]
.as_str()
.unwrap()
.parse::<HexEncodedBytes>()?
.inner())
{
// Return success message.
// Get expected multisig transaction payload hash bytes from provided entry function.
let expected_payload_hash_bytes =
HashValue::sha3_256_of(&to_bytes::<MultisigTransactionPayload>(
&self.entry_function_args.try_into()?,
)?)
.to_vec();
// Get actual multisig payload hash field stored on-chain, as bytes.
let mut actual_payload_hash_bytes =
view_json_option_hex_as_bytes(&multisig_transaction["payload_hash"])?;
// If payload hash not stored on-chain, get actual payload hash from on-chain payload.
if actual_payload_hash_bytes.is_empty() {
actual_payload_hash_bytes = HashValue::sha3_256_of(&view_json_option_hex_as_bytes(
&multisig_transaction["payload"],
)?)
.to_vec();
}
// If a match between expected payload hash and actual payload hash, return success message.
if expected_payload_hash_bytes.eq(&actual_payload_hash_bytes) {
Ok(json!({
"Status": "Transaction match",
"Multisig transaction": multisig_transaction
}))
// If a mismatch between expected payload hash and actual payload hash, error out.
} else {
// If a mismatch between expected bytes and actual hex, error out.
Err(CliError::UnexpectedError("Payload mismatch".to_string()))
Err(CliError::UnexpectedError(
"Transaction mismatch".to_string(),
))
}
}
}
Expand Down
23 changes: 18 additions & 5 deletions crates/aptos/src/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use aptos_build_info::build_information;
use aptos_crypto::ed25519::{Ed25519PrivateKey, Ed25519PublicKey};
use aptos_keygen::KeyGen;
use aptos_logger::{debug, Level};
use aptos_rest_client::{aptos_api_types::HashValue, Account, Client, State};
use aptos_rest_client::{
aptos_api_types::{HashValue, HexEncodedBytes},
Account, Client, State,
};
use aptos_telemetry::service::telemetry_is_disabled;
use aptos_types::{
account_address::create_multisig_account_address,
Expand Down Expand Up @@ -481,9 +484,19 @@ pub fn parse_json_file<T: for<'a> Deserialize<'a>>(path_ref: &Path) -> CliTypedR
})
}

/// Return reference to inner option vector for view function JSON field known to have option.
/// Convert a view function JSON field, known to have optional hex, into a bytes vector.
///
/// View functions represent an option as a JSON array titled `vec`.
pub fn get_view_json_option_vec_ref(value_ref: &serde_json::Value) -> &Vec<serde_json::Value> {
value_ref["vec"].as_array().unwrap()
/// A view function return represents an option via an inner JSON array titled `vec`.
pub fn view_json_option_hex_as_bytes(option_ref: &serde_json::Value) -> CliTypedResult<Vec<u8>> {
let option_vec_ref = option_ref["vec"].as_array().unwrap();
if option_vec_ref.is_empty() {
Ok(vec![])
} else {
Ok(option_vec_ref[0]
.as_str()
.unwrap()
.parse::<HexEncodedBytes>()?
.inner()
.to_vec())
}
}

0 comments on commit a6d6be7

Please sign in to comment.