Skip to content

Instantly share code, notes, and snippets.

@nathan-websculpt
nathan-websculpt / RPSv2.sol
Last active October 20, 2021 12:07
Two Player Rock, Paper, Scissors in Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
import "../node_modules/@OpenZeppelin/contracts/utils/math/SafeMath.sol";
import "../node_modules/@OpenZeppelin/contracts/security/ReentrancyGuard.sol";
//for use in this blog post: https://medium.com/@websculpt/rock-paper-scissors-in-solidity-part-3-commit-reveal-4d56a84cbe97
contract RPSv2 is ReentrancyGuard {
using SafeMath for uint;
@nathan-websculpt
nathan-websculpt / rpsv1.sol
Created September 27, 2021 20:11
Very basic representation of Rock, Paper, Scissors logic on Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v4.3/contracts/security/ReentrancyGuard.sol";
//for testing/learning purposes only -- not production code
contract rpsv1 is ReentrancyGuard{
mapping (address => uint) public playerBalances;
event Received(address, uint);
@nathan-websculpt
nathan-websculpt / StringTesting.sol
Created September 27, 2021 17:34
Compare and Concat strings in solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract StringTesting {
function stringCompare(string calldata _inputOne, string calldata _inputTwo) external pure returns (bool r) {
bool rslt;
@nathan-websculpt
nathan-websculpt / EscrowExample.sol
Created September 24, 2021 19:42
Example usage of Escrow -- not production-ready -- for learning purposes only
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v4.3/contracts/security/ReentrancyGuard.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v4.3/contracts/utils/escrow/Escrow.sol";
contract EscrowExample is ReentrancyGuard {
Escrow private immutable _escrow;
@nathan-websculpt
nathan-websculpt / OpenZeppelin_ReentrancyGuard.sol
Created September 22, 2021 18:03
Here is an example of the OpenZeppelin Reentrancy Guard in use
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v4.3/contracts/security/ReentrancyGuard.sol";
contract Attackee is ReentrancyGuard {
mapping(address => uint) public attackeeBalances;
function depositIntoAttackee() external payable {
@nathan-websculpt
nathan-websculpt / ReentrancyGuard_BlockedAttack.sol
Created September 22, 2021 15:55
This example shows a simple Reentrancy Guard in action. Even though the balance is not zero-ed out until after the call, this reentrancy guard stops the attacker's receive() from completing the attack. For learning purposes only.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract ReentrancyGuard {
bool private guardLocked;
modifier noReentry() {
require(!guardLocked, "Prevented by noReentry in ReentrancyGuard");
@nathan-websculpt
nathan-websculpt / ReentrancyGuard_simpleExample.sol
Last active September 24, 2021 20:40
This is an example Reentrancy Guard for teaching purposes
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract ReentrancyGuard {
bool private guardLocked;
modifier noReentry() {
require(!guardLocked, "Prevented by noReentry in ReentrancyGuard");
@nathan-websculpt
nathan-websculpt / ReentrancyAttackFIX.sol
Created September 17, 2021 14:31
The fix for ReentrancyAttackExample.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Attackee {
mapping(address => uint) public attackeeBalances;
function depositIntoAttackee() external payable {
attackeeBalances[msg.sender] += msg.value;
}
@nathan-websculpt
nathan-websculpt / ReentrancyAttackExample.sol
Created September 16, 2021 15:11
Attacker.performAttack can drain Attackee in a reentrancy attack that utilizes the Attacker's fallback function
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Attackee {
mapping(address => uint) public attackeeBalances;
function depositIntoAttackee() external payable {
attackeeBalances[msg.sender] += msg.value;
}
@nathan-websculpt
nathan-websculpt / AttackVulnerableWithdraw.sol
Created September 16, 2021 12:46
How to attack the VulnerableWithdraw.sol example
//this is called when Attackee sends Ether to this contract (Attacker)
fallback() external payable {
if(address(contractToAttack).balance >= 1 ether) {
contractToAttack.withdrawFromAttackee();
}
}
function performAttack() external payable {
require(msg.value >= 1 ether);
contractToAttack.depositIntoAttackee{value: 1 ether}();