Last active
March 17, 2019 22:55
-
-
Save ethicnology/7a0bc006f5c620c6dcde2835b9c710bd to your computer and use it in GitHub Desktop.
Hijackable smartContract : Let your classmates find the trick
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.3; | |
contract blackHijack{ | |
address hijack = address(this); | |
mapping(address => uint) public players; | |
mapping(address => uint) public cards; | |
mapping(address => bool) public twerk; | |
uint8 players_number = 0; | |
bool start_game; | |
bool dealer_draw; | |
uint dealer; | |
/**************************************************************************** | |
* @dev fundTheContract is needed to put around 60 Ether in the contract. * | |
* If the contract has no Ether, nobody can play. * | |
**************************************************************************** | |
* | |
* _________ | |
* / ======= \ | |
* / __________\ | |
* | ___________ | | |
* | | - | | | |
* | | | | | |
* | |_________| | | |
* \=____________/ | |
* / """"""""""" \ | |
* / ::::::::::::: \ | |
* (_________________) | |
* | |
* | |
*/ | |
function fundTheContract() public payable{ | |
} | |
/************************************************************************************* | |
* @dev getContractStatement to know the variable statement of the contract * | |
* ->player Here we define the struct returned : * | |
* 0 => Number of player registered. * | |
* 1 => Is here any game started by a registered player ? * | |
* 2 => Does someone faceTheDealer ? * | |
* 3 => The dealer hand, what he got with what he draw. * | |
* 4 => Current registered player address who call this function. * | |
* 5 => His hand. * | |
* 6 => Does he used the doubleTwerk function ? * | |
* 7 => His bet. * | |
* 8 => what the smartContract hodL. * | |
* * | |
************************************************************************************* | |
*/ | |
function getContractStatement() public view returns(uint8, bool, bool, uint, address, uint, bool, uint, uint256){ | |
require(players[msg.sender] != 0); | |
return(players_number, start_game, dealer_draw, dealer, msg.sender, cards[msg.sender], twerk[msg.sender], players[msg.sender], hijack.balance); | |
} | |
/************************************************************************************* | |
* @dev payToPlay to register a player with his bet, you can't play below 1 Ether. * | |
* @dev Casino need to put 60 Ether in the contract if you want to sit at the table. * | |
* ->player Bet more than 1 Ether (max 5), we love your coins. * | |
************************************************************************************* | |
* | |
* *********** | |
* ***** *********** | |
* ** ****** *** ******** | |
* **** ****** ** ******* | |
* *** ******* ** ****** | |
* *** ** * ** | |
* *|/------ -------\ ** * | |
* | |=| :===** | |
* | O | | O | }|* | |
* |---- | ---- | |* | |
* | |___ |\/ | |
* | | | |
* \ ----- | | |
* \ | | |
* -__ -- -/ | |
* | |
*/ | |
function payToPlay() public payable{ | |
require(hijack.balance >= 60 ether); | |
require(!start_game); | |
require(players_number <= 6); | |
require(msg.value >= 1 ether); | |
require(msg.value <= 5 ether); | |
players[msg.sender] = msg.value; | |
cards[msg.sender] = 0; | |
players_number++; | |
} | |
/**************************************** | |
* @dev drawACard to pick a card. * | |
* ->player Try your luck. * | |
**************************************** | |
* | |
* .------. | |
* .------. |A . | | |
* |A_ _ | .------; / \ | | |
* |( \/ )|-----. _ |(_,_) | | |
* | \ / | /\ |( ) | I A| | |
* | \/ A|/ \ |_x_) |------' | |
* `-----+'\ / | Y A| | |
* | \/ A|-----' | |
* `------' | |
* | |
* | |
*/ | |
function drawACard() public returns(uint){ | |
require(players[msg.sender] != 0); | |
require(!twerk[msg.sender]); | |
start_game = true; | |
cards[msg.sender] += random(); | |
return cards[msg.sender]; | |
} | |
/*************************************************************************** | |
* @dev doubleTwerk double your bet and draw only one more card. * | |
* ->player if you use doubleTwerk you can only faceTheDealer after. * | |
*************************************************************************** | |
* _.._ | |
* .' '. | |
* (____/`\ \ | |
* ( |' ' ) ) | |
* ) _\= _/ ( | |
* __..---.(`_.' ` \ ) | |
* `;-""-._(_( . `; ( | |
* / `-`'--' ; ) | |
* / / . ( . ,| |( | |
* _.-`'---...__,' /-,..___.-'--'_| |_) | |
* '-'``'-.._ ,' | / .........' | |
* ``;--"`; | `-` | |
* `'..__.' | |
* | |
*/ | |
function doubleTwerk() public payable{ | |
require(cards[msg.sender] != 0); | |
require(start_game); | |
require(msg.value == players[msg.sender]); | |
require(!twerk[msg.sender]); | |
players[msg.sender] += msg.value; | |
drawACard(); | |
twerk[msg.sender] = true; | |
} | |
/************************************************************************************ | |
* @dev faceTheDealer makes the dealer draw cards depending on blackHijack rules. * | |
* ->player If you face the dealer you can't draw any card. * | |
************************************************************************************ | |
* | |
* | |
* _..-''--'----_. | |
* ,''.-''| .---/ _`-._ | |
* ,' \ \ ;| | ,/ / `-._`-. | |
* ,' ,',\ \( | |// /,-._ / / | |
* ;.`. `,\ \`| |/ / | )/ / | |
* / /`_`.\_\ \| /_.-.'-''/ / | |
* / /_|_:.`. \ |;'`..') / / | |
* `-._`-._`.`.;`.\ ,' / / | |
* `-._`.`/ ,'-._/ / | |
* : `-/ \`-.._/ | |
* | : ;._ ( | |
* : | \ ` \ | |
* \ \ | | |
* : : ; | |
* | / | |
* ; ,' | |
* / / | |
* / / | |
* / | |
*/ | |
function faceTheDealer() public returns(uint){ | |
require(start_game); | |
require(cards[msg.sender] != 0); | |
if(!dealer_draw){ | |
dealer = 0; | |
} | |
dealer_draw = true; | |
while(dealer < 17){ | |
dealer += random(); | |
} | |
return dealer; | |
} | |
/************************************************************ | |
* @dev random generate a random number between 1 and 10. * | |
* ->player Trust us. * | |
************************************************************ | |
* ____ ____ ____ ___ ___ ___ ___ | |
* | \ / || \ | \ / \ | | | | |
* | D )| o || _ || \ | || _ _ | | |
* | / | || | || D || O || \_/ | | |
* | \ | _ || | || || || | | | |
* | . \| | || | || || || | | | |
* |__|\_||__|__||__|__||_____| \___/ |___|___| | |
* | |
*/ | |
function random() private view returns(uint) { | |
return uint(blockhash(block.number-1))%10 + 1; | |
} | |
/************************************************************ | |
* @dev rewardTheNoob reward all the winners. * | |
* ->player Contract always win. * | |
************************************************************ | |
* | |
* █ | |
* ███ | |
* ▄██████ | |
* █████████ | |
* ███████████▄ | |
* ▄██████████████ | |
* █████████████████ | |
* ███████████████████▄ | |
* ██████████████████████ | |
* █████████████████████████ | |
* ███████████████████████████▄ | |
* ██████████████████████████████ | |
* █████████████████████████████████ | |
* ███████████████████████████████████ | |
* █████████████████████████████████████ | |
* ███████████████████████████████▀ | |
* ▀████████████████████████ | |
* █████████████████ | |
* ███▄ █████████▀ ▄██▀ | |
* ████▄ ▀██ ▄█████ | |
* ███████ ▄▄██████ | |
* █████████▄ ▄█████████ | |
* ▀███████████████████ | |
* █████████████████ | |
* ▐█████████████ | |
* ██████████▀ | |
* ███████ | |
* ████▀ | |
* █ | |
* | |
* | |
* | |
*/ | |
function rewardTheNoob() public returns(uint8){ | |
require(start_game); | |
require(dealer_draw); | |
require(cards[msg.sender] != 0); | |
if(players_number == 1){ | |
dealer_draw = false; | |
start_game = false; | |
} | |
if(cards[msg.sender] > 21){ | |
killPlayer(msg.sender); | |
return 2; | |
} | |
if(dealer > 21){ | |
msg.sender.transfer(players[msg.sender] * 2); | |
killPlayer(msg.sender); | |
return 1; | |
} | |
if(cards[msg.sender] > dealer){ | |
msg.sender.transfer(players[msg.sender] * 2); | |
killPlayer(msg.sender); | |
return 1; | |
}else if(cards[msg.sender] == dealer){ | |
msg.sender.transfer(players[msg.sender]); | |
killPlayer(msg.sender); | |
return 0; | |
}else{ | |
killPlayer(msg.sender); | |
return 2; | |
} | |
} | |
/**************************************************** | |
* @dev killPlayer delete a player from mappings. * | |
* ->player Sorry for your loss. * | |
**************************************************** | |
* | |
* _,.-------.,_ | |
* ,;~' '~;, | |
* ,; ;, | |
* ; ; | |
* ,' ', | |
* ,; ;, | |
* ; ; . . ; ; | |
* | ; ______ ______ ; | | |
* | `/~" ~" . "~ "~\' | | |
* | ~ ,-~~~^~, | ,~^~~~-, ~ | | |
* | | }:{ | | | |
* | l / | \ ! | | |
* .~ (__,.--" .^. "--.,__) ~. | |
* | ---;' / | \ `;--- | | |
* \__. \/^\/ .__/ | |
* V| \ / |V | |
* | |T~\___!___!___/~T| | | |
* | |`IIII_I_I_I_IIII'| | | |
* | \,III I I I III,/ | | |
* \ `~~~~~~~~~~' / | |
* \ . . / | |
* \. ^ ./ | |
* ^~~~^~~~^ | |
* | |
*/ | |
function killPlayer(address _player) private{ | |
delete(cards[_player]); | |
delete(players[_player]); | |
delete(twerk[_player]); | |
players_number--; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment