[Ethereum] How to sign data locally without access to Ethereum node with web3.eth.sign, web3.eth.personal.sign functions

web3js

When I use web3.eth.sign, web3.eth.personal.sign functions to sign data I should pass to them account address.

eth.sign(dataToSign, address [, callback]) 
eth.personal.sign(dataToSign, address, password [, callback]) 

My understanding is that these functions are usually provided with address by connecting to Ethereum node. Then my understanding is that these functions communicate with node to retrieve respective for the address private key.

If I run these functions on a client that does not connected to a node, how these functions can be used to sign data? I guess they would get private key from keystore file or passphrase, or metamask that are available locally. How the retrieval of private key from these local sources is done, passed to web3.eth.sign, web3.eth.personal.sign functions?
An example, would be very much appreciated.

Best Answer

The private key is never passed from metamask to the application. Instead, your account on metamask is prompted to accept signing a transaction and the signature is then passed back to the application.

As for signing without connecting to a node or metamask, this can be done with ethereumjs-tx by signing a transaction object with a private key in web3js 0.20.xx or by using web3.eth.accounts.sign(data, privateKey); in web3 1.0.

Related Topic