Looks like Ethereum is supported in @polkadot/keyring
:
const PairFromSeed = {
ecdsa: (seed: Uint8Array): Keypair => secp256k1FromSeed(seed),
ed25519: (seed: Uint8Array): Keypair => ed25519FromSeed(seed),
ethereum: (seed: Uint8Array): Keypair => secp256k1FromSeed(seed),
sr25519: (seed: Uint8Array): Keypair => sr25519FromSeed(seed)
};
So you could do something like this:
// Import the keyring as required
import Keyring from '@polkadot/keyring';
// Create a keyring instance
const keyring = new Keyring({ type: 'ethereum' });
Moonbeam has excellent docs on this. As well as an example script:
import { ApiPromise, WsProvider } from '@polkadot/api';
import { Keyring } from '@polkadot/api';
const wsProvider = new WsProvider('WSS-API-ENDPOINT-HERE');
const api = await ApiPromise.create({ provider: wsProvider });
const keyring = new Keyring({ type: 'ethereum' });
const alice = keyring.addFromUri('ALICE-ACCOUNT-PRIVATE-KEY-HERE');
const bob = 'BOB-ACCOUNT-PUBLIC-KEY-HERE';
const tx = await api.tx.balances
.transfer(bob, 12345n)
const encodedCallData = tx.method.toHex()
console.log(encodedCallData)
const txHash = await tx
.signAndSend(alice);
console.log(`Submitted with hash ${txHash}`);
// Disconnect the API
api.disconnect();