-
Notifications
You must be signed in to change notification settings - Fork 1
/
v0.18.4.js
111 lines (90 loc) · 2.82 KB
/
v0.18.4.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
var fs = require("fs");
var path = require('path');
var express = require('express');
var Web3 = require('web3');
var app = express();
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider("http://127.0.0.1:8545"));
//web3.setProvider(new web3.providers.HttpProvider("https://api.myetherapi.com/eth"));
var bodyParser = require('body-parser');
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
app.use(express.static('public'));
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
//res.header('Access-Control-Allow-Credentials', false);
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
};
app.use(allowCrossDomain);
app.get('/getMsg', function(req, res){
res.send(web3.eth.coinbase + " " + web3.eth.blockNumber + " " + web3.eth.accounts);
});
app.get('/getBlock',function(req, res){
var block = web3.eth.getBlock(req.query.id);
res.send(block);
});
app.post('/newAccount', function(req, res){
res.send(web3.personal.newAccount(''));
});
app.get('/getBalance', function(req, res){
var address = req.query.address;
console.log(address);
var balance = web3.eth.getBalance(address);
res.send(balance);
});
app.get('/getPriKeys', function(req, res){
var p = "/Users/chengsilei/ethereum/eth/test/keystore/";
var keys = new Array();
var i = 0;
fs.readdir(p,function(err, files){
if (err) {
console.error(err);
}
files.forEach(function(file) {
filepath = path.join(p,file);
fs.readFile(filepath, function (err, data) {
if (err) {
return console.error(err);
}
keys[i++] = data.toString();
if (i == files.length) {
res.send(keys.toString());
}
});
});
});
});
app.get('/getPriKeyByAddress', function(req, res){
var p = "/Users/chengsilei/ethereum/eth/test/keystore/";
var address = req.query.address;
if (address[0] == '0' && address[1] == 'x') {
address = address.slice(2);
console.log(address);
}
fs.readdir(p,function(err, files){
if (err) {
console.error(err);
}
files.forEach(function(file) {
l = file.split("--");
if (l[l.length-1] == address) {
filepath = path.join(p,file);
fs.readFile(filepath, function (err, data) {
if (err) {
return console.error(err);
}
res.send(data.toString());
});
}
});
});
});
var server = app.listen(8011, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});