-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0a6f8e
commit 4511800
Showing
3 changed files
with
67 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use core::ops::{Deref, DerefMut}; | ||
use std::sync::Arc; | ||
|
||
use sp_runtime::MultiSignature; | ||
use sp_std::prelude::*; | ||
|
||
use tokio::sync::{Mutex, OwnedMutexGuard}; | ||
|
||
use async_trait::async_trait; | ||
|
||
#[cfg(not(feature = "std"))] | ||
use alloc::string::ToString; | ||
|
||
use crate::*; | ||
|
||
#[derive(Clone)] | ||
pub struct LockableSigner<S>(Arc<Mutex<S>>); | ||
|
||
impl<S: Signer + 'static> LockableSigner<S> { | ||
pub fn new(signer: S) -> Self { | ||
Self(Arc::new(Mutex::new(signer))) | ||
} | ||
|
||
pub async fn lock(&self) -> LockedSigner<S> { | ||
LockedSigner(self.0.clone().lock_owned().await) | ||
} | ||
} | ||
|
||
pub struct LockedSigner<S>(OwnedMutexGuard<S>); | ||
|
||
impl<S> Deref for LockedSigner<S> { | ||
type Target = S; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl<S> DerefMut for LockedSigner<S> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<S: Signer> Signer for LockedSigner<S> { | ||
fn account(&self) -> AccountId { | ||
self.0.account() | ||
} | ||
|
||
async fn nonce(&self) -> Option<u32> { | ||
self.0.nonce().await | ||
} | ||
|
||
async fn set_nonce(&mut self, nonce: u32) { | ||
self.0.set_nonce(nonce).await | ||
} | ||
|
||
async fn sign(&self, msg: &[u8]) -> Result<MultiSignature> { | ||
self.0.sign(msg).await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters