Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transaction Analysis allow unknown accounts #307

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/sargon-uniffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sargon-uniffi"
# Don't forget to update version in crates/sargon/Cargo.toml
version = "1.1.88"
version = "1.1.89"
edition = "2021"
build = "build.rs"

Expand Down
2 changes: 1 addition & 1 deletion crates/sargon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sargon"
# Don't forget to update version in crates/sargon-uniffi/Cargo.toml
version = "1.1.88"
version = "1.1.89"
edition = "2021"
build = "build.rs"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ mod tests {

#[test]
fn preprocessor_init_fail() {
let intent_with_invalid_persona =
TransactionIntent::sample_entity_addresses_requiring_auth(
vec![],
vec![Persona::sample_mainnet().address],
);

let result = ExtractorOfInstancesRequiredToSignTransactions::extract(
&Profile::sample_other(),
vec![TransactionIntent::sample()],
vec![intent_with_invalid_persona],
RoleKind::Primary,
);

assert!(matches!(result, Err(CommonError::UnknownAccount)));
assert!(matches!(result, Err(CommonError::UnknownPersona)));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still invalid for unknown persona. We might change it in the future, but feels strange for a manifest to contain an unknown persona.

}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions crates/sargon/src/signing/collector/signatures_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ mod tests {
}

#[test]
fn invalid_profile_unknown_account() {
fn profile_with_unknown_account() {
let res = SignaturesCollector::new(
SigningFinishEarlyStrategy::default(),
[TransactionIntent::sample_entities_requiring_auth(
Expand All @@ -446,7 +446,7 @@ mod tests {
&Profile::sample_from(IndexSet::new(), [], []),
RoleKind::Primary,
);
assert!(matches!(res, Err(CommonError::UnknownAccount)));
assert!(res.is_ok());
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub struct ExtractorOfEntitiesRequiringAuth;
impl ExtractorOfEntitiesRequiringAuth {
/// Matches entities requiring auth from a manifest summary with the entities in the given profile.
/// Returns a set of `AccountOrPersona` or empty if the manifest summary does not require auth.
/// Returns an error if an account or persona is unknown.
/// Returns an error if persona is unknown.
pub fn extract(
profile: &Profile,
summary: ManifestSummary,
Expand All @@ -17,7 +17,8 @@ impl ExtractorOfEntitiesRequiringAuth {
.addresses_of_accounts_requiring_auth
.iter()
.map(|a| profile.account_by_address(*a))
.collect::<Result<Vec<_>>>()?;
.filter_map(|a| a.ok())
.collect::<Vec<_>>();
Comment on lines +20 to +21
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual fix.


entities_requiring_auth.extend(
accounts
Expand Down Expand Up @@ -45,6 +46,7 @@ impl ExtractorOfEntitiesRequiringAuth {
#[cfg(test)]
mod tests {
use super::*;
use indexmap::IndexSet;
use radix_transactions::prelude::ManifestBuilder;

#[test]
Expand All @@ -67,7 +69,7 @@ mod tests {
manifest_summary,
);

assert!(matches!(result, Err(CommonError::UnknownAccount)));
assert_eq!(result, Ok(IndexSet::new()));
}

#[test]
Expand Down
62 changes: 39 additions & 23 deletions crates/sargon/src/system/sargon_os/sargon_os_signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,45 @@ mod test {
);
}

#[actix_rt::test]
async fn test_sign_transaction_intent_only_with_irrelevant_entity() {
let profile = Profile::sample();
let sut = boot_with_profile(&profile, None).await;

let irrelevant_account = Account::sample_mainnet_third();
let transaction = TransactionIntent::sample_entities_requiring_auth(
vec![&irrelevant_account],
vec![],
);

let outcome = sut
.sign_transaction(transaction, RoleKind::Primary)
.await
.unwrap();

assert_eq!(outcome.intent_signatures.signatures.len(), 0);
}

#[actix_rt::test]
async fn test_sign_transaction_intent_containing_irrelevant_entity() {
let profile = Profile::sample();
let sut = boot_with_profile(&profile, None).await;

let irrelevant_account = Account::sample_mainnet_third();
let relevant_account = Account::sample_mainnet();
let transaction = TransactionIntent::sample_entities_requiring_auth(
vec![&irrelevant_account, &relevant_account],
vec![],
);

let outcome = sut
.sign_transaction(transaction, RoleKind::Primary)
.await
.unwrap();

assert_eq!(outcome.intent_signatures.signatures.len(), 1);
}

#[actix_rt::test]
async fn test_sign_transaction_intent_rejected_due_to_all_factors_neglected(
) {
Expand Down Expand Up @@ -283,29 +322,6 @@ mod test {
assert_eq!(outcome, Err(CommonError::SigningRejected));
}

#[actix_rt::test]
async fn test_sign_fail_due_to_irrelevant_entity() {
let profile = Profile::sample();
let sut = boot_with_profile(
&profile,
Some(SigningFailure::NeglectedFactorSources(vec![
profile.device_factor_sources().first().unwrap().id,
])),
)
.await;

let irrelevant_account = Account::sample_mainnet_third();
let transaction = TransactionIntent::sample_entities_requiring_auth(
vec![&irrelevant_account],
vec![],
);

let outcome =
sut.sign_transaction(transaction, RoleKind::Primary).await;

assert_eq!(outcome, Err(CommonError::UnknownAccount));
}

async fn boot_with_profile(
profile: &Profile,
maybe_signing_failure: Option<SigningFailure>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,47 +544,6 @@ mod transaction_preview_analysis_tests {
)
}

#[actix_rt::test]
async fn signer_entities_not_found() {
let responses = prepare_responses(
LedgerState {
network: "".to_string(),
state_version: 0,
proposer_round_timestamp: "".to_string(),
epoch: 0,
round: 0,
},
TransactionPreviewResponse {
encoded_receipt: "".to_string(),
radix_engine_toolkit_receipt: Some(
ScryptoSerializableToolkitTransactionReceipt::Reject {
reason: "Test".to_string(),
},
),
logs: vec![],
receipt: TransactionReceipt {
status: TransactionReceiptStatus::Succeeded,
error_message: None,
},
},
);
let os =
prepare_os(MockNetworkingDriver::new_with_bodies(200, responses))
.await;

let result = os
.analyse_transaction_preview(
TransactionManifest::sample().instructions_string(),
Blobs::sample(),
true,
Nonce::sample(),
PublicKey::sample(),
)
.await;

assert_eq!(result, Err(CommonError::UnknownAccount))
}

#[actix_rt::test]
async fn execution_summary_parse_error() {
let responses = prepare_responses(
Expand Down Expand Up @@ -731,6 +690,51 @@ mod transaction_preview_analysis_tests {
)
}

#[actix_rt::test]
async fn signer_entities_not_found() {
let responses = prepare_responses(
LedgerState {
network: "".to_string(),
state_version: 0,
proposer_round_timestamp: "".to_string(),
epoch: 0,
round: 0,
},
TransactionPreviewResponse {
encoded_receipt: "".to_string(),
radix_engine_toolkit_receipt: Some(
ScryptoSerializableToolkitTransactionReceipt::Reject {
reason: "Test".to_string(),
},
),
logs: vec![],
receipt: TransactionReceipt {
status: TransactionReceiptStatus::Succeeded,
error_message: None,
},
},
);
let os =
prepare_os(MockNetworkingDriver::new_with_bodies(200, responses))
.await;

let result = os
.analyse_transaction_preview(
TransactionManifest::sample().instructions_string(),
Blobs::sample(),
true,
Nonce::sample(),
PublicKey::sample(),
)
.await;

// Just asserts that the execution path reached GW preview call
assert!(matches!(
result,
Err(CommonError::ExecutionSummaryFail { .. })
))
}

#[actix_rt::test]
async fn analyse_open_pre_auth_preview() {
let os = prepare_os(MockNetworkingDriver::new_always_failing()).await;
Expand Down
Loading