Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions lee/state_machine/src/public_transaction/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,30 @@ pub mod tests {
assert!(matches!(result, Err(LeeError::InvalidInput(_))));
}

#[test]
fn witness_set_cant_have_duplicate_signers() {
Comment thread
erhant marked this conversation as resolved.
Resolved
let (key1, _, addr1, addr2) = keys_for_tests();
let state = state_for_tests();
// both nonces match the current state, so only the repeat is at fault
let nonces = vec![0_u128.into(), 0_u128.into()];
let instruction = 1337;
let message = Message::try_new(
crate::test_methods::simple_balance_transfer().id().into(),
vec![addr1, addr2],
nonces,
instruction,
)
.unwrap();

let witness_set = WitnessSet::for_message(&message, &[&key1, &key1]);
let tx = PublicTransaction::new(message, witness_set);
let result = ValidatedStateDiff::from_public_transaction(&tx, &state, 1, 0);
assert!(matches!(
result,
Err(LeeError::InvalidInput(msg)) if msg.contains("Duplicate signers")
));
}

#[test]
fn number_of_nonces_must_match_number_of_signatures() {
let (key1, key2, addr1, addr2) = keys_for_tests();
Expand Down
37 changes: 37 additions & 0 deletions lee/state_machine/src/state/tests/privacy_preserving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,43 @@ fn transition_from_privacy_preserving_transaction_shielded() {
);
}

#[test]
fn privacy_preserving_witness_set_cant_have_duplicate_signers() {
let sender_keys = test_public_account_keys_1();
let recipient_keys = test_private_account_keys_1();

let mut state = V03State::new().with_public_accounts([(
sender_keys.account_id(),
Account {
program_owner: crate::test_methods::simple_balance_transfer().id().into(),
balance: 200,
..Account::default()
},
)]);
register_program(&mut state, &crate::test_methods::simple_balance_transfer());

let tx = shielded_balance_transfer_for_tests(&sender_keys, &recipient_keys, 37, &state);

// Re-sign the same message with the sender twice; both nonces match the
// current state, so only the repeat is at fault.
let (_, proof) = tx.witness_set.into_raw_parts();
let mut message = tx.message;
let nonce = message.nonces[0];
message.nonces = vec![nonce, nonce];
let witness_set = WitnessSet::for_message(
&message,
proof,
&[&sender_keys.signing_key, &sender_keys.signing_key],
);
let tx = PrivacyPreservingTransaction::new(message, witness_set);

let result = state.transition_from_privacy_preserving_transaction(&tx, 1, 0);
assert!(matches!(
result,
Err(LeeError::InvalidInput(msg)) if msg.contains("Duplicate signers")
));
}

#[test]
fn transition_from_privacy_preserving_transaction_private() {
let sender_keys = test_private_account_keys_1();
Expand Down
16 changes: 14 additions & 2 deletions lee/state_machine/src/validated_state_diff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,13 +623,19 @@ impl ValidatedStateDiff {
)
);

// Check that there are no duplicate signers
let signer_account_ids = tx.signer_account_ids();
ensure!(
n_unique(&signer_account_ids) == signer_account_ids.len(),
LeeError::InvalidInput("Duplicate signers found in witness set".into())
);

// Check the signatures are valid
ensure!(
witness_set.signatures_are_valid_for(message),
LeeError::InvalidInput("Invalid signature for given message and public key".into())
);

let signer_account_ids = tx.signer_account_ids();
// Check nonces corresponds to the current nonces on the public state.
for (account_id, nonce) in signer_account_ids.iter().zip(&message.nonces) {
let current_nonce = state.get_account_by_id(*account_id).nonce;
Expand Down Expand Up @@ -766,12 +772,18 @@ fn authenticate_public_transaction_signers(
)
);

// A repeated signer would advance its nonce once per entry.
let signer_account_ids = tx.signer_account_ids();
ensure!(
n_unique(&signer_account_ids) == signer_account_ids.len(),
LeeError::InvalidInput("Duplicate signers found in witness set".into())
);

ensure!(
witness_set.is_valid_for(message),
LeeError::InvalidInput("Invalid signature for given message and public key".into())
);

let signer_account_ids = tx.signer_account_ids();
for (account_id, nonce) in signer_account_ids.iter().zip(&message.nonces) {
let current_nonce = state.get_account_by_id(*account_id).nonce;
ensure!(
Expand Down
15 changes: 13 additions & 2 deletions lez/common/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,25 @@ impl LeeTransaction {
// TODO: Introduce type-safe wrapper around checked transaction, e.g. AuthenticatedTransaction
pub fn transaction_stateless_check(self) -> Result<Self, TransactionMalformationError> {
// Stateless checks here
let has_duplicate_signers = |keys: &[(lee::Signature, lee::PublicKey)]| {
let unique: std::collections::HashSet<AccountId> =
keys.iter().map(|(_, pk)| AccountId::from(pk)).collect();
unique.len() != keys.len()
};
match self {
Self::Public(tx) => {
if tx.witness_set().is_valid_for(tx.message()) {
if has_duplicate_signers(tx.witness_set().signatures_and_public_keys()) {
Err(TransactionMalformationError::DuplicateSigner)
} else if tx.witness_set().is_valid_for(tx.message()) {
Ok(Self::Public(tx))
} else {
Err(TransactionMalformationError::InvalidSignature)
}
}
Self::PrivacyPreserving(tx) => {
if tx.witness_set().signatures_are_valid_for(tx.message()) {
if has_duplicate_signers(tx.witness_set().signatures_and_public_keys()) {
Err(TransactionMalformationError::DuplicateSigner)
} else if tx.witness_set().signatures_are_valid_for(tx.message()) {
Ok(Self::PrivacyPreserving(tx))
} else {
Err(TransactionMalformationError::InvalidSignature)
Expand Down Expand Up @@ -165,6 +174,8 @@ pub enum TxKind {
pub enum TransactionMalformationError {
#[error("Invalid signature(-s)")]
InvalidSignature,
#[error("Duplicate signer in witness set")]
DuplicateSigner,
#[error("Failed to decode transaction with hash: {tx:?}")]
FailedToDecode { tx: HashType },
#[error("Transaction size {size} exceeds maximum allowed size of {max} bytes")]
Expand Down
Loading