-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.js
More file actions
200 lines (181 loc) · 5.59 KB
/
Copy pathapp.js
File metadata and controls
200 lines (181 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
var fs = require('fs');
var express = require('express');
var mongoose = require('mongoose');
var Image = require('./models/ImageModel');
var multer = require('multer');
var crypto = require('crypto');
var mime = require('mime');
var keygen = require('./keygen');
var exphbs = require('express-handlebars');
var send_op_return_tx = require('./op_return_tx');
var io;
var namespace;
var files_dir = './static/files';
var tmp_dir = './static/tmp';
if (!fs.existsSync(files_dir)){
fs.mkdirSync(files_dir);
}
if (!fs.existsSync(tmp_dir)){
fs.mkdirSync(tmp_dir);
}
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/bitcoin_timestamping', (err, connection) => {
if (err)
console.log(err);
else
console.log("Connected to mongodb...");
});
var config = require('./config');
var poll = require('./poll');
var poll_interval = 5000;
var current_filename = "";
var host = "api.blockcypher.com";
var path = "/v1/btc/test3";
var app = express();
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/views');
app.engine('handlebars', exphbs({ defaultLayout: 'layout' }));
app.set('view engine', 'handlebars');
function fileHash(filename, algorithm = 'sha256') {
return new Promise((resolve, reject) => {
let shasum = crypto.createHash(algorithm);
try {
let file = fs.ReadStream(filename);
file.on('data', function (data) {
shasum.update(data);
})
file.on('end', function () {
const hash = shasum.digest('hex');
namespace = hash.toString().substring(0, 6);
console.log("File hash = ", hash);
return resolve(hash);
})
} catch (error) {
return reject('Cannot hash file...');
}
});
}
function saveFileToDb(filename) {
return new Promise((resolve, reject) => {
var file_path = __dirname + '/static/files/' + filename;
var imageData = fs.readFileSync(file_path);
fileHash(file_path).then((hash) => {
const image = new Image({
filename: filename,
data: imageData,
hash: hash,
txhash: "",
date: new Date()
});
image.save().then(img => {
console.log("Saved image to MongoDB ");
resolve(hash);
fs.unlink(file_path, (err) => {
if (err)
console.log("Error deleting original file ", err);
});
}).catch(err => {
console.log(err);
throw err;
});
});
});
}
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './static/files/')
},
filename: function (req, file, cb) {
crypto.pseudoRandomBytes(16, function (err, raw) {
var filename = raw.toString('hex') + Date.now() + '.' + mime.extension(file.mimetype);
current_filename = filename;
console.log("Filename = ", filename);
cb(null, filename);
});
}
});
var upload = multer({ storage: storage });
app.get('/', (req, res) => {
res.render('index');
});
app.post('/upload-file', upload.single('file-to-upload'), (req, res) => {
var address = keygen.getDerivedAddress();
address.then((address_obj) => {
let address = address_obj.address;
let index = address_obj.index;
console.log("Received Address = " + address);
res.redirect('/pay?address=' + address + '&amount=' + config.amount + '&filename=' + current_filename + '&index=' + index);
});
});
app.get('/get-file', (req, res) => {
let tx_hash = req.query.txhash;
console.log("Finding image with txhash = ", tx_hash);
Image.findOne({txhash: tx_hash}, (err, image) => {
if(err)
{
console.log("Error retrieving file ", err);
}
else if(image != null)
{
fs.writeFileSync(__dirname + '/static/tmp/' + image.filename, image.data);
fs.readFile(__dirname + '/static/tmp/' + image.filename, function(err, data) {
res.writeHead(200, {'Content-Type': 'image/jpeg'});
res.end(data);
});
}
else {
res.send("Invalid hash!");
}
});
});
app.get('/pay', (req, res) => {
var address = req.query.address;
var amount = req.query.amount;
var filename = req.query.filename;
var index = req.query.index;
var qrURL = "https://chart.googleapis.com/chart?chs=200x200&chld=L|2&cht=qr&chl=bitcoin:" + address + "?amount=" + amount + "%26label=btc_timestamp";
let requestObject = {
host: host,
path: path + '/addrs/' + address + '/full', // test with mj9YseuxoGcGw8geYVC8RgPRVDBextAZky
method: 'GET'
};
console.log("Starting polling interval = ", config.interval);
let namespace_connection = io.of('/test');
poll_interval = setInterval(() => {
let poll_result = poll(requestObject);
poll_result.then((result) => {
console.log("Poll result = ", JSON.stringify(result));
namespace_connection.emit('conf', result);
if(result != 0 && result.tx[0] != undefined)
{
if(result.tx[0].confirmations > 0) {
clearInterval(poll_interval);
var hash = saveFileToDb(filename);
hash.then((file_hash) => {
send_op_return_tx.sendTx(file_hash, index).then((tx_hash) => {
console.log("Transaction hash = ", tx_hash);
namespace_connection.emit('data_hash', {data_bc_hash: tx_hash});
Image.findOneAndUpdate({hash: file_hash}, {txhash: tx_hash, date: new Date()}, (err, updatedDoc) => {
if(err)
console.log(err);
else
console.log("Updated document with tx hash! ", updatedDoc);
});
});
});
}
}
});
}, config.interval);
namespace_connection.on('connection', (socket) => {
socket.on('disconnect', () => {
console.log("A client disconnected from namespace with socket id = " + socket.id);
clearInterval(poll_interval);
});
});
res.render('pay', { qr: qrURL, amount: config.amount, address: address, ns: namespace });
});
var server = app.listen(3000, function(){
console.log('Listening on port 3000');
});
io = require('socket.io')(server);