Skip to content

Commit

Permalink
remove openChannelByRecipient logic
Browse files Browse the repository at this point in the history
  • Loading branch information
astroseger committed Oct 4, 2018
1 parent 52ca590 commit fff3de3
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 109 deletions.
34 changes: 0 additions & 34 deletions contracts/MultiPartyEscrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ contract MultiPartyEscrow {
mapping (uint256 => PaymentChannel) public channels;
mapping (address => uint256) public balances; //tokens which have been deposit but haven't been escrowed in the channels

//already used messages for openChannelByRecipient in order to prevent replay attack
mapping (bytes32 => bool) public usedMessages;

uint256 public nextChannelId; //id of the next channel (and size of channels)

ERC20 public token; // Address of token contract
Expand Down Expand Up @@ -97,38 +94,7 @@ contract MultiPartyEscrow {
return true;
}

//open a channel from the recipient side. Sender should send the signed permission to open the channel
function openChannelByRecipient(address sender, uint256 value, uint256 expiration, uint256 replicaId, uint256 messageNonce, bytes memory signature)
public
returns(bool)
{
require(balances[sender] >= value);

//compose the message which was signed
bytes32 message = prefixed(keccak256(abi.encodePacked(this, msg.sender, value, expiration, replicaId, messageNonce)));

//check for replay attack (message can be used only once)
require( ! usedMessages[message]);
usedMessages[message] = true;

// check that the signature is from the "sender"
require(recoverSigner(message, signature) == sender);

channels[nextChannelId] = PaymentChannel({
sender : sender,
recipient : msg.sender,
value : value,
replicaId : replicaId,
nonce : 0,
expiration : expiration
});
balances[sender] -= value;

emit EventChannelOpen(nextChannelId, sender, msg.sender, replicaId);
nextChannelId += 1;
return true;
}

function _channelSendbackAndReopenSuspended(uint256 channelId)
private
{
Expand Down
40 changes: 5 additions & 35 deletions test/MultiPartyEscrow_test1.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var ethereumjsabi = require('ethereumjs-abi');
var ethereumjsutil = require('ethereumjs-util');
let signFuns = require('./sign_mpe_funs');

//console.log(signFuns)


async function testErrorRevert(prom)
{
Expand Down Expand Up @@ -123,26 +123,15 @@ contract('MultiPartyEscrow', function(accounts) {

});

it ("Open the third channel from the server side", async function()
it ("Open the third channel", async function()
{
let expiration = web3.eth.getBlock(web3.eth.blockNumber).timestamp + 10000000
let expiration = web3.eth.getBlock(web3.eth.blockNumber).timestamp + 10000000
let value = 1000
let replicaId = 44
let messageNonce = 666
//open the channel from the server side
let sgn = await signFuns.waitSignedOpenChannelMessage(accounts[4], escrow.address, accounts[7], value, expiration, replicaId, messageNonce)

await escrow.openChannelByRecipient(accounts[4], value, expiration, replicaId , messageNonce, sgn, {from:accounts[7]})
//console.log(accounts)
assert.equal((await escrow.nextChannelId.call()).toNumber(), 3)

await escrow.openChannel(accounts[7], value, expiration, replicaId, {from:accounts[4]})
assert.equal((await escrow.nextChannelId.call()).toNumber(), 3)
assert.equal((await escrow.balances.call(accounts[4])).toNumber(), N2 - N3 - N1*2)


//try replay attack. It MUST fail

testErrorRevert(escrow.openChannelByRecipient(accounts[4], value, expiration, replicaId , messageNonce, sgn, {from:accounts[7]}))

});

it ("Extend and add funds to the third channel", async function()
Expand Down Expand Up @@ -181,24 +170,5 @@ contract('MultiPartyEscrow', function(accounts) {

});

it ("Check validity of the signatures with js-server part (open channel)", async function()
{
//open the channel message
let expiration = web3.eth.getBlock(web3.eth.blockNumber).timestamp + 10000000
let value = 1000
let replicaId = 44
let messageNonce = 666

let sgn = await signFuns.waitSignedOpenChannelMessage(accounts[4], escrow.address, accounts[7], value, expiration, replicaId, messageNonce)
assert.equal(signFuns.isValidSignatureOpenChannel(escrow.address, accounts[7], value, expiration, replicaId, messageNonce, sgn, accounts[4]), true, "signature should be ok")
assert.equal(signFuns.isValidSignatureOpenChannel(escrow.address, accounts[7], value, expiration, replicaId, messageNonce, sgn, accounts[5]), false, "signature should be false")
assert.equal(signFuns.isValidSignatureOpenChannel(escrow.address, accounts[7], value, expiration, replicaId, 42, sgn, accounts[4]), false, "signature should be false")
assert.equal(signFuns.isValidSignatureOpenChannel(escrow.address, accounts[7], value, expiration, 0 , messageNonce, sgn, accounts[4]), false, "signature should be false")
assert.equal(signFuns.isValidSignatureOpenChannel(escrow.address, accounts[7], value, 42, replicaId, messageNonce, sgn, accounts[4]), false, "signature should be false")
assert.equal(signFuns.isValidSignatureOpenChannel(escrow.address, accounts[7], 42, expiration, replicaId, messageNonce, sgn, accounts[4]), false, "signature should be false")
assert.equal(signFuns.isValidSignatureOpenChannel(escrow.address, accounts[4], value, expiration, replicaId, messageNonce, sgn, accounts[4]), false, "signature should be false")
assert.equal(signFuns.isValidSignatureOpenChannel(accounts[0], accounts[7], value, expiration, replicaId, messageNonce, sgn, accounts[4]), false, "signature should be false")
});

});

40 changes: 0 additions & 40 deletions test/sign_mpe_funs.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,13 @@ function composeClaimMessage(contractAddress, channelId, nonce, amount)
[contractAddress, channelId, nonce, amount]);
}

function composeOpenChannelMessage(contractAddress, recipientAddress, value, expiration, replicaId, messageNonce)
{
return ethereumjsabi.soliditySHA3(
["address", "address", "uint256", "uint256", "uint256", "uint256"],
[contractAddress, recipientAddress, value, expiration, replicaId, messageNonce]);

}


function signClaimMessage(fromAccount, contractAddress, channelId, nonce, amount, callback)
{
var message = composeClaimMessage(contractAddress, channelId, nonce, amount);
signMessage(fromAccount, message, callback);
}

function signOpenChannelMessage(fromAccount, contractAddress, recipientAddress, value, expiration, replicaId, messageNonce, callback)
{
var message = composeOpenChannelMessage(contractAddress, recipientAddress, value, expiration, replicaId, messageNonce);
signMessage(fromAccount, message, callback);
}


// this mimics the prefixing behavior of the ethSign JSON-RPC method.
function prefixed(hash) {
Expand All @@ -66,14 +52,6 @@ function isValidSignatureClaim(contractAddress, channelId, nonce, amount, signat
ethereumjsutil.stripHexPrefix(expectedSigner).toLowerCase();
}

function isValidSignatureOpenChannel(contractAddress, recipientAddress, value, expiration, replicaId, messageNonce, signature, expectedSigner) {
var message = prefixed(composeOpenChannelMessage(contractAddress, recipientAddress, value, expiration, replicaId, messageNonce));
var signer = recoverSigner(message, signature);
return signer.toLowerCase() ==
ethereumjsutil.stripHexPrefix(expectedSigner).toLowerCase();
}



async function waitSignedClaimMessage(fromAccount, contractAddress, channelId, nonce, amount)
{
Expand All @@ -91,25 +69,7 @@ async function waitSignedClaimMessage(fromAccount, contractAddress, channelId, n
return rezSign;
}

async function waitSignedOpenChannelMessage(fromAccount, contractAddress, recipientAddress, value, expiration, replicaId, messageNonce)
{
let detWait = true;
let rezSign;
signOpenChannelMessage(fromAccount, contractAddress, recipientAddress, value, expiration, replicaId, messageNonce, function(err,sgn)
{
detWait = false;
rezSign = sgn
});
while(detWait)
{
await sleep(1)
}
return rezSign;
}


module.exports.waitSignedClaimMessage = waitSignedClaimMessage;
module.exports.waitSignedOpenChannelMessage = waitSignedOpenChannelMessage;
module.exports.isValidSignatureClaim = isValidSignatureClaim;
module.exports.isValidSignatureOpenChannel = isValidSignatureOpenChannel;

0 comments on commit fff3de3

Please sign in to comment.