Skip to content

Commit

Permalink
Merge pull request #5 from vdrg/puzzle-wallet-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vdrg authored Oct 7, 2021
2 parents 67f568c + 3ec9dfa commit 98649bc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
5 changes: 0 additions & 5 deletions contracts/contracts/levels/PuzzleWallet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ contract PuzzleWallet {
_;
}

// TODO: remove thisssss
function takeOwnership() external {
owner = msg.sender;
}

function addToWhitelist(address addr) external onlyOwner {
whitelisted[addr] = true;
}
Expand Down
20 changes: 14 additions & 6 deletions contracts/contracts/levels/PuzzleWalletFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@ contract PuzzleWalletFactory is Level {
function createInstance(address /*_player*/) override public payable returns (address) {
require(msg.value == 1 ether, "Must send 1 ETH to create instance");

PuzzleWallet puzzle = new PuzzleWallet();
// Deploy the Wallet logic
PuzzleWallet walletLogic = new PuzzleWallet();
walletLogic.init();

puzzle.addToWhitelist(address(this));
// Proxy instance
bytes memory data= abi.encodeWithSelector(PuzzleWallet.init.selector, 100 ether);
PuzzleProxy proxy = new PuzzleProxy(address(this), address(walletLogic), data);

puzzle.deposit{value: 1 ether}(1 ether);
PuzzleWallet instance = PuzzleWallet(address(proxy));

return address(puzzle);
instance.addToWhitelist(address(this));
instance.deposit{ value: msg.value }(1 ether);

return address(proxy);
}

function validateInstance(address payable _instance, address /*_player*/) override public returns (bool) {
return _instance.balance == 0;
function validateInstance(address payable _instance, address _player) override public returns (bool) {
PuzzleWallet instance = PuzzleWallet(_instance);
return _instance.balance == 0 && instance.owner() == _player && instance.whitelisted(_player);
}
}
18 changes: 13 additions & 5 deletions contracts/test/levels/PuzzleWallet.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const ethutil = require('ethereumjs-util')
const PuzzleWalletFactory = artifacts.require('./levels/PuzzleWalletFactory.sol')
const PuzzleWallet = artifacts.require('./attacks/PuzzleWallet.sol')

const PuzzleProxy = artifacts.require('PuzzleProxy');
const PuzzleWalletFactory = artifacts.require('PuzzleWalletFactory')
const PuzzleWallet = artifacts.require('PuzzleWallet')
const Ethernaut = artifacts.require('./Ethernaut.sol')

const { BN, constants, expectEvent, expectRevert } = require('openzeppelin-test-helpers')
const utils = require('../utils/TestUtils')

Expand All @@ -26,11 +28,17 @@ contract('PuzzleWallet', function(accounts) {
{from: player, value: web3.utils.toWei('1', 'ether')}
)

assert.equal(await instance.owner(), level.address)
assert.equal(level.address, await instance.owner(), 'Owner is not the factory')
assert.equal(web3.utils.toWei('1', 'ether'), (await instance.balances(level.address)).toString())

const proxy = await PuzzleProxy.at(instance.address)
await proxy.proposeNewAdmin(player)

// check that the player has placed their address in the owner slot
assert.equal(player, await instance.owner(), "Player is not the owner")

// TODO: take ownership in a real way
await instance.takeOwnership({from: player})
// check that player is not whitelisted yet
assert.isFalse(await instance.whitelisted(player), 'Player is not whitelisted')

// Player whitelists herself
await instance.addToWhitelist(player, { from: player })
Expand Down

0 comments on commit 98649bc

Please sign in to comment.