Skip to content

Instantly share code, notes, and snippets.

View bertolo1988's full-sized avatar
🎯
Focusing

Tiago Bértolo bertolo1988

🎯
Focusing
View GitHub Profile
function isPrime(num) {
for (let i = 2, s = Math.sqrt(num); i <= s; i++)
if (num % i === 0) return false;
return num > 1;
}
function getMapOfPrimeNumbersUpTo(n) {
const result = new Map();
for (let i = 0; i < n; i++) {
if (isPrime(i)) result.set(i, true);
function deleteFbComments() {
let postDeleted = 0;
const CLICK_DELAY = 50;
function randomIntFromInterval(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function sleep(ms = 200) {
return new Promise((resolve) => setTimeout(resolve, ms));
function memoize(func) {
var memo = {};
var slice = Array.prototype.slice;
return function () {
var args = slice.call(arguments);
if (args in memo) return memo[args];
else return (memo[args] = func.apply(this, args));
};
}
@bertolo1988
bertolo1988 / ProvableBetFunctionCallback.solidity
Created September 6, 2019 00:47
Callback called by Provable.
function __callback( bytes32 _queryId, string memory _result, bytes memory _proof ) public {
require(msg.sender == provable_cbAddress());
require(ongoingBets[_queryId].sender != address(0x0) , 'query does not exist');
if(provable_randomDS_proofVerify__returnCode( _queryId, _result, _proof) != 0){
revert();
} else {
uint ceiling = (MAX_INT_FROM_BYTE ** NUM_RANDOM_BYTES_REQUESTED) - 1;
uint randomNumber = uint(keccak256(abi.encodePacked(_result))) % ceiling;
uint spin = (randomNumber % 100) + 1;
Bet memory bet = ongoingBets[_queryId];
@bertolo1988
bertolo1988 / ProvableBetFunction.solidity
Created September 6, 2019 00:40
The bet function using ProvableAPI 0.5.
function bet(uint stake, uint spinUnder)external notOwner returns (uint spinUnderInput, uint stakeInput, uint prize, uint payout, uint previousBalance){
require(stake >= minBet && maxBet >= stake , 'stake is outside allowed limits');
require(spinUnder >= MIN_ROLL_UNDER && spinUnder <= MAX_ROLL_UNDER, 'spinUnder must be between or equal to 11 and 91');
require(balances[msg.sender] >= stake, 'insufficient balance to cover stake');
(payout, prize) = getPrizeAndPayout(stake, spinUnder);
require(balances[owner] >= (prize.sub(stake)), 'insufficient contract balance to cover the prize');
previousBalance = balances[msg.sender];
subtractAmountFromUser(stake);
uint QUERY_EXECUTION_DELAY = 0;
uint GAS_FOR_CALLBACK = 200000;
@bertolo1988
bertolo1988 / Cheat.solidity
Last active September 6, 2019 00:09
To attack pseudo randoms.
function cheat(uint nonce, uint256 amount) external returns (uint stakeInput, uint spinUnderInput, uint spin, uint payout, uint prize, uint previousBalance, uint newBalance) {
uint randomnumber = uint(keccak256(abi.encodePacked(now, address(this), nonce))) % 100;
require(randomnumber >= 11, "rolled number too low");
require(randomnumber <= 89, "rolled number too high");
return instance.bet(amount, (randomnumber + 2));
}
@bertolo1988
bertolo1988 / Ownable.sol
Last active September 5, 2019 23:57
Wheel of fortune using vulnerable pseudo random generator.
/* @author https://github.com/bertolo1988 */
pragma solidity ^0.5.10;
contract Ownable {
address public owner;
constructor() public {
owner = msg.sender;
}
/* @author https://github.com/bertolo1988 */
pragma solidity ^0.5.10;
import './provableAPI_0.5.sol';
import './Ownable.sol';
import './SafeMath.sol';
contract Gamble is Ownable, usingProvable {
using SafeMath for uint;
@bertolo1988
bertolo1988 / connect_linkedin.js
Last active January 1, 2024 20:23
Quickly your LinkedIn network
// 1. load https://www.linkedin.com/mynetwork/
// 2. make sure your LinkedIn is in English
// 3. paste this script on chrome dev tools at your own risk
async function moreConnectionsPlease() {
// maximum limit of Connect buttons clicked
const LIMIT = 500;
// wait in ms before each scroll
const SCROLL_TIMEOUT = 600;
// bulk scroll will scroll this amount of times
@bertolo1988
bertolo1988 / aws-s3-upload-pic-demo.js
Last active January 3, 2020 21:28
Connect and upload a picture into s3
var AWS = require('aws-sdk')
const fs = require('fs')
const BUCKET = 'bucket'
const REGION = 'eu-west-1'
const ACCESS_KEY = 'AWS_ACCESS_KEY'
const SECRET_KEY = 'AWS_SECRET_KEY'
const localImage = './cat.png'
const imageRemoteName = `catImage_${new Date().getTime()}.png`