Skip to content

Commit

Permalink
Add required signers
Browse files Browse the repository at this point in the history
  • Loading branch information
sorki committed Dec 18, 2023
1 parent ae974c3 commit b031860
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ const start = (options = {}): FastifyInstance => {
registerRoute(app, import('./routes/txs/hash/withdrawals.js'));
registerRoute(app, import('./routes/txs/hash/metadata/index.js'));
registerRoute(app, import('./routes/txs/hash/metadata/cbor.js'));
registerRoute(app, import('./routes/txs/hash/required-signers.js'));

// utils
registerRoute(app, import('./routes/utils/addresses/xpub/xpub/role/index.js'));
Expand Down
56 changes: 56 additions & 0 deletions src/routes/txs/hash/required-signers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { getSchemaForEndpoint } from '@blockfrost/openapi';
import { isUnpaged } from '../../../utils/routes.js';
import { toJSONStream } from '../../../utils/string-utils.js';
import { FastifyInstance, FastifyRequest } from 'fastify';
import { SQLQuery } from '../../../sql/index.js';
import * as QueryTypes from '../../../types/queries/tx.js';
import { getDbSync } from '../../../utils/database.js';

async function route(fastify: FastifyInstance) {
fastify.route({
url: '/txs/:hash/required_signers',
method: 'GET',
schema: getSchemaForEndpoint('/txs/:hash:/required_signers'),
handler: async (request: FastifyRequest<QueryTypes.RequestParameters>, reply) => {
const clientDbSync = await getDbSync(fastify);

try {
const { rows } = await clientDbSync.query<QueryTypes.TxWits>(
SQLQuery.get('txs_hash_wits'),
[request.params.hash],
);

clientDbSync.release();

if (rows.length === 0) {
return reply.send([]);
}

const list: string[] = [];

for (const row of rows) {
list.push(row.hash);
}

const unpaged = isUnpaged(request);

if (unpaged) {
// Use of Reply.raw functions is at your own risk as you are skipping all the Fastify logic of handling the HTTP response
// https://www.fastify.io/docs/latest/Reference/Reply/#raw
reply.raw.writeHead(200, { 'Content-Type': 'application/json' });
await toJSONStream(list, reply.raw);
return reply;
} else {
return reply.send(list);
}
} catch (error) {
if (clientDbSync) {
clientDbSync.release();
}
throw error;
}
},
});
}

export default route;
1 change: 1 addition & 0 deletions src/sql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ const QUERY_FILES = {
txs_hash_mirs: 'txs/txs_hash_mirs.sql',
txs_hash_pool_updates: 'txs/txs_hash_pool_updates.sql',
txs_hash_metadata: 'txs/txs_hash_metadata.sql',
txs_hash_wits: 'txs/txs_hash_wits.sql',
} as const;

type QueryKey = keyof typeof QUERY_FILES;
Expand Down
5 changes: 5 additions & 0 deletions src/sql/txs/txs_hash_wits.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SELECT encode(wit.hash, 'hex') AS "hash"
FROM tx
JOIN extra_key_witness wit ON (wit.tx_id = tx.id)
WHERE encode(tx.hash, 'hex') = $1
ORDER BY wit.id
4 changes: 4 additions & 0 deletions src/types/queries/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,7 @@ export interface TxRedeemers {
fee: string;
redeemer_data_hash: string;
}

export interface TxWits {
hash: string;
}
52 changes: 52 additions & 0 deletions test/unit/fixtures/txs.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,20 @@ const response_txs_redeemers = [
},
];

const query_txs_required_signers = [
{ hash: 'd52e11f3e48436dd42dbec6d88c239732e503b8b7a32af58e5f87625' },
{ hash: '41b32682c413535dbca5178f92f3cee5dede31b995400b8c371e2469' },
{ hash: 'd52e11f3e48436dd42dbec6d88c239732e503b8b7a32af58e5f87625' },
{ hash: '666414964a05b01cef36427b8a0fb0f621806c43e66e7a4d3cca3bfb' },
];

const response_txs_required_signers = [
'd52e11f3e48436dd42dbec6d88c239732e503b8b7a32af58e5f87625',
'41b32682c413535dbca5178f92f3cee5dede31b995400b8c371e2469',
'd52e11f3e48436dd42dbec6d88c239732e503b8b7a32af58e5f87625',
'666414964a05b01cef36427b8a0fb0f621806c43e66e7a4d3cca3bfb',
];

const response_404 = {
error: 'Not Found',
message: 'The requested component has not been found.',
Expand Down Expand Up @@ -1542,6 +1556,28 @@ export default [
},
response: [],
},
{
name: 'respond with success and data on /txs/:hash/required_signers',
endpoint: '/txs/6e6644e0f8aeec3437bec536408fc007a6147d94098f2dbaeb6ad80d0508631b/required_signers',
sqlQueryMock: {
rows: query_found,
},
sqlQueryMock2: {
rows: query_txs_required_signers,
},
response: response_txs_required_signers,
},
{
name: 'respond with success and data on /txs/:hash/required_signers',
endpoint: '/txs/6e6644e0f8aeec3437bec536408fc007a6147d94098f2dbaeb6ad80d0508631b/required_signers',
sqlQueryMock: {
rows: query_found,
},
sqlQueryMock2: {
rows: [],
},
response: [],
},
/*
404s
*/
Expand Down Expand Up @@ -1637,6 +1673,14 @@ export default [
},
response: response_404,
},
{
name: 'respond with 404 and empty data on /txs/:hash/required_signers',
endpoint: '/txs/stonks_tx/required_signers',
sqlQueryMock: {
rows: [],
},
response: response_404,
},

/*
500s
Expand Down Expand Up @@ -1778,4 +1822,12 @@ export default [
},
response: response_500,
},
{
name: 'respond with 500 and null on /txs/:hash/required_signers',
endpoint: '/txs/stonks_tx/required_signers',
sqlQueryMock: {
rows: null,
},
response: response_500,
},
]; //as const;

0 comments on commit b031860

Please sign in to comment.