-
Notifications
You must be signed in to change notification settings - Fork 1
/
1.0.0-beta.26.js
144 lines (121 loc) · 3.23 KB
/
1.0.0-beta.26.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
var Web3 = require('web3');
var web3 = new Web3('http://localhost:8545');
var fs = require("fs");
var path = require('path');
var express = require('express');
var app = express();
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);
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);
});
app.get('/getBlockNumber', function(req, res){
web3.eth.getBlockNumber(function(err,data) {
if (err) {
console.error(err);
}
console.log(data);
res.send(String(data));
});
});
app.get('/getBlock',function(req, res){
web3.eth.getBlock(req.query.id, function(err,data) {
if (err) {
console.error(err);
}
console.log(data);
res.send(data);
});
});
app.get('/getBalance', function(req, res){
var address = req.query.address;
console.log(address);
web3.eth.getBalance(address, function(err,data) {
if (err) {
console.error(err);
}
console.log(data);
res.send(data);
});
});
app.post('/newAccount', function(req, res){
web3.eth.personal.newAccount('', function(err,data) {
if (err) {
console.error(err);
}
console.log(data);
res.send(data);
});
});
app.post('/importRawKey', function(req, res){
var pri = req.body.pri;
var pwd = req.body.pwd;
web3.eth.personal.importRawKey(pri, pwd, function(err,data) {
if (err) {
console.error(err);
}
console.log(data);
res.send(data);
});
});
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());
});
}
});
});
});