pallet_revive/evm/api/
account.rs1use crate::{
19 evm::{TransactionSigned, TransactionUnsigned},
20 H160,
21};
22use sp_runtime::AccountId32;
23
24pub struct Account(subxt_signer::eth::Keypair);
26
27impl Default for Account {
28 fn default() -> Self {
29 Self(subxt_signer::eth::dev::alith())
30 }
31}
32
33impl From<subxt_signer::eth::Keypair> for Account {
34 fn from(kp: subxt_signer::eth::Keypair) -> Self {
35 Self(kp)
36 }
37}
38
39impl Account {
40 pub fn from_secret_key(secret_key: [u8; 32]) -> Self {
42 subxt_signer::eth::Keypair::from_secret_key(secret_key).unwrap().into()
43 }
44
45 pub fn address(&self) -> H160 {
47 H160::from_slice(&self.0.public_key().to_account_id().as_ref())
48 }
49
50 pub fn substrate_account(&self) -> AccountId32 {
52 let mut account_id = AccountId32::new([0xEE; 32]);
53 <AccountId32 as AsMut<[u8; 32]>>::as_mut(&mut account_id)[..20]
54 .copy_from_slice(self.address().as_ref());
55 account_id
56 }
57
58 pub fn sign_transaction(&self, tx: TransactionUnsigned) -> TransactionSigned {
60 let payload = tx.unsigned_payload();
61 let signature = self.0.sign(&payload).0;
62 tx.with_signature(signature)
63 }
64}
65
66#[test]
67fn from_secret_key_works() {
68 let account = Account::from_secret_key(hex_literal::hex!(
69 "a872f6cbd25a0e04a08b1e21098017a9e6194d101d75e13111f71410c59cd57f"
70 ));
71
72 assert_eq!(
73 account.address(),
74 H160::from(hex_literal::hex!("75e480db528101a381ce68544611c169ad7eb342"))
75 )
76}