Use async-trait (#264)

* Use async-trait

* Use async-trait for store trait too
This commit is contained in:
Liu-Cheng Xu
2021-04-14 23:20:24 +08:00
committed by GitHub
parent 02960a3b0e
commit 0415b5d1f4
5 changed files with 57 additions and 49 deletions
+7 -9
View File
@@ -30,12 +30,9 @@ use sp_runtime::traits::{
SignedExtension,
Verify,
};
use std::{
future::Future,
pin::Pin,
};
/// Extrinsic signer.
#[async_trait::async_trait]
pub trait Signer<T: Runtime> {
/// Returns the account id.
fn account_id(&self) -> &T::AccountId;
@@ -47,10 +44,10 @@ pub trait Signer<T: Runtime> {
///
/// Some signers may fail, for instance because the hardware on which the keys are located has
/// refused the operation.
fn sign(
async fn sign(
&self,
extrinsic: SignedPayload<T>,
) -> Pin<Box<dyn Future<Output = Result<UncheckedExtrinsic<T>, String>> + Send>>;
) -> Result<UncheckedExtrinsic<T>, String>;
}
/// Extrinsic signer using a private key.
@@ -96,6 +93,7 @@ where
}
}
#[async_trait::async_trait]
impl<T, P> Signer<T> for PairSigner<T, P>
where
T: Runtime,
@@ -112,10 +110,10 @@ where
self.nonce
}
fn sign(
async fn sign(
&self,
extrinsic: SignedPayload<T>,
) -> Pin<Box<dyn Future<Output = Result<UncheckedExtrinsic<T>, String>> + Send>> {
) -> Result<UncheckedExtrinsic<T>, String> {
let signature = extrinsic.using_encoded(|payload| self.signer.sign(payload));
let (call, extra, _) = extrinsic.deconstruct();
let extrinsic = UncheckedExtrinsic::<T>::new_signed(
@@ -124,6 +122,6 @@ where
signature.into(),
extra,
);
Box::pin(async move { Ok(extrinsic) })
Ok(extrinsic)
}
}