{"size": 1891, "ext": "js", "max_stars_repo_path": "logout.js", "max_stars_repo_name": "sorpaas/casper-validator", "max_stars_repo_stars_event_min_datetime": "2018-05-30T10:02:24.000Z", "max_stars_repo_stars_event_max_datetime": "2018-05-30T10:02:24.000Z", "max_issues_repo_path": "logout.js", "max_issues_repo_name": "sorpaas/casper-validator", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "logout.js", "max_forks_repo_name": "sorpaas/casper-validator", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 29.0923076923, "max_line_length": 212, "alphanum_fraction": 0.6710735061}
const SIMPLE_CASPER_ABI = require("./simple_casper.json");

const Web3 = require("web3");
const RLP = require("rlp");
const secp256k1 = require('secp256k1');
const web3 = new Web3("ws://localhost:8546");

let ADDRESS = process.env["ADDRESS"].toLowerCase();
if (ADDRESS.startsWith("0x")) {
  ADDRESS = ADDRESS.substring(2);
}

let PRIVATE_KEY = process.env["PRIVATE_KEY"].toLowerCase();
if (PRIVATE_KEY.startsWith("0x")) {
  PRIVATE_KEY = PRIVATE_KEY.substring(2);
}

const VALIDATOR_INDEX = parseInt(process.env["VALIDATOR_INDEX"]);

const casper = new web3.eth.Contract(
  SIMPLE_CASPER_ABI,
  "0x0000000000000000000000000000000000000040"
);

const logout = async () => {
  console.log(`\nTrying to log out ...`);

  const validatorIndex = VALIDATOR_INDEX;
  console.log(`Validator index: ${validatorIndex}`);

  const currentEpoch = parseInt(await casper.methods.current_epoch().call());
  console.log(`Current epoch: ${currentEpoch}`);

  const rawMessage = RLP.encode([
    validatorIndex,
    currentEpoch
  ]);

  const hashed = web3.utils.sha3("0x" + rawMessage.toString("hex"));
  const signedHashed = secp256k1.sign(
    Buffer.from(hashed.substring(2), "hex"),
    Buffer.from(PRIVATE_KEY, "hex")
  );

  const signature = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 27 + signedHashed.recovery]).toString("hex") + signedHashed.signature.toString("hex");

  console.log(`Signature length: ${Buffer.from(signature, "hex").length}`);
  console.log(`Hashed: ${hashed}`);
  console.log(`Signature: ${"0x" + signature}`);

  const message = "0x" + RLP.encode([
    validatorIndex,
    currentEpoch,
    Buffer.from(signature, "hex")
  ]).toString("hex");

  console.log(await casper.methods.logout(message).send({
    from: `0x${ADDRESS}`,
  }));
};

logout()
  .then(v => console.log(v))
  .catch(err => console.error(err))
