ennucore
5 years ago
8 changed files with 128 additions and 63 deletions
@ -1,14 +0,0 @@
|
||||
extern crate rand; |
||||
use serde::{Serialize, Deserialize}; |
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq)] |
||||
pub struct PublicKey { |
||||
pub id: u64 |
||||
} |
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq)] |
||||
pub struct SecretKey {} |
||||
|
||||
pub fn gen_keys() -> (PublicKey, SecretKey) { |
||||
(PublicKey { id: rand::random::<u64>() }, SecretKey {}) |
||||
} |
@ -0,0 +1,31 @@
|
||||
extern crate rand; |
||||
use serde::{Serialize, Deserialize}; |
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)] |
||||
pub struct PublicKey { |
||||
pub id: u64 |
||||
} |
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)] |
||||
pub struct KeyPack { |
||||
pub public_key: PublicKey, |
||||
secret_key: u64, |
||||
} |
||||
|
||||
impl KeyPack { |
||||
pub fn gen() -> KeyPack { |
||||
KeyPack { public_key: PublicKey { id: rand::random::<u64>() }, secret_key: 0 } |
||||
} |
||||
} |
||||
|
||||
impl PublicKey { |
||||
pub fn verify_sign(self, sign: u64) -> bool { |
||||
true |
||||
} |
||||
} |
||||
|
||||
impl KeyPack { |
||||
pub fn sign(self, msg: u64) -> u64 { |
||||
0 |
||||
} |
||||
} |
@ -1,16 +1,14 @@
|
||||
use crate::transport::Transport; |
||||
use crate::addresses::{PublicKey, SecretKey, gen_keys}; |
||||
use crate::crypto::{PublicKey, KeyPack}; |
||||
|
||||
|
||||
pub struct IronForce { |
||||
transport: Transport, |
||||
public: PublicKey, |
||||
secret: SecretKey, |
||||
key_pack: KeyPack |
||||
} |
||||
|
||||
impl IronForce { |
||||
pub fn new() -> IronForce { |
||||
let (public, secret) = gen_keys(); |
||||
IronForce { transport: Transport {}, public, secret } |
||||
pub fn gen() -> IronForce { |
||||
IronForce { transport: Transport {}, key_pack: KeyPack::gen() } |
||||
} |
||||
} |
||||
|
@ -1,36 +1,43 @@
|
||||
use crate::crypto::{PublicKey, KeyPack}; |
||||
use sha2::Digest; |
||||
use serde::{Serialize, Deserialize}; |
||||
use crate::addresses::PublicKey; |
||||
use alloc::vec::Vec; |
||||
use pinecone::{from_bytes, to_vec}; |
||||
use serde::private::de::size_hint::from_bounds; |
||||
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq)] |
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)] |
||||
pub enum MsgType { |
||||
MultiCast, |
||||
ToTarget(PublicKey), |
||||
} |
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq)] |
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)] |
||||
pub struct Message { |
||||
pub msg_type: MsgType, |
||||
pub hash: u64, |
||||
pub body: u64, |
||||
pub body: Vec<u8>, |
||||
pub source: PublicKey, |
||||
hash: u64, |
||||
sign: u64, |
||||
} |
||||
|
||||
impl Message { |
||||
fn get_hash(self) -> Vec<u8> { |
||||
let mut hasher = sha2::Sha256::new(); |
||||
hasher.input(self.ser()); |
||||
hasher.result().to_vec() |
||||
} |
||||
|
||||
pub fn new(msg_type: MsgType, body: Vec<u8>, key_pack: KeyPack) -> Message { |
||||
let msg = Message { msg_type, body, source: key_pack.public_key, hash: 0, sign: 0 }; |
||||
msg |
||||
} |
||||
|
||||
pub fn ser(self) -> Vec<u8> { |
||||
let mut buf = [0u8; 8192]; |
||||
let mut ser = serde_cbor::Serializer::new( |
||||
serde_cbor::ser::SliceWrite::new(&mut buf[..]) |
||||
); |
||||
self.serialize(&mut ser).unwrap(); |
||||
let writer = ser.into_inner(); |
||||
buf.to_vec() |
||||
to_vec(&self).expect("Serialization failed") |
||||
} |
||||
|
||||
pub fn deserialize(serialized: Vec<u8>) -> Self { |
||||
let mut scratch = [0u8; 8192]; |
||||
let msg = serde_cbor::de::from_slice_with_scratch(&serialized.as_slice(), &mut scratch).unwrap(); |
||||
msg |
||||
from_bytes(&serialized).expect("Deserialization failed") |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue