Skip to content

Instantly share code, notes, and snippets.

@kobigurk
Last active August 29, 2015 14:20
Show Gist options
  • Save kobigurk/a977a1bbfb1f41150214 to your computer and use it in GitHub Desktop.
Save kobigurk/a977a1bbfb1f41150214 to your computer and use it in GitHub Desktop.
multisig
contract multisig {
function multisig() {
// when a contract has a function with the same name as itself,
// then that function is run at startup
m_numOwners = 1;
m_required = m_numOwners;
m_owners[msg.sender] = m_numOwners;
}
function transact(address _to, uint _value) external onlyowner {
// Each transaction is converted in a hash and awaits confirmation
if (confirm(m_owners[msg.sender], sha3(_to, _value)))
_to.send(_value);
}
function addOwner(address _newOwner) external onlyowner {
// Any owner can invite more, and all transactions need to be approved by more than half of them
if (!(isOwner(_newOwner))) {
m_numOwners++;
m_required = m_numOwners / 2 + 1;
m_owners[_newOwner] = m_numOwners;
}
}
function confirm(uint _owner, bytes32 _hash) internal returns (bool) {
// Does some bitshifting magic to confirm transactions
uint ownerBit = 2**_owner;
if (m_pending[_hash].confirmed & ownerBit == 0) {
m_pending[_hash].confirmed &= ownerBit;
if (++m_pending[_hash].numConfirmations >= m_required) {
delete m_pending[_hash];
return true;
}
}
}
// What follows are variables that are used to describe the function
modifier onlyowner() { if (isOwner(msg.sender)) _ }
// Accessors to allow reading function variables
function isOwner(address addr) returns (bool) { return m_owners[addr] > 0; }
function totalOwners() returns (uint) { return m_numOwners; }
function totalRequiredConfirmations() returns (uint) { return m_required; }
function pendingConfirmations(address _to, uint _value) returns (uint) { return m_pending[sha3(_to, _value)].numConfirmations; }
// Declaring the contract structure
uint m_numOwners;
uint m_required;
mapping(address => uint) m_owners;
mapping(bytes32 => Pending) m_pending;
struct Pending {
uint confirmed;
uint numConfirmations;
}
}
var ABI = [ { "constant" : false, "inputs" : [], "name" : "totalOwners", "outputs" : [ { "name" : "", "type" : "uint256" } ], "type" : "function" }, { "constant" : false, "inputs" : [ { "name" : "addr", "type" : "address" } ], "name" : "isOwner", "outputs" : [ { "name" : "", "type" : "bool" } ], "type" : "function" }, { "constant" : false, "inputs" : [], "name" : "totalRequiredConfirmations", "outputs" : [ { "name" : "", "type" : "uint256" } ], "type" : "function" }, { "constant" : false, "inputs" : [ { "name" : "_newOwner", "type" : "address" } ], "name" : "addOwner", "outputs" : [], "type" : "function" }, { "constant" : false, "inputs" : [ { "name" : "_to", "type" : "address" }, { "name" : "_value", "type" : "uint256" } ], "name" : "pendingConfirmations", "outputs" : [ { "name" : "", "type" : "uint256" } ], "type" : "function" }, { "constant" : false, "inputs" : [ { "name" : "_to", "type" : "address" }, { "name" : "_value", "type" : "uint256" } ], "name" : "transact", "outputs" : [], "type" : "function" }]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment