Skip to content

Commit

Permalink
Add required signers (#136)
Browse files Browse the repository at this point in the history
* Add required signers

* fixup! Add required signers

* tmp: bump openapi to beta.5

* chore: one liner transform

* chore: array of objects

* chore: add 404

* chore: fix fixtures

* chore: bump tests

* chore: stable blockfrost/openapi

* fix: remove unnecessary fork for unpaged result

---------

Co-authored-by: slowbackspace <[email protected]>
  • Loading branch information
sorki and slowbackspace authored Mar 5, 2024
1 parent 536dddf commit f55709b
Show file tree
Hide file tree
Showing 53 changed files with 255 additions and 169 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"dependencies": {
"@blockfrost/blockfrost-js": "5.4.0",
"@blockfrost/blockfrost-utils": "2.8.0",
"@blockfrost/openapi": "0.1.60",
"@blockfrost/openapi": "0.1.61",
"@emurgo/cardano-serialization-lib-nodejs": "11.5.0",
"@fastify/cors": "^8.3.0",
"@fastify/postgres": "^5.2.0",
Expand Down
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
49 changes: 49 additions & 0 deletions src/routes/txs/hash/required-signers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { getSchemaForEndpoint } from '@blockfrost/openapi';
import { FastifyInstance, FastifyRequest } from 'fastify';
import { SQLQuery } from '../../../sql/index.js';
import * as QueryTypes from '../../../types/queries/tx.js';
import * as ResponseTypes from '../../../types/responses/tx.js';
import { getDbSync } from '../../../utils/database.js';
import { handle404 } from '@blockfrost/blockfrost-utils/lib/fastify.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 query404 = await clientDbSync.query<QueryTypes.ResultFound>(SQLQuery.get('txs_404'), [
request.params.hash,
]);

if (query404.rows.length === 0) {
clientDbSync.release();
return handle404(reply);
}

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

clientDbSync.release();

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

return reply.send(rows);
} 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 "witness_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 TxWitnesses {
witness_hash: string;
}
1 change: 1 addition & 0 deletions src/types/responses/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export type Relay = OpenApiResponseTypes['pool_relays'];
export type TxRedeemers = OpenApiResponseTypes['tx_content_redeemers'];
export type TxMetadataCbor = OpenApiResponseTypes['tx_content_metadata_cbor'];
export type TxMetadata = OpenApiResponseTypes['tx_content_metadata'];
export type TxWitnesses = OpenApiResponseTypes['tx_content_required_signers'];
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 = [
{ witness_hash: 'd52e11f3e48436dd42dbec6d88c239732e503b8b7a32af58e5f87625' },
{ witness_hash: '41b32682c413535dbca5178f92f3cee5dede31b995400b8c371e2469' },
{ witness_hash: 'd52e11f3e48436dd42dbec6d88c239732e503b8b7a32af58e5f87625' },
{ witness_hash: '666414964a05b01cef36427b8a0fb0f621806c43e66e7a4d3cca3bfb' },
];

const response_txs_required_signers = [
{ witness_hash: 'd52e11f3e48436dd42dbec6d88c239732e503b8b7a32af58e5f87625' },
{ witness_hash: '41b32682c413535dbca5178f92f3cee5dede31b995400b8c371e2469' },
{ witness_hash: 'd52e11f3e48436dd42dbec6d88c239732e503b8b7a32af58e5f87625' },
{ witness_hash: '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;
Loading

0 comments on commit f55709b

Please sign in to comment.