Skip to content

Commit fa28a30

Browse files
authored
Refactor the CheckBioauthTx (#1366)
* Refactor the CheckBioauthTx * Fix tests
1 parent d36f75d commit fa28a30

2 files changed

Lines changed: 33 additions & 26 deletions

File tree

crates/pallet-bioauth/src/lib.rs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use codec::{Decode, Encode, MaxEncodedLen};
88
use frame_support::{
9-
dispatch::DispatchInfo,
109
traits::{ConstU32, IsSubType, StorageVersion},
1110
BoundedVec,
1211
};
@@ -15,7 +14,7 @@ use scale_info::TypeInfo;
1514
#[cfg(feature = "std")]
1615
use serde::{Deserialize, Serialize};
1716
use sp_runtime::{
18-
traits::{DispatchInfoOf, Dispatchable, SignedExtension},
17+
traits::{DispatchInfoOf, SignedExtension},
1918
transaction_validity::{TransactionValidity, TransactionValidityError},
2019
};
2120
use sp_std::{fmt::Debug, marker::PhantomData, prelude::*};
@@ -612,17 +611,9 @@ pub mod pallet {
612611
Ok(auth_ticket)
613612
}
614613

615-
pub fn check_tx(call: &Call<T>) -> TransactionValidity {
616-
let transaction = match call {
617-
Call::authenticate { req: transaction } => transaction,
618-
// Deny all unknown transactions.
619-
_ => {
620-
// The only supported transaction by this pallet is `authenticate`, so anything
621-
// else is illegal.
622-
return Err(TransactionValidityError::Invalid(InvalidTransaction::Call));
623-
}
624-
};
625-
614+
pub fn check_tx(
615+
transaction: &Authenticate<T::OpaqueAuthTicket, T::RobonodeSignature>,
616+
) -> TransactionValidity {
626617
let auth_ticket =
627618
Self::extract_auth_ticket_checked(transaction.clone()).map_err(|error| {
628619
log::error!("Auth Ticket could not be extracted: {error:?}");
@@ -745,7 +736,6 @@ impl<T: Config + Send + Sync> Debug for CheckBioauthTx<T> {
745736

746737
impl<T: Config + Send + Sync> SignedExtension for CheckBioauthTx<T>
747738
where
748-
T::RuntimeCall: Dispatchable<Info = DispatchInfo>,
749739
<T as frame_system::Config>::RuntimeCall: IsSubType<Call<T>>,
750740
{
751741
type AccountId = T::AccountId;
@@ -770,26 +760,43 @@ where
770760

771761
fn validate(
772762
&self,
773-
who: &Self::AccountId,
763+
_who: &Self::AccountId,
774764
call: &Self::Call,
775765
_info: &DispatchInfoOf<Self::Call>,
776766
_len: usize,
777767
) -> TransactionValidity {
778-
let _account_id = who;
779-
match call.is_sub_type() {
780-
Some(call) => Pallet::<T>::check_tx(call),
781-
_ => Ok(Default::default()),
782-
}
768+
Self::do_validate(call)
783769
}
784770

785771
fn validate_unsigned(
786772
call: &Self::Call,
787773
_info: &DispatchInfoOf<Self::Call>,
788774
_len: usize,
789775
) -> TransactionValidity {
790-
match call.is_sub_type() {
791-
Some(call) => Pallet::<T>::check_tx(call),
792-
_ => Ok(Default::default()),
793-
}
776+
Self::do_validate(call)
777+
}
778+
}
779+
780+
impl<T: Config + Send + Sync> CheckBioauthTx<T>
781+
where
782+
<T as frame_system::Config>::RuntimeCall: IsSubType<Call<T>>,
783+
{
784+
/// Perform the call validation.
785+
///
786+
/// For the `authenticate` call checks the validity and correctness and lets everything else
787+
/// through.
788+
fn do_validate(call: &T::RuntimeCall) -> TransactionValidity {
789+
// Reduce to calls from this pallet.
790+
let Some(call) = call.is_sub_type() else {
791+
return Ok(Default::default());
792+
};
793+
794+
// Reduce to `authenticate` call.
795+
let Call::authenticate { req: transaction } = call else {
796+
return Ok(Default::default());
797+
};
798+
799+
// Check the `authenticate` transaction.
800+
Pallet::<T>::check_tx(transaction)
794801
}
795802
}

crates/pallet-bioauth/src/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
use std::ops::Div;
55

66
use frame_support::{
7-
assert_err, assert_noop, assert_ok, assert_storage_noop, pallet_prelude::*, traits::ConstU32,
8-
BoundedVec,
7+
assert_err, assert_noop, assert_ok, assert_storage_noop, dispatch::DispatchInfo,
8+
pallet_prelude::*, traits::ConstU32, BoundedVec,
99
};
1010
use mockall::predicate;
1111

0 commit comments

Comments
 (0)