MultisigWallet.sol 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. pragma solidity ^0.4.4;
  2. // interface contract for multisig proxy contracts; see below for docs.
  3. contract multisig {
  4. // EVENTS
  5. // logged events:
  6. // Funds has arrived into the wallet (record how much).
  7. event Deposit(address _from, uint value);
  8. // Single transaction going out of the wallet (record who signed for it, how much, and to whom it's going).
  9. event SingleTransact(address owner, uint value, address to, bytes data);
  10. // 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).
  11. event MultiTransact(address owner, bytes32 operation, uint value, address to, bytes data);
  12. // Confirmation still needed for a transaction.
  13. event ConfirmationNeeded(bytes32 operation, address initiator, uint value, address to, bytes data);
  14. // FUNCTIONS
  15. // TODO: document
  16. function changeOwner(address _from, address _to) external;
  17. function execute(address _to, uint _value, bytes _data) external returns (bytes32);
  18. function confirm(bytes32 _h) returns (bool);
  19. }
  20. // usage:
  21. // bytes32 h = Wallet(w).from(oneOwner).execute(to, value, data);
  22. // Wallet(w).from(anotherOwner).confirm(h);
  23. contract Wallet is multisig, Shareable, daylimit {
  24. // TYPES
  25. // Transaction structure to remember details of transaction lest it need be saved for a later call.
  26. struct Transaction {
  27. address to;
  28. uint value;
  29. bytes data;
  30. }
  31. // METHODS
  32. // constructor - just pass on the owner array to the multiowned and
  33. // the limit to daylimit
  34. function Wallet(address[] _owners, uint _required, uint _daylimit)
  35. multiowned(_owners, _required) daylimit(_daylimit) {
  36. }
  37. // kills the contract sending everything to `_to`.
  38. function kill(address _to) onlymanyowners(sha3(msg.data)) external {
  39. suicide(_to);
  40. }
  41. // gets called when no other function matches
  42. function() {
  43. // just being sent some cash?
  44. if (msg.value > 0)
  45. Deposit(msg.sender, msg.value);
  46. }
  47. // Outside-visible transact entry point. Executes transaction immediately if below daily spend limit.
  48. // If not, goes into multisig process. We provide a hash on return to allow the sender to provide
  49. // shortcuts for the other confirmations (allowing them to avoid replicating the _to, _value
  50. // and _data arguments). They still get the option of using them if they want, anyways.
  51. function execute(address _to, uint _value, bytes _data) external onlyowner returns (bytes32 _r) {
  52. // first, take the opportunity to check that we're under the daily limit.
  53. if (underLimit(_value)) {
  54. SingleTransact(msg.sender, _value, _to, _data);
  55. // yes - just execute the call.
  56. _to.call.value(_value)(_data);
  57. return 0;
  58. }
  59. // determine our operation hash.
  60. _r = sha3(msg.data, block.number);
  61. if (!confirm(_r) && m_txs[_r].to == 0) {
  62. m_txs[_r].to = _to;
  63. m_txs[_r].value = _value;
  64. m_txs[_r].data = _data;
  65. ConfirmationNeeded(_r, msg.sender, _value, _to, _data);
  66. }
  67. }
  68. // confirm a transaction through just the hash. we use the previous transactions map, m_txs, in order
  69. // to determine the body of the transaction from the hash provided.
  70. function confirm(bytes32 _h) onlymanyowners(_h) returns (bool) {
  71. if (m_txs[_h].to != 0) {
  72. m_txs[_h].to.call.value(m_txs[_h].value)(m_txs[_h].data);
  73. MultiTransact(msg.sender, _h, m_txs[_h].value, m_txs[_h].to, m_txs[_h].data);
  74. delete m_txs[_h];
  75. return true;
  76. }
  77. }
  78. // INTERNAL METHODS
  79. function clearPending() internal {
  80. uint length = m_pendingIndex.length;
  81. for (uint i = 0; i < length; ++i)
  82. delete m_txs[m_pendingIndex[i]];
  83. super.clearPending();
  84. }
  85. // FIELDS
  86. // pending transactions we have at present.
  87. mapping (bytes32 => Transaction) m_txs;
  88. }