1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use crate::transaction::authenticator::AuthenticationKey;
use diem_crypto::{
ed25519::Ed25519PublicKey,
hash::{CryptoHasher, HashValue},
x25519,
};
pub use move_core_types::account_address::AccountAddress;
pub fn from_public_key(public_key: &Ed25519PublicKey) -> AccountAddress {
AuthenticationKey::ed25519(public_key).derived_address()
}
pub fn from_identity_public_key(identity_public_key: x25519::PublicKey) -> AccountAddress {
let mut array = [0u8; AccountAddress::LENGTH];
let pubkey_slice = identity_public_key.as_slice();
array.copy_from_slice(&pubkey_slice[x25519::PUBLIC_KEY_SIZE - AccountAddress::LENGTH..]);
AccountAddress::new(array)
}
mod hasher {
#[derive(serde::Deserialize, diem_crypto_derive::CryptoHasher)]
struct AccountAddress;
}
pub trait HashAccountAddress {
fn hash(&self) -> HashValue;
}
impl HashAccountAddress for AccountAddress {
fn hash(&self) -> HashValue {
let mut state = hasher::AccountAddressHasher::default();
state.update(self.as_ref());
state.finish()
}
}
#[cfg(test)]
mod test {
use super::{AccountAddress, HashAccountAddress};
use diem_crypto::hash::HashValue;
use hex::FromHex;
#[test]
fn address_hash() {
let address: AccountAddress = "ca843279e3427144cead5e4d5999a3d0".parse().unwrap();
let hash_vec =
&Vec::from_hex("6403c4906e79cf4536edada922040805c6a8d0e735fa4516a9cc40038bd125c8")
.expect("You must provide a valid Hex format");
let mut hash = [0u8; 32];
let bytes = &hash_vec[..32];
hash.copy_from_slice(bytes);
assert_eq!(address.hash(), HashValue::new(hash));
}
}