From 7b14672b1106cbcfe87ed41e6a258ec62c9bb64a Mon Sep 17 00:00:00 2001 From: Satinder Grewal Date: Tue, 24 Jan 2017 11:26:28 +1300 Subject: [PATCH] some updates to test fetchparams.js code --- routes/fetchparams.js | 229 +++++++++++++++++++++++++++++++++++------- 1 file changed, 194 insertions(+), 35 deletions(-) diff --git a/routes/fetchparams.js b/routes/fetchparams.js index 6be77a8..77b8e47 100644 --- a/routes/fetchparams.js +++ b/routes/fetchparams.js @@ -1,3 +1,5 @@ +var app = require('http').createServer(handler) +var io = require('socket.io')(app); var fs = require('fs'); var request = require('request'); var progress = require('request-progress'); @@ -5,46 +7,203 @@ const path = require('path') const url = require('url') const os = require('os') const sha256 = require('sha256') +const crypto = require('crypto') Promise = require('bluebird'); +app.listen(3000); +function handler (req, res) { + fs.readFile(__dirname + '/index.html', + function (err, data) { + if (err) { + res.writeHead(500); + return res.end('Error loading index.html'); + } + res.writeHead(200); + res.end(data); + }); +} -PARAMS_DIR="$HOME/Library/Application Support/ZcashParams" +if (os.platform() === 'darwin') { + var PARAMS_DIR = process.env.HOME + '/Library/Application Support/ZcashParams' +} +if (os.platform() === 'linux') { + var PARAMS_DIR = process.env.HOME + '/.zcash-params' +} -SPROUT_PKEY_NAME='sprout-proving.key' -SPROUT_PKEY_NAME_HASH='8bc20a7f013b2b58970cddd2e7ea028975c88ae7ceb9259a5344a16bc2c0eef7' -SPROUT_VKEY_NAME='sprout-verifying.key' -SPROUT_VKEY_NAME_HASH='4bd498dae0aacfd8e98dc306338d017d9c08dd0918ead18172bd0aec2fc5df82' -SPROUT_PKEY_URL="https://z.cash/downloads/$SPROUT_PKEY_NAME" -SPROUT_VKEY_URL="https://z.cash/downloads/$SPROUT_VKEY_NAME" -// The options argument is optional so you can omit it -progress(request('https://az412801.vo.msecnd.net/vhd/VMBuild_20141027/VirtualBox/IE11/Windows/IE11.Win8.1.For.Windows.VirtualBox.zip'), { - // throttle: 2000, // Throttle the progress event to 2000ms, defaults to 1000ms - // delay: 1000, // Only start to emit after 1000ms delay, defaults to 0ms - // lengthHeader: 'x-transfer-length' // Length header to use, defaults to content-length -}) -.on('progress', function (state) { - // The state is an object that looks like this: - // { - // percent: 0.5, // Overall percent (between 0 to 1) - // speed: 554732, // The download speed in bytes/sec - // size: { - // total: 90044871, // The total payload size in bytes - // transferred: 27610959 // The transferred payload size in bytes - // }, - // time: { - // elapsed: 36.235, // The total elapsed seconds since the start (3 decimals) - // remaining: 81.403 // The remaining seconds to finish (3 decimals) - // } - // } - console.log('progress', state); -}) -.on('error', function (err) { - // Do something with err -}) -.on('end', function () { - // Do something after request finishes +//PARAMS_DIR="$HOME/Library/Application Support/ZcashParams" + +var SPROUT_FILES_DATA = [{"file":"sprout-proving.key","hash":"8bc20a7f013b2b58970cddd2e7ea028975c88ae7ceb9259a5344a16bc2c0eef7"},{"file":"sprout-verifying.key","hash":"4bd498dae0aacfd8e98dc306338d017d9c08dd0918ead18172bd0aec2fc5df82"}] +var SPROUT_DL_URL="https://z.cash/downloads/" + +//console.log(SPROUT_FILES_DATA); + +SPROUT_FILES_DATA.forEach(function (value, index) { + //console.log(index); + //console.log(value); + //console.log(value.file); + //console.log(value.hash); + + fs.exists(value.file, function(exists) { + if (exists) { + console.log(value.file + ' already exists at location.'); + var tmphash + + var fd = fs.createReadStream(value.file); + var hash = crypto.createHash('sha256') + hash.setEncoding('hex'); + + fd.on('end', function() { + hash.end(); + console.log('hash is: ') + console.log(hash.read()); // the desired sha1sum + console.log(value.hash) + tmphash = hash.read(); + if (hash.read() === value.hash) { + console.log('File SHA256 sum matches.') + } else { + console.log('File SHA256 sum does not match.') + } + }); + // read all file and pipe it (write it) to the hash object + fd.pipe(hash); + + } else { + var DLFile = function() { + + return new Promise(function(resolve, reject) { + + console.log('file not there.'); + + progress(request(SPROUT_DL_URL+value.file), {}) + .on('progress', function (state) { + console.log('progress', state); + }) + .on('error', function (err) { + console.log(err); + }) + .on('end', function () { + // Do something after request finishes + console.log('download finished.') + var result = 'File ==> ' + value.file + ': DOWNLOADED' + }) + .pipe(fs.createWriteStream(value.file)); + + console.log(result) + resolve(result); + }) + } + + + var CheckFileSHA = function() { + + return new Promise(function(resolve, reject) { + var fd = fs.createReadStream(value.file); + var hash = crypto.createHash('sha256') + hash.setEncoding('hex'); + + fd.on('end', function() { + hash.end(); + console.log('hash is: ') + console.log(hash.read()); // the desired sha1sum + console.log(value.hash) + if (hash.read() === value.hash) { + console.log('File SHA256 sum matches.') + } else { + console.log('File SHA256 sum does not match.') + } + }); + // read all file and pipe it (write it) to the hash object + fd.pipe(hash); + + var result = 'SHA256 SUM Check: DONE' + + console.log(result) + resolve(result); + }) + } + + + DLFile() + .then(function(result) { + return CheckFileSHA(); + }) + } + }); + +}); + + +function CheckSHASum(file, hashstr) { + console.log(hashstr) + var shasum + + // the file you want to get the hash + + + if (shasum === hashstr ) { + return true + } else return false +} + + +/*var CheckFileExists = function() { + + return new Promise(function(resolve, reject) { + + if (path.existsSync('foo.txt')) {} + var result = 'Connecting To Pm2: done' + + console.log(result) + resolve(result); + }) +} + +var DLFile = function() { + + return new Promise(function(resolve, reject) { + var result = 'Killing Pm2: done' + + setTimeout(function() { + console.log(result) + resolve(result); + }, 2000) + }) +} + +var CheckSHASum = function() { + + return new Promise(function(resolve, reject) { + var result = 'Hiding Main Window: done' + + console.log(result) + resolve(result); + }) +} + +var MoveFile = function() { + + return new Promise(function(resolve, reject) { + var result = 'Quiting App: done' + + console.log(result) + resolve(result); + }) +} + +ConnectToPm2() +.then(function(result) { + return KillPm2(); }) -.pipe(fs.createWriteStream('IE11.Win8.1.For.Windows.VirtualBox.zip')); \ No newline at end of file +.then(HideMainWindow) +.then(QuitApp) +*/ + + + + + + +