Last active
September 8, 2018 16:58
-
-
Save aleph-v/374aaa5e5147448f2b3699e770807560 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.24; | |
contract stateChannel{ | |
uint blockPeriod; | |
uint neededSignatures; | |
struct participant{ | |
bool isNeeded; | |
uint256 block; | |
} | |
mapping(address => participant) channel; | |
bytes32 stateRoot; | |
uint constant BLOCKWEEK = 3000; | |
string constant prefix = "\x19Ethereum Signed Message:\n32"; | |
function updateState(bytes32 newRoot, bytes memory signature) public { | |
require(signature.length %65 == neededSignatures); | |
for(uint i=0; i < signature.length; i += 65){ | |
bytes32 r; | |
bytes32 s; | |
uint8 v; | |
assembly{ | |
let ptr := add(add(signature,0x20), mul(0x20,i)) | |
r := mload(ptr) | |
s := mload(add(ptr,0x20)) | |
v := byte(0,mload(add(ptr,0x40))) | |
} | |
bytes32 newHash = keccak256(abi.encodePacked(prefix,newRoot)); | |
address person = ecrecover(newHash, v,r,s); | |
require(person != 0); | |
require(channel[person].isNeeded); | |
require(channel[person].block != block.number); | |
channel[person].block = block.number; | |
} | |
stateRoot = newRoot; | |
} | |
function addPerson(address newPerson, bytes signature) public{ | |
require(signature.length %65 == neededSignatures); | |
for(uint i=0; i < signature.length; i += 65){ | |
bytes32 r; | |
bytes32 s; | |
uint8 v; | |
assembly{ | |
let ptr := add(add(signature,0x20), mul(0x20,i)) | |
r := mload(ptr) | |
s := mload(add(ptr,0x20)) | |
v := byte(0,mload(add(ptr,0x40))) | |
} | |
bytes32 newHash = keccak256(abi.encodePacked(prefix,keccak256(newPerson))); | |
address person = ecrecover(newHash, v,r,s); | |
require(person != 0); | |
require(channel[person].isNeeded); | |
require(channel[person].block != block.number); | |
channel[person].block = block.number; | |
} | |
neededSignatures++; | |
channel[newPerson] = participant(true, block.number); | |
} | |
function removePerson(address person) public { | |
require(channel[person].block < block.number - BLOCKWEEK && channel[person].block != 0); | |
channel[person].isNeeded = false; | |
neededSignatures--; | |
} | |
function updateMe() external{ | |
channel[msg.sender].block = block.number; | |
} | |
constructor(bytes32 channelHash, address[] participants){ | |
stateRoot = channelHash; | |
channel[msg.sender] = participant(true, block.number); | |
for(uint i =0; i<participants.length; i++){ | |
channel[participants[i]] = participant(true, block.number); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment