|
| 1 | +import { describe, test, beforeEach, afterEach } from 'node:test'; |
| 2 | +import * as assert from 'node:assert/strict'; |
| 3 | +import { STACKS_TESTNET } from '@stacks/network'; |
| 4 | +import { PgWriteStore } from '../../../src/datastore/pg-write-store.ts'; |
| 5 | +import { ApiServer, startApiServer } from '../../../src/api/init.ts'; |
| 6 | +import { migrate } from '../../test-helpers.ts'; |
| 7 | +import { hex } from '../test-helpers.ts'; |
| 8 | +import { TestBlockBuilder } from '../test-builders.ts'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Every v3 endpoint that takes a tx id or block hash answers a hash supplied without its `0x` |
| 12 | + * prefix with a `302` to the same URL with the prefix added, keeping the rest of the querystring. |
| 13 | + */ |
| 14 | + |
| 15 | +const PRINCIPAL = 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27'; |
| 16 | +const TX_1 = hex(1); |
| 17 | +const TX_2 = hex(2); |
| 18 | +const BLOCK_HASH = hex(0xb10c); |
| 19 | +const bare = (hash: string) => hash.slice(2); |
| 20 | + |
| 21 | +describe('v3 unprefixed hash redirects', () => { |
| 22 | + let db: PgWriteStore; |
| 23 | + let api: ApiServer; |
| 24 | + |
| 25 | + const get = (url: string) => api.fastifyApp.inject({ method: 'GET', url }); |
| 26 | + |
| 27 | + beforeEach(async () => { |
| 28 | + await migrate('up'); |
| 29 | + db = await PgWriteStore.connect({ |
| 30 | + usageName: 'tests', |
| 31 | + withNotifier: false, |
| 32 | + skipMigrations: true, |
| 33 | + }); |
| 34 | + api = await startApiServer({ datastore: db, chainId: STACKS_TESTNET.chainId }); |
| 35 | + await db.update( |
| 36 | + new TestBlockBuilder({ |
| 37 | + block_height: 1, |
| 38 | + block_hash: BLOCK_HASH, |
| 39 | + index_block_hash: BLOCK_HASH, |
| 40 | + }) |
| 41 | + .addTx({ tx_id: TX_1, sender_address: PRINCIPAL }) |
| 42 | + .build() |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(async () => { |
| 47 | + await api.terminate(); |
| 48 | + await db?.close(); |
| 49 | + await migrate('down'); |
| 50 | + }); |
| 51 | + |
| 52 | + const pathCases: [string, string, string][] = [ |
| 53 | + ['transaction', `/extended/v3/transactions/${bare(TX_1)}`, `/extended/v3/transactions/${TX_1}`], |
| 54 | + [ |
| 55 | + 'transaction with querystring', |
| 56 | + `/extended/v3/transactions/${bare(TX_1)}?include=post_conditions`, |
| 57 | + `/extended/v3/transactions/${TX_1}?include=post_conditions`, |
| 58 | + ], |
| 59 | + [ |
| 60 | + 'transaction events', |
| 61 | + `/extended/v3/transactions/${bare(TX_1)}/events?limit=5`, |
| 62 | + `/extended/v3/transactions/${TX_1}/events?limit=5`, |
| 63 | + ], |
| 64 | + [ |
| 65 | + 'principal transaction balance changes', |
| 66 | + `/extended/v3/principals/${PRINCIPAL}/transactions/${bare(TX_1)}/balance-changes`, |
| 67 | + `/extended/v3/principals/${PRINCIPAL}/transactions/${TX_1}/balance-changes`, |
| 68 | + ], |
| 69 | + [ |
| 70 | + 'block transactions', |
| 71 | + `/extended/v3/blocks/${bare(BLOCK_HASH)}/transactions`, |
| 72 | + `/extended/v3/blocks/${BLOCK_HASH}/transactions`, |
| 73 | + ], |
| 74 | + [ |
| 75 | + 'percent-encoded transaction', |
| 76 | + `/extended/v3/transactions/%30${bare(TX_1).slice(1)}`, |
| 77 | + `/extended/v3/transactions/${TX_1}`, |
| 78 | + ], |
| 79 | + [ |
| 80 | + 'percent-encoded block hash', |
| 81 | + `/extended/v3/blocks/%30${bare(BLOCK_HASH).slice(1)}/transactions`, |
| 82 | + `/extended/v3/blocks/${BLOCK_HASH}/transactions`, |
| 83 | + ], |
| 84 | + ]; |
| 85 | + |
| 86 | + for (const [name, url, location] of pathCases) { |
| 87 | + test(`redirects ${name} path param`, async () => { |
| 88 | + const res = await get(url); |
| 89 | + assert.equal(res.statusCode, 302); |
| 90 | + assert.equal(res.headers.location, location); |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + const queryCases: [string, string, string][] = [ |
| 95 | + [ |
| 96 | + 'batch, repeated', |
| 97 | + `/extended/v3/transactions/batch?tx_id=${bare(TX_1)}&tx_id=${TX_2}`, |
| 98 | + `/extended/v3/transactions/batch?tx_id=${TX_1}&tx_id=${TX_2}`, |
| 99 | + ], |
| 100 | + [ |
| 101 | + 'batch, comma-separated', |
| 102 | + `/extended/v3/transactions/batch?tx_id=${TX_1},${bare(TX_2)}`, |
| 103 | + `/extended/v3/transactions/batch?tx_id=${TX_1},${TX_2}`, |
| 104 | + ], |
| 105 | + [ |
| 106 | + 'batch, encoded comma', |
| 107 | + `/extended/v3/transactions/batch?tx_id=${bare(TX_1)}%2C${bare(TX_2)}`, |
| 108 | + `/extended/v3/transactions/batch?tx_id=${TX_1},${TX_2}`, |
| 109 | + ], |
| 110 | + [ |
| 111 | + 'principal balance changes, other params kept in place', |
| 112 | + `/extended/v3/principals/${PRINCIPAL}/balance-changes?limit=5&tx_id=${bare(TX_1)}`, |
| 113 | + `/extended/v3/principals/${PRINCIPAL}/balance-changes?limit=5&tx_id=${TX_1}`, |
| 114 | + ], |
| 115 | + [ |
| 116 | + 'batch, percent-encoded', |
| 117 | + `/extended/v3/transactions/batch?tx_id=%30${bare(TX_1).slice(1)},${TX_2}`, |
| 118 | + `/extended/v3/transactions/batch?tx_id=${TX_1},${TX_2}`, |
| 119 | + ], |
| 120 | + [ |
| 121 | + 'batch, valueless and malformed params kept verbatim', |
| 122 | + `/extended/v3/transactions/batch?flag&%E0=1&tx_id=${bare(TX_1)}`, |
| 123 | + `/extended/v3/transactions/batch?flag&%E0=1&tx_id=${TX_1}`, |
| 124 | + ], |
| 125 | + ]; |
| 126 | + |
| 127 | + for (const [name, url, location] of queryCases) { |
| 128 | + test(`redirects ${name} query param`, async () => { |
| 129 | + const res = await get(url); |
| 130 | + assert.equal(res.statusCode, 302); |
| 131 | + assert.equal(res.headers.location, location); |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + test('does not redirect prefixed hashes, heights or aliases', async () => { |
| 136 | + for (const url of [ |
| 137 | + `/extended/v3/transactions/${TX_1}`, |
| 138 | + `/extended/v3/transactions/batch?tx_id=${TX_1}`, |
| 139 | + `/extended/v3/blocks/${BLOCK_HASH}/transactions`, |
| 140 | + `/extended/v3/blocks/1/transactions`, |
| 141 | + `/extended/v3/blocks/latest/transactions`, |
| 142 | + ]) { |
| 143 | + const res = await get(url); |
| 144 | + assert.equal(res.statusCode, 200, url); |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + test('leaves malformed ids to schema validation', async () => { |
| 149 | + const res = await get(`/extended/v3/transactions/${bare(TX_1).slice(1)}`); |
| 150 | + assert.equal(res.statusCode, 400); |
| 151 | + }); |
| 152 | + |
| 153 | + test('redirect target resolves the transaction', async () => { |
| 154 | + const redirect = await get(`/extended/v3/transactions/${bare(TX_1)}`); |
| 155 | + const res = await get(redirect.headers.location as string); |
| 156 | + assert.equal(res.statusCode, 200); |
| 157 | + assert.equal(JSON.parse(res.body).tx_id, TX_1); |
| 158 | + }); |
| 159 | +}); |
0 commit comments