-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.js
218 lines (184 loc) · 7.04 KB
/
api.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
206
207
208
209
210
211
212
213
214
215
216
217
218
var web3 = require('web3');
var express = require('express');
var log4js = require('log4js');
var async = require('async');
var app = express();
var bodyParser = require('body-parser');
var request = require('request');
var redis = require('redis');
var interfaces = require('./lib/interfaces');
var utils = require('./lib/utils');
var NameRegJSON = require('./contracts').NameReg;
var RepJSON = require('./contracts').Rep_Trimmed;
// ENV configs
var redisHost = process.env.REDIS_HOST || 'localhost';
var redisPort = process.env.REDIS_PORT || 6379;
var redisPass = process.env.REDIS_PASS || '';
var port = process.env.PORT || 8000;
//var NOCACHE = process.env.NOCACHE || false;
var NOCACHE = true;
// TODO: Change these defaults to something other than SHA3(1) :) (also probably should change the
var adminKey = new Buffer(process.env.KEY || '044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d', 'hex');
var adminAddr = process.env.ADDR || '82a978b3f5962a5b0957d9ee9eef472ee55b42f1';
var nameRegAddr = process.env.NAMREG || NameRegJSON.addr;
console.log("nameReg", nameRegAddr);
var rpcport = process.env.RPCPORT || 8080;
var rpchost = process.env.RPCHOST || 'localhost';
var logFile = process.env.LOGFILE || 'logs/api.log';
var redisClient = redis.createClient(redisPort, redisHost);
if (redisPass.length)
redisClient.auth(redisPass, function() {
console.log("redis client authed");
});
var ethRpcUrl = 'http://' + rpchost + ':' + rpcport;
web3.setProvider(new web3.providers.HttpProvider(ethRpcUrl));
log4js.configure({
appenders: [
{type: 'console'},
{type: 'file', filename: logFile, maxLogSize: 20480}
]
});
var logger = log4js.getLogger();
var debugLogs = function(req, res, next) {
var input = Object.keys(req.params).length ? req.params : req.body;
logger.debug(req.url, JSON.stringify(input), req.connection.remoteAddress);
next();
}
var cached = function(req, res, next) {
if (req.method !== 'GET') return next();
if (NOCACHE) return next();
var cache = {success: false, data: {}};
redisClient.get(req.url, function(err, reply) {
if (!err && reply) cache = {success: true, data: reply};
req.cache = cache;
console.log("cache", req.cache);
next();
});
}
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(debugLogs);
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
var NameReg = web3.eth.contract(NameRegJSON.abi);
var nameReg = NameReg.at(NameRegJSON.addr);
var Rep = web3.eth.contract(RepJSON.abi);
var rep = Rep.at(RepJSON.addr);
var nr = new interfaces.NameReg(NameRegJSON.addr, NameRegJSON.abi, web3, ethRpcUrl, adminAddr, adminKey);
app.listen(port);
app.post('/inject', function(req, res) {
var tx = req.body.rawtx;
if (!tx || !tx.length || tx == '0x') return res.end("False");
if (tx.slice(0,2) == '0x') tx = tx.slice(2);
var msg = {
'jsonrpc': '2.0',
'method': 'eth_injectTransaction',
'params': ['0x' + tx],
'id': 0
};
request.post(ethRpcUrl, {form: JSON.stringify(msg)}, function(err, response, body) {
if (err) return res.json({success: false, data: err});
res.json({success: true, data: body});
});
});
// Register a new account on contract
app.post('/register', function(req, res) {
nr.register(req.body.address, req.body.name, req.body.epk, req.body.email, function(err, response) {
rep.unlockRep.sendTransaction(req.body.address, "init", {from: adminAddr, gasPrice: 100000000000000, gas: 1000000});
res.json({success: !err, data: err ? err : response});
});
});
app.get('/addr/:username', function(req, res) {
var name = req.params.username;
try {
var addr = nameReg.addrByName(req.params.username);
res.json({success: true, data: addr});
} catch (e) {
res.json({success: false, data: e});
}
});
app.get('/unlock/:username', function(req, res) {
var name = req.params.username;
try {
var addr = nameReg.addrByName(req.params.username);
rep.unlockRep.sendTransaction(addr, "init", {from: adminAddr, gasPrice: 100000000000000, gas: 1000000});
res.json({success: true, data: addr});
} catch (e) {
res.json({success: false, data: e});
}
});
app.get('/available/:username', function(req, res) {
var err = null;
var result = false;
try {
result = web3.toDecimal(nameReg.addrByName(req.params.username)) == 0;
} catch (e) {
err = e;
}
res.json({success: !err, data: err ? err : result});
});
app.get('/profile/name/:username/:field', cached, function(req, res) {
nr.getProfileField(nameReg.addrByName(req.params.username), function(err, profileField) {
if (err) res.json({success: false, data: err});
else {
redisClient.set(req.url, profileField);
res.json({success: true, data: profileField});
}
});
});
app.get('/profile/address/:address/:field', cached, function(req, res) {
nr.getProfileField(req.params.address, req.params.field, function(err, profileField) {
if (err) res.json({success: false, data: err});
else {
redisClient.set(req.url, profileField);
res.json({success: true, data: profileField});
}
});
});
app.get('/profile/name/:username', cached, function(req, res) {
if (req.cache && req.cache.success) return res.json(req.cache);
nr.getProfile(nameReg.addrByName(req.params.username), function(err, profile) {
if (err) res.json({success: false, data: err});
else {
redisClient.set(req.url, JSON.stringify(profile));
redisClient.get(profile.addr + '.id', function(err, userid) {
if (!err) profile.id = userid;
res.json({success: true, data: profile});
});
}
});
});
app.get('/profile/address/:addr', cached, function(req, res) {
nr.getProfile(req.params.addr, function(err, profile) {
if (err) res.json({success: false, data: err});
else {
redisClient.set(req.url, JSON.stringify(profile));
res.json({success: true, data: profile});
}
});
});
app.get('/log/:txhash', function(req, res) {
redisClient.get(utils.formatHex(req.params.txhash), function(err, reply) {
res.json({success: !err, data: err ? err : reply});
});
});
app.get('/tx/:txhash', cached, function(req, res) {
var txhash = req.params.txhash;
if (!req.cache.success)
web3.eth.getTransaction(txhash, function(err, res) {
if (err) res.json({success: false, data: err});
else {
redisClient.set(txhash, res);
res.json({success: true, data: res});
}
});
else res.json(req.cache);
});
app.get('/nonce/:address', function(req, res) {
web3.eth.getTransactionCount(req.params.address, function(err, nonce) {
res.json({success: !err, data: nonce});
});
});