Last active
October 17, 2018 16:47
-
-
Save merlox/f9f1db7dce721ef3f30560875298c853 to your computer and use it in GitHub Desktop.
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
function resetGame(game) { | |
return { | |
addressPlayer1: game.addressPlayer1, | |
addressPlayer2: game.addressPlayer2, | |
balancePlayer1: game.balancePlayer1, | |
balancePlayer2: game.balancePlayer2, | |
socketPlayer1: game.socketPlayer1, | |
socketPlayer2: game.socketPlayer2 | |
} | |
} | |
// Checks that the message given by the player is valid to continue playing and to reveal the results | |
function verifyMessage(signedMessage, nonce, call, bet, balance, sequence, playerAddress) { | |
const hash = generateHash(nonce, call, bet, balance, sequence) | |
const message = ethereumjs.soliditySHA3( | |
['string', 'bytes32'], | |
['\x19Ethereum Signed Message:\n32', hash] | |
) | |
const splitSignature = ethereumjsUtil.fromRpcSig(signedMessage) | |
const publicKey = ethereumjsUtil.ecrecover(message, splitSignature.v, splitSignature.r, splitSignature.s) | |
const signer = ethereumjsUtil.pubToAddress(publicKey).toString('hex') | |
const isMessageValid = (signer.toLowerCase() == ethereumjsUtil.stripHexPrefix(playerAddress).toLowerCase()) | |
return isMessageValid | |
} | |
function generateHash(nonce, call, bet, balance, sequence) { | |
const hash = '0x' + ethereumjs.soliditySHA3( | |
['uint256', 'uint256', 'uint256', 'uint256', 'uint256'], | |
[String(nonce), String(call), String(bet), String(balance), String(sequence)] | |
).toString('hex') | |
return hash | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment