use crate::{
evm::{TransactionSigned, TransactionUnsigned},
H160,
};
use sp_runtime::AccountId32;
pub struct Account(subxt_signer::eth::Keypair);
impl Default for Account {
fn default() -> Self {
Self(subxt_signer::eth::dev::alith())
}
}
impl From<subxt_signer::eth::Keypair> for Account {
fn from(kp: subxt_signer::eth::Keypair) -> Self {
Self(kp)
}
}
impl Account {
pub fn from_secret_key(secret_key: [u8; 32]) -> Self {
subxt_signer::eth::Keypair::from_secret_key(secret_key).unwrap().into()
}
pub fn address(&self) -> H160 {
H160::from_slice(&self.0.public_key().to_account_id().as_ref())
}
pub fn substrate_account(&self) -> AccountId32 {
let mut account_id = AccountId32::new([0xEE; 32]);
<AccountId32 as AsMut<[u8; 32]>>::as_mut(&mut account_id)[..20]
.copy_from_slice(self.address().as_ref());
account_id
}
pub fn sign_transaction(&self, tx: TransactionUnsigned) -> TransactionSigned {
let payload = tx.unsigned_payload();
let signature = self.0.sign(&payload).0;
tx.with_signature(signature)
}
}
#[test]
fn from_secret_key_works() {
let account = Account::from_secret_key(hex_literal::hex!(
"a872f6cbd25a0e04a08b1e21098017a9e6194d101d75e13111f71410c59cd57f"
));
assert_eq!(
account.address(),
H160::from(hex_literal::hex!("75e480db528101a381ce68544611c169ad7eb342"))
)
}