Commit ffb20a97 authored by Éloïs's avatar Éloïs
Browse files

[enh] create peer card v11 (ws2pv2)

parent 8465a34c
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -166,6 +166,11 @@ dependencies = [
 "cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "difference"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "dirs"
version = "1.0.2"
@@ -281,6 +286,7 @@ version = "0.8.0-a0.1"
dependencies = [
 "base58 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "base64 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)",
 "byteorder 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
 "duniter-crypto 0.2.0-a0.1",
 "lazy_static 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
 "linked-hash-map 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -324,6 +330,7 @@ dependencies = [
 "duniter-documents 0.8.0-a0.1",
 "duniter-module 0.1.0-a0.1",
 "lazy_static 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
 "pretty_assertions 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
 "regex 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
 "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",
 "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -662,6 +669,15 @@ name = "pkg-config"
version = "0.3.11"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "pretty_assertions"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "proc-macro2"
version = "0.4.6"
@@ -1147,6 +1163,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum crossbeam-deque 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f739f8c5363aca78cfb059edf753d8f0d36908c348f3d8d1503f03d8b75d9cf3"
"checksum crossbeam-epoch 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "927121f5407de9956180ff5e936fe3cf4324279280001cd56b669d28ee7e9150"
"checksum crossbeam-utils 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2760899e32a1d58d5abb31129f8fae5de75220bc2176e77ff7c627ae45c918d9"
"checksum difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198"
"checksum dirs 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "37a76dd8b997af7107d0bb69d43903cf37153a18266f8b3fdb9911f28efb5444"
"checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab"
"checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0"
@@ -1184,6 +1201,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum pbr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "deb73390ab68d81992bd994d145f697451bb0b54fd39738e72eef32458ad6907"
"checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831"
"checksum pkg-config 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)" = "110d5ee3593dbb73f56294327fe5668bcc997897097cbc76b51e7aed3f52452f"
"checksum pretty_assertions 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3a029430f0d744bc3d15dd474d591bed2402b645d024583082b9f63bb936dac6"
"checksum proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "effdb53b25cdad54f8f48843d67398f7ef2e14f12c1b4cb4effc549a6462a4d6"
"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
"checksum quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e44651a0dc4cdd99f71c83b561e221f714912d11af1a4dff0631f923d53af035"
+8 −0
Original line number Diff line number Diff line
@@ -134,6 +134,10 @@ impl super::Signature for Signature {
        }
    }

    fn to_bytes_vector(&self) -> Vec<u8> {
        self.0.to_vec()
    }

    fn to_base64(&self) -> String {
        base64::encode(&self.0[..]) // need to take a slice for required trait `AsRef<[u8]>`
    }
@@ -215,6 +219,10 @@ impl super::PublicKey for PublicKey {
        }
    }

    fn to_bytes_vector(&self) -> Vec<u8> {
        self.0.to_vec()
    }

    fn verify(&self, message: &[u8], signature: &Self::Signature) -> bool {
        crypto::ed25519::verify(message, &self.0, &signature.0)
    }
+18 −0
Original line number Diff line number Diff line
@@ -103,6 +103,9 @@ pub trait Signature: Clone + Display + Debug + PartialEq + Eq + Hash {
    /// [`BaseConvertionError`]: enum.BaseConvertionError.html
    fn from_base64(base64_string: &str) -> Result<Self, BaseConvertionError>;

    /// Convert Signature into butes vector
    fn to_bytes_vector(&self) -> Vec<u8>;

    /// Encode the signature into Base64 string format.
    fn to_base64(&self) -> String;
}
@@ -135,6 +138,12 @@ impl Signature for Sig {
    fn from_base64(_base64_string: &str) -> Result<Self, BaseConvertionError> {
        unimplemented!()
    }
    fn to_bytes_vector(&self) -> Vec<u8> {
        match *self {
            Sig::Ed25519(ed25519_sig) => ed25519_sig.to_bytes_vector(),
            Sig::Schnorr() => panic!("Schnorr algo not yet supported !"),
        }
    }
    fn to_base64(&self) -> String {
        match *self {
            Sig::Ed25519(ed25519_sig) => ed25519_sig.to_base64(),
@@ -165,6 +174,9 @@ pub trait PublicKey: Clone + Display + Debug + PartialEq + Eq + Hash + ToBase58
    /// [`BaseConvertionError`]: enum.BaseConvertionError.html
    fn from_base58(base58_string: &str) -> Result<Self, BaseConvertionError>;

    /// Convert into bytes vector
    fn to_bytes_vector(&self) -> Vec<u8>;

    /// Verify a signature with this public key.
    fn verify(&self, message: &[u8], signature: &Self::Signature) -> bool;
}
@@ -208,6 +220,12 @@ impl PublicKey for PubKey {
    fn from_base58(_base58_string: &str) -> Result<Self, BaseConvertionError> {
        unimplemented!()
    }
    fn to_bytes_vector(&self) -> Vec<u8> {
        match *self {
            PubKey::Ed25519(ed25519_pubkey) => ed25519_pubkey.to_bytes_vector(),
            PubKey::Schnorr() => panic!("Schnorr algo not yet supported !"),
        }
    }
    fn verify(&self, message: &[u8], signature: &Self::Signature) -> bool {
        match *self {
            PubKey::Ed25519(ed25519_pubkey) => if let Sig::Ed25519(ed25519_sig) = signature {
+1 −0
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ path = "lib.rs"
[dependencies]
base58 = "0.1.*"
base64 = "0.9.*"
byteorder = "1.2.3"
duniter-crypto = { path = "../crypto" }
lazy_static = "1.0.*"
linked-hash-map = "0.5.*"
+59 −0
Original line number Diff line number Diff line
@@ -30,15 +30,19 @@ extern crate serde_derive;

extern crate base58;
extern crate base64;
extern crate byteorder;
extern crate crypto;
extern crate duniter_crypto;
extern crate linked_hash_map;
extern crate regex;
extern crate serde;

use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use duniter_crypto::keys::BaseConvertionError;
use std::cmp::Ordering;
use std::fmt::{Debug, Display, Error, Formatter};
use std::io::Cursor;
use std::mem;

pub mod blockchain;

@@ -78,6 +82,11 @@ impl Default for Hash {
}

impl Hash {
    /// Convert Hash into bytes vector
    pub fn to_bytes_vector(&self) -> Vec<u8> {
        self.0.to_vec()
    }

    /// Convert a `Hash` to an hex string.
    pub fn to_hex(&self) -> String {
        let strings: Vec<String> = self.0.iter().map(|b| format!("{:02X}", b)).collect();
@@ -210,7 +219,57 @@ impl Ord for Blockstamp {
    }
}

#[derive(Debug)]
/// Error when converting a byte vector to Blockstamp
pub enum ReadBytesBlockstampError {
    /// Bytes vector is too short
    TooShort(),
    /// Bytes vector is too long
    TooLong(),
    /// IoError
    IoError(::std::io::Error),
}

impl From<::std::io::Error> for ReadBytesBlockstampError {
    fn from(e: ::std::io::Error) -> Self {
        ReadBytesBlockstampError::IoError(e)
    }
}

impl Blockstamp {
    /// Convert Blockstamp into bytes vector
    pub fn to_bytes_vector(&self) -> Vec<u8> {
        let mut bytes = Vec::with_capacity(36);
        // BlockId
        let mut buffer = [0u8; mem::size_of::<u32>()];
        buffer
            .as_mut()
            .write_u32::<BigEndian>(self.id.0)
            .expect("Unable to write");
        bytes.extend_from_slice(&buffer);
        // BlockHash
        bytes.extend(self.hash.0.to_bytes_vector());
        bytes
    }
    /// Create Blockstamp from bytes slice
    pub fn from_bytes_slice(bytes: &[u8]) -> Result<Blockstamp, ReadBytesBlockstampError> {
        if bytes.len() > 36 {
            Err(ReadBytesBlockstampError::TooLong())
        } else if bytes.len() < 36 {
            Err(ReadBytesBlockstampError::TooShort())
        } else {
            // read id
            let mut id_bytes = Cursor::new(bytes[0..4].to_vec());
            let id = BlockId(id_bytes.read_u32::<BigEndian>()?);
            // read hash
            let mut hash_datas: [u8; 32] = [0u8; 32];
            hash_datas.copy_from_slice(&bytes[4..36]);
            let hash = BlockHash(Hash(hash_datas));
            // return Blockstamp
            Ok(Blockstamp { id, hash })
        }
    }

    /// Create a `BlockUId` from a text.
    pub fn from_string(src: &str) -> Result<Blockstamp, BlockUIdParseError> {
        let mut split = src.split('-');
Loading