2020-04-18 21:31:40 +02:00
|
|
|
use crate::{Keystore, Blockchain};
|
|
|
|
|
use std::collections::HashMap;
|
2021-01-18 00:18:35 +01:00
|
|
|
use serde::{Serialize, Deserialize, Serializer, Deserializer};
|
|
|
|
|
use serde::de::Error;
|
|
|
|
|
use std::fs::File;
|
|
|
|
|
use std::io::Read;
|
2020-04-18 21:31:40 +02:00
|
|
|
|
|
|
|
|
pub struct Context {
|
|
|
|
|
pub(crate) settings: Settings,
|
|
|
|
|
pub(crate) keystore: Keystore,
|
|
|
|
|
pub(crate) blockchain: Blockchain,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Context {
|
|
|
|
|
/// Creating an essential context to work with
|
|
|
|
|
pub fn new(settings: Settings, keystore: Keystore, blockchain: Blockchain) -> Context {
|
|
|
|
|
Context { settings, keystore, blockchain }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Load keystore and return Context
|
|
|
|
|
pub fn load_keystore<S: Into<String>>(mut self, name: S, password: S) -> Context {
|
|
|
|
|
let filename = &name.into();
|
|
|
|
|
match Keystore::from_file(filename, &password.into()) {
|
|
|
|
|
None => {
|
|
|
|
|
println!("Error loading keystore '{}'!", filename);
|
|
|
|
|
},
|
|
|
|
|
Some(keystore) => {
|
|
|
|
|
self.keystore = keystore;
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_keystore(&self) -> Keystore {
|
|
|
|
|
self.keystore.clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_keystore(&mut self, keystore: Keystore) {
|
|
|
|
|
self.keystore = keystore;
|
|
|
|
|
}
|
2021-01-14 18:34:43 +01:00
|
|
|
|
|
|
|
|
pub fn get_blockchain(&self) -> &Blockchain {
|
|
|
|
|
&self.blockchain
|
|
|
|
|
}
|
2020-04-18 21:31:40 +02:00
|
|
|
}
|
|
|
|
|
|
2021-01-18 00:18:35 +01:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2020-04-18 21:31:40 +02:00
|
|
|
pub struct Settings {
|
|
|
|
|
pub chain_id: u32,
|
|
|
|
|
pub version: u32,
|
2021-01-18 00:18:35 +01:00
|
|
|
pub key_file: String
|
2020-04-18 21:31:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Settings {
|
2021-01-18 00:18:35 +01:00
|
|
|
pub fn new<S: Into<String>>(settings: S) -> serde_json::Result<Settings> {
|
|
|
|
|
serde_json::from_str(&settings.into())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn load(file_name: &str) -> Option<Settings> {
|
|
|
|
|
match File::open(file_name) {
|
|
|
|
|
Ok(mut file) => {
|
|
|
|
|
let mut text = String::new();
|
|
|
|
|
file.read_to_string(&mut text);
|
|
|
|
|
let loaded = serde_json::from_str(&text);
|
|
|
|
|
return if loaded.is_ok() {
|
|
|
|
|
Some(loaded.unwrap())
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Err(..) => None
|
|
|
|
|
}
|
2020-04-18 21:31:40 +02:00
|
|
|
}
|
|
|
|
|
}
|