Implemented loading Settings from file. Implemented mining of Keystore (key pair). Changed Transaction structure a lot. Added an icon to windows build. Changed some HTML.

This commit is contained in:
Revertron
2021-01-18 00:18:35 +01:00
parent 4703ae6f49
commit 70b3a833b9
15 changed files with 11036 additions and 299 deletions
+46 -26
View File
@@ -1,20 +1,25 @@
extern crate crypto;
extern crate serde;
extern crate serde_json;
use crypto::ed25519::{keypair, signature, verify};
use rand::{thread_rng, Rng};
use std::fs;
use std::fmt;
use std::fs;
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;
use serde::export::fmt::Error;
use serde::{Serialize, Deserialize, Serializer, Deserializer};
// For deserialization
use serde::de::{Error as DeError, Visitor};
use serde::export::Formatter;
use crate::hash_is_good;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Keystore {
private_key: Key,
public_key: Key,
private_key: Bytes,
public_key: Bytes,
#[serde(skip)]
seed: Vec<u8>
}
@@ -25,12 +30,12 @@ impl Keystore {
let mut rng = thread_rng();
rng.fill(&mut buf);
let (private, public) = keypair(&buf);
Keystore {private_key: Key::from_bytes(&private), public_key: Key::from_bytes(&public), seed: Vec::from(&buf[..])}
Keystore {private_key: Bytes::from_bytes(&private), public_key: Bytes::from_bytes(&public), seed: Vec::from(&buf[..])}
}
pub fn from_bytes(seed: &[u8]) -> Self {
let (private, public) = keypair(&seed);
Keystore {private_key: Key::from_bytes(&private), public_key: Key::from_bytes(&public), seed: Vec::from(seed)}
Keystore {private_key: Bytes::from_bytes(&private), public_key: Bytes::from_bytes(&public), seed: Vec::from(seed)}
}
pub fn from_file(filename: &str, _password: &str) -> Option<Self> {
@@ -44,11 +49,22 @@ impl Keystore {
}
}
pub fn get_public(&self) -> Key {
//TODO Implement error conditions
pub fn save(&self, filename: &str, password: &str) {
match File::create(Path::new(filename)) {
Ok(mut f) => {
//TODO implement key encryption
f.write_all(&self.seed);
}
Err(_) => { println!("Error saving key file!"); }
}
}
pub fn get_public(&self) -> Bytes {
self.public_key.clone()
}
pub fn get_private(&self) -> Key {
pub fn get_private(&self) -> Bytes {
self.private_key.clone()
}
@@ -59,20 +75,24 @@ impl Keystore {
pub fn check(&self, message: &[u8], public_key: &[u8], signature: &[u8]) -> bool {
verify(message, public_key, signature)
}
pub fn hash_is_good(&self, difficulty: usize) -> bool {
hash_is_good(self.public_key.as_bytes(), difficulty)
}
}
#[derive(Clone)]
pub struct Key {
pub struct Bytes {
data: Vec<u8>
}
impl Key {
impl Bytes {
pub fn new(data: Vec<u8>) -> Self {
Key { data }
Bytes { data }
}
pub fn from_bytes(data: &[u8]) -> Self {
Key { data: Vec::from(data) }
Bytes { data: Vec::from(data) }
}
pub fn length(&self) -> usize {
@@ -101,21 +121,21 @@ impl Key {
}
pub fn zero32() -> Self {
Key { data: [0u8; 32].to_vec() }
Bytes { data: [0u8; 32].to_vec() }
}
pub fn zero64() -> Self {
Key { data: [0u8; 64].to_vec() }
Bytes { data: [0u8; 64].to_vec() }
}
}
impl Default for Key {
fn default() -> Key {
Key { data: Vec::new() }
impl Default for Bytes {
fn default() -> Bytes {
Bytes { data: Vec::new() }
}
}
impl PartialEq for Key {
impl PartialEq for Bytes {
fn eq(&self, other: &Self) -> bool {
crate::utils::same_hash(&self.data, &other.data)
}
@@ -125,23 +145,23 @@ impl PartialEq for Key {
}
}
impl fmt::Debug for Key {
impl fmt::Debug for Bytes {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str(&crate::utils::to_hex(&self.data))
}
}
impl Serialize for Key {
impl Serialize for Bytes {
fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
S: Serializer {
serializer.serialize_str(&crate::utils::to_hex(&self.data))
}
}
struct KeyVisitor;
struct BytesVisitor;
impl<'de> Visitor<'de> for KeyVisitor {
type Value = Key;
impl<'de> Visitor<'de> for BytesVisitor {
type Value = Bytes;
fn expecting(&self, formatter: &mut Formatter) -> Result<(), Error> {
formatter.write_str("32 or 64 bytes")
@@ -149,7 +169,7 @@ impl<'de> Visitor<'de> for KeyVisitor {
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: DeError, {
if value.len() == 64 || value.len() == 128 {
Ok(Key::new(crate::from_hex(value).unwrap()))
Ok(Bytes::new(crate::from_hex(value).unwrap()))
} else {
Err(E::custom("Key must be 32 or 64 bytes!"))
}
@@ -157,15 +177,15 @@ impl<'de> Visitor<'de> for KeyVisitor {
fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E> where E: DeError, {
if value.len() == 32 || value.len() == 64 {
Ok(Key::from_bytes(value))
Ok(Bytes::from_bytes(value))
} else {
Err(E::custom("Key must be 32 or 64 bytes!"))
}
}
}
impl<'dd> Deserialize<'dd> for Key {
impl<'dd> Deserialize<'dd> for Bytes {
fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'dd>>::Error> where D: Deserializer<'dd> {
deserializer.deserialize_str(KeyVisitor)
deserializer.deserialize_str(BytesVisitor)
}
}