moonbeam-foundation / moonbeam

An Ethereum-compatible smart contract parachain on Polkadot
https://moonbeam.network
GNU General Public License v3.0
919 stars 337 forks source link

Signing transactions with ethers/ viem #3033

Closed dudo50 closed 1 week ago

dudo50 commented 2 weeks ago

Hello, is it possible to sign Moonbeam transactions with ethers or viem signers? ph

Because we had no success with this. Is there any function to convert Ethers to Substrate EVM signer? Thanks!

RomarQ commented 2 weeks ago

You can do this for substrate transactions:

import { Keyring } from "@polkadot/api";
import type { KeyringPair } from "@polkadot/keyring/types";

const keyringEth = new Keyring({ type: "ethereum" });

export const signer: KeyringPair = keyringEth.addFromUri("0x...private_key");

tx.signAndSend(signer);
dudo50 commented 2 weeks ago

@RomarQ that would, however, require us to have the private key of the user. We get the ethers signer from the wallet connect component so we do not have their private key. Is there any workaround on how to convert ethers to substrate-compatible signers? Thanks!

RomarQ commented 2 weeks ago

You can use wallets like (Talisman and SubWallet) which offer support for both Substrate and Ethereum ecosystems.

If you want a other solution, you will need to handle the encoding and use a different signing method for substrate transactions, since signMessage does some special things to the data being signed (something like eth_sign(message) => sign(keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))))