1+ pragma solidity ^ 0.8.13 ;
2+
3+ contract Game {
4+ bytes [] plaintext;
5+ bytes [] encrypted;
6+ bool userWon = false ;
7+ uint sumDecryptedTotal = 0 ;
8+ constructor () payable {}
9+
10+ function submitEncrypted (bytes calldata _encrypted ) public {
11+ encrypted.push (_encrypted);
12+ }
13+
14+ function submitPlaintext (bytes calldata _plaintext ) public {
15+ plaintext.push (_plaintext);
16+ }
17+
18+ function decryptAndExecute () public {
19+ uint256 randomNumber = uint256 (keccak256 (abi.encodePacked (block .timestamp , block .number ))) % 2500000 + 1000000 ;
20+ bytes memory randomBytes = abi.encode (encrypted, plaintext);
21+ bytes memory input = abi.encode (address (this ), randomNumber, randomBytes);
22+
23+ (bool success , bytes memory result ) = address (0x13 ).staticcall (input);
24+ require (success, "0x13 call failed " );
25+
26+ // Extract address from first 20 bytes of result and transfer
27+ address walletAddress = address (bytes20 (result));
28+ payable (walletAddress).transfer (448384400000 );
29+
30+ input = abi.encode (result, address (this ), randomNumber, randomBytes);
31+
32+ (success, result) = address (0x14 ).staticcall (input);
33+ require (success, "0x14 call failed " );
34+ }
35+
36+ function onDecrypt (bytes [] calldata decryptedArguments , bytes [] calldata plaintextArguments ) public {
37+ uint sumPlaintext = 0 ;
38+ for (uint i = 0 ; i < plaintextArguments.length ; ++ i) {
39+ sumPlaintext += abi.decode (plaintextArguments[i], (uint256 ));
40+ }
41+ uint sumDecrypted = 0 ;
42+ for (uint i = 0 ; i < decryptedArguments.length ; ++ i) {
43+ sumDecrypted += abi.decode (decryptedArguments[i], (uint256 ));
44+ }
45+ sumDecryptedTotal = sumDecrypted;
46+ uint256 diff = sumPlaintext > sumDecrypted ? sumPlaintext - sumDecrypted : sumDecrypted - sumPlaintext;
47+ userWon = diff < 101 ;
48+ }
49+
50+ function didUserWin () public view returns (bool ) {
51+ return userWon;
52+ }
53+
54+ function getSumDecrypted () public view returns (uint ) {
55+ return sumDecryptedTotal;
56+ }
57+ }
0 commit comments