Skip to content

Commit

Permalink
Add experimental async support
Browse files Browse the repository at this point in the history
  • Loading branch information
olanod committed Nov 29, 2022
1 parent 1a5d860 commit 3247709
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 20 deletions.
1 change: 1 addition & 0 deletions rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nightly
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![feature(async_fn_in_trait, impl_trait_projections)]
// #![feature(result_option_inspect)]
#![cfg_attr(not(feature = "std"), no_std)]
//! `libwallet` is the one-stop tool to build easy, slightly opinionated crypto wallets
Expand All @@ -14,7 +15,7 @@ mod key_pair;
mod substrate_ext;

use arrayvec::ArrayVec;
use core::{convert::TryInto, fmt, future::Future};
use core::{convert::TryInto, fmt};
use key_pair::any::AnySignature;

#[cfg(feature = "mnemonic")]
Expand All @@ -32,13 +33,12 @@ type Message = ArrayVec<u8, { MSG_MAX_SIZE }>;
/// Abstration for storage of private keys that are protected by some credentials.
pub trait Vault {
type Credentials;
type AuthDone: Future<Output = core::result::Result<(), Self::Error>>;
type Error;

/// Use a set of credentials to make the guarded keys available to the user.
/// It returns a `Future` to allow for vaults that might take an arbitrary amount
/// of time getting the secret ready like waiting for some user phisical interaction.
fn unlock(&mut self, cred: impl Into<Self::Credentials>) -> Self::AuthDone;
async fn unlock(&mut self, cred: impl Into<Self::Credentials>) -> Result<(), Self::Error>;

/// Get the root account container of the supported private key pairs
/// if the vault hasn't been unlocked it should return `None`
Expand Down
9 changes: 3 additions & 6 deletions src/vault/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,11 @@ impl std::error::Error for Error {}

impl Vault for OSKeyring {
type Credentials = Pin;
type AuthDone = Ready<Result<(), Self::Error>>;
type Error = Error;

fn unlock(&mut self, pin: impl Into<Self::Credentials>) -> Self::AuthDone {
async fn unlock(&mut self, pin: impl Into<Self::Credentials>) -> Result<(), Self::Error> {
let pin = pin.into();
let res = self
.get_key(&pin)
self.get_key(&pin)
.or_else(|err| {
self.auto_generate
.ok_or(err)
Expand All @@ -106,8 +104,7 @@ impl Vault for OSKeyring {
.and_then(move |r| {
self.root = Some(r);
Ok(())
});
core::future::ready(res)
})
}

fn get_root(&self) -> Option<&RootAccount> {
Expand Down
9 changes: 3 additions & 6 deletions src/vault/pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,10 @@ pub struct PassCreds {
impl Vault for Pass {
type Credentials = PassCreds;
type Error = Error;
type AuthDone = Ready<Result<(), Self::Error>>;

fn unlock(&mut self, creds: impl Into<Self::Credentials>) -> Self::AuthDone {
async fn unlock(&mut self, creds: impl Into<Self::Credentials>) -> Result<(), Self::Error> {
let creds = creds.into();
let res = self
.get_key(&creds)
self.get_key(&creds)
.or_else(|err| {
self.auto_generate
.ok_or(err)
Expand All @@ -132,8 +130,7 @@ impl Vault for Pass {
.and_then(move |r| {
self.root = Some(r);
Ok(())
});
core::future::ready(res)
})
}

fn get_root(&self) -> Option<&RootAccount> {
Expand Down
7 changes: 2 additions & 5 deletions src/vault/simple.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::future::{ready, Ready};

use crate::{RootAccount, Vault};

/// A vault that holds secrets in memory
Expand Down Expand Up @@ -80,11 +78,10 @@ impl std::error::Error for Error {}
impl Vault for Simple {
type Credentials = ();
type Error = Error;
type AuthDone = Ready<Result<(), Self::Error>>;

fn unlock(&mut self, _cred: impl Into<Self::Credentials>) -> Self::AuthDone {
async fn unlock(&mut self, _cred: impl Into<Self::Credentials>) -> Result<(), Self::Error> {
self.unlocked = self.locked.take();
ready(Ok(()))
Ok(())
}

fn get_root(&self) -> Option<&RootAccount> {
Expand Down

0 comments on commit 3247709

Please sign in to comment.