http: refactor route handlers.
This commit is contained in:
parent
bffc225179
commit
7a1e539700
@ -656,25 +656,34 @@ HTTPServer.prototype._init = function _init() {
|
||||
|
||||
// UTXO by address
|
||||
this.get('/coin/address/:address', co(function* (req, res) {
|
||||
var coins;
|
||||
var address = req.options.address;
|
||||
var result = [];
|
||||
var i, coins, coin;
|
||||
|
||||
enforce(req.options.address, 'Address is required.');
|
||||
enforce(!this.chain.options.spv, 'Cannot get coins in SPV mode.');
|
||||
|
||||
coins = yield this.node.getCoinsByAddress(req.options.address);
|
||||
coins = yield this.node.getCoinsByAddress(address);
|
||||
|
||||
res.send(200, coins.map(function(coin) {
|
||||
return coin.getJSON(this.network);
|
||||
}, this));
|
||||
for (i = 0; i < coins.length; i++) {
|
||||
coin = coins[i];
|
||||
result.push(coin.getJSON(this.network));
|
||||
}
|
||||
|
||||
res.send(200, result);
|
||||
}));
|
||||
|
||||
// UTXO by id
|
||||
this.get('/coin/:hash/:index', co(function* (req, res) {
|
||||
var hash = req.options.hash;
|
||||
var index = req.options.index;
|
||||
var coin;
|
||||
|
||||
enforce(req.options.hash, 'Hash is required.');
|
||||
enforce(req.options.index != null, 'Index is required.');
|
||||
enforce(!this.chain.options.spv, 'Cannot get coins in SPV mode.');
|
||||
|
||||
coin = yield this.node.getCoin(req.options.hash, req.options.index);
|
||||
coin = yield this.node.getCoin(hash, index);
|
||||
|
||||
if (!coin) {
|
||||
res.send(404);
|
||||
@ -686,57 +695,81 @@ HTTPServer.prototype._init = function _init() {
|
||||
|
||||
// Bulk read UTXOs
|
||||
this.post('/coin/address', co(function* (req, res) {
|
||||
var coins;
|
||||
var address = req.options.address;
|
||||
var result = [];
|
||||
var i, coins, coin;
|
||||
|
||||
enforce(req.options.address, 'Address is required.');
|
||||
enforce(!this.chain.options.spv, 'Cannot get coins in SPV mode.');
|
||||
|
||||
coins = yield this.node.getCoinsByAddress(req.options.address);
|
||||
coins = yield this.node.getCoinsByAddress(address);
|
||||
|
||||
res.send(200, coins.map(function(coin) {
|
||||
return coin.getJSON(this.network);
|
||||
}, this));
|
||||
for (i = 0; i < coins.length; i++) {
|
||||
coin = coins[i];
|
||||
result.push(coin.getJSON(this.network));
|
||||
}
|
||||
|
||||
res.send(200, result);
|
||||
}));
|
||||
|
||||
// TX by hash
|
||||
this.get('/tx/:hash', co(function* (req, res) {
|
||||
var tx;
|
||||
var hash = req.options.hash;
|
||||
var meta, view;
|
||||
|
||||
enforce(req.options.hash, 'Hash is required.');
|
||||
enforce(!this.chain.options.spv, 'Cannot get TX in SPV mode.');
|
||||
|
||||
tx = yield this.node.getMeta(req.options.hash);
|
||||
meta = yield this.node.getMeta(hash);
|
||||
|
||||
if (!tx) {
|
||||
if (!meta) {
|
||||
res.send(404);
|
||||
return;
|
||||
}
|
||||
|
||||
res.send(200, tx.getJSON(this.network));
|
||||
view = yield this.node.getMetaView(meta);
|
||||
|
||||
res.send(200, meta.getJSON(this.network, view));
|
||||
}));
|
||||
|
||||
// TX by address
|
||||
this.get('/tx/address/:address', co(function* (req, res) {
|
||||
var txs;
|
||||
var address = req.options.address;
|
||||
var result = [];
|
||||
var i, metas, meta, view;
|
||||
|
||||
enforce(req.options.address, 'Address is required.');
|
||||
enforce(!this.chain.options.spv, 'Cannot get TX in SPV mode.');
|
||||
|
||||
txs = yield this.node.getMetaByAddress(req.options.address);
|
||||
metas = yield this.node.getMetaByAddress(address);
|
||||
|
||||
res.send(200, txs.map(function(tx) {
|
||||
return tx.getJSON(this.network);
|
||||
}, this));
|
||||
for (i = 0; i < metas.length; i++) {
|
||||
meta = metas[i];
|
||||
view = yield this.node.getMetaView(meta);
|
||||
result.push(meta.getJSON(this.network, view));
|
||||
}
|
||||
|
||||
res.send(200, result);
|
||||
}));
|
||||
|
||||
// Bulk read TXs
|
||||
this.post('/tx/address', co(function* (req, res) {
|
||||
var txs;
|
||||
var address = req.options.address;
|
||||
var result = [];
|
||||
var i, metas, meta, view;
|
||||
|
||||
enforce(req.options.address, 'Address is required.');
|
||||
enforce(!this.chain.options.spv, 'Cannot get TX in SPV mode.');
|
||||
|
||||
txs = yield this.node.getMetaByAddress(req.options.address);
|
||||
metas = yield this.node.getMetaByAddress(address);
|
||||
|
||||
res.send(200, txs.map(function(tx) {
|
||||
return tx.getJSON(this.network);
|
||||
}, this));
|
||||
for (i = 0; i < metas.length; i++) {
|
||||
meta = metas[i];
|
||||
view = yield this.node.getMetaView(meta);
|
||||
result.push(meta.getJSON(this.network, view));
|
||||
}
|
||||
|
||||
res.send(200, result);
|
||||
}));
|
||||
|
||||
// Block by hash/height
|
||||
@ -745,6 +778,7 @@ HTTPServer.prototype._init = function _init() {
|
||||
var block, view, height;
|
||||
|
||||
enforce(hash != null, 'Hash or height required.');
|
||||
enforce(!this.chain.options.spv, 'Cannot get block in SPV mode.');
|
||||
|
||||
block = yield this.chain.db.getBlock(hash);
|
||||
|
||||
@ -767,18 +801,19 @@ HTTPServer.prototype._init = function _init() {
|
||||
|
||||
// Mempool snapshot
|
||||
this.get('/mempool', co(function* (req, res) {
|
||||
var txs;
|
||||
var result = [];
|
||||
var i, hash, hashes;
|
||||
|
||||
if (!this.mempool) {
|
||||
res.send(500, { error: 'No mempool available.' });
|
||||
return;
|
||||
enforce(this.mempool, 'No mempool available.');
|
||||
|
||||
hashes = this.mempool.getSnapshot();
|
||||
|
||||
for (i = 0; i < hashes.length; i++) {
|
||||
hash = hashes[i];
|
||||
result.push(util.revHex(hash));
|
||||
}
|
||||
|
||||
txs = this.mempool.getHistory();
|
||||
|
||||
res.send(200, txs.map(function(tx) {
|
||||
return tx.getJSON(this.network);
|
||||
}, this));
|
||||
res.send(200, result);
|
||||
}));
|
||||
|
||||
// Broadcast TX
|
||||
@ -1138,20 +1173,31 @@ HTTPServer.prototype._init = function _init() {
|
||||
var options = req.options;
|
||||
var acct = options.name || options.account;
|
||||
var coins = yield req.wallet.getCoins(acct);
|
||||
var result = [];
|
||||
var i, coin;
|
||||
|
||||
sortCoins(coins);
|
||||
|
||||
res.send(200, coins.map(function(coin) {
|
||||
return coin.getJSON(this.network);
|
||||
}, this));
|
||||
for (i = 0; i < coins.length; i++) {
|
||||
coin = coins[i];
|
||||
result.push(coin.getJSON(this.network));
|
||||
}
|
||||
|
||||
res.send(200, result);
|
||||
}));
|
||||
|
||||
// Locked coins
|
||||
this.get('/wallet/:id/coin/locked', co(function* (req, res) {
|
||||
var locked = this.wallet.getLocked();
|
||||
res.send(200, locked.map(function(outpoint) {
|
||||
return outpoint.toJSON();
|
||||
}));
|
||||
var result = [];
|
||||
var i, outpoint;
|
||||
|
||||
for (i = 0; i < locked.length; i++) {
|
||||
outpoint = locked[i];
|
||||
result.push(outpoint.toJSON());
|
||||
}
|
||||
|
||||
res.send(200, result);
|
||||
}));
|
||||
|
||||
// Lock coin
|
||||
@ -1204,15 +1250,19 @@ HTTPServer.prototype._init = function _init() {
|
||||
var options = req.options;
|
||||
var acct = options.name || options.account;
|
||||
var txs = yield req.wallet.getHistory(acct);
|
||||
var details;
|
||||
var result = [];
|
||||
var i, details, item;
|
||||
|
||||
sortTX(txs);
|
||||
|
||||
details = yield req.wallet.toDetails(txs);
|
||||
|
||||
res.send(200, details.map(function(tx) {
|
||||
return tx.toJSON();
|
||||
}));
|
||||
for (i = 0; i < details.length; i++) {
|
||||
item = details[i];
|
||||
result.push(item.toJSON());
|
||||
}
|
||||
|
||||
res.send(200, result);
|
||||
}));
|
||||
|
||||
// Wallet Pending TXs
|
||||
@ -1220,15 +1270,19 @@ HTTPServer.prototype._init = function _init() {
|
||||
var options = req.options;
|
||||
var acct = options.name || options.account;
|
||||
var txs = yield req.wallet.getPending(acct);
|
||||
var details;
|
||||
var result = [];
|
||||
var i, details, item;
|
||||
|
||||
sortTX(txs);
|
||||
|
||||
details = yield req.wallet.toDetails(txs);
|
||||
|
||||
res.send(200, details.map(function(tx) {
|
||||
return tx.toJSON();
|
||||
}));
|
||||
for (i = 0; i < details.length; i++) {
|
||||
item = details[i];
|
||||
result.push(item.toJSON());
|
||||
}
|
||||
|
||||
res.send(200, result);
|
||||
}));
|
||||
|
||||
// Wallet TXs within time range
|
||||
@ -1237,9 +1291,15 @@ HTTPServer.prototype._init = function _init() {
|
||||
var acct = options.name || options.account;
|
||||
var txs = yield req.wallet.getRange(acct, options);
|
||||
var details = yield req.wallet.toDetails(txs);
|
||||
res.send(200, details.map(function(tx) {
|
||||
return tx.toJSON();
|
||||
}));
|
||||
var result = [];
|
||||
var i, item;
|
||||
|
||||
for (i = 0; i < details.length; i++) {
|
||||
item = details[i];
|
||||
result.push(item.toJSON());
|
||||
}
|
||||
|
||||
res.send(200, result);
|
||||
}));
|
||||
|
||||
// Last Wallet TXs
|
||||
@ -1249,9 +1309,15 @@ HTTPServer.prototype._init = function _init() {
|
||||
var limit = options.limit;
|
||||
var txs = yield req.wallet.getLast(acct, limit);
|
||||
var details = yield req.wallet.toDetails(txs);
|
||||
res.send(200, details.map(function(tx) {
|
||||
return tx.toJSON();
|
||||
}));
|
||||
var result = [];
|
||||
var i, item;
|
||||
|
||||
for (i = 0; i < details.length; i++) {
|
||||
item = details[i];
|
||||
result.push(item.toJSON());
|
||||
}
|
||||
|
||||
res.send(200, result);
|
||||
}));
|
||||
|
||||
// Wallet TX
|
||||
@ -1584,11 +1650,16 @@ HTTPServer.prototype._initIO = function _initIO() {
|
||||
});
|
||||
|
||||
this.walletdb.on('address', function(id, receive) {
|
||||
receive = receive.map(function(address) {
|
||||
return address.toJSON();
|
||||
});
|
||||
self.server.io.to(id).emit('wallet address', receive);
|
||||
self.server.io.to('!all').emit('wallet address', id, receive);
|
||||
var json = [];
|
||||
var i, address;
|
||||
|
||||
for (i = 0; i < receive.length; i++) {
|
||||
address = receive[i];
|
||||
json.push(address.toJSON());
|
||||
}
|
||||
|
||||
self.server.io.to(id).emit('wallet address', json);
|
||||
self.server.io.to('!all').emit('wallet address', id, json);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user