From ccb94c8a22ea445e5c15c60c8ac00a490245c599 Mon Sep 17 00:00:00 2001 From: david barinas Date: Thu, 25 Apr 2024 16:54:31 -0500 Subject: [PATCH] feat: update the libwallet interface --- libwallet/examples/account_generation.rs | 2 +- libwallet/src/key_pair.rs | 8 +++--- libwallet/src/lib.rs | 33 ++++++++++++++---------- libwallet/src/vault/simple.rs | 27 +++++++++++-------- sube/Cargo.toml | 7 +++-- 5 files changed, 44 insertions(+), 33 deletions(-) diff --git a/libwallet/examples/account_generation.rs b/libwallet/examples/account_generation.rs index 0f70185..001db03 100644 --- a/libwallet/examples/account_generation.rs +++ b/libwallet/examples/account_generation.rs @@ -1,7 +1,7 @@ use libwallet::{self, vault}; use std::env; -type Wallet = libwallet::Wallet; +type Wallet = libwallet::Wallet>; #[async_std::main] async fn main() -> Result<(), Box> { diff --git a/libwallet/src/key_pair.rs b/libwallet/src/key_pair.rs index 172ec7b..b72cec4 100644 --- a/libwallet/src/key_pair.rs +++ b/libwallet/src/key_pair.rs @@ -1,4 +1,4 @@ -use core::fmt::Debug; +use core::{convert::TryFrom, fmt::Debug}; pub use derive::Derive; @@ -25,13 +25,14 @@ impl Signature for Bytes {} /// Something that can sign messages pub trait Signer { - type Signature: Signature + Into; + type Signature: Signature; async fn sign_msg>(&self, data: M) -> Result; async fn verify>(&self, msg: M, sig: &[u8]) -> bool; } - /// Wrappers to represent any supported key pair. pub mod any { + use crate::Signer; + use super::{Public, Signature}; use core::fmt; @@ -145,6 +146,7 @@ pub mod any { #[cfg(not(feature = "sr25519"))] _None, } + impl AsRef<[u8]> for AnySignature { fn as_ref(&self) -> &[u8] { match self { diff --git a/libwallet/src/lib.rs b/libwallet/src/lib.rs index 0359cd4..7b7ded4 100644 --- a/libwallet/src/lib.rs +++ b/libwallet/src/lib.rs @@ -14,6 +14,7 @@ pub mod util; #[cfg(feature = "substrate")] mod substrate_ext; +use account::Account; use arrayvec::ArrayVec; use core::{cell::RefCell, convert::TryInto, fmt}; use key_pair::any::AnySignature; @@ -27,6 +28,7 @@ pub use mnemonic::{Language, Mnemonic}; pub use vault::Vault; pub mod vault; + const MSG_MAX_SIZE: usize = u8::MAX as usize; type Message = ArrayVec; @@ -73,9 +75,10 @@ where /// ``` /// # use libwallet::{Wallet, Error, vault, Vault}; /// # use std::convert::TryInto; - /// # type Result = std::result::Result<(), Error<::Error>>; + /// # type SimpleVault = vault::Simple; + /// # type Result = std::result::Result<(), Error<::Error>>; /// # #[async_std::main] async fn main() -> Result { - /// # let vault = vault::Simple::generate(&mut rand_core::OsRng); + /// # let vault = SimpleVault::generate(&mut rand_core::OsRng); /// let mut wallet: Wallet<_> = Wallet::new(vault); /// if wallet.is_locked() { /// wallet.unlock(None, None).await?; @@ -119,9 +122,10 @@ where /// /// ``` /// # use libwallet::{Wallet, vault, Error, Signer, Vault}; - /// # type Result = std::result::Result<(), Error<::Error>>; + /// # type SimpleVault = vault::Simple; + /// # type Result = std::result::Result<(), Error<::Error>>; /// # #[async_std::main] async fn main() -> Result { - /// # let vault = vault::Simple::generate(&mut rand_core::OsRng); + /// # let vault = SimpleVault::generate(&mut rand_core::OsRng); /// let mut wallet: Wallet<_> = Wallet::new(vault); /// wallet.unlock(None, None).await?; /// @@ -145,9 +149,10 @@ where /// /// ``` /// # use libwallet::{Wallet, vault, Error, Vault}; - /// # type Result = std::result::Result<(), Error<::Error>>; + /// # type SimpleVault = vault::Simple; + /// # type Result = std::result::Result<(), Error<::Error>>; /// # #[async_std::main] async fn main() -> Result { - /// # let vault = vault::Simple::generate(&mut rand_core::OsRng); + /// # let vault = SimpleVault::generate(&mut rand_core::OsRng); /// let mut wallet: Wallet<_> = Wallet::new(vault); /// wallet.sign_later(&[0x01, 0x02, 0x03]); /// @@ -169,9 +174,10 @@ where /// /// ``` /// # use libwallet::{Wallet, vault, Error, Vault}; - /// # type Result = std::result::Result<(), Error<::Error>>; + /// # type SimpleVault = vault::Simple; + /// # type Result = std::result::Result<(), Error<::Error>>; /// # #[async_std::main] async fn main() -> Result { - /// # let vault = vault::Simple::generate(&mut rand_core::OsRng); + /// # let vault = SimpleVault::generate(&mut rand_core::OsRng); /// let mut wallet: Wallet<_> = Wallet::new(vault); /// wallet.unlock(None, None).await?; /// @@ -183,18 +189,16 @@ where /// assert_eq!(wallet.pending().count(), 0); /// # Ok(()) } /// ``` - pub async fn sign_pending(&mut self) -> Result, ()> { + pub async fn sign_pending(&mut self) -> Result, M>, ()> { let mut signatures = ArrayVec::new(); for (msg, a) in self.pending_sign.take() { let signer = a .map(|idx| self.account(idx)) .unwrap_or_else(|| self.default_signer().expect("Signer not set")); - let message = signer.sign_msg(&msg).await?.into(); - + let message = signer.sign_msg(&msg).await?; signatures.push(message); } - Ok(signatures) } @@ -202,9 +206,10 @@ where /// /// ``` /// # use libwallet::{Wallet, vault, Error, Vault}; - /// # type Result = std::result::Result<(), Error<::Error>>; + /// # type SimpleVault = vault::Simple; + /// # type Result = std::result::Result<(), Error<::Error>>; /// # #[async_std::main] async fn main() -> Result { - /// # let vault = vault::Simple::generate(&mut rand_core::OsRng); + /// # let vault = SimpleVault::generate(&mut rand_core::OsRng); /// let mut wallet: Wallet<_> = Wallet::new(vault); /// wallet.sign_later(&[0x01]); /// wallet.sign_later(&[0x02]); diff --git a/libwallet/src/vault/simple.rs b/libwallet/src/vault/simple.rs index 76dc7c8..7ff4078 100644 --- a/libwallet/src/vault/simple.rs +++ b/libwallet/src/vault/simple.rs @@ -1,4 +1,5 @@ use core::convert::TryInto; +use core::marker::PhantomData; use arrayvec::ArrayVec; @@ -9,19 +10,21 @@ use crate::{ }; /// A vault that holds secrets in memory -pub struct Simple { +pub struct Simple { locked: Option<[u8; 32]>, unlocked: Option<[u8; 32]>, + _phantom: PhantomData } -impl Simple { +impl Simple { /// A vault with a random seed, once dropped the the vault can't be restored /// /// ``` /// # use libwallet::{vault, Error, Derive, Pair, Vault}; - /// # type Result = std::result::Result<(), ::Error>; + /// # type SimpleVault = vault::Simple; + /// # type Result = std::result::Result<(), ::Error>; /// # #[async_std::main] async fn main() -> Result { - /// let mut vault = vault::Simple::generate(&mut rand_core::OsRng); + /// let mut vault = SimpleVault::generate(&mut rand_core::OsRng); /// let root = vault.unlock(None, None).await?; /// # Ok(()) /// } @@ -34,6 +37,7 @@ impl Simple { Simple { locked: Some(crate::util::random_bytes::<_, 32>(rng)), unlocked: None, + _phantom: Default::default() } } @@ -62,6 +66,7 @@ impl Simple { Simple { locked: Some(entropy), unlocked: None, + _phantom: Default::default(), } } @@ -86,10 +91,10 @@ impl core::fmt::Display for Error { #[cfg(feature = "std")] impl std::error::Error for Error {} -impl Vault for Simple { +impl> Vault for Simple { type Credentials = Option; type Error = Error; - type Id = Option>; + type Id = Option; type Account = AccountSigner; async fn unlock( @@ -100,11 +105,11 @@ impl Vault for Simple { self.unlocked = self.locked.take(); let pin = creds.into(); let root_account = self.get_key(pin.unwrap_or_default())?; - let path = path.as_ref().map(|r| { - core::str::from_utf8(r.as_slice()) - .expect("it must be a valid utf8 string") - }); - + // let path = path.as_ref().map(|r| { + // core::str::from_utf8(r.as_slice()) + // .expect("it must be a valid utf8 string") + // }); + let path = path.as_ref().map(|x| x.as_ref()); Ok(AccountSigner::new(path).unlock(&root_account)) } } diff --git a/sube/Cargo.toml b/sube/Cargo.toml index 9e2a57b..0c9e1f9 100644 --- a/sube/Cargo.toml +++ b/sube/Cargo.toml @@ -12,7 +12,7 @@ log = "0.4.17" async-trait = "0.1.53" blake2 = { version = "0.10.5", default-features = false } codec = { version = "3.1.2", package = "parity-scale-codec", default-features = false } -frame-metadata = { version = "15.0.0", git = "https://github.com/paritytech/frame-metadata.git", default-features = false, features = ["serde_full", "decode"] } +frame-metadata = { version = "16.0.0", git = "https://github.com/paritytech/frame-metadata.git", default-features = false, features = ["serde_full", "decode"] } hex = { version = "0.4.3", default-features = false, features = ["alloc"] } twox-hash = { version = "1.6.2", default-features = false } serde = { version = "1.0.137", default-features = false } @@ -54,9 +54,8 @@ http-web = ["jsonrpc", "surf/wasm-client"] json = ["v14", "scales/json"] std = [] builder = [] -v12 = ["frame-metadata/v12"] -v13 = ["frame-metadata/v13"] -v14 = ["frame-metadata/v14", "scale-info"] + +v14 = ["scale-info"] ws = ["async-std", "async-tungstenite", "async-mutex", "futures-util", "futures-channel", "jsonrpc"] wss = ["ws", "async-tls", "async-tungstenite/async-tls"]