Skip to content

Instantly share code, notes, and snippets.

@libertylocked
Last active November 30, 2017 19:42
Show Gist options
  • Save libertylocked/52e32fb09ea18d88af489dcb62b829d4 to your computer and use it in GitHub Desktop.
Save libertylocked/52e32fb09ea18d88af489dcb62b829d4 to your computer and use it in GitHub Desktop.
mixing contract using one time pad with an operator
/**
* A mixing contract using one time pad
* Do not copy this code as it is not tested or audited.
*/
pragma solidity 0.4.18;
contract MixingOTP {
address operator;
mapping(address => bool) payers;
mapping(address => bool) paid;
mapping(address => bool) revealedRecipients;
bool revealed = false;
uint payerCount = 0;
uint paidCount = 0;
uint amount = 0;
address paddedRecipients = 0;
event paymentSubmitted(address payer, address encryptedPayee);
modifier onlyPayer() {
require(payers[msg.sender]);
_;
}
modifier onlyRecipient() {
require(revealedRecipients[msg.sender]);
_;
}
modifier onlyWhenFunded() {
require(paidCount == payerCount);
_;
}
modifier onlyOperator() {
require(msg.sender == operator);
_;
}
function MixingOTP(uint _amount, address[] _payers) public {
operator = msg.sender;
amount = _amount;
for (uint i = 0; i < _payers.length; i++) {
payers[_payers[i]] = true;
}
payerCount = _payers.length;
}
function sendMoney(address paddedAddress) onlyPayer payable public {
require(!paid[msg.sender]);
require(msg.value == amount);
paddedRecipients = uint160(paddedRecipients) ^ uint160(paddedAddress);
paidCount++;
paymentSubmitted(msg.sender, paddedAddress); // emit event
}
function revealRecipients(address[] _recipients)
onlyWhenFunded onlyOperator public
{
require(!revealed);
require(_recipients.length == payerCount);
address addrXored = 0;
uint i;
for (i = 0; i < _recipients.length; i++) {
addrXored = uint160(addrXored) ^ uint160(_recipients[i]);
}
require(addrXored == paddedRecipients);
for (i = 0; i < _recipients.length; i++) {
revealedRecipients[_recipients[i]] = true;
}
revealed = true;
}
function withdraw() onlyRecipient public {
revealedRecipients[msg.sender] = false;
msg.sender.transfer(amount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment