Skip to content

Commit

Permalink
[PLA-1528] Adds wallet importer tool (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardocustodio authored Jan 23, 2024
1 parent 71bd4ab commit a32fc81
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 9 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ path = "src/wallet.rs"

[dependencies]
wallet_lib = { path = "../lib" }
rpassword = "7.3"
tokio = { version = "1.28.0", default-features = false, features = [
"rt",
"macros",
Expand All @@ -29,3 +30,6 @@ reqwest = "0.11.10"
graphql_client = "0.10.0"
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = "1.0.79"
sp-core = { version = "6.0.0", git = "https://github.com/paritytech/substrate.git", features = ["full_crypto"] }
sp-application-crypto = { version = "6.0.0", git = "https://github.com/paritytech/substrate.git" }
hex = "0.4.3"
65 changes: 56 additions & 9 deletions bin/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
//! Wallet daemon for efinity blockchain
//! Wallet daemon for Enjin Matrixchain
//!
//! It polls for transactions, signs them and sends them to the blockchain.
//! It polls for transactions from Enjin Platform
//! signs them and sends them to the blockchain.
//!
//! A configuration file is needed(See README for specifics)
use sp_core::crypto::Ss58Codec;
use sp_core::{sr25519, Pair};
use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use std::{sync::Arc, time::Duration};

use graphql_client::{GraphQLQuery, Response};
use reqwest;
use serde::Deserialize;
use serde_json::Value;
use std::env;
use std::error::Error;
use std::process::exit;
use subxt::DefaultConfig;
use tokio::signal;
use wallet_lib::wallet_trait::Wallet;
Expand Down Expand Up @@ -64,36 +71,76 @@ async fn update_user(
.await?;

let result: Response<update_user::ResponseData> = res.json().await?;
let data = result.data.expect("Missing response");
let data = result.data.expect("You are connected to a multi-tenant platform but the daemon has failed to update your account. Check your access token or if you are connected to the correct platform.");

Ok(data.update_user)
}

fn write_seed(seed: String) -> std::io::Result<()> {
let split_seed: Vec<String> = seed.split("///").map(|s| s.to_string()).collect();
let mnemonic = split_seed[0].clone();
let password = split_seed
.iter()
.skip(1)
.cloned()
.collect::<Vec<String>>()
.join(" ");

let key = sr25519::Pair::from_phrase(&mnemonic, Some(&*password))
.expect("Invalid mnemonic phrase or password");

let public_key = key.0.public();
let address = public_key.to_ss58check();
let hex_key = hex::encode(public_key);
let file_name = format!("73723235{}", hex_key);

println!("Public Key (Hex): 0x{}", hex_key.clone());
println!("Address (SS58): {}", address);
println!("Store file name: {}", file_name);

let mut file = File::create(format!("store/{}", file_name))?;
file.write_all(format!("\"{}\"", mnemonic).as_bytes())?;

Ok(())
}

#[tokio::main(flavor = "multi_thread")]
async fn main() {
let args: Vec<String> = env::args().skip(1).collect();

if let Some(arg) = args.first() {
if arg == "import" {
println!("Enjin Platform - Import Wallet");
let seed = rpassword::prompt_password("Please type your 12-word mnemonic: ").unwrap();
write_seed(seed).expect("Failed to import your wallet");
exit(0)
}
}

tracing_subscriber::fmt::init();

let (wallet_pair, graphql_endpoint, token) = load_wallet::<DefaultConfig>(load_config()).await;

let public_key = wallet_pair
.wallet
.account_id()
.await
.expect("Failed to decode daemon public key");
.expect("We have failed to decode your daemon public key");

let is_tenant = get_packages(graphql_endpoint.clone())
.await
.expect("Failed to connect with Enjin Platform");
.expect("We could not connect to Enjin Platform, check your connection or the url");

if is_tenant {
let updated = update_user(graphql_endpoint.clone(), token.clone(), public_key.to_string())
.await
.expect("You are connected to a multi-tenant platform but the daemon failed to update your user");
.expect("You are connected to a multi-tenant platform but the daemon has failed to update your account. Check your access token or if you are connected to the correct platform.");

println!("** Updated your wallet daemon address at Enjin Platform **");
println!(
"** Your account at Enjin Platform has been updated with your wallet daemon address"
);

if !updated {
panic!("You are connected to a multi-tenant platform but the daemon failed to update your user")
panic!("You are connected to a multi-tenant platform but the daemon has failed to update your account. Check your access token or if you are connected to the correct platform.")
}
}

Expand Down

0 comments on commit a32fc81

Please sign in to comment.