Skip to content

Commit

Permalink
Fix no_std support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Neopallium committed Jun 21, 2024
1 parent b0a6f8e commit 4511800
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 53 deletions.
5 changes: 5 additions & 0 deletions crates/polymesh-api-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ pub use block::*;
pub mod signer;
pub use signer::*;

#[cfg(feature = "std")]
pub mod lockable_signer;
#[cfg(feature = "std")]
pub use lockable_signer::*;

pub mod transaction;
pub use transaction::*;

Expand Down
62 changes: 62 additions & 0 deletions crates/polymesh-api-client/src/lockable_signer.rs
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
}
}
53 changes: 0 additions & 53 deletions crates/polymesh-api-client/src/signer.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
#[cfg(feature = "std")]
use sp_core::Pair;

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"))]
Expand Down Expand Up @@ -81,54 +76,6 @@ pub mod dev {
}
}

#[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
}
}

#[async_trait]
pub trait Signer: Send + Sync {
fn account(&self) -> AccountId;
Expand Down

0 comments on commit 4511800

Please sign in to comment.