Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseAbram committed Aug 12, 2024
1 parent 9c905d5 commit 94249f6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
28 changes: 21 additions & 7 deletions crates/threshold-signature-server/src/validator/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,10 @@ pub async fn new_reshare(
)
.map_err(|e| ValidatorErr::VerifyingKeyError(e.to_string()))?;

let is_proper_signer = &validators_info
.iter()
.any(|validator_info| validator_info.tss_account == *signer.account_id());
let is_proper_signer =
is_proper_signer(signer.account_id(), validators_info.clone(), &app_state.kv_store).await?;

if !is_proper_signer {
// delete old keyshare if has it and not next_signer
if app_state.kv_store.kv().exists(&hex::encode(NETWORK_PARENT_KEY)).await? {
app_state.kv_store.kv().delete(&hex::encode(NETWORK_PARENT_KEY)).await?
}
return Ok(StatusCode::MISDIRECTED_REQUEST);
}

Expand Down Expand Up @@ -364,3 +359,22 @@ pub async fn prune_old_holders(
validators_info.clone()
})
}

/// Checks if TSS is a proper signer and if isn't deletes their parent key if they have one
pub async fn is_proper_signer(
account_id: &AccountId32,
validators_info: Vec<ValidatorInfo>,
kv_manager: &KvManager,
) -> Result<bool, ValidatorErr> {
let is_proper_signer =
&validators_info.iter().any(|validator_info| validator_info.tss_account == *account_id);
if *is_proper_signer {
Ok(true)
} else {
// delete old keyshare if has it and not next_signer
if kv_manager.kv().exists(&hex::encode(NETWORK_PARENT_KEY)).await? {
kv_manager.kv().delete(&hex::encode(NETWORK_PARENT_KEY)).await?
}
Ok(false)
}
}
22 changes: 21 additions & 1 deletion crates/threshold-signature-server/src/validator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::{
validator::get_signer_and_x25519_secret_from_mnemonic,
},
validator::{
api::{prune_old_holders, validate_new_reshare},
api::{is_proper_signer, prune_old_holders, validate_new_reshare},
errors::ValidatorErr,
},
};
Expand Down Expand Up @@ -255,3 +255,23 @@ async fn test_forbidden_keys() {
let should_pass = check_forbidden_key("test");
assert_eq!(should_pass.unwrap(), ());
}

#[tokio::test]
#[serial]
async fn test_deletes_key() {
initialize_test_logger().await;
clean_tests();

let dave = AccountKeyring::Dave;
let kv = setup_client().await;
let reservation = kv.kv().reserve_key(hex::encode(NETWORK_PARENT_KEY)).await.unwrap();
kv.kv().put(reservation, vec![10]).await.unwrap();

let is_proper_signer_result =
is_proper_signer(&dave.to_account_id().into(), vec![], &kv).await.unwrap();
assert!(!is_proper_signer_result);

let has_key = kv.kv().exists(&hex::encode(NETWORK_PARENT_KEY)).await.unwrap();
assert!(!has_key);
clean_tests();
}

0 comments on commit 94249f6

Please sign in to comment.