Skip to content

Instantly share code, notes, and snippets.

@AlwaysBCoding
Created March 13, 2017 19:35
Show Gist options
  • Save AlwaysBCoding/be7d7c3f2d57bfc472ff89d2686d8e27 to your computer and use it in GitHub Desktop.
Save AlwaysBCoding/be7d7c3f2d57bfc472ff89d2686d8e27 to your computer and use it in GitHub Desktop.
Ethereum Ðapp Development - Video 19 | Implementing an Authentication Scheme
<h1>Example Text</h1>
<script>
var message = "DecypherTV";
var userEthereumClient;
window.addEventListener("load", function() {
userEthereumClient = window.web3;
})
document.querySelector("h1").addEventListener("click", function(event) {
userEthereumClient.eth.sign(userEthereumClient.eth.coinbase, window.web3.sha3(message), function(error, data) {
var signedAuthMessage = data;
document.cookie = "signedAuthMessage=" + signedAuthMessage;
})
})
// Require EthUtil NPM Module
var sigDecoded = EthUtil.fromRPCSig(signedAuthMessage);
var recoveredPub = EthUtil.ecrecover(window.web3.sha3(message), sigDecoded.v, sigDecoded.r, sigDecoded.s);
var recoveredAddress = EthUtil.pubToAddress(recoveredPub).toString("hex");
// Authentication Logic (user is logged in?)
userEthereumClient.eth.coinbase === recoveredAddress;
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment