Skip to content

Commit

Permalink
Compile in stable changing Signer trait + cleanup deps
Browse files Browse the repository at this point in the history
  • Loading branch information
olanod committed May 2, 2024
1 parent 107832f commit 335c355
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 287 deletions.
47 changes: 21 additions & 26 deletions sube/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ edition = "2021"
repository = "https://github.com/valibre-org/virto-dk/sube"

[dependencies]
log = "0.4.17"
async-once-cell = "0.4.4"
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 = "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", "std"] }
log = "0.4.17"
scale-info = { version = "2.1.1", default-features = false, optional = true }
scales = { path="../scales", package = "scale-serialization", default-features = false, features = ["codec", "experimental-serializer", "json", "std"] }
serde = { version = "1.0.137", default-features = false }
# TODO: shouldn't be a base dependeny. remove after: https://github.com/virto-network/virto-sdk/issues/53
serde_json = { version = "1.0.80", default-features = false, features = ["alloc"] }
twox-hash = { version = "1.6.2", default-features = false }
url = "2.5.0"

# http backend
surf = { version = "2.3.2", default_features = false, optional = true }
Expand All @@ -34,8 +36,6 @@ async-tls = { version = "0.11.0", default-features = false, optional = true }

# bin target
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 }
paste = { version = "1.0", optional = true }
wasm-bindgen = { version = "0.2.91", optional = true }
once_cell = { version = "1.17.1", optional = true }
Expand All @@ -48,27 +48,22 @@ 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", ] }
libwallet = { path = "../libwallet", default_features=false, features=["substrate", "mnemonic", "sr25519", "util_pin", "rand", "std" ] }
rand_core = "0.6.3"

[features]
# default = ["json", "std"]
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", "wasm-bindgen", "url", "serde_json"]
json = [ "scales/json"]
default = ["v14"]
test = ["std", "wss", "ws", "http", "json", "v14", "dep:async-std"]
http = ["dep:jsonrpc", "surf/h1-client-rustls"]
http-web = ["dep:jsonrpc", "dep:wasm-bindgen", "surf/wasm-client"]
json = ["scales/json"]
std = []

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"]
v14 = ["dep:scale-info", "frame-metadata/current"]
ws = ["dep:async-mutex", "dep:async-std", "dep:ewebsock", "dep:futures-channel", "dep:futures-util", "dep:jsonrpc", "async-std/unstable"]
wss = ["dep:async-tls", "ws", "ewebsock/tls", "async-std/unstable"]
examples = ["dep:rand_core"]
js = ["http-web", "json", "v14", 'async-std/unstable', "ws"]

[package.metadata.docs.rs]
features = ["http"]
Expand Down
2 changes: 1 addition & 1 deletion sube/examples/query_balance_builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use async_trait::async_trait;

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

#[async_std::main]
async fn main() -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion sube/examples/query_balance_ws.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use env_logger;
use serde::Deserialize;
use sube::{builder::SubeBuilder, sube, Error, ExtrinsicBody, Response, Result, SignerFn};
use sube::{sube, Error, ExtrinsicBody, Response, Result, SubeBuilder};

#[derive(Debug, Deserialize)]
pub struct AccountInfo {
Expand Down
25 changes: 8 additions & 17 deletions sube/examples/send_tx.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
#![feature(async_closure)]

use futures_util::TryFutureExt;
use libwallet::{self, vault, Account, Signature};
use rand_core::OsRng;
use serde_json::json;
use std::env;
use sube::sube;
use sube::{sube, Error};

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

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(" ");
Expand All @@ -23,26 +19,21 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
};

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

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

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

println!("Secret phrase: \"{phrase}\"");
println!("Default Account: 0x{}", account.unwrap());
Expand Down
28 changes: 15 additions & 13 deletions sube/examples/send_tx_builder.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
#![feature(async_closure)]

use jsonrpc::error;
use libwallet::{self, vault, Account, Signature};
use rand_core::OsRng;
use serde_json::json;
use std::env;
use sube::builder::SubeBuilder;
use std::{env, error::Error};
use sube::SubeBuilder;

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

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

let (vault, phrase) = if phrase.is_empty() {
Expand All @@ -22,23 +19,28 @@ async fn main() -> Result<()> {
};

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

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

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

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

Ok(())
}
18 changes: 4 additions & 14 deletions sube/examples/send_tx_libwallet.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
#![feature(async_closure)]


use futures_util::TryFutureExt;
use libwallet::{self, vault, Account, Signature};
use rand_core::OsRng;
use serde_json::json;
use std::env;
use std::{env, error::Error};
use sube::sube;

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

use anyhow::{anyhow, Result};

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

let (vault, phrase) = if phrase.is_empty() {
Expand All @@ -24,13 +19,9 @@ async fn main() -> Result<()> {
};

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

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


let response = sube!(
"wss://rococo-rpc.polkadot.io/balances/transfer" =>
Expand All @@ -41,8 +32,7 @@ async fn main() -> Result<()> {
"value": 100000
}))
)
.await
.map_err(|err| anyhow!(format!("SubeError {:?}", err)))?;
.await?;

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

0 comments on commit 335c355

Please sign in to comment.