We implement Ethereum code on this page, including generating an identity (address, private key and public key). We then sign a message with the private key, and then can extract the address of the sender using message.
Node.js Ethereum |
Theory
With Ethereum we create an identity, with a private key, a public key and an address. We can then create a hash of a message with Keccak. Next we can sign a message with the private key, and then extract the address from the signature. A sample run is:
Message: hello -----Identity------- { address: '0xea88768Ffc2110E343c60F2c376B9B23A93061D7', privateKey: '0x861857e53a49bc48e5ada74cf036ab08ec0b20f9b0277c7f356dc665e7fda759', publicKey: '795aa674eefacdae249299666e05fd2e4dd9bb4dff183425d6c6542482b3f8acd76a4c93cc3c51b41bf1b52f28e59222e7f877c6d92be82e39c46fb6118d50b1' } -----Message Hash (Keccak)------- 0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8 ------Signature of message---- 0xbddb58dd3983ef8e08f7fbb3c83f35f0660ea9d0386cb864625b91b6a15b9dee018bf0a62cf15634cdcafd25da7f0b3140bb5a234a801ab0f69bce8329e66f641c ------Extracting Signer from signature------ 0xea88768Ffc2110E343c60F2c376B9B23A93061D7 ------Transaction------- f86c8085012a05f200825208943fd91467c1dc509ff146c617872f706771cfb80b880de0b6b3a7640000801ca0bd787ee0fc48dacfac0432ec9bd06adc0b8caa0d695c4218b72b21904d90a18da01c02b03158000708832281da95a128677b65e80c09f832766f438136a94e7912
The following is some sample code
const EthCrypto = require('eth-crypto'); const SolidityCli = require('solidity-cli'); const args = process.argv.slice(3); const message= args[0]; console.log("Message:\t",message); const identity = EthCrypto.createIdentity(); console.log("\n-----Identity-------\n",identity); const messageHash = EthCrypto.hash.keccak256(message); console.log("\n-----Message Hash (Keccak)-------\n",messageHash); const signature = EthCrypto.sign( identity.privateKey, // privateKey messageHash // hash of message ); console.log("\n------Signature of message----\n",signature); const signer = EthCrypto.recover( signature, EthCrypto.hash.keccak256(message) // signed message hash ); console.log("\n------Extracting Signer from signature------\n",signer); const rawTransaction = { from: identity.address, to: '0x3fD91467C1dc509Ff146c617872f706771cFB80b', value: 1000000000000000000, nonce: 0, gasPrice: 5000000000, gasLimit: 21000 }; const serializedTx = EthCrypto.signTransaction( rawTransaction, identity.privateKey ); console.log("\n------Transaction-------\n",serializedTx); //const receipt = web3.eth.sendSignedTransaction(serializedTx);