Skip to content
This repository has been archived by the owner on Jun 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #291 from tendermint/bucky/id
Browse files Browse the repository at this point in the history
Support Id generation from ed25519 pubkeys
  • Loading branch information
tarcieri authored Jul 16, 2019
2 parents c9f48bb + 3dfbc8f commit f4ddd3b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
35 changes: 34 additions & 1 deletion tendermint-rs/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::error::Error;
#[cfg(feature = "serde")]
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use sha2::{Digest, Sha256};
use signatory::ecdsa::curve::secp256k1;
use signatory::{ecdsa::curve::secp256k1, ed25519};
use std::{
fmt::{self, Debug, Display},
str::FromStr,
Expand Down Expand Up @@ -59,6 +59,7 @@ impl Debug for Id {
}
}

// TODO: should be RIPEMD160(SHA256(pk))
impl From<secp256k1::PublicKey> for Id {
fn from(pk: secp256k1::PublicKey) -> Id {
let digest = Sha256::digest(pk.as_bytes());
Expand All @@ -68,6 +69,16 @@ impl From<secp256k1::PublicKey> for Id {
}
}

// SHA256(pk)[:20]
impl From<ed25519::PublicKey> for Id {
fn from(pk: ed25519::PublicKey) -> Id {
let digest = Sha256::digest(pk.as_bytes());
let mut bytes = [0u8; LENGTH];
bytes.copy_from_slice(&digest[..LENGTH]);
Id(bytes)
}
}

/// Decode account ID from hex
impl FromStr for Id {
type Err = Error;
Expand Down Expand Up @@ -111,3 +122,25 @@ impl Serialize for Id {
self.to_string().serialize(serializer)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_ed25519_id() {
// test vector for pubkey and id (address)
let pubkey_hex = "14253D61EF42D166D02E68D540D07FDF8D65A9AF0ACAA46302688E788A8521E2";
let id_hex = "0CDA3F47EF3C4906693B170EF650EB968C5F4B2C";

// decode pubkey and address
let pubkey_bytes = &hex::decode_upper(pubkey_hex).unwrap();
let id_bytes = Id::from_str(id_hex).expect("expected id_hex to decode properly");

// get id for pubkey
let pubkey = ed25519::PublicKey::from_bytes(pubkey_bytes).unwrap();
let id = Id::from(pubkey);

assert_eq!(id_bytes.ct_eq(&id).unwrap_u8(), 1);
}
}
2 changes: 1 addition & 1 deletion tendermint-rs/src/channel/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
pub struct Id(pub u64);

impl Id {
/// Get the current voting power as an integer
/// Get the current channel id as an integer
pub fn value(self) -> u64 {
self.0
}
Expand Down

0 comments on commit f4ddd3b

Please sign in to comment.