Skip to content
Snippets Groups Projects
Commit fbf539a8 authored by Wei Tang's avatar Wei Tang
Browse files

Update AuthorityId to be H384

parent 7a2e6a27
Branches
No related merge requests found
......@@ -2430,6 +2430,7 @@ dependencies = [
"parity-codec-derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)",
"shasper-crypto 0.1.0",
"sr-primitives 0.1.0 (git+https://github.com/paritytech/substrate)",
"sr-std 0.1.0 (git+https://github.com/paritytech/substrate)",
"substrate-primitives 0.1.0 (git+https://github.com/paritytech/substrate)",
......
pub extern crate bls_aggregates;
mod bls {
pub use bls_aggregates::AggregatePublicKey;
pub mod bls {
pub use bls_aggregates::AggregatePublicKey as AggregatePublic;
pub use bls_aggregates::AggregateSignature;
pub use bls_aggregates::Keypair as Pair;
pub use bls_aggregates::PublicKey;
pub use bls_aggregates::SecretKey;
pub use bls_aggregates::PublicKey as Public;
pub use bls_aggregates::SecretKey as Secret;
pub use bls_aggregates::Signature;
}
......@@ -12,6 +12,7 @@ substrate-primitives = { git = "https://github.com/paritytech/substrate", defaul
sr-std = { git = "https://github.com/paritytech/substrate", default-features = false }
serde = { version = "1.0", optional = true, default-features = false }
serde_derive = { version = "1.0", optional = true }
shasper-crypto = { path = "../crypto", default-features = false }
[features]
default = ["std"]
......@@ -24,4 +25,5 @@ std = [
"serde",
"serde/std",
"serde_derive",
"shasper-crypto/std",
]
......@@ -16,97 +16,57 @@
#[cfg(feature = "std")]
use serde::{Serialize, Serializer, Deserialize, Deserializer};
use H256;
#[cfg(feature = "std")]
use primitives::{ed25519, hexdisplay};
/// An identifier for an authority in the consensus algorithm. The same size as ed25519::Public.
#[derive(Clone, Copy, PartialEq, Eq, Default, Encode, Decode)]
pub struct AuthorityId(pub [u8; 32]);
use crypto::bls;
#[cfg(feature = "std")]
impl ::std::fmt::Display for AuthorityId {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{}", ed25519::Public(self.0).to_ss58check())
}
}
use primitives::bytes;
#[cfg(feature = "std")]
impl ::std::fmt::Debug for AuthorityId {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
let h = format!("{}", hexdisplay::HexDisplay::from(&self.0));
write!(f, "{} ({}…{})", ed25519::Public(self.0).to_ss58check(), &h[0..8], &h[60..])
}
construct_fixed_hash! {
/// Fixed 384-bit hash.
pub struct H384(48);
}
#[cfg(feature = "std")]
impl ::std::hash::Hash for AuthorityId {
fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
pub type AuthorityId = H384;
impl AsRef<[u8; 32]> for AuthorityId {
fn as_ref(&self) -> &[u8; 32] {
&self.0
#[cfg(feature = "std")]
impl Serialize for H384 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
bytes::serialize(&self.0, serializer)
}
}
impl AsRef<[u8]> for AuthorityId {
fn as_ref(&self) -> &[u8] {
&self.0[..]
#[cfg(feature = "std")]
impl<'de> Deserialize<'de> for H384 {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
bytes::deserialize_check_len(deserializer, bytes::ExpectedLen::Exact(48))
.map(|x| H384::from_slice(&x))
}
}
impl Into<[u8; 32]> for AuthorityId {
fn into(self) -> [u8; 32] {
self.0
impl ::parity_codec::Encode for H384 {
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.0.using_encoded(f)
}
}
impl From<[u8; 32]> for AuthorityId {
fn from(a: [u8; 32]) -> Self {
AuthorityId(a)
impl ::parity_codec::Decode for H384 {
fn decode<I: ::parity_codec::Input>(input: &mut I) -> Option<Self> {
<[u8; 48] as ::parity_codec::Decode>::decode(input).map(H384)
}
}
impl AsRef<AuthorityId> for AuthorityId {
fn as_ref(&self) -> &AuthorityId {
&self
impl H384 {
pub fn into_public(&self) -> Option<bls::Public> {
bls::Public::from_bytes(self.as_ref()).ok()
}
}
impl Into<H256> for AuthorityId {
fn into(self) -> H256 {
self.0.into()
pub fn from_public(public: bls::Public) -> Self {
H384::from_slice(&public.as_bytes())
}
}
#[cfg(feature = "std")]
impl Into<AuthorityId> for ed25519::Public {
impl Into<AuthorityId> for bls::Public {
fn into(self) -> AuthorityId {
AuthorityId(self.0)
}
}
#[cfg(feature = "std")]
impl From<AuthorityId> for ed25519::Public {
fn from(id: AuthorityId) -> Self {
ed25519::Public(id.0)
}
}
#[cfg(feature = "std")]
impl Serialize for AuthorityId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
ed25519::serialize(&self, serializer)
}
}
#[cfg(feature = "std")]
impl<'de> Deserialize<'de> for AuthorityId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
ed25519::deserialize(deserializer)
AuthorityId::from_public(self)
}
}
......@@ -30,15 +30,12 @@ extern crate sr_std as rstd;
extern crate serde_derive;
#[cfg(feature = "std")]
extern crate serde;
pub extern crate shasper_crypto as crypto;
mod authority_id;
pub use authority_id::AuthorityId;
construct_fixed_hash! {
/// Fixed 384-bit hash.
pub struct H384(48);
}
pub use primitives::{H256, OpaqueMetadata};
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment