Skip to content

Instantly share code, notes, and snippets.

@yidongw
yidongw / Alan W
Created December 20, 2018 23:45
Perun15
function LCOpenTimeout() public {
require(msg.sender == alice.id && status == LCStatus.Init);
if (now > timeout) {
EventLCNotOpened();
selfdestruct(alice.id);
}
}
@yidongw
yidongw / Alan W
Created December 20, 2018 23:39
Perun14
function CheckSignature(address verifier, uint vid, address p1, uint cash1, uint subchan1, address Ingrid,
address p2, uint cash2, uint subchan2, uint validity, uint version, bytes sig) private view returns (bool) {
bytes32 msgHash = keccak256(vid, p1, cash1, subchan1, Ingrid, p2, cash2, subchan2, validity, version);
return libSignatures.verify(verifier, msgHash, sig);
}
function CheckVersion(address verifierA, address verifierB, uint vid, VirtualContract memory vc, uint version, bytes sigA, bytes sigB) private view returns (bool) {
if (!CheckSignature(verifierA, vid, vc.p1, vc.cash1, vc.subchan1, vc.Ingrid, vc.p2, vc.cash2, vc.subchan2, vc.validity, version, sigA))
return false;
bytes32 msgHash = keccak256(vid, vc.p1, vc.cash1, vc.subchan1, vc.Ingrid, vc.p2, vc.cash2, vc.subchan2, vc.validity, version, sigA);
@yidongw
yidongw / Alan W
Created December 20, 2018 23:37
Perun13
function VCAlreadyClosed(uint vid, bytes sig) AliceOrBob public {
require((msg.sender != virtual[vid].Ingrid && (virtual[vid].status == VCStatus.Closing || virtual[vid].status == VCStatus.ClosingFinal)) ||
(msg.sender == virtual[vid].Ingrid && virtual[vid].status == VCStatus.Timeouted));
bytes32 msgHash = keccak256(vid, alreadyClosed);
require(libSignatures.verify(Other(msg.sender, alice.id, bob.id), msgHash, sig));
EventClosed();
selfdestruct(msg.sender);
}
@yidongw
yidongw / Alan W
Created December 20, 2018 23:26
Perun12
function VCCloseTimeoutTimeout(uint vid) AliceOrBob public {
VirtualContract memory vc = virtual[vid];
require(vc.status == VCStatus.Timeouted && msg.sender != vc.Ingrid);
if (now > vc.timeout) {
EventClosed();
selfdestruct(msg.sender);
}
}
@yidongw
yidongw / Alan W
Created December 20, 2018 23:25
Perun11
function VCCloseTimeout(uint vid, address p1, uint cash1, uint subchan1, address Ingrid,
address p2, uint cash2, uint subchan2, uint validity, bytes sig) AliceOrBob public {
require(virtual[vid].status != VCStatus.Closed && virtual[vid].status != VCStatus.Timeouted && msg.sender != Ingrid);
CheckVC(vid, p1, cash1, subchan1, Ingrid, p2, cash2, subchan2, validity, sig);
if (now > validity + 2 * closingTime) {
virtual[vid].status = VCStatus.Timeouted;
virtual[vid].Ingrid = Ingrid;
virtual[vid].timeout = now + closingTime;
EventVCClosing(vid);
@yidongw
yidongw / Alan W
Created December 20, 2018 23:21
Perun10
function VCCloseFinalTimeout(uint vid) AliceOrBob public {
VirtualContract memory vc = virtual[vid];
require(vc.status == VCStatus.ClosingFinal && msg.sender == vc.Ingrid);
if (now > vc.timeout) {
alice.totalTransfers += int(vc.cashFinal1) - int(vc.cash1);
bob.totalTransfers += int(vc.cashFinal2) - int(vc.cash2);
virtual[vid].status = VCStatus.Closed;
EventClosed();
}
}
@yidongw
yidongw / Alan W
Created December 20, 2018 23:03
Perun9
function VCCloseFinal(uint vid, uint cash1A, uint cash2A, uint versionA, bytes sigA, bytes sigAB,
uint cash1B, uint cash2B, uint versionB, bytes sigB, bytes sigBA) AliceOrBob public {
VirtualContract memory vc = virtual[vid];
require(msg.sender == vc.Ingrid && (vc.status == VCStatus.WaitingToClose || vc.status == VCStatus.Closing));
require(CheckVersion(vc.p1, vc.p2, vid, vc, versionA, sigA, sigAB));
require(CheckVersion(vc.p2, vc.p1, vid, vc, versionB, sigB, sigBA));
if (versionA >= versionB) {
vc.cashFinal1 = cash1A;
vc.cashFinal2 = cash2A;
@yidongw
yidongw / Alan W
Created December 20, 2018 23:02
Perun8
function VCClose(uint vid, uint cash1, uint cash2, uint version, bytes sig, bytes sigB) AliceOrBob public {
VirtualContract memory vc = virtual[vid];
require(vc.status == VCStatus.Closing);
require(msg.sender != vc.Ingrid);
vc.cashFinal1 = cash1;
vc.cashFinal2 = cash2;
require(CheckVersion(Other(msg.sender, vc.p1, vc.p2), msg.sender, vid, vc, version, sig, sigB));
EventVCClose(vid, cash1, cash2, version, sig, sigB); // smart contract is getting the info for Ingrid
vc.status = VCStatus.WaitingToClose;
virtual[vid] = vc;
@yidongw
yidongw / Alan W
Created December 20, 2018 22:59
Perun7
function VCCloseInitTimeout(uint vid) AliceOrBob public {
require(virtual[vid].status == VCStatus.Closing && msg.sender == virtual[vid].Ingrid);
if (now > virtual[vid].timeout) {
EventClosed();
selfdestruct(msg.sender);
}
}
@yidongw
yidongw / Alan W
Created December 20, 2018 22:56
Perun6
function VCCloseInit(uint vid, address p1, uint cash1, uint subchan1, address Ingrid,
address p2, uint cash2, uint subchan2, uint validity, bytes sig) AliceOrBob public {
require(now > validity && Ingrid == msg.sender && virtual[vid].status == VCStatus.Empty);
CheckVC(vid, p1, cash1, subchan1, Ingrid, p2, cash2, subchan2, validity, sig); // open certificate
virtual[vid] = VirtualContract(p1, cash1, subchan1, Ingrid, p2, cash2, subchan2, validity, VCStatus.Closing, 0, 0, now + closingTime);
EventVCClosingInit(vid);
}