2022-07-11 19:15:28 +02:00
|
|
|
import { Application, Request, Response } from 'express';
|
|
|
|
import axios from 'axios';
|
2022-09-05 23:13:45 +09:00
|
|
|
import * as bitcoinjs from 'bitcoinjs-lib';
|
2022-07-11 19:15:28 +02:00
|
|
|
import config from '../../config';
|
|
|
|
import websocketHandler from '../websocket-handler';
|
|
|
|
import mempool from '../mempool';
|
|
|
|
import feeApi from '../fee-api';
|
|
|
|
import mempoolBlocks from '../mempool-blocks';
|
2023-03-03 13:59:17 +09:00
|
|
|
import bitcoinApi, { bitcoinCoreApi } from './bitcoin-api-factory';
|
2022-07-11 19:15:28 +02:00
|
|
|
import { Common } from '../common';
|
|
|
|
import backendInfo from '../backend-info';
|
|
|
|
import transactionUtils from '../transaction-utils';
|
|
|
|
import { IEsploraApi } from './esplora-api.interface';
|
|
|
|
import loadingIndicators from '../loading-indicators';
|
|
|
|
import { TransactionExtended } from '../../mempool.interfaces';
|
|
|
|
import logger from '../../logger';
|
|
|
|
import blocks from '../blocks';
|
|
|
|
import bitcoinClient from './bitcoin-client';
|
|
|
|
import difficultyAdjustment from '../difficulty-adjustment';
|
2022-11-27 13:46:23 +09:00
|
|
|
import transactionRepository from '../../repositories/TransactionRepository';
|
2022-12-09 10:32:58 -06:00
|
|
|
import rbfCache from '../rbf-cache';
|
2022-07-11 19:15:28 +02:00
|
|
|
|
|
|
|
class BitcoinRoutes {
|
|
|
|
public initRoutes(app: Application) {
|
|
|
|
app
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'transaction-times', this.getTransactionTimes)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'outspends', this.$getBatchedOutspends)
|
2022-11-27 13:46:23 +09:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'cpfp/:txId', this.$getCpfpInfo)
|
2022-07-11 19:15:28 +02:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'difficulty-adjustment', this.getDifficultyChange)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'fees/recommended', this.getRecommendedFees)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'fees/mempool-blocks', this.getMempoolBlocks)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'backend-info', this.getBackendInfo)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'init-data', this.getInitData)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'validate-address/:address', this.validateAddress)
|
2022-12-13 17:11:37 -06:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'tx/:txId/rbf', this.getRbfHistory)
|
2022-12-09 10:32:58 -06:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'tx/:txId/cached', this.getCachedTx)
|
2022-12-14 16:51:53 -06:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'replacements', this.getRbfReplacements)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'fullrbf/replacements', this.getFullRbfReplacements)
|
2022-07-11 19:15:28 +02:00
|
|
|
.post(config.MEMPOOL.API_URL_PREFIX + 'tx/push', this.$postTransactionForm)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'donations', async (req, res) => {
|
|
|
|
try {
|
|
|
|
const response = await axios.get(`${config.EXTERNAL_DATA_SERVER.MEMPOOL_API}/donations`, { responseType: 'stream', timeout: 10000 });
|
|
|
|
response.data.pipe(res);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).end();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'donations/images/:id', async (req, res) => {
|
|
|
|
try {
|
|
|
|
const response = await axios.get(`${config.EXTERNAL_DATA_SERVER.MEMPOOL_API}/donations/images/${req.params.id}`, {
|
|
|
|
responseType: 'stream', timeout: 10000
|
|
|
|
});
|
|
|
|
response.data.pipe(res);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).end();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'contributors', async (req, res) => {
|
|
|
|
try {
|
|
|
|
const response = await axios.get(`${config.EXTERNAL_DATA_SERVER.MEMPOOL_API}/contributors`, { responseType: 'stream', timeout: 10000 });
|
|
|
|
response.data.pipe(res);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).end();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'contributors/images/:id', async (req, res) => {
|
|
|
|
try {
|
|
|
|
const response = await axios.get(`${config.EXTERNAL_DATA_SERVER.MEMPOOL_API}/contributors/images/${req.params.id}`, {
|
|
|
|
responseType: 'stream', timeout: 10000
|
|
|
|
});
|
|
|
|
response.data.pipe(res);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).end();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'translators', async (req, res) => {
|
|
|
|
try {
|
|
|
|
const response = await axios.get(`${config.EXTERNAL_DATA_SERVER.MEMPOOL_API}/translators`, { responseType: 'stream', timeout: 10000 });
|
|
|
|
response.data.pipe(res);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).end();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'translators/images/:id', async (req, res) => {
|
|
|
|
try {
|
|
|
|
const response = await axios.get(`${config.EXTERNAL_DATA_SERVER.MEMPOOL_API}/translators/images/${req.params.id}`, {
|
|
|
|
responseType: 'stream', timeout: 10000
|
|
|
|
});
|
|
|
|
response.data.pipe(res);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).end();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'blocks', this.getBlocks.bind(this))
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'blocks/:height', this.getBlocks.bind(this))
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash', this.getBlock)
|
2022-11-22 19:08:09 +09:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/summary', this.getStrippedBlockTransactions)
|
2022-11-25 19:32:50 +09:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/audit-summary', this.getBlockAuditSummary)
|
2023-04-26 13:49:01 +04:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'blocks/tip/height', this.getBlockTipHeight)
|
2022-11-22 19:08:09 +09:00
|
|
|
.post(config.MEMPOOL.API_URL_PREFIX + 'psbt/addparents', this.postPsbtCompletion)
|
2023-02-16 15:36:16 +09:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'blocks-bulk/:from', this.getBlocksByBulk.bind(this))
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'blocks-bulk/:from/:to', this.getBlocksByBulk.bind(this))
|
2022-07-11 19:15:28 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
if (config.MEMPOOL.BACKEND !== 'esplora') {
|
|
|
|
app
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'mempool', this.getMempool)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'mempool/txids', this.getMempoolTxIds)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'mempool/recent', this.getRecentMempoolTransactions)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'tx/:txId', this.getTransaction)
|
|
|
|
.post(config.MEMPOOL.API_URL_PREFIX + 'tx', this.$postTransaction)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'tx/:txId/hex', this.getRawTransaction)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'tx/:txId/status', this.getTransactionStatus)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'tx/:txId/outspends', this.getTransactionOutspends)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/header', this.getBlockHeader)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'blocks/tip/hash', this.getBlockTipHash)
|
2022-07-25 14:54:00 -03:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/raw', this.getRawBlock)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/txids', this.getTxIdsForBlock)
|
2022-07-11 19:15:28 +02:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/txs', this.getBlockTransactions)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/txs/:index', this.getBlockTransactions)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'block-height/:height', this.getBlockHeight)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'address/:address', this.getAddress)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'address/:address/txs', this.getAddressTransactions)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'address/:address/txs/chain/:txId', this.getAddressTransactions)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'address-prefix/:prefix', this.getAddressPrefix)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private getInitData(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const result = websocketHandler.getInitData();
|
|
|
|
res.json(result);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private getRecommendedFees(req: Request, res: Response) {
|
|
|
|
if (!mempool.isInSync()) {
|
|
|
|
res.statusCode = 503;
|
|
|
|
res.send('Service Unavailable');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const result = feeApi.getRecommendedFee();
|
|
|
|
res.json(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
private getMempoolBlocks(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const result = mempoolBlocks.getMempoolBlocks();
|
|
|
|
res.json(result);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private getTransactionTimes(req: Request, res: Response) {
|
|
|
|
if (!Array.isArray(req.query.txId)) {
|
|
|
|
res.status(500).send('Not an array');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const txIds: string[] = [];
|
|
|
|
for (const _txId in req.query.txId) {
|
|
|
|
if (typeof req.query.txId[_txId] === 'string') {
|
|
|
|
txIds.push(req.query.txId[_txId].toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const times = mempool.getFirstSeenForTransactions(txIds);
|
|
|
|
res.json(times);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async $getBatchedOutspends(req: Request, res: Response) {
|
|
|
|
if (!Array.isArray(req.query.txId)) {
|
|
|
|
res.status(500).send('Not an array');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (req.query.txId.length > 50) {
|
|
|
|
res.status(400).send('Too many txids requested');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const txIds: string[] = [];
|
|
|
|
for (const _txId in req.query.txId) {
|
|
|
|
if (typeof req.query.txId[_txId] === 'string') {
|
|
|
|
txIds.push(req.query.txId[_txId].toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const batchedOutspends = await bitcoinApi.$getBatchedOutspends(txIds);
|
|
|
|
res.json(batchedOutspends);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-27 13:46:23 +09:00
|
|
|
private async $getCpfpInfo(req: Request, res: Response) {
|
2022-07-11 19:15:28 +02:00
|
|
|
if (!/^[a-fA-F0-9]{64}$/.test(req.params.txId)) {
|
|
|
|
res.status(501).send(`Invalid transaction ID.`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tx = mempool.getMempool()[req.params.txId];
|
2022-11-27 13:46:23 +09:00
|
|
|
if (tx) {
|
|
|
|
if (tx?.cpfpChecked) {
|
|
|
|
res.json({
|
|
|
|
ancestors: tx.ancestors,
|
|
|
|
bestDescendant: tx.bestDescendant || null,
|
2022-11-27 17:48:25 +09:00
|
|
|
descendants: tx.descendants || null,
|
|
|
|
effectiveFeePerVsize: tx.effectiveFeePerVsize || null,
|
2022-11-27 13:46:23 +09:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const cpfpInfo = Common.setRelativesAndGetCpfpInfo(tx, mempool.getMempool());
|
2022-07-11 19:15:28 +02:00
|
|
|
|
2022-11-27 13:46:23 +09:00
|
|
|
res.json(cpfpInfo);
|
2022-07-11 19:15:28 +02:00
|
|
|
return;
|
2022-11-27 13:46:23 +09:00
|
|
|
} else {
|
2023-03-01 11:30:33 -06:00
|
|
|
let cpfpInfo;
|
|
|
|
if (config.DATABASE.ENABLED) {
|
|
|
|
cpfpInfo = await transactionRepository.$getCpfpInfo(req.params.txId);
|
2023-03-06 00:02:21 -06:00
|
|
|
}
|
|
|
|
if (cpfpInfo) {
|
|
|
|
res.json(cpfpInfo);
|
|
|
|
return;
|
2023-03-01 11:30:33 -06:00
|
|
|
} else {
|
|
|
|
res.json({
|
|
|
|
ancestors: []
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2022-07-11 19:15:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private getBackendInfo(req: Request, res: Response) {
|
|
|
|
res.json(backendInfo.getBackendInfo());
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getTransaction(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const transaction = await transactionUtils.$getTransactionExtended(req.params.txId, true);
|
|
|
|
res.json(transaction);
|
|
|
|
} catch (e) {
|
|
|
|
let statusCode = 500;
|
|
|
|
if (e instanceof Error && e instanceof Error && e.message && e.message.indexOf('No such mempool or blockchain transaction') > -1) {
|
|
|
|
statusCode = 404;
|
|
|
|
}
|
|
|
|
res.status(statusCode).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getRawTransaction(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const transaction: IEsploraApi.Transaction = await bitcoinApi.$getRawTransaction(req.params.txId, true);
|
|
|
|
res.setHeader('content-type', 'text/plain');
|
|
|
|
res.send(transaction.hex);
|
|
|
|
} catch (e) {
|
|
|
|
let statusCode = 500;
|
|
|
|
if (e instanceof Error && e.message && e.message.indexOf('No such mempool or blockchain transaction') > -1) {
|
|
|
|
statusCode = 404;
|
|
|
|
}
|
|
|
|
res.status(statusCode).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 23:13:45 +09:00
|
|
|
/**
|
|
|
|
* Takes the PSBT as text/plain body, parses it, and adds the full
|
|
|
|
* parent transaction to each input that doesn't already have it.
|
|
|
|
* This is used for BTCPayServer / Trezor users which need access to
|
|
|
|
* the full parent transaction even with segwit inputs.
|
|
|
|
* It will respond with a text/plain PSBT in the same format (hex|base64).
|
|
|
|
*/
|
2022-11-22 21:45:05 +09:00
|
|
|
private async postPsbtCompletion(req: Request, res: Response): Promise<void> {
|
2022-09-05 23:13:45 +09:00
|
|
|
res.setHeader('content-type', 'text/plain');
|
2022-09-10 16:03:31 +09:00
|
|
|
const notFoundError = `Couldn't get transaction hex for parent of input`;
|
2022-09-05 23:13:45 +09:00
|
|
|
try {
|
|
|
|
let psbt: bitcoinjs.Psbt;
|
|
|
|
let format: 'hex' | 'base64';
|
2022-09-10 16:03:31 +09:00
|
|
|
let isModified = false;
|
2022-09-05 23:13:45 +09:00
|
|
|
try {
|
|
|
|
psbt = bitcoinjs.Psbt.fromBase64(req.body);
|
|
|
|
format = 'base64';
|
2022-09-10 16:03:31 +09:00
|
|
|
} catch (e1) {
|
2022-09-05 23:13:45 +09:00
|
|
|
try {
|
|
|
|
psbt = bitcoinjs.Psbt.fromHex(req.body);
|
|
|
|
format = 'hex';
|
2022-09-10 16:03:31 +09:00
|
|
|
} catch (e2) {
|
2022-09-05 23:13:45 +09:00
|
|
|
throw new Error(`Unable to parse PSBT`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const [index, input] of psbt.data.inputs.entries()) {
|
|
|
|
if (!input.nonWitnessUtxo) {
|
|
|
|
// Buffer.from ensures it won't be modified in place by reverse()
|
2022-09-10 16:03:31 +09:00
|
|
|
const txid = Buffer.from(psbt.txInputs[index].hash)
|
|
|
|
.reverse()
|
|
|
|
.toString('hex');
|
|
|
|
|
2022-11-22 21:45:05 +09:00
|
|
|
let transactionHex: string;
|
2022-09-10 16:03:31 +09:00
|
|
|
// If missing transaction, return 404 status error
|
|
|
|
try {
|
2022-11-22 21:45:05 +09:00
|
|
|
transactionHex = await bitcoinApi.$getTransactionHex(txid);
|
|
|
|
if (!transactionHex) {
|
2022-09-10 16:03:31 +09:00
|
|
|
throw new Error('');
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(`${notFoundError} #${index} @ ${txid}`);
|
2022-09-05 23:13:45 +09:00
|
|
|
}
|
2022-09-10 16:03:31 +09:00
|
|
|
|
2022-09-05 23:13:45 +09:00
|
|
|
psbt.updateInput(index, {
|
2022-11-22 21:45:05 +09:00
|
|
|
nonWitnessUtxo: Buffer.from(transactionHex, 'hex'),
|
2022-09-05 23:13:45 +09:00
|
|
|
});
|
2022-09-10 16:03:31 +09:00
|
|
|
if (!isModified) {
|
|
|
|
isModified = true;
|
|
|
|
}
|
2022-09-05 23:13:45 +09:00
|
|
|
}
|
|
|
|
}
|
2022-09-10 16:03:31 +09:00
|
|
|
if (isModified) {
|
|
|
|
res.send(format === 'hex' ? psbt.toHex() : psbt.toBase64());
|
|
|
|
} else {
|
|
|
|
// Not modified
|
|
|
|
// 422 Unprocessable Entity
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422
|
2022-09-10 16:09:43 +09:00
|
|
|
res.status(422).send(`Psbt had no missing nonWitnessUtxos.`);
|
2022-09-10 16:03:31 +09:00
|
|
|
}
|
2022-09-05 23:13:45 +09:00
|
|
|
} catch (e: any) {
|
2022-09-10 16:03:31 +09:00
|
|
|
if (e instanceof Error && new RegExp(notFoundError).test(e.message)) {
|
|
|
|
res.status(404).send(e.message);
|
|
|
|
} else {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
2022-09-05 23:13:45 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 19:15:28 +02:00
|
|
|
private async getTransactionStatus(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const transaction = await transactionUtils.$getTransactionExtended(req.params.txId, true);
|
|
|
|
res.json(transaction.status);
|
|
|
|
} catch (e) {
|
|
|
|
let statusCode = 500;
|
|
|
|
if (e instanceof Error && e.message && e.message.indexOf('No such mempool or blockchain transaction') > -1) {
|
|
|
|
statusCode = 404;
|
|
|
|
}
|
|
|
|
res.status(statusCode).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 19:32:50 +09:00
|
|
|
private async getStrippedBlockTransactions(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const transactions = await blocks.$getStrippedBlockTransactions(req.params.hash);
|
|
|
|
res.setHeader('Expires', new Date(Date.now() + 1000 * 3600 * 24 * 30).toUTCString());
|
|
|
|
res.json(transactions);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 19:15:28 +02:00
|
|
|
private async getBlock(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const block = await blocks.$getBlock(req.params.hash);
|
|
|
|
|
|
|
|
const blockAge = new Date().getTime() / 1000 - block.timestamp;
|
|
|
|
const day = 24 * 3600;
|
|
|
|
let cacheDuration;
|
|
|
|
if (blockAge > 365 * day) {
|
|
|
|
cacheDuration = 30 * day;
|
|
|
|
} else if (blockAge > 30 * day) {
|
|
|
|
cacheDuration = 10 * day;
|
|
|
|
} else {
|
|
|
|
cacheDuration = 600
|
|
|
|
}
|
|
|
|
|
|
|
|
res.setHeader('Expires', new Date(Date.now() + 1000 * cacheDuration).toUTCString());
|
|
|
|
res.json(block);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getBlockHeader(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const blockHeader = await bitcoinApi.$getBlockHeader(req.params.hash);
|
|
|
|
res.setHeader('content-type', 'text/plain');
|
|
|
|
res.send(blockHeader);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 19:32:50 +09:00
|
|
|
private async getBlockAuditSummary(req: Request, res: Response) {
|
2022-07-11 19:15:28 +02:00
|
|
|
try {
|
2022-11-25 19:32:50 +09:00
|
|
|
const transactions = await blocks.$getBlockAuditSummary(req.params.hash);
|
2022-07-11 19:15:28 +02:00
|
|
|
res.setHeader('Expires', new Date(Date.now() + 1000 * 3600 * 24 * 30).toUTCString());
|
|
|
|
res.json(transactions);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getBlocks(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
if (['mainnet', 'testnet', 'signet'].includes(config.MEMPOOL.NETWORK)) { // Bitcoin
|
|
|
|
const height = req.params.height === undefined ? undefined : parseInt(req.params.height, 10);
|
|
|
|
res.setHeader('Expires', new Date(Date.now() + 1000 * 60).toUTCString());
|
|
|
|
res.json(await blocks.$getBlocks(height, 15));
|
|
|
|
} else { // Liquid, Bisq
|
|
|
|
return await this.getLegacyBlocks(req, res);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-16 15:36:16 +09:00
|
|
|
private async getBlocksByBulk(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
if (['mainnet', 'testnet', 'signet'].includes(config.MEMPOOL.NETWORK) === false) { // Liquid, Bisq - Not implemented
|
2023-02-17 21:21:21 +09:00
|
|
|
return res.status(404).send(`This API is only available for Bitcoin networks`);
|
|
|
|
}
|
2023-02-24 15:06:47 +09:00
|
|
|
if (config.MEMPOOL.MAX_BLOCKS_BULK_QUERY <= 0) {
|
|
|
|
return res.status(404).send(`This API is disabled. Set config.MEMPOOL.MAX_BLOCKS_BULK_QUERY to a positive number to enable it.`);
|
|
|
|
}
|
2023-02-17 21:21:21 +09:00
|
|
|
if (!Common.indexingEnabled()) {
|
|
|
|
return res.status(404).send(`Indexing is required for this API`);
|
2023-02-16 15:36:16 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
const from = parseInt(req.params.from, 10);
|
2023-02-24 15:06:47 +09:00
|
|
|
if (!req.params.from || from < 0) {
|
2023-02-16 15:36:16 +09:00
|
|
|
return res.status(400).send(`Parameter 'from' must be a block height (integer)`);
|
|
|
|
}
|
|
|
|
const to = req.params.to === undefined ? await bitcoinApi.$getBlockHeightTip() : parseInt(req.params.to, 10);
|
2023-02-24 15:06:47 +09:00
|
|
|
if (to < 0) {
|
2023-02-16 15:36:16 +09:00
|
|
|
return res.status(400).send(`Parameter 'to' must be a block height (integer)`);
|
|
|
|
}
|
|
|
|
if (from > to) {
|
|
|
|
return res.status(400).send(`Parameter 'to' must be a higher block height than 'from'`);
|
|
|
|
}
|
2023-02-24 15:06:47 +09:00
|
|
|
if ((to - from + 1) > config.MEMPOOL.MAX_BLOCKS_BULK_QUERY) {
|
|
|
|
return res.status(400).send(`You can only query ${config.MEMPOOL.MAX_BLOCKS_BULK_QUERY} blocks at once.`);
|
|
|
|
}
|
2023-02-16 15:36:16 +09:00
|
|
|
|
|
|
|
res.setHeader('Expires', new Date(Date.now() + 1000 * 60).toUTCString());
|
2023-02-17 21:21:21 +09:00
|
|
|
res.json(await blocks.$getBlocksBetweenHeight(from, to));
|
2023-02-16 15:36:16 +09:00
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 19:15:28 +02:00
|
|
|
private async getLegacyBlocks(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const returnBlocks: IEsploraApi.Block[] = [];
|
2022-12-27 05:28:57 -06:00
|
|
|
const tip = blocks.getCurrentBlockHeight();
|
|
|
|
const fromHeight = Math.min(parseInt(req.params.height, 10) || tip, tip);
|
2022-07-11 19:15:28 +02:00
|
|
|
|
|
|
|
// Check if block height exist in local cache to skip the hash lookup
|
|
|
|
const blockByHeight = blocks.getBlocks().find((b) => b.height === fromHeight);
|
|
|
|
let startFromHash: string | null = null;
|
|
|
|
if (blockByHeight) {
|
|
|
|
startFromHash = blockByHeight.id;
|
|
|
|
} else {
|
|
|
|
startFromHash = await bitcoinApi.$getBlockHash(fromHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
let nextHash = startFromHash;
|
|
|
|
for (let i = 0; i < 10 && nextHash; i++) {
|
|
|
|
const localBlock = blocks.getBlocks().find((b) => b.id === nextHash);
|
|
|
|
if (localBlock) {
|
|
|
|
returnBlocks.push(localBlock);
|
|
|
|
nextHash = localBlock.previousblockhash;
|
|
|
|
} else {
|
2023-03-03 13:59:17 +09:00
|
|
|
const block = await bitcoinCoreApi.$getBlock(nextHash);
|
2022-07-11 19:15:28 +02:00
|
|
|
returnBlocks.push(block);
|
|
|
|
nextHash = block.previousblockhash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res.setHeader('Expires', new Date(Date.now() + 1000 * 60).toUTCString());
|
|
|
|
res.json(returnBlocks);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getBlockTransactions(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
loadingIndicators.setProgress('blocktxs-' + req.params.hash, 0);
|
|
|
|
|
|
|
|
const txIds = await bitcoinApi.$getTxIdsForBlock(req.params.hash);
|
|
|
|
const transactions: TransactionExtended[] = [];
|
|
|
|
const startingIndex = Math.max(0, parseInt(req.params.index || '0', 10));
|
|
|
|
|
|
|
|
const endIndex = Math.min(startingIndex + 10, txIds.length);
|
|
|
|
for (let i = startingIndex; i < endIndex; i++) {
|
|
|
|
try {
|
|
|
|
const transaction = await transactionUtils.$getTransactionExtended(txIds[i], true, true);
|
|
|
|
transactions.push(transaction);
|
|
|
|
loadingIndicators.setProgress('blocktxs-' + req.params.hash, (i - startingIndex + 1) / (endIndex - startingIndex) * 100);
|
|
|
|
} catch (e) {
|
|
|
|
logger.debug('getBlockTransactions error: ' + (e instanceof Error ? e.message : e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.json(transactions);
|
|
|
|
} catch (e) {
|
|
|
|
loadingIndicators.setProgress('blocktxs-' + req.params.hash, 100);
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getBlockHeight(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const blockHash = await bitcoinApi.$getBlockHash(parseInt(req.params.height, 10));
|
|
|
|
res.send(blockHash);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getAddress(req: Request, res: Response) {
|
|
|
|
if (config.MEMPOOL.BACKEND === 'none') {
|
|
|
|
res.status(405).send('Address lookups cannot be used with bitcoind as backend.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const addressData = await bitcoinApi.$getAddress(req.params.address);
|
|
|
|
res.json(addressData);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof Error && e.message && (e.message.indexOf('too long') > 0 || e.message.indexOf('confirmed status') > 0)) {
|
|
|
|
return res.status(413).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getAddressTransactions(req: Request, res: Response) {
|
|
|
|
if (config.MEMPOOL.BACKEND === 'none') {
|
|
|
|
res.status(405).send('Address lookups cannot be used with bitcoind as backend.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const transactions = await bitcoinApi.$getAddressTransactions(req.params.address, req.params.txId);
|
|
|
|
res.json(transactions);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof Error && e.message && (e.message.indexOf('too long') > 0 || e.message.indexOf('confirmed status') > 0)) {
|
|
|
|
return res.status(413).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getAdressTxChain(req: Request, res: Response) {
|
|
|
|
res.status(501).send('Not implemented');
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getAddressPrefix(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const blockHash = await bitcoinApi.$getAddressPrefix(req.params.prefix);
|
|
|
|
res.send(blockHash);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getRecentMempoolTransactions(req: Request, res: Response) {
|
|
|
|
const latestTransactions = Object.entries(mempool.getMempool())
|
|
|
|
.sort((a, b) => (b[1].firstSeen || 0) - (a[1].firstSeen || 0))
|
|
|
|
.slice(0, 10).map((tx) => Common.stripTransaction(tx[1]));
|
|
|
|
|
|
|
|
res.json(latestTransactions);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getMempool(req: Request, res: Response) {
|
|
|
|
const info = mempool.getMempoolInfo();
|
|
|
|
res.json({
|
|
|
|
count: info.size,
|
|
|
|
vsize: info.bytes,
|
|
|
|
total_fee: info.total_fee * 1e8,
|
|
|
|
fee_histogram: []
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getMempoolTxIds(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const rawMempool = await bitcoinApi.$getRawMempool();
|
|
|
|
res.send(rawMempool);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 13:49:01 +04:00
|
|
|
private getBlockTipHeight(req: Request, res: Response) {
|
2022-07-11 19:15:28 +02:00
|
|
|
try {
|
2023-04-26 13:49:01 +04:00
|
|
|
const result = blocks.getCurrentBlockHeight();
|
|
|
|
if (!result) {
|
|
|
|
return res.status(503).send(`Service Temporarily Unavailable`);
|
|
|
|
}
|
|
|
|
res.setHeader('content-type', 'text/plain');
|
|
|
|
res.send(result.toString());
|
2022-07-11 19:15:28 +02:00
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getBlockTipHash(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const result = await bitcoinApi.$getBlockHashTip();
|
|
|
|
res.setHeader('content-type', 'text/plain');
|
|
|
|
res.send(result);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 14:54:00 -03:00
|
|
|
private async getRawBlock(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const result = await bitcoinApi.$getRawBlock(req.params.hash);
|
2022-07-27 23:33:18 +02:00
|
|
|
res.setHeader('content-type', 'application/octet-stream');
|
2022-07-25 14:54:00 -03:00
|
|
|
res.send(result);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 19:15:28 +02:00
|
|
|
private async getTxIdsForBlock(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const result = await bitcoinApi.$getTxIdsForBlock(req.params.hash);
|
|
|
|
res.json(result);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async validateAddress(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const result = await bitcoinClient.validateAddress(req.params.address);
|
|
|
|
res.json(result);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 10:32:58 -06:00
|
|
|
private async getRbfHistory(req: Request, res: Response) {
|
|
|
|
try {
|
2022-12-17 09:39:06 -06:00
|
|
|
const replacements = rbfCache.getRbfTree(req.params.txId) || null;
|
2022-12-13 17:11:37 -06:00
|
|
|
const replaces = rbfCache.getReplaces(req.params.txId) || null;
|
|
|
|
res.json({
|
|
|
|
replacements,
|
|
|
|
replaces
|
|
|
|
});
|
2022-12-09 10:32:58 -06:00
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-14 16:51:53 -06:00
|
|
|
private async getRbfReplacements(req: Request, res: Response) {
|
|
|
|
try {
|
2022-12-17 09:39:06 -06:00
|
|
|
const result = rbfCache.getRbfTrees(false);
|
2022-12-14 16:51:53 -06:00
|
|
|
res.json(result);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getFullRbfReplacements(req: Request, res: Response) {
|
|
|
|
try {
|
2022-12-17 09:39:06 -06:00
|
|
|
const result = rbfCache.getRbfTrees(true);
|
2022-12-14 16:51:53 -06:00
|
|
|
res.json(result);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 10:32:58 -06:00
|
|
|
private async getCachedTx(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const result = rbfCache.getTx(req.params.txId);
|
|
|
|
if (result) {
|
|
|
|
res.json(result);
|
|
|
|
} else {
|
2023-03-06 00:02:21 -06:00
|
|
|
res.status(204).send();
|
2022-12-09 10:32:58 -06:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 19:15:28 +02:00
|
|
|
private async getTransactionOutspends(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const result = await bitcoinApi.$getOutspends(req.params.txId);
|
|
|
|
res.json(result);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private getDifficultyChange(req: Request, res: Response) {
|
|
|
|
try {
|
2022-08-27 10:25:24 +02:00
|
|
|
const da = difficultyAdjustment.getDifficultyAdjustment();
|
|
|
|
if (da) {
|
|
|
|
res.json(da);
|
|
|
|
} else {
|
|
|
|
res.status(503).send(`Service Temporarily Unavailable`);
|
|
|
|
}
|
2022-07-11 19:15:28 +02:00
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async $postTransaction(req: Request, res: Response) {
|
|
|
|
res.setHeader('content-type', 'text/plain');
|
|
|
|
try {
|
|
|
|
let rawTx;
|
|
|
|
if (typeof req.body === 'object') {
|
|
|
|
rawTx = Object.keys(req.body)[0];
|
|
|
|
} else {
|
|
|
|
rawTx = req.body;
|
|
|
|
}
|
|
|
|
const txIdResult = await bitcoinApi.$sendRawTransaction(rawTx);
|
|
|
|
res.send(txIdResult);
|
|
|
|
} catch (e: any) {
|
|
|
|
res.status(400).send(e.message && e.code ? 'sendrawtransaction RPC error: ' + JSON.stringify({ code: e.code, message: e.message })
|
|
|
|
: (e.message || 'Error'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async $postTransactionForm(req: Request, res: Response) {
|
|
|
|
res.setHeader('content-type', 'text/plain');
|
|
|
|
const matches = /tx=([a-z0-9]+)/.exec(req.body);
|
|
|
|
let txHex = '';
|
|
|
|
if (matches && matches[1]) {
|
|
|
|
txHex = matches[1];
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const txIdResult = await bitcoinClient.sendRawTransaction(txHex);
|
|
|
|
res.send(txIdResult);
|
|
|
|
} catch (e: any) {
|
|
|
|
res.status(400).send(e.message && e.code ? 'sendrawtransaction RPC error: ' + JSON.stringify({ code: e.code, message: e.message })
|
|
|
|
: (e.message || 'Error'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new BitcoinRoutes();
|