Last active
November 14, 2017 12:02
-
-
Save jbaylina/ef1b15783644f531a553f60e999fa257 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.16; | |
contract LiquidPledging { | |
uint public size; | |
bytes32 public hash; | |
address public addr; | |
function LiquidPledging() public { | |
size = 0xFFFFFFFFFFFF; | |
} | |
function addPlugin() public { | |
uint _size; | |
address _addr = msg.sender; | |
assembly { | |
_size := extcodesize(_addr) | |
} | |
size = _size; | |
hash = getCodeHash(_addr); | |
addr = _addr; | |
} | |
function getCodeHash(address _addr) constant public returns (bytes32) { | |
bytes memory o_code; | |
assembly { | |
// retrieve the size of the code, this needs assembly | |
let size := extcodesize(_addr) | |
// allocate output byte array - this could also be done without assembly | |
// by using o_code = new bytes(size) | |
o_code := mload(0x40) | |
// new "memory end" including padding | |
mstore(0x40, add(o_code, and(add(add(size, 0x20), 0x1f), not(0x1f)))) | |
// store length in memory | |
mstore(o_code, size) | |
// actually retrieve the code, this needs assembly | |
extcodecopy(_addr, add(o_code, 0x20), 0, size) | |
} | |
return keccak256(o_code); | |
} | |
} | |
// 0xb96cc836096c1e7fb923bec3ca454eaa0358de54 | |
contract ExampleBad { | |
function ExampleBad(LiquidPledging lp) public { | |
lp.addPlugin(); | |
} | |
} | |
contract ExampleGood { | |
bool initPending; | |
function ExampleGood() public { | |
require(msg.sender != tx.origin); // Avoids beeing created directly by mistake. | |
initPending = true; | |
} | |
function initExample1(LiquidPledging lp) public { | |
require(initPending); | |
initPending = false; | |
lp.addPlugin(); | |
} | |
} | |
contract ExampleGoodFactory { | |
function ExampleGoodFactory(LiquidPledging lp) public { | |
ExampleGood e = new ExampleGood(); | |
e.initExample1(lp); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment