Skip to content

Commit

Permalink
feat: pjs vault
Browse files Browse the repository at this point in the history
  • Loading branch information
S0c5 committed Apr 26, 2024
1 parent f946cd5 commit 4c590fb
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 6 deletions.
7 changes: 4 additions & 3 deletions libwallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ serde = {version = "1.0", default-features = false, features = ["derive"], optio
hmac = {version = "0.12.1", default-features = false, optional = true}
pbkdf2 = {version = "0.11.0", default-features = false, optional = true}
sha2 = {version = "0.10.2", default-features = false, optional = true}

mnemonic = {package = "bip0039", version = "0.10.1", default-features = false, optional = true}
pjs = { git= "https://github.com/S0c5/pjs-rs.git", branch = "feat/get-current-account", optional = true }
mnemonic = { package = "bip0039", version = "0.10.1", default-features = false, optional = true}

rand_core = {version = "0.6.3", optional = true}
# substrate related
Expand All @@ -35,7 +35,7 @@ serde_json = {version = "1.0", default-features = false, features = ["alloc"]}
dirs = "4.0"

[features]
default = ["std", "substrate", "vault_simple", "mnemonic", "rand", "vault_pass", "vault_os", "util_pin"]
default = ["std", "substrate", "vault_simple", "mnemonic", "rand", "vault_pass", "vault_os", "util_pin", "vault_pjs"]
rand = ["rand_core", "schnorrkel?/getrandom"]
sr25519 = ["dep:schnorrkel"]
std = [
Expand All @@ -46,6 +46,7 @@ util_pin = ["pbkdf2", "hmac", "sha2"]
vault_os = ["keyring"]
vault_pass = ["prs-lib"]
vault_simple = []
vault_pjs = ["pjs"]

[workspace]
members = [
Expand Down
14 changes: 11 additions & 3 deletions libwallet/src/vault.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
//! Collection of supported Vault backends
#[cfg(feature = "vault_os")]
mod os;
// #[cfg(feature = "vault_pass")]
mod pass;
mod simple;

#[cfg(feature = "vault_os")]
pub use os::*;
#[cfg(feature = "vault_pjs")]
pub mod pjs;
#[cfg(feature = "vault_pass")]
mod pass;
mod simple;

#[cfg(feature = "vault_pass")]
pub use pass::*;
pub use simple::*;


#[cfg(feature = "vault_pjs")]
pub use pjs::*;


use crate::account::Account;

/// Abstraction for storage of private keys that are protected by some credentials.
Expand Down
64 changes: 64 additions & 0 deletions libwallet/src/vault/pjs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use crate::{Signer, Vault};
use core::marker::PhantomData;
use pjs::{Account, Error, PjsExtension};


#[derive(Clone)]
struct Pjs {
inner: PjsExtension,
}

impl Pjs {
pub async fn connect(name: &str) -> Result<Self, Error> {
Pjs {
inner: PjsExtension::connect(name).await?,
}
}

pub async fn list_accounts(&mut self) -> Result<Vec<Account>, Error> {
self.inner.fetch_accounts().await?;
self.inner.accounts()
}

pub fn select_account(&mut self, idx: u8) {
self.inner.select_account(idx)
}
}

impl Signer for Pjs {
type Signature = AsRef<[u8]>;

async fn sign_msg<M: AsRef<[u8]>>(&self, msg: M) -> Result<Self::Signature, ()> {
self.inner.sign(msg).await.map_err(|_| Err(()))
}

async fn verify<M: AsRef<[u8]>>(&self, msg: M, sig: &[u8]) -> bool {
unimplemented!()
}
}

impl Account for Pjs {
fn public(&self) -> impl crate::Public {
self.inner
.current_account()
.expect("an account must be defined")
.address()
}
}

impl Vault for Pjs {
type Id = u8;
type Credentials = String;
type Account = Account;
type Error = Error;

async fn unlock(
&mut self,
account: Self::Id,
cred: impl Into<Self::Credentials>,
) -> Result<Self::Account, Self::Error> {
let pjs_signer = self.clone();
pjs_signer.select_account(account);
return Ok(pjs_signer)
}
}

0 comments on commit 4c590fb

Please sign in to comment.