Skip to content
Snippets Groups Projects
Commit 770b7e85 authored by qh11's avatar qh11
Browse files

Update address.rs

parent 167404cb
No related branches found
No related tags found
No related merge requests found
extern crate data_encoding;
extern crate ring;
use serde::{Serialize, Deserialize};
use ring::digest::{Context, SHA256};
use data_encoding::HEXLOWER;
// 20-byte address
#[derive(Eq, PartialEq, Serialize, Deserialize, Clone, Hash, Default, Copy)]
pub struct Address([u8; 20]);
......@@ -48,7 +55,13 @@ impl std::fmt::Debug for Address {
impl Address {
pub fn from_public_key_bytes(bytes: &[u8]) -> Address {
unimplemented!()
let mut context = Context::new(&SHA256);
context.update(&bytes[..]);
let result = context.finish();
let mut a:[u8; 20] = [0u8;20];
a.copy_from_slice(&(result.as_ref()[12..32]));
Address(a)
//unimplemented!()
}
}
// DO NOT CHANGE THIS COMMENT, IT IS FOR AUTOGRADER. BEFORE TEST
......@@ -62,6 +75,8 @@ mod test {
let test_key = hex!("0a0b0c0d0e0f0e0d0a0b0c0d0e0f0e0d0a0b0c0d0e0f0e0d0a0b0c0d0e0f0e0d");
let addr = Address::from_public_key_bytes(&test_key);
let correct_addr: Address = hex!("1851a0eae0060a132cf0f64a0ffaea248de6cba0").into();
println!("addr:{}",addr);
println!("correct_addr:{}",correct_addr);
assert_eq!(addr, correct_addr);
// "b69566be6e1720872f73651d1851a0eae0060a132cf0f64a0ffaea248de6cba0" is the hash of
// "0a0b0c0d0e0f0e0d0a0b0c0d0e0f0e0d0a0b0c0d0e0f0e0d0a0b0c0d0e0f0e0d"
......@@ -69,4 +84,4 @@ mod test {
}
}
// DO NOT CHANGE THIS COMMENT, IT IS FOR AUTOGRADER. AFTER TEST
\ No newline at end of file
// DO NOT CHANGE THIS COMMENT, IT IS FOR AUTOGRADER. AFTER TEST
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment