<commit_before>"use strict";

var Web3 = require('web3');
var request = require('request');

var web3 = new Web3();
web3.setProvider(new Web3.providers.HttpProvider('http://localhost:8545'));

var CONTRACT_ADDRESS = "0xeb67cE7C8723e7adE36b828f7FbBCbA3391D5A55";
var CONTRACT_INTERFACE = [ { "constant": false, "inputs": [ { "name": "channelId", "type": "bytes32" }, { "name": "payment", "type": "uint256" }, { "name": "v", "type": "uint8" }, { "name": "r", "type": "bytes32" }, { "name": "s", "type": "bytes32" } ], "name": "claim", "outputs": [], "payable": false, "type": "function" }, { "constant": true, "inputs": [ { "name": "channelId", "type": "bytes32" }, { "name": "value", "type": "uint256" } ], "name": "getHash", "outputs": [ { "name": "", "type": "bytes32", "value": "0xad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5" } ], "payable": false, "type": "function" }, { "constant": false, "inputs": [ { "name": "channelId", "type": "bytes32" } ], "name": "close", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "type": "function" }, { "constant": true, "inputs": [ { "name": "sender", "type": "address" }, { "name": "channelId", "type": "bytes32" } ], "name": "canFinishSettle", "outputs": [ { "name": "", "type": "bool", "value": false } ], "payable": false, "type": "function" }, { "constant": false, "inputs": [], "name": "kill", "outputs": [], "payable": false, "type": "function" }, { "constant": false, "inputs": [ { "name": "receiver", "type": "address" }, { "name": "duration", "type": "uint256" }, { "name": "settlementPeriod", "type": "uint256" } ], "name": "createChannel", "outputs": [], "payable": true, "type": "function" }, { "constant": true, "inputs": [ { "name": "sender", "type": "address" }, { "name": "channelId", "type": "bytes32" } ], "name": "canStartSettle", "outputs": [ { "name": "", "type": "bool", "value": true } ], "payable": false, "type": "function" }, { "constant": false, "inputs": [ { "name": "channelId", "type": "bytes32" }, { "name": "payment", "type": "uint256" } ], "name": "settle", "outputs": [], "payable": false, "type": "function" }, { "constant": false, "inputs": [ { "name": "channelId", "type": "bytes32" }, { "name": "payment", "type": "uint256" } ], "name": "startSettle", "outputs": [], "payable": false, "type": "function" }, { "constant": false, "inputs": [ { "name": "channelId", "type": "bytes32" } ], "name": "deposit", "outputs": [], "payable": true, "type": "function" }, { "constant": true, "inputs": [ { "name": "channelId", "type": "bytes32" }, { "name": "payment", "type": "uint256" }, { "name": "v", "type": "uint8" }, { "name": "r", "type": "bytes32" }, { "name": "s", "type": "bytes32" } ], "name": "canClaim", "outputs": [ { "name": "", "type": "bool", "value": false } ], "payable": false, "type": "function" }, { "constant": true, "inputs": [ { "name": "channelId", "type": "bytes32" } ], "name": "isOpenChannel", "outputs": [ { "name": "", "type": "bool", "value": false } ], "payable": false, "type": "function" }, { "constant": false, "inputs": [ { "name": "channelId", "type": "bytes32" }, { "name": "payment", "type": "uint256" } ], "name": "finishSettle", "outputs": [], "payable": false, "type": "function" }, { "constant": true, "inputs": [ { "name": "sender", "type": "address" }, { "name": "channelId", "type": "bytes32" } ], "name": "canDeposit", "outputs": [ { "name": "", "type": "bool", "value": true } ], "payable": false, "type": "function" }, { "inputs": [], "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "sender", "type": "address" }, { "indexed": true, "name": "receiver", "type": "address" }, { "indexed": false, "name": "channelId", "type": "bytes32" } ], "name": "DidCreateChannel", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "channelId", "type": "bytes32" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "DidDeposit", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "channelId", "type": "bytes32" }, { "indexed": false, "name": "payment", "type": "uint256" } ], "name": "DidClaim", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "channelId", "type": "bytes32" }, { "indexed": false, "name": "payment", "type": "uint256" } ], "name": "DidStartSettle", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "channelId", "type": "bytes32" }, { "indexed": false, "name": "payment", "type": "uint256" }, { "indexed": false, "name": "oddValue", "type": "uint256" } ], "name": "DidSettle", "type": "event" } ];

/**
 * Wrapper for the payment channel contract.
 *
 * @param {string} address - Address of the deployed contract.
 * @param {Object[]} abi - Interface of the deployed contract.
 * @constructor
 */
var ChannelContract = function (address, abi) {
    this.contract = web3.eth.contract(abi).at(address);
};

ChannelContract.prototype.getHash = function (channelId, value) {
  return this.contract.getHash(channelId, value);
};

var CONTRACT = new ChannelContract(CONTRACT_ADDRESS, CONTRACT_INTERFACE);

/**
 * Cost of creating a channel.
 * @type {number}
 */
ChannelContract.CREATE_CHANNEL_GAS = 300000;

var DAY_IN_SECONDS = 86400;

/**
 * FIXME Settlement period for the contract.
 * @type {number}
 */
ChannelContract.SETTLEMENT_PERIOD = 2 * DAY_IN_SECONDS;

/**
 * @type {number}
 */
ChannelContract.DURATION = 2 * DAY_IN_SECONDS;

/**
 * Initiate payment channel between `sender` and `receiver`, with initial amount set to `value`.
 * @param sender
 * @param receiver
 * @param value
 * @param callback
 */
ChannelContract.prototype.buildPaymentChannel = function (sender, receiver, value, callback) {
    var self = this;
    console.log("Building payment channel");
    console.log("from " + sender + " to " + receiver + ", initial amount set to " + value);
    var settlementPeriod = ChannelContract.SETTLEMENT_PERIOD;
    var duration = ChannelContract.DURATION;
    var options = {
        from: sender,
        value: value,
        gas: ChannelContract.CREATE_CHANNEL_GAS
    };
    this.contract.createChannel(receiver, duration, settlementPeriod, options);
    var didCreateChannelEvent = this.contract.DidCreateChannel({sender: sender, receiver: receiver});
    console.log("Waiting for the channel to be created on the blockchain: watching for DidCreateChannel event");
    didCreateChannelEvent.watch(function (error, result) {
        var channelId = result.args.channelId;
        console.log("The channel " + channelId + " is created");
        var paymentChannel = new PaymentChannel(sender, receiver, channelId, self, value, 0);
        didCreateChannelEvent.stopWatching();
        console.log("No longer watching for DidCreateChannel event");
        callback(error, paymentChannel);
    });
};

ChannelContract.prototype.claim = function (receiver, channelId, value, v, r, s, callback) {
    var self = this;
    console.log("Claim");
    console.log(channelId);
    console.log(value);
    console.log(parseInt(v));
    console.log(r);
    console.log(s);
    self.contract.claim(channelId, value, parseInt(v), r, s, {from: receiver});
    var didClaimEvent = self.contract.DidClaim({channelId: channelId});
    didClaimEvent.watch(function (error, result) {
        console.log("Claimed " + result.args.value + " from " + result.args.channelId);
        didClaimEvent.stopWatching();
        callback(error, result.args.value);
    });
};

/**
 * The Payment Channel
 * @param {string} sender - Ethereum address of the client.
 * @param {string} receiver - Ethereum address of the server.
 * @param {string} channelId - Identifier of the channel.
 * @param {ChannelContract} contract - Payment channel contract.
 * @param {number} value - Total value of the channel.
 * @param {number} spent - Value sent by {sender} to {receiver}.
 * @constructor
 */
var PaymentChannel = function (sender, receiver, channelId, contract, value, spent) { // FIXME remove contract parameter
    this.sender = sender;
    this.receiver = receiver;
    this.channelId = channelId;
    this.value = value;
    this.spent = spent;
};

/**
 * Sign value transfer.
 * @param {number} value - Transferred value.
 * @returns {{r: string, s: string, v: string}}
 */
PaymentChannel.prototype.sign = function (value) {
    var hash = CONTRACT.getHash(this.channelId, value);
    var signature = web3.eth.sign(this.sender, hash);
    signature = signature.substr(2, signature.length);

    return {
        r: "0x" + signature.substr(0, 64),
        s: "0x" + signature.substr(64, 64),
        v: web3.toHex(web3.toDecimal(signature.substr(128, 2)) + 27)
    }
};

/**
 *
 * @param {Payment} payment
 * @returns {PaymentChannel}
 */
PaymentChannel.fromPayment = function (payment) {
    return new PaymentChannel(payment.sender, payment.receiver, payment.channelId, null, payment.channelValue, payment.value);
};

/**
 * @param {Object} options
 * @constructor
 */
var Payment = function (options) {
    this.channelId = options.channelId;
    this.sender = options.sender;
    this.receiver = options.receiver;
    this.price = options.price;
    this.value = options.value;
    this.channelValue = options.channelValue;
    this.v = options.v;
    this.r = options.r;
    this.s = options.s;
};

/**
 * Build {Payment} based on PaymentChannel and monetary value to send.
 *
 * @param {PaymentChannel} paymentChannel
 * @param {number} price
 * @returns {Payment}
 */
Payment.fromPaymentChannel = function (paymentChannel, price) {
    var value = price + paymentChannel.spent;
    var value = 0;
    var signature = paymentChannel.sign(value);
    var options = {
        channelId: paymentChannel.channelId,
        sender: paymentChannel.sender,
        receiver: paymentChannel.receiver,
        price: price,
        value: value,
        channelValue: paymentChannel.value,
        v: signature.v,
        r: signature.r,
        s: signature.s
    };
    return new Payment(options);
};

var close = function (paymentChannel, callback) {
    console.log("Trying to close PaymentChannel " + paymentChannel.channelId);

    callback(null, 0);
};

module.exports = {
    web3: web3,
    contract: CONTRACT,
    close: close,
    Payment: Payment,
    PaymentChannel: PaymentChannel
};
<commit_msg>Remove debugging statement<commit_after>"use strict";

var Web3 = require('web3');
var request = require('request');

var web3 = new Web3();
web3.setProvider(new Web3.providers.HttpProvider('http://localhost:8545'));

var CONTRACT_ADDRESS = "0xeb67cE7C8723e7adE36b828f7FbBCbA3391D5A55";
var CONTRACT_INTERFACE = [ { "constant": false, "inputs": [ { "name": "channelId", "type": "bytes32" }, { "name": "payment", "type": "uint256" }, { "name": "v", "type": "uint8" }, { "name": "r", "type": "bytes32" }, { "name": "s", "type": "bytes32" } ], "name": "claim", "outputs": [], "payable": false, "type": "function" }, { "constant": true, "inputs": [ { "name": "channelId", "type": "bytes32" }, { "name": "value", "type": "uint256" } ], "name": "getHash", "outputs": [ { "name": "", "type": "bytes32", "value": "0xad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5" } ], "payable": false, "type": "function" }, { "constant": false, "inputs": [ { "name": "channelId", "type": "bytes32" } ], "name": "close", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "type": "function" }, { "constant": true, "inputs": [ { "name": "sender", "type": "address" }, { "name": "channelId", "type": "bytes32" } ], "name": "canFinishSettle", "outputs": [ { "name": "", "type": "bool", "value": false } ], "payable": false, "type": "function" }, { "constant": false, "inputs": [], "name": "kill", "outputs": [], "payable": false, "type": "function" }, { "constant": false, "inputs": [ { "name": "receiver", "type": "address" }, { "name": "duration", "type": "uint256" }, { "name": "settlementPeriod", "type": "uint256" } ], "name": "createChannel", "outputs": [], "payable": true, "type": "function" }, { "constant": true, "inputs": [ { "name": "sender", "type": "address" }, { "name": "channelId", "type": "bytes32" } ], "name": "canStartSettle", "outputs": [ { "name": "", "type": "bool", "value": true } ], "payable": false, "type": "function" }, { "constant": false, "inputs": [ { "name": "channelId", "type": "bytes32" }, { "name": "payment", "type": "uint256" } ], "name": "settle", "outputs": [], "payable": false, "type": "function" }, { "constant": false, "inputs": [ { "name": "channelId", "type": "bytes32" }, { "name": "payment", "type": "uint256" } ], "name": "startSettle", "outputs": [], "payable": false, "type": "function" }, { "constant": false, "inputs": [ { "name": "channelId", "type": "bytes32" } ], "name": "deposit", "outputs": [], "payable": true, "type": "function" }, { "constant": true, "inputs": [ { "name": "channelId", "type": "bytes32" }, { "name": "payment", "type": "uint256" }, { "name": "v", "type": "uint8" }, { "name": "r", "type": "bytes32" }, { "name": "s", "type": "bytes32" } ], "name": "canClaim", "outputs": [ { "name": "", "type": "bool", "value": false } ], "payable": false, "type": "function" }, { "constant": true, "inputs": [ { "name": "channelId", "type": "bytes32" } ], "name": "isOpenChannel", "outputs": [ { "name": "", "type": "bool", "value": false } ], "payable": false, "type": "function" }, { "constant": false, "inputs": [ { "name": "channelId", "type": "bytes32" }, { "name": "payment", "type": "uint256" } ], "name": "finishSettle", "outputs": [], "payable": false, "type": "function" }, { "constant": true, "inputs": [ { "name": "sender", "type": "address" }, { "name": "channelId", "type": "bytes32" } ], "name": "canDeposit", "outputs": [ { "name": "", "type": "bool", "value": true } ], "payable": false, "type": "function" }, { "inputs": [], "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "sender", "type": "address" }, { "indexed": true, "name": "receiver", "type": "address" }, { "indexed": false, "name": "channelId", "type": "bytes32" } ], "name": "DidCreateChannel", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "channelId", "type": "bytes32" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "DidDeposit", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "channelId", "type": "bytes32" }, { "indexed": false, "name": "payment", "type": "uint256" } ], "name": "DidClaim", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "channelId", "type": "bytes32" }, { "indexed": false, "name": "payment", "type": "uint256" } ], "name": "DidStartSettle", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "channelId", "type": "bytes32" }, { "indexed": false, "name": "payment", "type": "uint256" }, { "indexed": false, "name": "oddValue", "type": "uint256" } ], "name": "DidSettle", "type": "event" } ];

/**
 * Wrapper for the payment channel contract.
 *
 * @param {string} address - Address of the deployed contract.
 * @param {Object[]} abi - Interface of the deployed contract.
 * @constructor
 */
var ChannelContract = function (address, abi) {
    this.contract = web3.eth.contract(abi).at(address);
};

ChannelContract.prototype.getHash = function (channelId, value) {
  return this.contract.getHash(channelId, value);
};

var CONTRACT = new ChannelContract(CONTRACT_ADDRESS, CONTRACT_INTERFACE);

/**
 * Cost of creating a channel.
 * @type {number}
 */
ChannelContract.CREATE_CHANNEL_GAS = 300000;

var DAY_IN_SECONDS = 86400;

/**
 * FIXME Settlement period for the contract.
 * @type {number}
 */
ChannelContract.SETTLEMENT_PERIOD = 2 * DAY_IN_SECONDS;

/**
 * @type {number}
 */
ChannelContract.DURATION = 2 * DAY_IN_SECONDS;

/**
 * Initiate payment channel between `sender` and `receiver`, with initial amount set to `value`.
 * @param sender
 * @param receiver
 * @param value
 * @param callback
 */
ChannelContract.prototype.buildPaymentChannel = function (sender, receiver, value, callback) {
    var self = this;
    console.log("Building payment channel");
    console.log("from " + sender + " to " + receiver + ", initial amount set to " + value);
    var settlementPeriod = ChannelContract.SETTLEMENT_PERIOD;
    var duration = ChannelContract.DURATION;
    var options = {
        from: sender,
        value: value,
        gas: ChannelContract.CREATE_CHANNEL_GAS
    };
    this.contract.createChannel(receiver, duration, settlementPeriod, options);
    var didCreateChannelEvent = this.contract.DidCreateChannel({sender: sender, receiver: receiver});
    console.log("Waiting for the channel to be created on the blockchain: watching for DidCreateChannel event");
    didCreateChannelEvent.watch(function (error, result) {
        var channelId = result.args.channelId;
        console.log("The channel " + channelId + " is created");
        var paymentChannel = new PaymentChannel(sender, receiver, channelId, self, value, 0);
        didCreateChannelEvent.stopWatching();
        console.log("No longer watching for DidCreateChannel event");
        callback(error, paymentChannel);
    });
};

ChannelContract.prototype.claim = function (receiver, channelId, value, v, r, s, callback) {
    var self = this;
    console.log("Claim");
    console.log(channelId);
    console.log(value);
    console.log(parseInt(v));
    console.log(r);
    console.log(s);
    self.contract.claim(channelId, value, parseInt(v), r, s, {from: receiver});
    var didClaimEvent = self.contract.DidClaim({channelId: channelId});
    didClaimEvent.watch(function (error, result) {
        console.log("Claimed " + result.args.value + " from " + result.args.channelId);
        didClaimEvent.stopWatching();
        callback(error, result.args.value);
    });
};

/**
 * The Payment Channel
 * @param {string} sender - Ethereum address of the client.
 * @param {string} receiver - Ethereum address of the server.
 * @param {string} channelId - Identifier of the channel.
 * @param {ChannelContract} contract - Payment channel contract.
 * @param {number} value - Total value of the channel.
 * @param {number} spent - Value sent by {sender} to {receiver}.
 * @constructor
 */
var PaymentChannel = function (sender, receiver, channelId, contract, value, spent) { // FIXME remove contract parameter
    this.sender = sender;
    this.receiver = receiver;
    this.channelId = channelId;
    this.value = value;
    this.spent = spent;
};

/**
 * Sign value transfer.
 * @param {number} value - Transferred value.
 * @returns {{r: string, s: string, v: string}}
 */
PaymentChannel.prototype.sign = function (value) {
    var hash = CONTRACT.getHash(this.channelId, value);
    var signature = web3.eth.sign(this.sender, hash);
    signature = signature.substr(2, signature.length);

    return {
        r: "0x" + signature.substr(0, 64),
        s: "0x" + signature.substr(64, 64),
        v: web3.toHex(web3.toDecimal(signature.substr(128, 2)) + 27)
    }
};

/**
 *
 * @param {Payment} payment
 * @returns {PaymentChannel}
 */
PaymentChannel.fromPayment = function (payment) {
    return new PaymentChannel(payment.sender, payment.receiver, payment.channelId, null, payment.channelValue, payment.value);
};

/**
 * @param {Object} options
 * @constructor
 */
var Payment = function (options) {
    this.channelId = options.channelId;
    this.sender = options.sender;
    this.receiver = options.receiver;
    this.price = options.price;
    this.value = options.value;
    this.channelValue = options.channelValue;
    this.v = options.v;
    this.r = options.r;
    this.s = options.s;
};

/**
 * Build {Payment} based on PaymentChannel and monetary value to send.
 *
 * @param {PaymentChannel} paymentChannel
 * @param {number} price
 * @returns {Payment}
 */
Payment.fromPaymentChannel = function (paymentChannel, price) {
    var value = price + paymentChannel.spent; // TICK
    var signature = paymentChannel.sign(value);
    var options = {
        channelId: paymentChannel.channelId,
        sender: paymentChannel.sender,
        receiver: paymentChannel.receiver,
        price: price,
        value: value,
        channelValue: paymentChannel.value,
        v: signature.v,
        r: signature.r,
        s: signature.s
    };
    return new Payment(options);
};

var close = function (paymentChannel, callback) {
    console.log("Trying to close PaymentChannel " + paymentChannel.channelId);

    callback(null, 0);
};

module.exports = {
    web3: web3,
    contract: CONTRACT,
    close: close,
    Payment: Payment,
    PaymentChannel: PaymentChannel
};

{'fraction_non_alphanumeric': 0.20269093021004744, 'fraction_numerical': 0.01931081211886555, 'mean_word_length': 4.424783407718562, 'pattern_counts': {'":': 444, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6133438', 'n_tokens_mistral': 6781, 'n_tokens_neox': 6544, 'n_words': 1750}