Skip to content

Commit

Permalink
impl From<PublicKey> for Address {}
Browse files Browse the repository at this point in the history
  • Loading branch information
seunlanlege authored and fu5ha committed Apr 24, 2018
1 parent c877288 commit 6c7d485
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 27 deletions.
56 changes: 47 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions nano-lib-rs/Cargo.toml
Expand Up @@ -15,6 +15,7 @@ blake2 = "0.7"
ed25519-dalek = { version = "0.6", features = ["nightly"] }
nanopow-rs = { path = "../nanopow-rs" }
data-encoding = "2.1"
data-encoding-macro = "0.1.1"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
Expand Down
74 changes: 57 additions & 17 deletions nano-lib-rs/src/keys.rs
@@ -1,32 +1,72 @@
use nanopow_rs::InputHash;
use super::hash::{Hash, Hasher};

pub use ed25519_dalek::{Keypair, PublicKey, SecretKey, Signature, PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH, SIGNATURE_LENGTH};
use blake2::{
digest::{Input, VariableOutput},
Blake2b
};
use data_encoding::Encoding;
pub use ed25519_dalek::{
Keypair,
PublicKey,
SecretKey,
Signature,
PUBLIC_KEY_LENGTH,
SECRET_KEY_LENGTH,
SIGNATURE_LENGTH
};
use nanopow_rs::InputHash;

impl Hash for PublicKey {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write(self.as_bytes())
}
fn hash<H: Hasher>(&self, state: &mut H) {
state.write(self.as_bytes())
}
}

const XRB_ENCODING: Encoding = new_encoding!{
symbols: "13456789abcdefghijkmnopqrstuwxyz",
};

pub struct Address(pub String);

impl From<PublicKey> for Address {
fn from(_key: PublicKey) -> Self {
unimplemented!();
}
fn from(key: PublicKey) -> Self {
let mut p_key = key.to_bytes().to_vec();
let mut h = [0u8, 4].to_vec();
h.append(&mut p_key);
let checksum = XRB_ENCODING.encode(&compute_address_checksum(key));
let address = {
let encoded_addr = XRB_ENCODING.encode(&h);
let mut addr = String::from("xrb_");
addr.push_str(encoded_addr.get(4..).unwrap());
addr.push_str(&checksum);
addr
};

Address(address)
}
}


/// the address checksum is the 5byte hash of the public key reversed
///
pub fn compute_address_checksum(key: PublicKey) -> [u8; 5] {
let mut blake = Blake2b::new(5).unwrap();
let mut buf = [0u8; 5];
blake.process(key.as_bytes());
blake.variable_result(&mut buf).unwrap();
buf.reverse();
buf
}

pub struct Account {
pub public_key: PublicKey,
pub address: Address,
pub public_key: PublicKey,
pub address: Address,
}

impl From<PublicKey> for Account {
fn from(key: PublicKey) -> Self {
Account {
public_key: key.clone(),
address: key.into(),
}
}
fn from(key: PublicKey) -> Self {
Account {
public_key: key.clone(),
address: key.into(),
}
}
}
1 change: 1 addition & 0 deletions nano-lib-rs/src/lib.rs
Expand Up @@ -22,6 +22,7 @@ extern crate ed25519_dalek;

extern crate bytes;
extern crate data_encoding;
#[macro_use] extern crate data_encoding_macro;

extern crate nanopow_rs;

Expand Down
1 change: 0 additions & 1 deletion src/main.rs
@@ -1,4 +1,3 @@
#![feature(conservative_impl_trait)]
extern crate tokio;
extern crate tokio_io;
extern crate tokio_timer;
Expand Down

0 comments on commit 6c7d485

Please sign in to comment.