From 9e205ea1a5ac712142407160455c16c1151b6925 Mon Sep 17 00:00:00 2001 From: david barinas Date: Sun, 28 Apr 2024 10:57:18 -0500 Subject: [PATCH] refactor: Refactor code structure and improve async functions in Pjs struct. Add missing imports and fix formatting issues --- libwallet/src/vault/pjs.rs | 33 +++++++++++++++++++-------------- pjs-rs/Cargo.toml | 2 +- pjs-rs/src/lib.rs | 3 +++ 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/libwallet/src/vault/pjs.rs b/libwallet/src/vault/pjs.rs index d94d0cc..6f44f35 100644 --- a/libwallet/src/vault/pjs.rs +++ b/libwallet/src/vault/pjs.rs @@ -1,23 +1,22 @@ - -use pjs::{Error, PjsExtension, Account as PjsAccount}; -use crate::{any::AnySignature, Account, Signature, Signer, Vault}; -use core::{fmt::Display, marker::PhantomData}; - +extern crate alloc; +use crate::{any::AnySignature, Account, Signer, Vault}; +use pjs::{Account as PjsAccount, Error, PjsExtension}; +use alloc::vec::Vec; #[derive(Clone)] -struct Pjs { +pub struct Pjs { inner: PjsExtension, } impl Pjs { pub async fn connect(name: &str) -> Result { - Pjs { + Ok(Pjs { inner: PjsExtension::connect(name).await?, - } + }) } pub async fn list_accounts(&mut self) -> Result, Error> { self.inner.fetch_accounts().await?; - self.inner.accounts() + Ok(self.inner.accounts()) } pub fn select_account(&mut self, idx: u8) { @@ -29,7 +28,8 @@ impl Signer for Pjs { type Signature = AnySignature; async fn sign_msg(&self, msg: impl AsRef<[u8]>) -> Result { - self.inner.sign(msg).await.map_err(|_| Err(())) + let sig = self.inner.sign(msg.as_ref()).await.map_err(|_| ())?; + Ok(AnySignature::from(sig)) } async fn verify(&self, _: impl AsRef<[u8]>, _: impl AsRef<[u8]>) -> bool { @@ -39,10 +39,15 @@ impl Signer for Pjs { impl Account for Pjs { fn public(&self) -> impl crate::Public { - self.inner + let mut key = [0u8; 32]; + + let pub_key = self.inner .get_selected() .expect("an account must be defined") - .address() + .address(); + + key.copy_from_slice(pub_key.as_bytes()); + key } } @@ -57,14 +62,14 @@ impl core::fmt::Display for Pjs { impl Vault for Pjs { type Id = u8; - type Credentials = String; + type Credentials = (); type Account = Pjs; type Error = Error; async fn unlock( &mut self, account: Self::Id, - cred: impl Into, + _: impl Into, ) -> Result { let mut pjs_signer = self.clone(); pjs_signer.select_account(account); diff --git a/pjs-rs/Cargo.toml b/pjs-rs/Cargo.toml index bed4478..585afa6 100644 --- a/pjs-rs/Cargo.toml +++ b/pjs-rs/Cargo.toml @@ -14,4 +14,4 @@ features = [ ] [lib] -crate-type = ["cdylib"] +crate-type = ["cdylib", "lib"] diff --git a/pjs-rs/src/lib.rs b/pjs-rs/src/lib.rs index d82596e..e355ef7 100644 --- a/pjs-rs/src/lib.rs +++ b/pjs-rs/src/lib.rs @@ -20,6 +20,7 @@ macro_rules! get { const NULL: JsValue = JsValue::null(); #[wasm_bindgen] +#[derive(Clone)] pub struct PjsExtension { pjs: JsValue, accounts: Vec, @@ -183,10 +184,12 @@ impl Account { pub fn name(&self) -> String { self.name.clone() } + #[wasm_bindgen(getter)] pub fn address(&self) -> String { self.address.clone() } + #[wasm_bindgen(getter)] pub fn network(&self) -> Network { self.net