Guest User

Untitled

a guest
Apr 3rd, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. // Unlock account before the login attempt
  2. web3.personal.unlockAccount(req.body.address, req.body.password, function(error, unlocked) {
  3. console.log(`>>>>> Login - User is unlocked: ${unlocked}`);
  4. if (unlocked) {
  5.  
  6. // Signing/ Encryption
  7. const addr = req.body.address;
  8. const msg = req.body.password;
  9. const hex_msg = '0x' + toHex(msg);
  10. let signature = web3.eth.sign(addr, hex_msg);
  11.  
  12. console.log(`address -----> ${addr}`);
  13. console.log(`msg ---------> ${msg}`);
  14. console.log(`hex(msg) ----> ${hex_msg}`);
  15. console.log(`sig ---------> ${signature}`);
  16.  
  17. const r = signature.slice(0, 66)
  18. const s = '0x' + signature.slice(66, 130)
  19. const v = '0x' + signature.slice(130, 132)
  20. const v_decimal = web3.toDecimal(v);
  21.  
  22. console.log(`r -----------> ${r}`);
  23. console.log(`s -----------> ${s}`);
  24. console.log(`v -----------> ${v}`);
  25. console.log(`vd ----------> ${v_decimal}`);
  26.  
  27. // Validation/Decryption
  28. const fixed_msg = `x19Ethereum Signed Message:n${msg.length}${msg}`
  29. const fixed_msg_sha = '0x' + web3.sha3(fixed_msg)
  30.  
  31. loginContractInstance.isSigned.call(addr, fixed_msg_sha, v_decimal, r,s, function (err, signed) {
  32. console.log(`>>>>> Login - Signature: ${signed}`);
  33.  
  34. if (signed) {
  35. // Saving login attempt
  36. loginContractInstance.successfulLogin.sendTransaction(req.body.address, req.body.password,
  37. {from:'6ded1c5b448819a6cde4293e33fbe54583ef5c52', gas:200000},
  38. function(err, transactionHash) {
  39. if (!err) {
  40. console.log(`>>>>> Login - login details (${addr}, ${msg}) successfully saved @ ${transactionHash}`);
  41. console.log(`>>>>> Login - Successfully login`);
  42.  
  43. } else {
  44. console.log(`>>>>> Login - login transaction failed: ${err}`);
  45. }
  46.  
  47. // set primary account to mine
  48. web3.eth.coinbase = req.body.address;
  49.  
  50. console.log(`>>>>> Login - Current User: ${web3.eth.coinbase} is logged in`);
  51.  
  52. var balanceWei = web3.eth.getBalance(req.body.address).toNumber();
  53. var balance = web3.fromWei(balanceWei, 'ether');
  54. res.json({
  55. allowedLogin: true,
  56. address: req.body.address,
  57. balance: balance,
  58. token: generateToken(addr)
  59. });
  60.  
  61. });
  62. } else {
  63. loginContractInstance.recoverAddr.call(addr, fixed_msg_sha, v_decimal, r,s, function(err, actual) {
  64. console.log(`>>>>> Login - Failed login: Signature not matched:${addr} = ${actual}`);
  65. saveFailedLoginAttempts(loginContractInstance, req, res);
  66. });
  67. }
  68. });
  69. } else {
  70. console.log(`>>>>> Login - Failed login: User is locked`);
  71. saveFailedLoginAttempts(loginContractInstance, req, res);
  72. }
  73.  
  74. });
  75.  
  76. contract Login {
  77.  
  78. event LoginAttempt(address sender, string password);
  79.  
  80. address private sender;
  81. string private password;
  82.  
  83. function successfulLogin (address _sender, string _password) public {
  84. LoginAttempt(_sender, _password);
  85. sender = _sender;
  86. password = _password;
  87. }
  88.  
  89. function failedLogin (address _sender, string _password) public {
  90. LoginAttempt(_sender, _password);
  91. }
  92.  
  93. function recoverAddr(bytes32 msgHash, uint8 v, bytes32 r, bytes32 s) public pure returns (address) {
  94. return ecrecover(msgHash, v, r, s);
  95. }
  96.  
  97. function isSigned(address _addr, bytes32 msgHash, uint8 v, bytes32 r, bytes32 s) public pure returns (bool) {
  98. return ecrecover(msgHash, v, r, s) == _addr;
  99. }
Add Comment
Please, Sign In to add comment