-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
236 lines (191 loc) · 6.91 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var Eos = require('./eos-pro/eosjs/src/index');
var eos = Eos({httpEndpoint: 'https://api.eosio.cr/', chainId: "aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906"});
var app = express();
var port = 3000;
//view
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
var fetchUrl = require("fetch").fetchUrl;
// source file is iso-8859-15 but it is converted to utf-8 automatically
let price;
function getPrice() {
setInterval(function(){
fetchUrl("https://api.coinmarketcap.com/v2/ticker/1765/?convert=EUR", function(error, meta, body){
let x = body.toString();
let f = JSON.parse(x);
price = f.data.quotes.USD.price.toString();
});
}, 6000 * 10 * 10);
}
getPrice();
//Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
//Set static path for assets
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res) {
eos.getInfo({}).then(result=> {
let blockNum = result.head_block_num;
let lastBlock = result.last_irreversible_block_num;
let timestamp = result.head_block_time;
let miner = result.head_block_producer;
let cpulimit = result.block_cpu_limit;
res.render('eoswalletpro', {
blockNum: blockNum,
lastBlock: lastBlock,
timestamp: timestamp,
miner: miner,
cpulimit: cpulimit,
price: price
});
}).catch(err=>{res.send({error: err}); res.end()});;
});
app.listen(port, function() {
console.log(`Listening on port ${port}`);
});
app.post('/getkeyaccount', function(req, res, status){
let params = req.body;
eos.getKeyAccounts(params.pubkey).then(result1=>{
let accounts = result1.account_names;
res.send({accounts: accounts});
res.end();
}).catch(err=>{
res.send({e: err});
res.end();
});
});
app.post('/lookupacct', function(req, res, status){
eos.getAccount(req.body.targetAcct).then(result=>{
let account = req.body.targetAcct;
let created = result.created;
let ram = result.ram_quota;
let bandwidth = result.delegated_bandwidth;
let keyreturn = result.permissions[0].required_auth.keys[0].key;
res.send({account: account, created: created, ram: ram, bandwidth: bandwidth, pubkey: keyreturn});
res.end();
}).catch(err=>{res.send(err); res.end();});
});
app.post('/getbalance', function (req, res){
eos.getTableRows({code: 'eosio.token', scope: req.body.targetAcct, table: 'accounts', json: true}).then(result2=>{
res.send(result2.rows);
res.end();
}).catch(err=>{res.send({e: err}); res.end();});
});
app.post('/pubtoacct', function(req, res){
eos.getAccount(req.body.account_target).then(result=>{
let ram_quota = result.ram_quota;
let ram_usage = result.ram_usage;
let bandwidth = result.delegated_bandwidth;
let cpu_limit = result.cpu_limit.available;
let created = result.created;
let account = result.account_name;
let requiredkey = result.permissions[0].required_auth.keys[0].key;
if (result.account_name) {
eos.getTableRows({code: 'eosio.token', scope: req.body.account_target, table: 'accounts', json: true}).then(result2=>{
let balances = result2;
res.send({
returnkey: requiredkey,
account: account,
balances: {balances: balances},
ram_quota: ram_quota,
cpu_limit: cpu_limit,
ram_usage: ram_usage,
bandwidth: bandwidth,
created: created
});
res.end();
}).catch(function(){res.send({error: "error"}); res.end()});;
}
}).catch(function(){res.send({error: "error"}); res.end()});
});
//----------------------- LOGIN WITH PUBLIC KEY ------------------------//
app.post('/login', function(req, res, status){
let params = req.body;
let pubkey = req.body.pubkey
eos.getAccount(params.account).then(result1=>{
let required = result1.permissions[0].required_auth.keys[0].key;
if (req.body.pubkeys === required) {
res.send({login: true, returnkey: required});
res.end();
} else {
res.send({e: "Error - key does not match account permissions"});
res.end();
}
}).catch(err=>{res.send({e: "Error - could not find account"}); res.end();});
});
//----------------------- LOGIN WITH PUBLIC KEY ------------------------//
//This still needs to be implemented safely -------------------------//
//----------------------- CREATE NEW ACCOUNT ------------------------//
let alphabet = "abcdefghijklmnopqurstuvwxyz12345"
app.post('/createaccount', function(req, res, status) {
let key = req.body.pubkey;
let rand = alphabet[Math.floor(Math.random()*alphabet.length)];
eos.newaccount({creator: 'justin', name: rand, owner: key, active: key}).then(result=>{
res.send(result);
res.end();
}).catch(err=>{res.send(err); res.end();});
});
//----------------------- CREATE NEW ACCOUNT ------------------------//
//----------------------- CREATE RAW EOS TRANSACTION ------------------------//
app.post('/transaction', function(req, res, status) {
let params = req.body;
let memo = params.memo;
eos.getAccount(params.to).then(result1=>{
if (params.memo && memo.length < 200) {
memo = params.memo;
} else {
memo = '';
}
if (params.to && params.amount && result1.account_name) {
eos.getAccount(params.from).then(result2=>{
if (result2.cpu_limit.available > 800) {
eos.transfer(params.from, params.to, params.amount, memo, {broadcast: false, sign: false}).then(result=>{
let packedtr = result.transaction;
let packedTr = JSON.stringify(packedtr);
let stringBuf = JSON.stringify(result.buffer);
res.send({buf: stringBuf, packedTr: packedTr});
res.end();
}).catch(err => {
res.send({e: err});
res.end();
});
} else {
res.send({e: "Your account has exceeded its assigned CPU limit. Please try again later."});
res.end();
}
}).catch(err=>{res.send({e: "The account you are trying to send from does not exit"}); res.end();})
} else {
res.send({e: "Error - The account you are trying to send to does not exit"});
res.end();
}
}).catch(err=>{res.send({e: "The account you are trying to send to does not exit"}); res.end();});
});
//----------------------- CREATE RAW EOS TRANSACTION ------------------------//
//----------------------- PUSH SIGNED TRANSACTION ------------------------//
app.post('/pushtransaction', function(req, res) {
if (req.body.sigs) {
let sigver = Eos.modules.ecc.Signature.from(req.body.sigs).toString();
let lasig = [sigver];
let transi = JSON.parse(req.body.packedTr);
let package = {
compression: 'none',
transaction: transi,
signatures: lasig
}
//Pushes tx in correct format
eos.pushTransaction(package).then(result=>{
res.send(result);
res.end();
}).catch(err => {
res.send({e: "Either, you have exceeded your accounts balance, entered the wrong private key, or something else went wrong"});
res.end();
});
} else {
res.send({e: "Invalid signature produced"});
res.end();
}
})
//----------------------- PUSH SIGNED TRANSACTION ------------------------//