Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardocustodio committed Nov 5, 2024
1 parent 8f08873 commit c9f2a81
Show file tree
Hide file tree
Showing 11 changed files with 488 additions and 744 deletions.
1,069 changes: 444 additions & 625 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ edition = "2021"
[dependencies]
tracing = "0.1.40"
rpassword = "7.3.1"
subxt = { version = "0.37.0", features = ["native", "unstable-reconnecting-rpc-client"] }
subxt-signer = "0.37.0"
subxt = { version = "0.38.0", features = ["native", "reconnecting-rpc-client"] }
subxt-signer = "0.38.0"
backoff = { version = "0.4", default-features = false, features = ["tokio"] }
backon = "1.2.0"
tokio = { version = "1.40.0", features = [
Expand All @@ -25,7 +25,6 @@ serde_json = "1.0.132"
hex = "0.4.3"
tracing-subscriber = "0.3.18"
config = "0.14.0"
secrecy = "0.8.0"
sp-core = { version = "34.0.0", default-features = false, features = [
"std",
] }
Expand Down
26 changes: 7 additions & 19 deletions src/config_loader.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
#![allow(dead_code)]
#![allow(unused)]

use config::Config;
use secrecy::ExposeSecret;
use serde::Deserialize;
use sp_core::crypto::{Ss58AddressFormat, Ss58Codec};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::{env, fs};
use subxt_signer::bip39::Mnemonic;
use subxt_signer::sr25519::Keypair;
use subxt_signer::{SecretString, SecretUri};
use subxt_signer::{ExposeSecret, SecretString, SecretUri};

pub(crate) const CONFIG_FILE_ENV_NAME: &str = "CONFIG_FILE";
pub(crate) const DEFAULT_CONFIG_FILE: &str = "config.json";
Expand All @@ -25,17 +21,6 @@ pub struct Configuration {
api: String,
}

impl Configuration {
pub(crate) fn new(node: String, relay_node: String, master_key: PathBuf, api: String) -> Self {
Self {
node,
relay_node,
master_key,
api,
}
}
}

pub fn load_config() -> Configuration {
let config_file = env::var(CONFIG_FILE_ENV_NAME).unwrap_or_else(|_| {
Path::new(env!("CARGO_MANIFEST_DIR"))
Expand All @@ -54,9 +39,12 @@ pub fn load_config() -> Configuration {
}

fn get_password(env_name: &str) -> SecretString {
SecretString::new(
env::var(env_name).expect(&format!("Password {} not loaded in memory", env_name)),
)
match env::var(env_name) {
Ok(p) => SecretString::from(p),
Err(_) => {
panic!("Password {} not loaded in memory", env_name)
}
}
}

async fn get_keys(key_store_path: &Path, password: SecretString) -> Keypair {
Expand Down
5 changes: 2 additions & 3 deletions src/importer.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use secrecy::ExposeSecret;
use sp_core::crypto::{Ss58AddressFormat, Ss58Codec};
use std::fs;
use std::path::Path;
use std::str::FromStr;
use subxt_signer::sr25519::Keypair;
use subxt_signer::SecretUri;
use subxt_signer::{ExposeSecret, SecretUri};

pub fn write_seed(seed: String) -> std::io::Result<()> {
// TODO: Get the path for the store from config_loader
let p = Path::new(env!("CARGO_MANIFEST_DIR")).join("store");
let uri = SecretUri::from_str(&*seed).expect("valid URI");
let uri = SecretUri::from_str(&seed).expect("valid URI");
let keypair_tx = Keypair::from_uri(&uri).expect("valid keypair");

fs::write(
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::too_many_arguments)]
pub use importer::write_seed;
pub use multitenant::set_multitenant;
pub use platform_client::{set_wallet_account, update_transaction};
Expand Down
19 changes: 5 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
#![allow(missing_docs)]
#![allow(dead_code)]
#![allow(unused)]

use std::env;
use std::fs::File;
use std::process::exit;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::sync::Arc;
use std::time::Duration;
use subxt::backend::rpc::reconnecting_rpc_client::{Client, ExponentialBackoff};
use subxt::ext::scale_decode::visitor::types::BitSequence;
use subxt::backend::rpc::reconnecting_rpc_client::{ExponentialBackoff, RpcClient};
use subxt::{OnlineClient, PolkadotConfig};
use subxt_signer::sr25519::Keypair;
use subxt_signer::SecretUri;
use tokio::signal;
use tokio::task::JoinHandle;
use wallet_daemon::config_loader::{load_config, load_wallet};
use wallet_daemon::{
set_multitenant, write_seed, DeriveWalletJob, SubscriptionParams, TransactionJob,
Expand All @@ -33,15 +24,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
}

let (keypair, matrix_url, relay_url, platform_url, platform_token) =
let (keypair, matrix_url, _relay_url, platform_url, platform_token) =
load_wallet(load_config()).await;
let signing = hex::encode(keypair.public_key().0);

tracing_subscriber::fmt::init();

set_multitenant(signing, platform_url.clone(), platform_token.clone()).await;

let rpc_client = Client::builder()
let rpc_client = RpcClient::builder()
.retry_policy(
ExponentialBackoff::from_millis(100)
.max_delay(Duration::from_secs(10))
Expand All @@ -57,7 +48,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let update_task = chain_client.updater();
tokio::spawn(async move {
update_task.perform_runtime_updates().await;
let _ = update_task.perform_runtime_updates().await;
});

let chain_client = Arc::new(chain_client);
Expand Down
10 changes: 2 additions & 8 deletions src/multitenant.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#![allow(dead_code)]
#![allow(unused)]

use crate::graphql;
use graphql_client::{GraphQLQuery, Response};
use reqwest::Client;
Expand All @@ -13,10 +10,7 @@ struct Platform {
packages: HashMap<String, Value>,
}

async fn get_packages(
platform_url: String,
platform_token: String,
) -> Result<bool, Box<dyn std::error::Error>> {
async fn get_packages(platform_url: String) -> Result<bool, Box<dyn std::error::Error>> {
let platform = platform_url.replace("/graphql", "");

let client = Client::new();
Expand Down Expand Up @@ -54,7 +48,7 @@ async fn update_user(
}

pub async fn set_multitenant(account: String, platform_url: String, platform_token: String) {
let is_tenant = get_packages(platform_url.clone(), platform_token.clone())
let is_tenant = get_packages(platform_url.clone())
.await
.expect("We could not connect to Enjin Platform, check your connection or the url");

Expand Down
7 changes: 2 additions & 5 deletions src/platform_client.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#![allow(dead_code)]
#![allow(unused)]

use crate::graphql::{set_wallet_account, update_transaction, SetWalletAccount, UpdateTransaction};
use backon::{BlockingRetryable, ExponentialBuilder, Retryable};
use backon::{ExponentialBuilder, Retryable};
use graphql_client::GraphQLQuery;
use reqwest::Client;
use std::time::Duration;

pub struct PlatformExponentialBuilder(ExponentialBuilder);
pub struct PlatformExponentialBuilder();
impl PlatformExponentialBuilder {
pub fn default() -> ExponentialBuilder {
ExponentialBuilder::default()
Expand Down
27 changes: 16 additions & 11 deletions src/subscription.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
#![allow(dead_code)]
#![allow(unused)]

use std::sync::{Arc, Mutex};
use subxt::client::ClientRuntimeUpdater;
use subxt::ext::subxt_core;
use subxt::{OnlineClient, PolkadotConfig};
use subxt_core::config::substrate;
use tokio::task::JoinHandle;

#[derive(Debug)]
pub struct SubscriptionParams {
rpc: Arc<OnlineClient<PolkadotConfig>>,
block_header: Arc<Mutex<Option<substrate::SubstrateHeader<u32, substrate::BlakeTwo256>>>>,
spec_version: Arc<Mutex<Option<u32>>>,
}

impl SubscriptionParams {
pub fn new(rpc: Arc<OnlineClient<PolkadotConfig>>) -> Arc<Self> {
let block_header = Arc::new(Mutex::new(None));
let spec_version = Arc::new(Mutex::new(None));

let subscription = Arc::new(Self {
rpc: Arc::clone(&rpc),
block_header: Arc::clone(&block_header),
spec_version: Arc::clone(&spec_version),
});

let block_sub = Arc::clone(&subscription);
Expand All @@ -41,28 +40,34 @@ impl SubscriptionParams {
self: Arc<Self>,
updater: ClientRuntimeUpdater<PolkadotConfig>,
) -> Result<(), Box<dyn std::error::Error + Sync + Send>> {
let mut already_fetched = false;
let mut update_stream = updater.runtime_updates().await.unwrap();

while let Some(Ok(update)) = update_stream.next().await {
let version = update.runtime_version().spec_version;
let mut spec_version = self.spec_version.lock().unwrap();

match updater.apply_update(update) {
Ok(()) => {
*spec_version = Some(version);
tracing::info!("Upgrade to specVersion: {} successful", version)
}
Err(e) => {
if !already_fetched {
tracing::info!("Using specVersion {} to sign transactions", version);
already_fetched = true;
} else {
Err(e) => match *spec_version {
Some(v) => {
if v == version {
continue;
}

tracing::error!(
"Upgrade to specVersion {} failed {:?}. Please restart your daemon.",
version,
e
);
}
}
None => {
*spec_version = Some(version);
tracing::info!("Using specVersion {} to sign transactions", version);
}
},
};
}

Expand Down
Loading

0 comments on commit c9f2a81

Please sign in to comment.