-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
executable file
·205 lines (176 loc) · 4.35 KB
/
app.js
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
201
202
203
204
205
/*
* NOTE:
* This app is buggy, and still in alpha.
*
*/
const path = require('path')
const express = require('express')
const exphbs = require('express-handlebars')
const walletRPC = require('bitcoin-core');
const querystring = require('querystring');
const app = express();
const redis = require('ioredis');
const client = redis.createClient({host : 'localhost', port : 6379});
// CONNECT AND AUTH WALLET
const walletSIGT = new walletRPC({
host: 'localhost',
port: 33334,
username: '',
password: ''
})
wallet();
client.on('ready',function() {
console.log("Redis is ready");
});
client.on('error',function() {
console.log("Error in Redis");
});
app.engine('.hbs', exphbs({
defaultLayout: 'main',
extname: '.hbs',
layoutsDir: path.join(__dirname, 'views/layouts')
}))
app.set('view engine', '.hbs')
app.set('views', path.join(__dirname, 'views'))
/*
app.get('/', function(req, res) {
res.render('index', {
})
app.use(express.static(path.join(__dirname + '/public')));
})
*/
var newBlockCount = 0;
var keyNum = 0;
var connections = 0;
var last10 = [];
function wallet() {
console.log('wallet running')
walletSIGT.getBlockCount(function(err,res) {
console.log("RUNNING: @getBlockCount@wallet")
newBlockCount = res;
keyNum = res - 501;
console.log("Block Count: " + res)
checkDB();
})
walletSIGT.getConnectionCount(function(err,res) {
console.log("RUNNING: @getConnectionCount@wallet")
console.log("Connections: " + res)
connections = res;
})
}
function checkDB() {
/* DEV ONLY */
//newBlockCount = 500
console.log("RUNNING: @checkDB")
if (newBlockCount != 0 && newBlockCount != undefined) {
console.log(newBlockCount)
client.get(newBlockCount, function(err, reply) {
if(reply == null) {
getBlocks();
} else {
console.log("Already synced!")
getKey();
}
});
}
}
function getBlocks() {
console.log("RUNNING: @getBlocks")
var i = newBlockCount - 501;
getNextBlock();
function getNextBlock() {
if (newBlockCount != 0 && i < newBlockCount) {
walletSIGT.getBlockHash(i, function(err,res) {
client.set(i, res);
checkID()
})
} else if (i == newBlockCount) {
getKey();
}
}
function checkID() {
i++;
getNextBlock();
}
}
function getKey() {
if (newBlockCount != 0 && keyNum < newBlockCount) {
client.get(keyNum).then(function (result) {
getBlockInfo(keyNum, result);
});
keyNum++;
}
}
function getBlockInfo(i, key) {
console.log("RUNNING: @getBlockInfo " + key)
walletSIGT.getBlock(key, function(err,res) {
res = JSON.stringify(res)
client.set('block'+i, res);
});
if (i == newBlockCount) {
renderBlockInfo();
} else if (i < newBlockCount) {
getKey();
}
}
function renderBlockInfo() {
renderBlockInfo = function(){};
var list10 = [];
var n = newBlockCount - 500;
outer();
function inner() {
client.get('block'+n).then(function (result) {
list10.push(JSON.parse(result))
outer();
});
}
function outer() {
if (n < newBlockCount) {
console.log(n)
n++;
inner();
}
}
var query = null;
app.get('/', function(req, res) {
res.render('index', {
conn: connections,
stuff: list10
})
app.use(express.static(path.join(__dirname + '/public')));
})
app.get('/block', function(req, res) {
query = req.query;
if (query.block != null) {
query.block = parseInt(query.block);
var hash, doBlock;
walletSIGT.getBlockHash(query.block, function(err,res) {
hash = res;
doBlock = 1;
if (doBlock == 1) {
walletSIGT.getBlock(hash, function(err,res) {
renderItem(res);
doBlock = 0;
})
}
});
} else if (query.blockHash != null) {
walletSIGT.getBlock(query.blockHash, function(err,res) {
res.time = new Date(res.time*1000).toLocaleString();
renderItem(res);
});
} else if (query.address != null) {
walletSIGT.getReceivedByAddress(query.address, function(err,res) {
renderItem(res);
});
}
function renderItem(data) {
res.render('block', {
conn: connections,
stuff: data
})
app.use(express.static(path.join(__dirname + '/public')));
}
})
}
app.listen(8080, '0.0.0.0');