remove MultisigWallet in favor of ConsenSys/MultiSigWallet

pull/328/head
Francisco Giordano 8 years ago
parent 60bc6a6da5
commit 58e2e4d742
  1. 125
      contracts/MultisigWallet.sol
  2. 28
      contracts/ownership/Multisig.sol
  3. 183
      contracts/ownership/Shareable.sol
  4. 119
      test/MultisigWallet.js
  5. 103
      test/Shareable.js
  6. 12
      test/helpers/MultisigWalletMock.sol
  7. 16
      test/helpers/ShareableMock.sol

@ -1,125 +0,0 @@
pragma solidity ^0.4.11;
import "./ownership/Multisig.sol";
import "./ownership/Shareable.sol";
import "./DayLimit.sol";
/**
* MultisigWallet
* Usage:
* bytes32 h = Wallet(w).from(oneOwner).execute(to, value, data);
* Wallet(w).from(anotherOwner).confirm(h);
*/
contract MultisigWallet is Multisig, Shareable, DayLimit {
struct Transaction {
address to;
uint256 value;
bytes data;
}
/**
* Constructor, sets the owners addresses, number of approvals required, and daily spending limit
* @param _owners A list of owners.
* @param _required The amount required for a transaction to be approved.
*/
function MultisigWallet(address[] _owners, uint256 _required, uint256 _daylimit)
Shareable(_owners, _required)
DayLimit(_daylimit) { }
/**
* @dev destroys the contract sending everything to `_to`.
*/
function destroy(address _to) onlymanyowners(keccak256(msg.data)) external {
selfdestruct(_to);
}
/**
* @dev Fallback function, receives value and emits a deposit event.
*/
function() payable {
// just being sent some cash?
if (msg.value > 0)
Deposit(msg.sender, msg.value);
}
/**
* @dev Outside-visible transaction entry point. Executes transaction immediately if below daily
* spending limit. If not, goes into multisig process. We provide a hash on return to allow the
* sender to provide shortcuts for the other confirmations (allowing them to avoid replicating
* the _to, _value, and _data arguments). They still get the option of using them if they want,
* anyways.
* @param _to The receiver address
* @param _value The value to send
* @param _data The data part of the transaction
*/
function execute(address _to, uint256 _value, bytes _data) external onlyOwner returns (bytes32 _r) {
// first, take the opportunity to check that we're under the daily limit.
if (underLimit(_value)) {
SingleTransact(msg.sender, _value, _to, _data);
// yes - just execute the call.
if (!_to.call.value(_value)(_data)) {
revert();
}
return 0;
}
// determine our operation hash.
_r = keccak256(msg.data, block.number);
if (!confirm(_r) && txs[_r].to == 0) {
txs[_r].to = _to;
txs[_r].value = _value;
txs[_r].data = _data;
ConfirmationNeeded(_r, msg.sender, _value, _to, _data);
}
}
/**
* @dev Confirm a transaction by providing just the hash. We use the previous transactions map,
* txs, in order to determine the body of the transaction from the hash provided.
* @param _h The transaction hash to approve.
*/
function confirm(bytes32 _h) onlymanyowners(_h) returns (bool) {
if (txs[_h].to != 0) {
assert(txs[_h].to.call.value(txs[_h].value)(txs[_h].data));
MultiTransact(msg.sender, _h, txs[_h].value, txs[_h].to, txs[_h].data);
delete txs[_h];
return true;
}
}
/**
* @dev Updates the daily limit value.
* @param _newLimit uint256 to represent the new limit.
*/
function setDailyLimit(uint256 _newLimit) onlymanyowners(keccak256(msg.data)) external {
_setDailyLimit(_newLimit);
}
/**
* @dev Resets the value spent to enable more spending
*/
function resetSpentToday() onlymanyowners(keccak256(msg.data)) external {
_resetSpentToday();
}
// INTERNAL METHODS
/**
* @dev Clears the list of transactions pending approval.
*/
function clearPending() internal {
uint256 length = pendingsIndex.length;
for (uint256 i = 0; i < length; ++i) {
delete txs[pendingsIndex[i]];
}
super.clearPending();
}
// FIELDS
// pending transactions we have at present.
mapping (bytes32 => Transaction) txs;
}

@ -1,28 +0,0 @@
pragma solidity ^0.4.11;
/**
* @title Multisig
* @dev Interface contract for multisig proxy contracts; see below for docs.
*/
contract Multisig {
// EVENTS
// logged events:
// Funds has arrived into the wallet (record how much).
event Deposit(address _from, uint256 value);
// Single transaction going out of the wallet (record who signed for it, how much, and to whom it's going).
event SingleTransact(address owner, uint256 value, address to, bytes data);
// Multi-sig transaction going out of the wallet (record who signed for it last, the operation hash, how much, and to whom it's going).
event MultiTransact(address owner, bytes32 operation, uint256 value, address to, bytes data);
// Confirmation still needed for a transaction.
event ConfirmationNeeded(bytes32 operation, address initiator, uint256 value, address to, bytes data);
// FUNCTIONS
// TODO: document
function changeOwner(address _from, address _to) external;
function execute(address _to, uint256 _value, bytes _data) external returns (bytes32);
function confirm(bytes32 _h) returns (bool);
}

@ -1,183 +0,0 @@
pragma solidity ^0.4.11;
/**
* @title Shareable
* @dev inheritable "property" contract that enables methods to be protected by requiring the
* acquiescence of either a single, or, crucially, each of a number of, designated owners.
* @dev Usage: use modifiers onlyowner (just own owned) or onlymanyowners(hash), whereby the same hash must be provided by some number (specified in constructor) of the set of owners (specified in the constructor) before the interior is executed.
*/
contract Shareable {
// struct for the status of a pending operation.
struct PendingState {
uint256 yetNeeded;
uint256 ownersDone;
uint256 index;
}
// the number of owners that must confirm the same operation before it is run.
uint256 public required;
// list of owners
address[256] owners;
// index on the list of owners to allow reverse lookup
mapping(address => uint256) ownerIndex;
// the ongoing operations.
mapping(bytes32 => PendingState) pendings;
bytes32[] pendingsIndex;
// this contract only has six types of events: it can accept a confirmation, in which case
// we record owner and operation (hash) alongside it.
event Confirmation(address owner, bytes32 operation);
event Revoke(address owner, bytes32 operation);
// simple single-sig function modifier.
modifier onlyOwner {
require(isOwner(msg.sender));
_;
}
/**
* @dev Modifier for multisig functions.
* @param _operation The operation must have an intrinsic hash in order that later attempts can be
* realised as the same underlying operation and thus count as confirmations.
*/
modifier onlymanyowners(bytes32 _operation) {
if (confirmAndCheck(_operation)) {
_;
}
}
/**
* @dev Constructor is given the number of sigs required to do protected "onlymanyowners"
* transactions as well as the selection of addresses capable of confirming them.
* @param _owners A list of owners.
* @param _required The amount required for a transaction to be approved.
*/
function Shareable(address[] _owners, uint256 _required) {
owners[1] = msg.sender;
ownerIndex[msg.sender] = 1;
for (uint256 i = 0; i < _owners.length; ++i) {
owners[2 + i] = _owners[i];
ownerIndex[_owners[i]] = 2 + i;
}
required = _required;
require(required <= owners.length);
}
/**
* @dev Revokes a prior confirmation of the given operation.
* @param _operation A string identifying the operation.
*/
function revoke(bytes32 _operation) external {
uint256 index = ownerIndex[msg.sender];
// make sure they're an owner
if (index == 0) {
return;
}
uint256 ownerIndexBit = 2**index;
var pending = pendings[_operation];
if (pending.ownersDone & ownerIndexBit > 0) {
pending.yetNeeded++;
pending.ownersDone -= ownerIndexBit;
Revoke(msg.sender, _operation);
}
}
/**
* @dev Gets an owner by 0-indexed position (using numOwners as the count)
* @param ownerIndex uint256 The index of the owner
* @return The address of the owner
*/
function getOwner(uint256 ownerIndex) external constant returns (address) {
return address(owners[ownerIndex + 1]);
}
/**
* @dev Checks if given address is an owner.
* @param _addr address The address which you want to check.
* @return True if the address is an owner and fase otherwise.
*/
function isOwner(address _addr) constant returns (bool) {
return ownerIndex[_addr] > 0;
}
/**
* @dev Function to check is specific owner has already confirme the operation.
* @param _operation The operation identifier.
* @param _owner The owner address.
* @return True if the owner has confirmed and false otherwise.
*/
function hasConfirmed(bytes32 _operation, address _owner) constant returns (bool) {
var pending = pendings[_operation];
uint256 index = ownerIndex[_owner];
// make sure they're an owner
if (index == 0) {
return false;
}
// determine the bit to set for this owner.
uint256 ownerIndexBit = 2**index;
return !(pending.ownersDone & ownerIndexBit == 0);
}
/**
* @dev Confirm and operation and checks if it's already executable.
* @param _operation The operation identifier.
* @return Returns true when operation can be executed.
*/
function confirmAndCheck(bytes32 _operation) internal returns (bool) {
// determine what index the present sender is:
uint256 index = ownerIndex[msg.sender];
// make sure they're an owner
require(index != 0);
var pending = pendings[_operation];
// if we're not yet working on this operation, switch over and reset the confirmation status.
if (pending.yetNeeded == 0) {
// reset count of confirmations needed.
pending.yetNeeded = required;
// reset which owners have confirmed (none) - set our bitmap to 0.
pending.ownersDone = 0;
pending.index = pendingsIndex.length++;
pendingsIndex[pending.index] = _operation;
}
// determine the bit to set for this owner.
uint256 ownerIndexBit = 2**index;
// make sure we (the message sender) haven't confirmed this operation previously.
if (pending.ownersDone & ownerIndexBit == 0) {
Confirmation(msg.sender, _operation);
// ok - check if count is enough to go ahead.
if (pending.yetNeeded <= 1) {
// enough confirmations: reset and run interior.
delete pendingsIndex[pendings[_operation].index];
delete pendings[_operation];
return true;
} else {
// not enough: record that this owner in particular confirmed.
pending.yetNeeded--;
pending.ownersDone |= ownerIndexBit;
}
}
return false;
}
/**
* @dev Clear the pending list.
*/
function clearPending() internal {
uint256 length = pendingsIndex.length;
for (uint256 i = 0; i < length; ++i) {
if (pendingsIndex[i] != 0) {
delete pendings[pendingsIndex[i]];
}
}
delete pendingsIndex;
}
}

@ -1,119 +0,0 @@
'use strict';
var MultisigWalletMock = artifacts.require('./helpers/MultisigWalletMock.sol');
require('./helpers/transactionMined.js');
contract('MultisigWallet', function(accounts) {
let shouldntFail = function(err) {
assert.isFalse(!!err);
};
it('should send balance to passed address upon death', async function() {
//Give account[0] 20 ether
web3.eth.sendTransaction({from: web3.eth.coinbase, to: accounts[0], value: web3.toWei('20','ether')}, shouldntFail);
let dailyLimit = 10;
let ownersRequired = 2;
//Create MultisigWallet contract with 10 ether
let wallet = await MultisigWalletMock.new(accounts, ownersRequired, dailyLimit, {value: web3.toWei('10', 'ether')});
//Get balances of owner and wallet after wallet creation.
let ownerBalance = web3.eth.getBalance(accounts[0]);
let walletBalance = web3.eth.getBalance(wallet.address);
let hash = 1234;
//Call destroy function from two different owner accounts, satisfying owners required
await wallet.destroy(accounts[0], {data: hash});
let txnHash = await wallet.destroy(accounts[0], {from: accounts[1], data: hash});
//Get balances of owner and wallet after destroy function is complete, compare with previous values
let newOwnerBalance = web3.eth.getBalance(accounts[0]);
let newWalletBalance = web3.eth.getBalance(wallet.address);
assert.isTrue(newOwnerBalance > ownerBalance);
assert.isTrue(newWalletBalance < walletBalance);
});
it('should execute transaction if below daily limit', async function() {
//Give account[0] 20 ether
web3.eth.sendTransaction({from: web3.eth.coinbase, to: accounts[0], value: web3.toWei('20','ether')}, shouldntFail);
let dailyLimit = 10;
let ownersRequired = 2;
//Create MultisigWallet contract with 10 ether
let wallet = await MultisigWalletMock.new(accounts, ownersRequired, dailyLimit, {value: web3.toWei('10', 'ether')});
let accountBalance = web3.eth.getBalance(accounts[2]);
let hash = 1234;
//Owner account0 commands wallet to send 9 wei to account2
let txnHash = await wallet.execute(accounts[2], 9, hash);
//Balance of account2 should have increased
let newAccountBalance = web3.eth.getBalance(accounts[2]);
assert.isTrue(newAccountBalance.greaterThan(accountBalance));
});
it('should prevent execution of transaction if above daily limit', async function() {
//Give account[0] 20 ether
web3.eth.sendTransaction({from: web3.eth.coinbase, to: accounts[0], value: web3.toWei('20','ether')}, shouldntFail);
let dailyLimit = 10;
let ownersRequired = 2;
//Create MultisigWallet contract with 10 ether
let wallet = await MultisigWalletMock.new(accounts, ownersRequired, dailyLimit, {value: web3.toWei('10', 'ether')});
let accountBalance = web3.eth.getBalance(accounts[2]);
let hash = 1234;
//Owner account0 commands wallet to send 9 wei to account2
let txnHash = await wallet.execute(accounts[2], 9, hash);
//Balance of account2 should have increased
let newAccountBalance = web3.eth.getBalance(accounts[2]);
assert.isTrue(newAccountBalance > accountBalance);
accountBalance = newAccountBalance;
hash = 4567;
//Owner account0 commands wallet to send 2 more wei to account2, going over the daily limit of 10
txnHash = await wallet.execute(accounts[2], 2, hash);
//Balance of account2 should not change
newAccountBalance = web3.eth.getBalance(accounts[2]);
assert.equal(newAccountBalance.toString(), accountBalance.toString());
});
it('should execute transaction if above daily limit and enough owners approve', async function() {
//Give account[0] 20 ether
web3.eth.sendTransaction({from: web3.eth.coinbase, to: accounts[0], value: web3.toWei('20','ether')}, shouldntFail);
let dailyLimit = 10;
let ownersRequired = 2;
//Create MultisigWallet contract with 10 ether
let wallet = await MultisigWalletMock.new(accounts, ownersRequired, dailyLimit, {value: web3.toWei('10', 'ether')});
let accountBalance = web3.eth.getBalance(accounts[2]);
let hash = 1234;
//Owner account0 commands wallet to send 11 wei to account2
let txnHash = await wallet.execute(accounts[2], 11, hash);
//Balance of account2 should not change
let newAccountBalance = web3.eth.getBalance(accounts[2]);
assert.equal(newAccountBalance.toString(), accountBalance.toString());
accountBalance = newAccountBalance;
//Owner account1 commands wallet to send 11 wei to account2
txnHash = await wallet.execute(accounts[2], 2, hash);
//Balance of account2 should change
newAccountBalance = web3.eth.getBalance(accounts[2]);
assert.isTrue(newAccountBalance > accountBalance);
});
});

@ -1,103 +0,0 @@
var ShareableMock = artifacts.require("./helpers/ShareableMock.sol");
contract('Shareable', function(accounts) {
it('should construct with correct owners and number of sigs required', async function() {
let requiredSigs = 2;
let owners = accounts.slice(1,4);
let shareable = await ShareableMock.new(owners, requiredSigs);
let required = await shareable.required();
assert.equal(required, requiredSigs);
let owner = await shareable.getOwner(0);
assert.equal(owner, accounts[0]);
for(let i = 0; i < accounts.length; i++) {
let owner = await shareable.getOwner(i);
let isowner = await shareable.isOwner(accounts[i]);
if(i <= owners.length) {
assert.equal(accounts[i], owner);
assert.isTrue(isowner);
} else {
assert.notEqual(accounts[i], owner);
assert.isFalse(isowner);
}
}
});
it('should only perform multisig function with enough sigs', async function() {
let requiredSigs = 3;
let owners = accounts.slice(1,4);
let shareable = await ShareableMock.new(owners, requiredSigs);
let hash = 1234;
let initCount = await shareable.count();
initCount = initCount.toString();
for(let i = 0; i < requiredSigs; i++) {
await shareable.increaseCount(hash, {from: accounts[i]});
let count = await shareable.count();
if(i == requiredSigs - 1) {
assert.equal(Number(initCount)+1, count.toString());
} else {
assert.equal(initCount, count.toString());
}
}
});
it('should require approval from different owners', async function() {
let requiredSigs = 2;
let owners = accounts.slice(1,4);
let shareable = await ShareableMock.new(owners, requiredSigs);
let hash = 1234;
let initCount = await shareable.count();
initCount = initCount.toString();
//Count shouldn't increase when the same owner calls repeatedly
for(let i = 0; i < 2; i++) {
await shareable.increaseCount(hash);
let count = await shareable.count();
assert.equal(initCount, count.toString());
}
});
it('should reset sig count after operation is approved', async function() {
let requiredSigs = 3;
let owners = accounts.slice(1,4);
let shareable = await ShareableMock.new(owners, requiredSigs);
let hash = 1234;
let initCount = await shareable.count();
for(let i = 0; i < requiredSigs * 3; i++) {
await shareable.increaseCount(hash, {from: accounts[i % 4]});
let count = await shareable.count();
if((i%(requiredSigs)) == requiredSigs - 1) {
initCount = Number(initCount)+1;
assert.equal(initCount, count);
} else {
assert.equal(initCount.toString(), count);
}
}
});
it('should not perform multisig function after an owner revokes', async function() {
let requiredSigs = 3;
let owners = accounts.slice(1,4);
let shareable = await ShareableMock.new(owners, requiredSigs);
let hash = 1234;
let initCount = await shareable.count();
for(let i = 0; i < requiredSigs; i++) {
if(i == 1) {
await shareable.revoke(hash, {from: accounts[i-1]});
}
await shareable.increaseCount(hash, {from: accounts[i]});
let count = await shareable.count();
assert.equal(initCount.toString(), count);
}
});
});

@ -1,12 +0,0 @@
pragma solidity ^0.4.11;
import "../../contracts/MultisigWallet.sol";
contract MultisigWalletMock is MultisigWallet {
uint256 public totalSpending;
function MultisigWalletMock(address[] _owners, uint256 _required, uint256 _daylimit)
MultisigWallet(_owners, _required, _daylimit) payable { }
function changeOwner(address _from, address _to) external { }
}

@ -1,16 +0,0 @@
pragma solidity ^0.4.11;
import "../../contracts/ownership/Shareable.sol";
contract ShareableMock is Shareable {
uint256 public count = 0;
function ShareableMock(address[] _owners, uint256 _required) Shareable(_owners, _required) {
}
function increaseCount(bytes32 action) onlymanyowners(action) {
count = count + 1;
}
}
Loading…
Cancel
Save