diff --git a/bin/subkey/src/main.rs b/bin/subkey/src/main.rs index e6bf091f2a..f9ce19271e 100644 --- a/bin/subkey/src/main.rs +++ b/bin/subkey/src/main.rs @@ -108,6 +108,18 @@ trait Crypto: Sized { format_account_id::(public_key.clone()), public_key.to_ss58check_with_version(v) ); + } else if let Ok(message) = hex::decode(uri) { + let account_id = plasm_account_from_public(&message[..]); + println!( + "Public Key (hex): `{}` is account:\n \ + Plasm AccountId: {}\n \ + Ethereum Address: {:?}\n \ + Plasm Address: {}", + uri, + account_id, + eth_account_from_public(&message), + account_id.to_ss58check_with_version(Ss58AddressFormat::PlasmAccount) + ); } else { println!("Invalid phrase/URI given"); } @@ -583,6 +595,17 @@ fn print_usage(matches: &ArgMatches) { println!("{}", matches.usage()); } +fn plasm_account_from_public(full_public: &[u8]) -> plasm_primitives::AccountId { + use sp_runtime::MultiSigner; + let public = + sp_core::ecdsa::Public::from_full(full_public).expect("Not convert from_full publickey."); + MultiSigner::from(public).into_account() +} + +fn eth_account_from_public(full_public: &[u8]) -> sp_core::H160 { + sp_core::H160::from_slice(&sp_core::hashing::keccak_256(&full_public)[12..32]) +} + #[cfg(test)] mod tests { use super::*; @@ -652,4 +675,17 @@ mod tests { assert_eq!(d1, d2); } + + #[test] + fn should_from_public_work() { + let full_public = hex!["3e909fb1f265942dbbf5e0659ee5c74820c8ecb5a55eecba5752f3cd55293ef98fa8efc553f174ddf3f7e2b5d99ab731da7cff2c1ed7d20579c59e85ab6772dc"]; + assert_eq!( + plasm_account_from_public(&full_public[..]).to_string(), + "5HYaXhCVqDgjkbpUTXJKzc2bFNSwpt7Xm7znS7WaqGNSUEfG" + ); + assert_eq!( + eth_account_from_public(&full_public[..]).as_bytes(), + &hex!["e0e0b97949687e5cdc9ca843c0428bd0437e176d"][..] + ); + } }