First commit of 0.5.* branch.

This commit is contained in:
Revertron
2021-05-02 12:55:51 +02:00
parent 8bb2d9af9f
commit 9949d13e62
19 changed files with 202 additions and 422 deletions
+28 -32
View File
@@ -1,4 +1,5 @@
use std::fmt;
use std::fmt::{Display, Formatter};
use serde::{Deserialize, Serialize, Serializer};
use serde::ser::SerializeStruct;
@@ -6,29 +7,37 @@ use serde::ser::SerializeStruct;
use crate::blockchain::hash_utils::*;
use crate::bytes::Bytes;
use crate::dns::protocol::DnsRecord;
use std::fmt::{Display, Formatter};
use crate::{CLASS_ORIGIN, CLASS_DOMAIN};
extern crate serde;
extern crate serde_json;
#[derive(Clone, Deserialize, PartialEq)]
pub struct Transaction {
#[serde(default, skip_serializing_if = "Bytes::is_zero")]
pub identity: Bytes,
#[serde(default, skip_serializing_if = "Bytes::is_zero")]
pub confirmation: Bytes,
pub class: String,
pub data: String,
pub pub_key: Bytes,
#[serde(default, skip_serializing_if = "Bytes::is_zero")]
pub owner: Bytes,
}
impl Transaction {
pub fn from_str(identity: String, method: String, data: String, pub_key: Bytes) -> Self {
pub fn from_str(identity: String, method: String, data: String, owner: Bytes) -> Self {
let hash = hash_identity(&identity, None);
let confirmation = hash_identity(&identity, Some(&pub_key));
return Self::new(hash, confirmation, method, data, pub_key);
let confirmation = hash_identity(&identity, Some(&owner));
return Self::new(hash, confirmation, method, data, owner);
}
pub fn new(identity: Bytes, confirmation: Bytes, method: String, data: String, pub_key: Bytes) -> Self {
Transaction { identity, confirmation, class: method, data, pub_key }
pub fn new(identity: Bytes, confirmation: Bytes, method: String, data: String, owner: Bytes) -> Self {
Transaction { identity, confirmation, class: method, data, owner }
}
pub fn origin(hash: Bytes, owner: Bytes) -> Self {
let data = serde_json::to_string(&Origin { zones: hash }).unwrap();
Transaction { identity: Bytes::default(), confirmation: Bytes::default(), class: String::from(CLASS_ORIGIN), data, owner }
}
pub fn from_json(json: &str) -> Option<Self> {
@@ -50,13 +59,13 @@ impl Transaction {
pub fn check_identity(&self, domain: &str) -> bool {
let hash = hash_identity(&domain, None);
let confirmation = hash_identity(&domain, Some(&self.pub_key));
let confirmation = hash_identity(&domain, Some(&self.owner));
self.identity.eq(&hash) && self.confirmation.eq(&confirmation)
}
/// Returns [DomainData] from this transaction if it has it
pub fn get_domain_data(&self) -> Option<DomainData> {
if self.class == "domain" {
if self.class == CLASS_DOMAIN {
if let Ok(data) = serde_json::from_str::<DomainData>(&self.data) {
return Some(data)
}
@@ -69,12 +78,9 @@ impl Transaction {
match what {
None => { TransactionType::Signing }
Some(transaction) => {
if let Some(_) = transaction.get_domain_data() {
if transaction.class == CLASS_DOMAIN {
return TransactionType::Domain;
}
if let Ok(_) = serde_json::from_str::<ZoneData>(&transaction.data) {
return TransactionType::Zone;
}
TransactionType::Unknown
}
}
@@ -88,7 +94,7 @@ impl fmt::Debug for Transaction {
.field("confirmation", &self.confirmation)
.field("class", &self.class)
.field("data", &self.data)
.field("pub_key", &&self.pub_key)
.field("pub_key", &&self.owner)
.finish()
}
}
@@ -100,7 +106,7 @@ impl Serialize for Transaction {
structure.serialize_field("confirmation", &self.confirmation)?;
structure.serialize_field("class", &self.class)?;
structure.serialize_field("data", &self.data)?;
structure.serialize_field("pub_key", &self.pub_key)?;
structure.serialize_field("pub_key", &self.owner)?;
structure.end()
}
}
@@ -109,38 +115,28 @@ pub enum TransactionType {
Unknown,
Signing,
Domain,
Zone,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct DomainData {
pub domain: Bytes,
pub zone: String,
pub info: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub records: Vec<DnsRecord>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub contacts: Vec<ContactsData>,
#[serde(default)]
pub owners: Vec<Bytes>
}
impl DomainData {
pub fn new(domain: Bytes, zone: String, records: Vec<DnsRecord>, contacts: Vec<ContactsData>, owners: Vec<Bytes>) -> Self {
Self { domain, zone, records, contacts, owners }
pub fn new(domain: Bytes, zone: String, info: String, records: Vec<DnsRecord>, contacts: Vec<ContactsData>) -> Self {
Self { domain, zone, info, records, contacts }
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct ZoneData {
pub name: String,
pub difficulty: u32,
pub yggdrasil: bool,
#[serde(default)]
pub owners: Vec<Bytes>
}
impl Display for ZoneData {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.write_str(&format!("{} ({})", self.name, self.difficulty))
}
pub struct Origin {
zones: Bytes
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]