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