Battle 404 Randomizer

by RabbiGains

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.2.0/crypto-js.min.js"></script>

JavaScript

// edit blockObject with the proper hash 
// for the block you would like to verify the winner for.
// you can get the blockhash from
// https://mempool.space/api/v1/blocks/820000

const blockObject = [
	{ 
		block: 882000, 
		hash: "000000000000000000023eced16719b1b3e187f882371515630e9ff6cee6a3ee" //update this
    
	}
];
const winners = calculateWinners(blockObject);
        document.body.textContent = JSON.stringify(winners, null, 2);
        

function getRandomNumber(hash) {
  const seed = CryptoJS.SHA256(hash).toString()
  const decimal = parseInt(seed.slice(0, 8), 16) / 0xffffffff
  return Math.floor(decimal * 404) + 1
}

function calculateWinners(blocks) {
  return blocks.map((block) => ({
    block: block.block,
    winner: getRandomNumber(block.hash),
  }))
}