From 87ffeaa833f3e815de29a2193ef81c6d9b532612 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 22 Jan 2019 09:04:57 +1100 Subject: [PATCH] Add method to bls::PublicKey for short ID --- beacon_chain/utils/bls/Cargo.toml | 1 + beacon_chain/utils/bls/src/public_key.rs | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/beacon_chain/utils/bls/Cargo.toml b/beacon_chain/utils/bls/Cargo.toml index 9e782b059..0dc64e483 100644 --- a/beacon_chain/utils/bls/Cargo.toml +++ b/beacon_chain/utils/bls/Cargo.toml @@ -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" } diff --git a/beacon_chain/utils/bls/src/public_key.rs b/beacon_chain/utils/bls/src/public_key.rs index be260bf79..d3c735e8b 100644 --- a/beacon_chain/utils/bls/src/public_key.rs +++ b/beacon_chain/utils/bls/src/public_key.rs @@ -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 {