allow polkadot-primitives to compile on nightly

This commit is contained in:
Robert Habermeier
2018-01-31 22:27:07 +01:00
parent 29c3a585a1
commit 0035369f82
9 changed files with 58 additions and 30 deletions
+17 -3
View File
@@ -14,15 +14,29 @@
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
use std::fmt;
use core::fmt;
use serde::{de, Serializer, Deserializer};
#[cfg(not(feature = "std"))]
mod alloc_types {
pub use ::alloc::string::String;
pub use ::alloc::vec::Vec;
}
#[cfg(feature = "std")]
mod alloc_types {
pub use ::std::vec::Vec;
pub use ::std::string::String;
}
pub use self::alloc_types::*;
/// Serializes a slice of bytes.
pub fn serialize<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error> where
S: Serializer,
{
let hex = ::rustc_hex::ToHex::to_hex(bytes);
let hex: String = ::rustc_hex::ToHex::to_hex(bytes);
serializer.serialize_str(&format!("0x{}", hex))
}
@@ -38,7 +52,7 @@ pub fn serialize_uint<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error>
return serializer.serialize_str("0x0");
}
let hex = ::rustc_hex::ToHex::to_hex(bytes);
let hex: String = ::rustc_hex::ToHex::to_hex(bytes);
let has_leading_zero = !hex.is_empty() && &hex[0..1] == "0";
serializer.serialize_str(
&format!("0x{}", if has_leading_zero { &hex[1..] } else { &hex })
+1 -1
View File
@@ -16,7 +16,7 @@
//! Contract execution data.
use bytes;
use bytes::{self, Vec};
/// Contract call data.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
+7 -6
View File
@@ -16,6 +16,7 @@
//! Simple Ed25519 API.
use bytes::Vec;
use untrusted;
use ring::{rand, signature};
use rustc_hex::FromHex;
@@ -108,14 +109,14 @@ impl Pair {
}
/// Make a new key pair from the raw secret.
pub fn from_secret(secret: &[u8; 32]) -> Pair {
let mut pkcs8_bytes = FromHex::from_hex("302e020100300506032b657004220420").unwrap();
let mut pkcs8_bytes: Vec<u8> = FromHex::from_hex("302e020100300506032b657004220420").unwrap();
pkcs8_bytes.extend_from_slice(&secret[..]);
Pair(signature::Ed25519KeyPair::from_pkcs8_maybe_unchecked(untrusted::Input::from(&pkcs8_bytes)).unwrap())
}
/// Make a new key pair from the raw secret and public key (it will check to make sure
/// they correspond to each other).
pub fn from_both(secret_public: &[u8; 64]) -> Option<Pair> {
let mut pkcs8_bytes = FromHex::from_hex("3053020101300506032b657004220420").unwrap();
let mut pkcs8_bytes: Vec<u8> = FromHex::from_hex("3053020101300506032b657004220420").unwrap();
pkcs8_bytes.extend_from_slice(&secret_public[0..32]);
pkcs8_bytes.extend_from_slice(&[0xa1u8, 0x23, 0x03, 0x21, 0x00]);
pkcs8_bytes.extend_from_slice(&secret_public[32..64]);
@@ -151,13 +152,13 @@ impl Signature {
impl From<&'static str> for Public {
fn from(hex: &'static str) -> Self {
let mut r = [0u8; 32];
r.copy_from_slice(&FromHex::from_hex(hex).unwrap()[0..32]);
r.copy_from_slice(&FromHex::from_hex::<Vec<_>>(hex).unwrap()[0..32]);
Public(r)
}
}
impl From<&'static str> for Pair {
fn from(hex: &'static str) -> Self {
let data = FromHex::from_hex(hex).expect("Key pair given is static so hex should be good.");
let data = FromHex::from_hex::<Vec<_>>(hex).expect("Key pair given is static so hex should be good.");
match data.len() {
32 => {
let mut r = [0u8; 32];
@@ -178,7 +179,7 @@ impl From<&'static str> for Pair {
impl From<&'static str> for Signature {
fn from(hex: &'static str) -> Self {
let mut r = [0u8; 64];
r.copy_from_slice(&FromHex::from_hex(hex).unwrap()[0..64]);
r.copy_from_slice(&FromHex::from_hex::<Vec<_>>(hex).unwrap()[0..64]);
Signature(r)
}
}
@@ -228,7 +229,7 @@ mod test {
let pair = Pair::from_seed(b"12345678901234567890123456789012");
let public = pair.public();
assert_eq!(public, "2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee".into());
let message = FromHex::from_hex("2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee00000000000000000228000000d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a4500000000000000").unwrap();
let message: Vec<u8> = FromHex::from_hex("2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee00000000000000000228000000d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a4500000000000000").unwrap();
let signature = pair.sign(&message[..]);
assert!(signature.verify(&message[..], &public));
}
+2 -2
View File
@@ -24,7 +24,7 @@ impl<'a> HexDisplay<'a> {
pub fn from(d: &'a AsBytesRef) -> Self { HexDisplay(d.as_bytes_ref()) }
}
impl<'a> ::std::fmt::Display for HexDisplay<'a> {
impl<'a> ::core::fmt::Display for HexDisplay<'a> {
fn fmt(&self, fmtr: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
for byte in self.0 {
try!( fmtr.write_fmt(format_args!("{:02x}", byte)));
@@ -47,7 +47,7 @@ impl AsBytesRef for [u8] {
fn as_bytes_ref(&self) -> &[u8] { &self }
}
impl AsBytesRef for Vec<u8> {
impl AsBytesRef for ::bytes::Vec<u8> {
fn as_bytes_ref(&self) -> &[u8] { &self }
}
+8 -1
View File
@@ -18,6 +18,9 @@
#![warn(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std"), feature(alloc))]
extern crate rustc_hex;
extern crate serde;
extern crate ring;
@@ -35,7 +38,7 @@ extern crate serde_derive;
#[macro_use]
extern crate uint as uint_crate;
#[cfg(feature="std")]
#[cfg(feature = "std")]
extern crate core;
#[cfg(test)]
extern crate polkadot_serializer;
@@ -43,6 +46,10 @@ extern crate polkadot_serializer;
#[macro_use]
extern crate pretty_assertions;
#[cfg(not(feature = "std"))]
#[macro_use]
extern crate alloc;
mod bytes;
pub mod block;
pub mod contract;
+1 -1
View File
@@ -16,7 +16,7 @@
//! Parachain data types.
use bytes;
use bytes::{self, Vec};
/// Unique identifier of a parachain.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Serialize, Deserialize)]
+1 -1
View File
@@ -16,7 +16,7 @@
//! Validator primitives.
use bytes;
use bytes::{self, Vec};
use parachain;
/// Parachain outgoing message.