Created
September 1, 2017 03:59
-
-
Save vincentserpoul/25b728b6b3d4be6548a2fb1b7e5dca3d to your computer and use it in GitHub Desktop.
log in with ethereum/metamask
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from "react"; | |
| import Web3 from "web3"; | |
| import ethUtil from "ethereumjs-util"; | |
| import "./index.css"; | |
| const Login = () => { | |
| return ( | |
| <div id="login" onClick={handleClick}> | |
| LOGIN | |
| </div> | |
| ); | |
| }; | |
| let web3c; | |
| if (typeof web3 !== "undefined") { | |
| web3c = new Web3(web3.currentProvider); // eslint-disable-line no-undef | |
| } else { | |
| // set the provider you want from Web3.providers | |
| web3c = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); | |
| } | |
| const handleClick = () => { | |
| let data = toHex("i am a string"); | |
| web3c.currentProvider.sendAsync( | |
| { id: 1, method: "personal_sign", params: [web3c.eth.accounts[0], data] }, | |
| (err, result) => { | |
| console.log(result); | |
| const sig = result.result; | |
| const sigParams1 = ethUtil.fromRpcSig(sig); | |
| console.log(sigParams1.r, sigParams1.v, sigParams1.s); | |
| // Same data as before | |
| const data = "i am a string"; | |
| const message = ethUtil.toBuffer(data); | |
| const msgHash = ethUtil.hashPersonalMessage(message); | |
| console.log("hash", msgHash.toString("hex")); | |
| // Get the address of whoever signed this message | |
| const signature = ethUtil.toBuffer(sig); | |
| const sigParams = ethUtil.fromRpcSig(signature); | |
| const publicKey = ethUtil.ecrecover( | |
| msgHash, | |
| sigParams.v, | |
| sigParams.r, | |
| sigParams.s | |
| ); | |
| const sender = ethUtil.publicToAddress(publicKey); | |
| const addr = ethUtil.bufferToHex(sender); | |
| console.log(addr.toString("hex")); | |
| } | |
| ); | |
| }; | |
| export default Login; | |
| const toHex = s => { | |
| let hex = ""; | |
| for (let i = 0; i < s.length; i++) { | |
| hex += "" + s.charCodeAt(i).toString(16); | |
| } | |
| return `0x${hex}`; | |
| }; | |
| // strong blind place risk opera peasant hidden supply primary boat injury card | |
| // i am a string | |
| // 0x0e67ab4d6B455539D02add5dfC749c591e476298 | |
| // 0x361ad676dc3424892dcef0f7f45d277116628575559e9ebdd647df9ed27055b431bff9e46c626d543728279cd648e2898eebc7a955b6115e5f02b1fb06b829201b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment