Created
September 22, 2018 05:29
-
-
Save geggleto/2bedd7bd8b61cb785f7918c567b0063c to your computer and use it in GitHub Desktop.
Express Service to verify Metamask Message and recover Ethereum Account used to sign
This file contains 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
/** | |
Locally / Non-SSL: node verify-address.js local | |
**/ | |
const LOCAL = (process.argv[2] === "local"); | |
const fs = require('fs'); | |
const express = require('express'); | |
const app = express(); | |
const bodyParser = require('body-parser'); | |
const http = require('http'); | |
const https = require('https'); | |
const Web3 = require('Web3'); | |
let httpServer; | |
let web3 = new Web3(new Web3.providers.HttpProvider('https://****/')); | |
app.use(bodyParser.json()); | |
app.get('/', function (req, res) { | |
res.send('hello'); | |
}); | |
/** | |
Method | |
web3.personal.sign(message, signer, (err, signature) => { | |
axios.post('https://.../auth', { | |
address: signer, | |
signature : signature, | |
message : message, | |
original_message : original_message | |
}).then(response => { | |
**/ | |
app.all('/verify', function (req, res) { | |
let signatureHex = req.body.signature; | |
let address = req.body.address; | |
let original_message = req.body.original_message; | |
let recoveredAddress = web3.eth.accounts.recover(original_message, signatureHex); | |
if (recoveredAddress.toUpperCase() === address.toUpperCase()) { //verified | |
res.send(JSON.stringify({ | |
verified: true | |
})); | |
} else { //failed | |
res.send(JSON.stringify({ | |
verified: false | |
})); | |
} | |
}); | |
if (LOCAL === false) { | |
const credentials = { | |
key: fs.readFileSync('/etc/letsencrypt/live/***/privkey.pem'), | |
cert: fs.readFileSync('/etc/letsencrypt/live/***/fullchain.pem') | |
}; | |
httpServer = https.createServer(credentials, app); | |
} else { | |
httpServer = http.createServer(app); | |
} | |
console.log("Listening on port 3100"); | |
httpServer.listen(3100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use this
const Web3 = require('web3');
instead of
const Web3 = require('Web3');