Intro: I am trying to create a web app where I can send some tokens from my personal account to another. I found out a possible way to achieve my goal using ethers.js in the following way:
// connect to metamask let provider = new ethers.providers.Web3Provider(window.ethereum) // define who signs the contract const signer = new ethers.Wallet(your_private_key_string, provider); // token infos const tokenAddress = "0xEwgwegwdsB87f8Ebd86wegwegvweg" const tokenAbi = [ // Some details about the token "function transfer(address to, uint amount)" ]; // define contract const tokenContract = new ethers.Contract(piaAddress, piaAbi, provider) // send tokens from my wallet to someone else's. tokenContract.connect(signer).transfer("receiverAddress", "500000000")
Problem: As you can see from the code, at one point I need to provide your_private_key_string. This would be fine if no one had access to my web app, but since I should be hosted in the internet, it would be exposed to all users thus making it vulnerable.
Question: Would you be able to suggest a smart and elegant way to perform the same task but avoiding to share my private key?
Note: the web-app is developed with Django in case it might help.