Add method to bls::PublicKey for short ID

This commit is contained in:
Paul Hauner 2019-01-22 09:04:57 +11:00
parent b8ba0cd698
commit 87ffeaa833
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
2 changed files with 11 additions and 0 deletions

View File

@ -7,4 +7,5 @@ edition = "2018"
[dependencies]
bls-aggregates = { git = "https://github.com/sigp/signature-schemes" }
hashing = { path = "../hashing" }
hex = "0.3"
ssz = { path = "../ssz" }

View File

@ -1,5 +1,6 @@
use super::SecretKey;
use bls_aggregates::PublicKey as RawPublicKey;
use hex::encode as hex_encode;
use ssz::{decode_ssz_list, ssz_encode, Decodable, DecodeError, Encodable, SszStream};
use std::hash::{Hash, Hasher};
@ -19,6 +20,15 @@ impl PublicKey {
pub fn as_raw(&self) -> &RawPublicKey {
&self.0
}
/// Returns the last 6 bytes of the SSZ encoding of the public key, as a hex string.
///
/// Useful for providing a short identifier to the user.
pub fn concatenated_hex_id(&self) -> String {
let bytes = ssz_encode(self);
let end_bytes = &bytes[bytes.len().saturating_sub(6)..bytes.len()];
hex_encode(end_bytes)
}
}
impl Encodable for PublicKey {