Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature sube-light #45

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions libwallet/src/key_pair.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use core::{convert::TryFrom, fmt::Debug};

use core::{convert::TryInto, fmt::Debug};
pub use derive::Derive;

use self::any::AnySignature;
Expand All @@ -20,7 +19,11 @@ pub trait Pair: Signer + Derive {
pub trait Public: AsRef<[u8]> + Debug {}
impl<const N: usize> Public for Bytes<N> {}

pub trait Signature: AsRef<[u8]> + Debug + PartialEq {}
pub trait Signature: AsRef<[u8]> + Debug + PartialEq {
fn as_bytes<const N: usize>(&self) -> Bytes<N> {
self.as_ref().try_into().expect("error")
}
}
impl<const N: usize> Signature for Bytes<N> {}

/// Something that can sign messages
Expand Down
40 changes: 26 additions & 14 deletions sube/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ license = "Apache-2.0"
version = "1.0.0"
authors = ["Daniel Olano <[email protected]>"]
edition = "2021"
repository = "https://github.com/valibre-org/sube"
repository = "https://github.com/valibre-org/virto-dk/sube"

[dependencies]
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", 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 }
async-once-cell = "0.4.4"

scale-info = { version = "2.1.1", default-features = false, optional = true }
jsonrpc = { version = "0.12.1", default-features = false, optional = true }
scales = { path = "../scales", package = "scale-serialization", default-features = false, features = ["codec", "experimental-serializer"] }
scales = { path="../scales", package = "scale-serialization", default-features = false, features = ["codec", "experimental-serializer"] }

# http backend
surf = { version = "2.3.2", default_features = false, optional = true }
Expand All @@ -36,30 +36,39 @@ async-tls = { version = "0.11.0", default-features = false, optional = true }
async-std = { version = "1.11.0", default_features = false, optional = true }
serde_json = { version = "1.0.80", default-features = false, optional = true }
url = { version = "2.2.2", optional = true }

wasm-bindgen = "0.2.63"
paste = { version = "1.0", optional = true }
wasm-bindgen = { version = "0.2.91", optional = true }
once_cell = { version = "1.17.1", optional = true }
heapless = { version = "0.7.16", optional = true }
anyhow = { version = "1.0.40", optional = true }
rand_core = {version = "0.6.3", optional = true }
ewebsock = { git = "https://github.com/S0c5/ewebsock.git", optional = true, branch = "enhacement/aviod-blocking-operations-with-mpsc-futures" }
env_logger = "0.11.3"

[dev-dependencies]
async-std = { version = "1.11.0", features = ["attributes"] }
hex-literal = "0.3.4"
libwallet = { path = "../libwallet", default_features=false, features=["substrate", "mnemonic", "sr25519", "util_pin", "rand", ] }

[features]
# default = ["json", "std"]
default = ["async-std", "builder", "v14", "url", "wss", "ws", "http", "json"]
default = ["async-std", "builder", "url", "wss", "ws", "http", "json", "v14", "serde_json"]
#default = ["v14", "ws", "decode", "json", "std"]
# decode = ["v14", "serde", "scales/codec"]
# encode = ["v14", ]
http = ["jsonrpc", "surf/h1-client-rustls"]
http-web = ["jsonrpc", "surf/wasm-client"]
json = ["v14", "scales/json"]
http-web = ["jsonrpc", "surf/wasm-client", "wasm-bindgen", "url", "serde_json"]
json = [ "scales/json"]
std = []
builder = []
v12 = ["frame-metadata/v12"]
v13 = ["frame-metadata/v13"]
v14 = ["frame-metadata/v14", "scale-info"]
ws = ["async-std", "async-tungstenite", "async-mutex", "futures-util", "futures-channel", "jsonrpc"]
wss = ["ws", "async-tls", "async-tungstenite/async-tls"]

builder = ["paste", "once_cell", "heapless"]
# v12 = ["frame-metadata/v12"]
# v13 = ["frame-metadata/v13"]
v14 = ["scale-info", "frame-metadata/current"]
ws = ["async-std", "async-mutex", "futures-channel", "jsonrpc", "async-std/unstable", "ewebsock", 'futures-util']
wss = ["ws", "async-tls", "ewebsock/tls", "async-std/unstable"]
examples = ["anyhow", "rand_core"]
js = ["http-web", "json", "v14", "builder", 'async-std/unstable', "ws"]

[package.metadata.docs.rs]
features = ["http"]
Expand All @@ -69,3 +78,6 @@ members = [
"sube-js",
"cli"
]

[patch.crates-io]
cookie = { git = "https://github.com/S0c5/cookie-rs.git" }
14 changes: 10 additions & 4 deletions sube/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@

A client library for Substrate chains, doing less by design than [subxt](https://github.com/paritytech/substrate-subxt) with a big focus on size and portability so it can run in constrainted environments like the browser.

Making use of the type information in a chain's metadata(`>= v14`) and powered by our [Scales](https://github.com/virto-network/scales) library, Sube allows automatic conversion between the [SCALE](https://github.com/paritytech/parity-scale-codec) binary format used by the blockchain with a human-readable representation like JSON without having to hardcode type information for each network.
When submitting extrinsics Sube only does that, it's your responsability to sign the payload with a different tool first(e.g. [libwallet](https://github.com/valibre-org/libwallet)) before you feed the extrinsic data to the library.
Making use of the type information in a chain's metadata(`>= v15`) and powered by our [Scales](../scales/) library, Sube allows automatic conversion between the [SCALE](https://github.com/paritytech/parity-scale-codec) binary format used by the blockchain with a human-readable representation like JSON without having to hardcode type information for each network.
When submitting extrinsics Sube only does that, it's your responsability to sign the payload with a different tool first(e.g. [libwallet](../libwallet)) before you feed the extrinsic data to the library.

Sube supports multiple backends under different feature flags like `http`, `http-web` or `ws`/`wss`, with [future support](https://github.com/virto-network/sube/milestone/3) for a [`smoldot`](https://github.com/paritytech/smoldot) based light-node.
Sube supports multiple backends under different feature flags like `http`, `http-web` or `ws`/`wss`.


## Example Usage

To make Queries/Extrinsics using Sube, you can use the `SubeBuilder` or the convenient `sube!` macro. [here are the examples](./examples/)


## Progressive decentralization

> 🛠️ ⚠️ [Upcoming feature](https://github.com/virto-network/sube/milestone/2)

The true _raison d'etre_ of Sube is not to create yet another Substrate client but to enable the Virto.Network and any project in the ecosystem to reach a broader audience of end-users and developers by lowering the technical entry barrier and drasticaly improving the overal user experience of interacting with blockchains. We call it **progressive decentralization**.
The true _raison d'etre_ of Sube is not to create yet another Substrate client but to enable the Virto.Network and any project in the ecosystem to reach a broader audience of end-users and developers by lowering the technical entry barrier and drastically improving the overall user experience of interacting with blockchains. We call it **progressive decentralization**.

When paired with our plugin runtime [Valor](https://github.com/virto-network/valor), Sube can be exposed as an HTTP API that runs both in the server and the browser and be composed with other plugins to create higher level APIs that a client aplication can use from any plattform thanks to the ubiquitousness of HTTP.
We imagine existing centralized projects easily integrating with Substrate blockchains in the server with the option to progressively migrate to a decentralized set-up with whole backends later running in the user device(web browser included).

But progressive decentralization goes beyond the migration of a centralized project, it's rather about giving users the the best experience by possibly combining the best of both worlds. A Sube powered application can start being served from a server to have an immediate response and 0 start-up time and since plugins can be hot-swapped, the blockchain backend can be switched from HTTP to lightnode transparently without the application code ever realizing, giving our users with bad connectivity and slower devices the opportunity to enjoy the best possible user experience without compromizing decentralization.

1 change: 0 additions & 1 deletion sube/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
path = ".."
features = [
"std",
"v14",
"http",
# "wss",
]
Expand Down
13 changes: 7 additions & 6 deletions sube/examples/query_balance.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@



use sube::{ builder::SubeBuilder as Sube };
use async_trait::async_trait;
use core::future::{Future, IntoFuture};
use sube::{sube, Response, Result};

#[async_std::main]
async fn main () {
let a = Sube("wss://rpc.polkadot.io").await?;
async fn main() -> Result<()> {
let result = sube!("wss://rococo-rpc.polkadot.io/system/account/0x3c85f79f28628bee75cdb9eddfeae249f813fad95f84120d068fbc990c4b717d").await?;

println!("{:?}", result);
Ok(())
}
16 changes: 16 additions & 0 deletions sube/examples/query_balance_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use async_trait::async_trait;

use env_logger;
use sube::{builder::SubeBuilder, sube, ExtrinsicBody, Response, Result, SignerFn};

#[async_std::main]
async fn main() -> Result<()> {
env_logger::init();

let builder = SubeBuilder::default()
.with_url("wss://rococo-rpc.polkadot.io/system/account/0x3c85f79f28628bee75cdb9eddfeae249f813fad95f84120d068fbc990c4b717d")
.await?;

println!("{:?}", builder);
Ok(())
}
33 changes: 33 additions & 0 deletions sube/examples/query_balance_ws.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use env_logger;
use serde::Deserialize;
use sube::{builder::SubeBuilder, sube, Error, ExtrinsicBody, Response, Result, SignerFn};

#[derive(Debug, Deserialize)]
pub struct AccountInfo {
pub nonce: u64,
pub consumers: u64,
pub providers: u64,
pub sufficients: u64,
pub data: AccountData,
}

#[derive(Debug, Deserialize)]
pub struct AccountData {
pub free: u128,
pub reserved: u128,
pub frozen: u128,
pub flags: u128,
}

#[async_std::main]
async fn main() -> Result<()> {
let response = SubeBuilder::default()
.with_url("wss://rococo-rpc.polkadot.io/system/account/0x3c85f79f28628bee75cdb9eddfeae249f813fad95f84120d068fbc990c4b717d")
.await?;

if let Response::Value(v) = response {
println!("{}", v);
}

Ok(())
}
49 changes: 49 additions & 0 deletions sube/examples/send_tx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use futures_util::TryFutureExt;
use libwallet::{self, vault, Signature};
use rand_core::OsRng;
use serde_json::json;
use std::env;
use sube::sube;

type Wallet = libwallet::Wallet<vault::Simple>;

use anyhow::{anyhow, Result};

#[async_std::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let phrase = env::args().skip(1).collect::<Vec<_>>().join(" ");

let (vault, phrase) = if phrase.is_empty() {
vault::Simple::generate_with_phrase(&mut rand_core::OsRng)
} else {
let phrase: libwallet::Mnemonic = phrase.parse().expect("Invalid phrase");
(vault::Simple::from_phrase(&phrase), phrase)
};

let mut wallet = Wallet::new(vault);
wallet
.unlock(None)
.await
.map_err(|_| anyhow!("error wallet"))?;

let account = wallet.default_account();
let public = account.public();

let response = sube!("wss://rococo-rpc.polkadot.io/balances/transfer" => {
signer: |message: &[u8]| Ok(wallet.sign(message).as_bytes()),
sender: public.as_ref(),
body: json!({
"dest": {
"Id": public.as_ref()
},
"value": 100000
}),
})
.await
.map_err(|err| anyhow!(format!("SubeError {:?}", err)))?;

println!("Secret phrase: \"{phrase}\"");
println!("Default Account: 0x{account}");

Ok(())
}
42 changes: 42 additions & 0 deletions sube/examples/send_tx_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use jsonrpc::error;
use libwallet::{self, vault, Signature};
use rand_core::OsRng;
use serde_json::json;
use std::env;
use sube::builder::SubeBuilder;

type Wallet = libwallet::Wallet<vault::Simple>;
use anyhow::{anyhow, Result};

#[async_std::main]
async fn main() -> Result<()> {
let phrase = env::args().skip(1).collect::<Vec<_>>().join(" ");

let (vault, phrase) = if phrase.is_empty() {
vault::Simple::generate_with_phrase(&mut rand_core::OsRng)
} else {
let phrase: libwallet::Mnemonic = phrase.parse().expect("Invalid phrase");
(vault::Simple::from_phrase(&phrase), phrase)
};

let mut wallet = Wallet::new(vault);

wallet.unlock(None).await.map_err(|e| anyhow!("error"))?;

let account = wallet.default_account();

let response = SubeBuilder::default()
.with_url("wss://rococo-rpc.polkadot.io/balances/transfer")
.with_signer(|message: &[u8]| Ok(wallet.sign(message).as_bytes()))
.with_sender(wallet.default_account().public().as_ref())
.with_body(json!({
"dest": {
"Id": wallet.default_account().public().as_ref()
},
"value": 100000
}))
.await
.map_err(|err| anyhow!(format!("Error {:?}", err)))?;

Ok(())
}
48 changes: 48 additions & 0 deletions sube/examples/send_tx_libwallet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use futures_util::TryFutureExt;
use libwallet::{self, vault, Signature};
use rand_core::OsRng;
use serde_json::json;
use std::env;
use sube::sube;

type Wallet = libwallet::Wallet<vault::Simple>;

use anyhow::{anyhow, Result};

#[async_std::main]
async fn main() -> Result<()> {
let phrase = env::args().skip(1).collect::<Vec<_>>().join(" ");

let (vault, phrase) = if phrase.is_empty() {
vault::Simple::generate_with_phrase(&mut rand_core::OsRng)
} else {
let phrase: libwallet::Mnemonic = phrase.parse().expect("Invalid phrase");
(vault::Simple::from_phrase(&phrase), phrase)
};

let mut wallet = Wallet::new(vault);
wallet
.unlock(None)
.await
.map_err(|e| anyhow!("Error unlocking the wallet"))?;

let account = wallet.default_account();
let public = account.public();

let response = sube!(
"wss://rococo-rpc.polkadot.io/balances/transfer" =>
(wallet, json!({
"dest": {
"Id": public.as_ref(),
},
"value": 100000
}))
)
.await
.map_err(|err| anyhow!(format!("SubeError {:?}", err)))?;

println!("Secret phrase: \"{phrase}\"");
// println!("Default Account: 0x{account}");

Ok(())
}
Loading
Loading