Skip to content

Commit

Permalink
[authenticator] improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
davidiw committed Oct 17, 2023
1 parent 7e4e270 commit dabacb9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
30 changes: 15 additions & 15 deletions api/types/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1323,21 +1323,21 @@ impl TryFrom<MultiKeySignature> for AccountAuthenticator {

let mut signatures = vec![];
for indexed_signature in value.signatures {
let signature = match indexed_signature.signature {
Signature::Ed25519(s) => {
let signature = s
.inner()
.try_into()
.context("Failed to parse given public_key bytes as Ed25519Signature")?;
AnySignature::ed25519(signature)
},
Signature::Secp256k1Ecdsa(s) => {
let signature = s.inner().try_into().context(
"Failed to parse given public_key bytes as Secp256k1EcdsaSignature",
)?;
AnySignature::secp256k1_ecdsa(signature)
},
};
let signature =
match indexed_signature.signature {
Signature::Ed25519(s) => {
let signature = s.inner().try_into().context(
"Failed to parse given public_key bytes as Ed25519Signature",
)?;
AnySignature::ed25519(signature)
},
Signature::Secp256k1Ecdsa(s) => {
let signature = s.inner().try_into().context(
"Failed to parse given signature as Secp256k1EcdsaSignature",
)?;
AnySignature::secp256k1_ecdsa(signature)
},
};
signatures.push((indexed_signature.index, signature));
}

Expand Down
16 changes: 11 additions & 5 deletions types/src/transaction/authenticator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,8 @@ impl MultiKeyAuthenticator {
pub fn new(public_keys: MultiKey, signatures: Vec<(u8, AnySignature)>) -> Result<Self> {
ensure!(
public_keys.len() < (u8::MAX as usize),
"Too many public keys in MultiKeyAuthenticator."
"Too many public keys, {}, in MultiKeyAuthenticator.",
public_keys.len(),
);

let mut signatures_bitmap = aptos_bitvec::BitVec::with_num_bits(public_keys.len() as u16);
Expand All @@ -749,11 +750,14 @@ impl MultiKeyAuthenticator {
for (idx, signature) in signatures {
ensure!(
(idx as usize) < public_keys.len(),
"Signature index is out of public key range."
"Signature index is out of public key range, {} < {}.",
idx,
public_keys.len(),
);
ensure!(
!signatures_bitmap.is_set(idx as u16),
"Duplicate signature index."
"Duplicate signature index, {}.",
idx
);
signatures_bitmap.set(idx as u16);
any_signatures.push(signature);
Expand Down Expand Up @@ -783,11 +787,13 @@ impl MultiKeyAuthenticator {
pub fn verify<T: Serialize + CryptoHash>(&self, message: &T) -> Result<()> {
ensure!(
self.signatures_bitmap.count_ones() as usize == self.signatures.len(),
"Mismatch in signatures and signatures_bitmap"
"Mismatch in number of signatures and the number of bits set in the signatures_bitmap, {} != {}.", self.signatures_bitmap.count_ones(), self.signatures.len(),
);
ensure!(
self.signatures.len() >= self.public_keys.signatures_required() as usize,
"Not enough signatures for verification"
"Not enough signatures for verification, {} < {}.",
self.signatures.len(),
self.public_keys.signatures_required(),
);
for (idx, signature) in
std::iter::zip(self.signatures_bitmap.iter_ones(), self.signatures.iter())
Expand Down

0 comments on commit dabacb9

Please sign in to comment.