testmempool accept more validation & switch to JSON array format

This commit is contained in:
Mononaut authored and softsimon committed 2024-04-21 10:05:58 +07:00
1 parent f3232b2d5c
commit 2a43255802
6 files changed
+29 -8

No files matched your search

+1 -1
View File
@@ -751,13 +751,13 @@ class BitcoinRoutes {
}
private async $testTransactions(req: Request, res: Response) {
res.setHeader('content-type', 'text/plain');
try {
const rawTxs = Common.getTransactionsFromRequest(req);
const maxfeerate = parseFloat(req.query.maxfeerate as string);
const result = await bitcoinApi.$testMempoolAccept(rawTxs, maxfeerate);
res.send(result);
} catch (e: any) {
res.setHeader('content-type', 'text/plain');
res.status(400).send(e.message && e.code ? 'testmempoolaccept RPC error: ' + JSON.stringify({ code: e.code, message: e.message })
: (e.message || 'Error'));
}
+8 -4
View File
@@ -946,12 +946,16 @@ export class Common {
return this.validateTransactionHex(matches[1].toLowerCase());
}
static getTransactionsFromRequest(req: Request): string[] {
if (typeof req.body !== 'string') {
throw Object.assign(new Error('Non-string request body'), { code: -1 });
static getTransactionsFromRequest(req: Request, limit: number = 25): string[] {
if (!Array.isArray(req.body) || req.body.some(hex => typeof hex !== 'string')) {
throw Object.assign(new Error('Invalid request body (should be an array of hexadecimal strings)'), { code: -1 });
}
const txs = req.body.split(',');
if (limit && req.body.length > limit) {
throw Object.assign(new Error('Exceeded maximum of 25 transactions'), { code: -1 });
}
const txs = req.body;
return txs.map(rawTx => {
// Support both upper and lower case hex
+1
View File
@@ -131,6 +131,7 @@ class Server {
})
.use(express.urlencoded({ extended: true }))
.use(express.text({ type: ['text/plain', 'application/base64'] }))
.use(express.json())
;
if (config.DATABASE.ENABLED && config.FIAT_PRICE.ENABLED) {