Changed and refactored block and transaction structures. Moved signature from transaction scope to block.

This commit is contained in:
Revertron
2021-03-02 18:11:17 +01:00
parent d5a19c92c3
commit 92ebefce91
11 changed files with 206 additions and 215 deletions
+18 -31
View File
@@ -16,18 +16,17 @@ pub struct Transaction {
pub method: String,
pub data: String,
pub pub_key: Bytes,
pub signature: Bytes,
}
impl Transaction {
pub fn from_str(identity: String, method: String, data: String, pub_key: Bytes) -> Self {
let hash = Self::hash_identity(&identity);
let confirmation = Self::hash_with_key(&identity, &pub_key);
let hash = hash_identity(&identity, None);
let confirmation = hash_identity(&identity, Some(&pub_key));
return Self::new(hash, confirmation, method, data, pub_key);
}
pub fn new(identity: Bytes, confirmation: Bytes, method: String, data: String, pub_key: Bytes) -> Self {
Transaction { identity, confirmation, method, data, pub_key, signature: Bytes::zero64() }
Transaction { identity, confirmation, method, data, pub_key }
}
pub fn from_json(json: &str) -> Option<Self> {
@@ -37,10 +36,6 @@ impl Transaction {
}
}
pub fn set_signature(&mut self, hash: Bytes) {
self.signature = hash;
}
pub fn get_bytes(&self) -> Vec<u8> {
// Let it panic if something is not okay
serde_json::to_vec(&self).unwrap()
@@ -51,26 +46,9 @@ impl Transaction {
serde_json::to_string(&self).unwrap()
}
pub fn hash_identity(identity: &str) -> Bytes {
let mut buf: [u8; 32] = [0; 32];
let mut digest = Sha256::new();
digest.input_str(identity);
digest.result(&mut buf);
Bytes::from_bytes(&buf)
}
pub fn hash_with_key(identity: &str, key: &Bytes) -> Bytes {
let mut buf: [u8; 32] = [0; 32];
let mut digest = Sha256::new();
digest.input_str(identity);
digest.input(key.as_bytes());
digest.result(&mut buf);
Bytes::from_bytes(&buf)
}
pub fn check_for(&self, domain: &str) -> bool {
let hash = Self::hash_identity(&domain);
let confirmation = Self::hash_with_key(&domain, &self.pub_key);
pub fn check_identity(&self, domain: &str) -> bool {
let hash = hash_identity(&domain, None);
let confirmation = hash_identity(&domain, Some(&self.pub_key));
self.identity.eq(&hash) && self.confirmation.eq(&confirmation)
}
}
@@ -83,20 +61,29 @@ impl fmt::Debug for Transaction {
.field("method", &self.method)
.field("data", &self.data)
.field("pub", &&self.pub_key)
.field("sign", &&self.signature)
.finish()
}
}
impl Serialize for Transaction {
fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where S: Serializer {
let mut structure = serializer.serialize_struct("Transaction", 6).unwrap();
let mut structure = serializer.serialize_struct("Transaction", 4).unwrap();
structure.serialize_field("identity", &self.identity)?;
structure.serialize_field("confirmation", &self.confirmation)?;
structure.serialize_field("method", &self.method)?;
structure.serialize_field("data", &self.data)?;
structure.serialize_field("pub_key", &self.pub_key)?;
structure.serialize_field("signature", &self.signature)?;
structure.end()
}
}
pub fn hash_identity(identity: &str, key: Option<&Bytes>) -> Bytes {
let mut buf: [u8; 32] = [0; 32];
let mut digest = Sha256::new();
digest.input_str(identity);
if let Some(key) = key {
digest.input(key.as_bytes());
}
digest.result(&mut buf);
Bytes::from_bytes(&buf)
}