Skip to content

Commit

Permalink
merge dev
Browse files Browse the repository at this point in the history
  • Loading branch information
S0c5 committed Mar 24, 2024
2 parents 7d23859 + 07d0030 commit 67d0f7f
Show file tree
Hide file tree
Showing 16 changed files with 2,407 additions and 39 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
on: [push, pull_request]

name: Continuous integration

jobs:
check:
name: Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: check

test:
name: Test Suite
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: test
args: --features=std

fmt:
name: Rustfmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- run: rustup component add rustfmt
- uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- run: rustup component add clippy
- uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warnings
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target/
Cargo.lock
.vscode/
**/*.rs.bk
**/*.rs.bk
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# VirtoSDK

> ⚠️ Work in progress. The Sube repository is now the VirtoSDK where we are gathering all our client side tools and will add higher lever abstractions for applications to easily integrate Web3 services.
10 changes: 5 additions & 5 deletions libwallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mnemonic = {package = "bip0039", version = "0.10.1", default-features = false, o

rand_core = {version = "0.6.3", optional = true}
# substrate related
schnorrkel = {version = "0.10.2", default-features = false, optional = true}# soft derivation in no_std
schnorrkel = {version = "0.11.4", default-features = false, optional = true}# soft derivation in no_std
rand_chacha = {version = "0.3.1", default-features = false, optional = true}

# vault os
Expand All @@ -35,17 +35,17 @@ serde_json = {version = "1.0", default-features = false, features = ["alloc"]}
dirs = "4.0"

[features]
default = ["std", "substrate", "vault_simple", "mnemonic", "rand"]
rand = ["rand_core"]
sr25519 = ["schnorrkel/u64_backend", "schnorrkel/getrandom"]
# The library has no default features but this can be uncommented during development
# default = [ "std", "substrate", "vault_simple", "vault_os", "vault_pass", "mnemonic", "util_pin", "rand", ]
rand = ["rand_core", "schnorrkel?/getrandom"]
sr25519 = ["dep:schnorrkel"]
std = [
"rand_core/getrandom",
]
substrate = ["sr25519"]
util_pin = ["pbkdf2", "hmac", "sha2"]
vault_os = ["keyring"]
vault_pass = ["prs-lib"]
vault_simple = []

[workspace]
members = [
Expand Down
4 changes: 2 additions & 2 deletions libwallet/js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ wasm-bindgen-test = "0.3.34"
features = ["js"]

[features]
default = ["wallet", "js", "hex", "util_pin", "vault_simple"]
default = ["wallet", "js", "hex", "util_pin"]
hex = ["dep:hex"]
js = ["std"]
std = []
util_pin = ["libwallet/util_pin"]
vault_simple = ["libwallet/vault_simple", "libwallet/mnemonic", "libwallet/rand"]
vault_simple = ["libwallet/mnemonic", "libwallet/rand"]
wallet = ["libwallet/serde", "libwallet/sr25519", "libwallet/substrate"]
6 changes: 6 additions & 0 deletions libwallet/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
default:
just --choose

check-no-std:
cargo build --features substrate --target wasm32-unknown-unknown
cargo build --features substrate --target riscv32i-unknown-none-elf
18 changes: 16 additions & 2 deletions libwallet/src/key_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ pub mod any {

fn public(&self) -> Self::Public {
match self {
#[cfg(feature = "sr25519")]
Pair::Sr25519(p) => AnyPublic::Sr25519(p.public()),
#[cfg(not(feature = "sr25519"))]
Pair::_None => unreachable!(),
}
}
}
Expand All @@ -76,7 +79,10 @@ pub mod any {
Self: Sized,
{
match self {
#[cfg(feature = "sr25519")]
Pair::Sr25519(kp) => Pair::Sr25519(kp.derive(path)),
#[cfg(not(feature = "sr25519"))]
Pair::_None => unreachable!(),
}
}
}
Expand All @@ -100,7 +106,10 @@ pub mod any {

fn verify<M: AsRef<[u8]>>(&self, msg: M, sig: &[u8]) -> bool {
match self {
#[cfg(feature = "sr25519")]
Pair::Sr25519(p) => super::Signer::verify(p, msg, sig),
#[cfg(not(feature = "sr25519"))]
Pair::_None => unreachable!(),
}
}
}
Expand All @@ -116,7 +125,10 @@ pub mod any {
impl AsRef<[u8]> for AnyPublic {
fn as_ref(&self) -> &[u8] {
match self {
#[cfg(feature = "sr25519")]
AnyPublic::Sr25519(p) => p.as_ref(),
#[cfg(not(feature = "sr25519"))]
AnyPublic::_None => unreachable!(),
}
}
}
Expand Down Expand Up @@ -232,13 +244,13 @@ pub mod sr25519 {

#[cfg(not(feature = "rand_chacha"))]
fn derive_simple(key: SecretKey, j: Junction) -> SecretKey {
key.derived_key_simple(ChainCode(j), &[]).0
key.derived_key_simple(ChainCode(j), []).0
}
#[cfg(feature = "rand_chacha")]
fn derive_simple(key: SecretKey, j: Junction) -> SecretKey {
use rand_core::SeedableRng;
// As noted in https://docs.rs/schnorrkel/latest/schnorrkel/context/fn.attach_rng.html
// it's not recommended by should be ok for our simple use cases
// it's not recommended but should be ok for our simple use cases
let rng = rand_chacha::ChaChaRng::from_seed([0; 32]);
key.derived_key_simple_rng(ChainCode(j), &[], rng).0
}
Expand Down Expand Up @@ -363,6 +375,8 @@ mod derive {
#[cfg(test)]
mod tests {
use super::*;
extern crate alloc;
use alloc::vec::Vec;

#[test]
fn substrate_junctions() {
Expand Down
17 changes: 2 additions & 15 deletions libwallet/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![feature(async_fn_in_trait, impl_trait_projections)]
#![feature(trait_alias)]
// #![feature(result_option_inspect)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(any(test, feature = "std")), no_std)]
//! `libwallet` is the one-stop tool to build easy, slightly opinionated crypto wallets
//! that run in all kinds of environments and plattforms including embedded hardware,
//! mobile apps or the Web.
Expand Down Expand Up @@ -97,7 +97,7 @@ where
def.unlock(root);
})
.await
.map_err(|e| Error::Vault(e))?;
.map_err(Error::Vault)?;
self.is_locked = false;
}
Ok(())
Expand Down Expand Up @@ -306,19 +306,6 @@ mod util {
phrase
}

macro_rules! seed_from_entropy {
($seed: ident, $pin: expr) => {
#[cfg(feature = "util_pin")]
let protected_seed = $pin.protect::<64>($seed);
#[cfg(feature = "util_pin")]
let $seed = &protected_seed;
#[cfg(not(feature = "util_pin"))]
let _ = &$pin; // use the variable to avoid warning
};
}

pub(crate) use seed_from_entropy;

/// A simple pin credential that can be used to add some
/// extra level of protection to seeds stored in vaults
#[derive(Default, Copy, Clone)]
Expand Down
24 changes: 19 additions & 5 deletions libwallet/src/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
mod os;
#[cfg(feature = "vault_pass")]
mod pass;
#[cfg(feature = "vault_simple")]
mod simple;

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

use crate::{any, key_pair, Derive};
use crate::{any, Derive};

/// Abstration for storage of private keys that are protected by some credentials.
pub trait Vault {
Expand All @@ -36,14 +34,16 @@ pub trait Vault {
#[derive(Debug)]
pub struct RootAccount {
#[cfg(feature = "substrate")]
sub: key_pair::sr25519::Pair,
sub: crate::key_pair::sr25519::Pair,
}

impl RootAccount {
fn from_bytes(seed: &[u8]) -> Self {
#[cfg(not(feature = "substrate"))]
let _ = seed;
RootAccount {
#[cfg(feature = "substrate")]
sub: <key_pair::sr25519::Pair as crate::Pair>::from_bytes(seed),
sub: <crate::key_pair::sr25519::Pair as crate::Pair>::from_bytes(seed),
}
}
}
Expand All @@ -61,6 +61,20 @@ impl<'a> Derive for &'a RootAccount {
"m/" => unimplemented!(),
#[cfg(feature = "substrate")]
_ => self.sub.derive("//default").into(),
#[cfg(not(feature = "substrate"))]
_ => unreachable!(),
}
}
}

macro_rules! seed_from_entropy {
($seed: ident, $pin: expr) => {
#[cfg(feature = "util_pin")]
let protected_seed = $pin.protect::<64>($seed);
#[cfg(feature = "util_pin")]
let $seed = &protected_seed;
#[cfg(not(feature = "util_pin"))]
let _ = &$pin; // use the variable to avoid warning
};
}
pub(crate) use seed_from_entropy;
16 changes: 8 additions & 8 deletions libwallet/src/vault/simple.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use core::convert::TryInto;

use crate::util::{seed_from_entropy, Pin};
use super::seed_from_entropy;
use crate::util::Pin;
use crate::{RootAccount, Vault};

/// A vault that holds secrets in memory
Expand Down Expand Up @@ -39,20 +38,21 @@ impl Simple {
R: rand_core::CryptoRng + rand_core::RngCore,
{
let phrase = crate::util::gen_phrase(rng, Default::default());
(
Self::from_phrase(&phrase),
phrase
)
(Self::from_phrase(&phrase), phrase)
}

#[cfg(feature = "mnemonic")]
// Provide your own seed
pub fn from_phrase(phrase: impl AsRef<str>) -> Self {
use core::convert::TryInto;
let phrase = phrase
.as_ref()
.parse::<mnemonic::Mnemonic>()
.expect("mnemonic");
let entropy = phrase.entropy().try_into().expect("Size should be 32 bytes");
let entropy = phrase
.entropy()
.try_into()
.expect("Size should be 32 bytes");

Simple {
locked: Some(entropy),
Expand Down
1 change: 0 additions & 1 deletion rust-toolchain

This file was deleted.

29 changes: 29 additions & 0 deletions scales/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "scale-serialization"
description = "SCALE Serialization"
version = "1.0.0-beta2"
authors = ["Daniel Olano <[email protected]>"]
edition = "2018"
repository = "https://github.com/virto-network/scales"
license = "Apache-2.0"

[dependencies]
bytes = { version = "1.1.0", default-features = false }
scale-info = { version = "2.1.1", default-features = false, features = ["serde"] }
serde = { version = "1.0.137", default-features = false }
serde_json = { version = "1.0.80", default-features = false, optional = true }
codec = { version = "3.1.2", package = "parity-scale-codec", default-features = false, optional = true }
hex = { version = "0.4.3", default-features = false, features = ["alloc"], optional = true }

[features]
default = ["std", "codec", "json", "hex", "experimental-serializer"]
std = ["scale-info/std", "bytes/std"]
experimental-serializer = []
json = ["serde_json/preserve_order"]

[dev-dependencies]
anyhow = "1.0.57"
codec = { version = "3.1.2", package = "parity-scale-codec", features = ["derive"] }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
serde = { version = "1.0.137", default-features = false, features = ["derive"] }
serde_json = { version = "1.0.80", default-features = false, features = ["alloc"] }
35 changes: 35 additions & 0 deletions scales/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# scales - SCALE Serialization

Making use of [type information](https://github.com/paritytech/scale-info) this library allows
conversion to/from [SCALE](https://github.com/paritytech/parity-scale-codec) encoded data,
specially useful when conversion is for dynamic types like JSON.

### From SCALE

`scales::Value` wraps the raw SCALE binary data and the type id within type registry
giving you an object that can be serialized to any compatible format.

```rust
let value = scales::Value::new(scale_binary_data, type_id, &type_registry);
serde_json::to_string(value)?;
```

### To SCALE

Public methods from the `scales::serializer::*` module(feature `experimental-serializer`)
allow for a best effort conversion of dynamic types(e.g. `serde_json::Value`) to SCALE
binary format. The serializer tries to be smart when interpreting the input and convert it
to the desired format dictated by the provided type in the registry.

```rust
// simple conversion
let scale_data = to_vec(some_serializable_input); // or to_bytes(&mut bytes, input);

// with type info
let scale_data = to_vec_with_info(input, Some((&registry, type_id)));

// from an unordered list of properties that make an object
let input = vec![("prop_b", 123), ("prop_a", 456)];
let scale_data = to_vec_from_iter(input, (&registry, type_id));
```

Loading

0 comments on commit 67d0f7f

Please sign in to comment.