Created
February 9, 2019 05:21
-
-
Save rodrigosetti/bbcceecc63f0a1e8522c26cd3332b5de to your computer and use it in GitHub Desktop.
Riddles smart contract
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
pragma solidity ^0.5.0; | |
contract Owned { | |
constructor() public { owner = msg.sender; } | |
address payable owner; | |
modifier onlyOwner { | |
require( | |
msg.sender == owner, | |
"Only owner can call this function." | |
); | |
_; | |
} | |
} | |
contract Riddles is Owned { | |
bytes32[] private answerHashes; | |
uint256 public answerIndex = 0; | |
constructor(bytes32[] memory _answerHashes) public { | |
answerHashes = _answerHashes; | |
} | |
function sendAnswer(bytes memory answer) public onlyOwner returns (bool) { | |
require(answerIndex < answerHashes.length); | |
bytes32 answerHash = sha256(answer); | |
if (answerHash == answerHashes[answerIndex]) { | |
// right answer | |
answerIndex++; | |
if (answerIndex == answerHashes.length) { | |
// end of the riddle challenge! | |
selfdestruct(msg.sender); | |
} | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment