Is it possible to sign a user in web3 without MetaMask? I am trying to execute a smart contract I created and I'm using Infura to connect to the Kovan test network, but web3.eth.accounts[0] is undefined. How can I sign a user in without having to use MetaMask? Is this even possible? If not, how should I approach this problem? Please and thank you!
[Ethereum] How to login with web3 without MetaMask
infurametamaskweb3js
Best Answer
What you should do in this case is use the web3-provider-engine. An example code snippet functioning with a custom private key and Infura API URL (Or even TestRPC) would be the following:
You will be able to sign any transactions you want with your supplied private key by specifying your public key as the "from" value in the settings object of each perspective web3 function call.
EDIT: I personally despise the web3 library and as a result, I decided to revamp the snippet to use the ethers.js library which has a more human-readable API:
That's basically it. You can now send transactions with it or construct Contract objects to interact with contracts with it. Refer to the ethers.js documentation for more.