0

Let's say I have an arbitrary contract deployed on the ethereum network. Now I want to build a frontend that interfaces with it using the user's wallet. Obviously I cannot have access to the user's private key and execute the transaction myself (as I would in a testing situation). What is the proper way to prompt the user to sign a transaction that is created from the front-end form info without touching their private keys? (using ethers.js)

2
  • So more specifically, what is the right way to connect to a wallet's JSONRPCPROVIDER, and do all wallets have JSONRPCPROVIDERs connected to their accounts? docs.ethers.io/v5/single-page/#/v5/api/providers/…
    – Vance
    Aug 8 at 20:43
  • Use Web3js or ethers.js to interact with the user wallet, you understand??
    – zmy
    Aug 9 at 3:03
0

You can use ethers (or web3js) and make your life even easier with something like web3modal.

In order to interact with the user's wallet you will need to ask them to connect to your app. For web3modal the documentation is pretty straight forward, but there are examples for native metamask as well. If all goes well, you'll get access the instance of the provider.

Ethers: the library splits that instance into two concepts, a provider and a signer. The provider is the one for reading data, while the signer is the one for sending transactions and doing user related stuff.

Web3js: this mixes the provider with the signer so you'll only have to keep track of one. P.S. web3Modal will actually yield this type of provider by default.

// Example with web3Modal and ethers

const web3Modal = new Web3Modal({
  network: "mainnet",
  cacheProvider: true,
  providerOptions: {} 
});

const web3Provider = await web3Modal.connect();
const ethersProvider = new ethers.providers.Web3Provider(web3Provider)
const ethersSigner = ethersProvider.getSigner()


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.