Skip to content

Commit

Permalink
fix: querying Dreps with drep_script bech32 prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
slowbackspace committed Sep 23, 2024
1 parent c120a55 commit 82be016
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 32 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Querying Dreps with `drep_script` bech32 prefix

## [2.2.0] - 2024-09-12

### Added
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"JSONStream": "^1.3.5",
"ajv": "^8.12.0",
"axios": "^1.5.0",
"bech32": "^2.0.0",
"config": "3.3.9",
"crc": "^4.3.2",
"cross-env": "^7.0.3",
Expand Down
17 changes: 11 additions & 6 deletions src/routes/governance/dreps/drep-id/delegators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { getDbSync } from '../../../../utils/database.js';
import { SQLQuery } from '../../../../sql/index.js';
import { getSchemaForEndpoint } from '@blockfrost/openapi';
import { isUnpaged } from '../../../../utils/routes.js';
import { handle400Custom } from '@blockfrost/blockfrost-utils/lib/fastify.js';
import { drepIdToRaw } from '../../../../utils/bech32.js';

async function route(fastify: FastifyInstance) {
fastify.route({
Expand All @@ -13,6 +15,14 @@ async function route(fastify: FastifyInstance) {
schema: getSchemaForEndpoint('/governance/dreps/{drep_id}/delegators'),

handler: async (request: FastifyRequest<QueryTypes.RequestParametersDRepID>, reply) => {
let drepHex;

try {
drepHex = drepIdToRaw(request.params.drep_id);
} catch {
return handle400Custom(reply, 'Invalid or malformed drep id.');
}

const clientDbSync = await getDbSync(fastify);

try {
Expand All @@ -24,12 +34,7 @@ async function route(fastify: FastifyInstance) {
)
: await clientDbSync.query<QueryTypes.DRepsDrepIDDelegators>(
SQLQuery.get('governance_dreps_drep_id_delegators'),
[
request.query.order,
request.query.count,
request.query.page,
request.params.drep_id,
],
[request.query.order, request.query.count, request.query.page, drepHex],
);

clientDbSync.release();
Expand Down
13 changes: 11 additions & 2 deletions src/routes/governance/dreps/drep-id/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { FastifyInstance, FastifyRequest } from 'fastify';
import * as QueryTypes from '../../../../types/queries/governance.js';
import * as ResponseTypes from '../../../../types/responses/governance.js';
import { getDbSync } from '../../../../utils/database.js';
import { handle404 } from '../../../../utils/error-handler.js';
import { handle400Custom, handle404 } from '../../../../utils/error-handler.js';
import { SQLQuery } from '../../../../sql/index.js';
import { getSchemaForEndpoint } from '@blockfrost/openapi';
import { drepIdToRaw } from '../../../../utils/bech32.js';

async function route(fastify: FastifyInstance) {
fastify.route({
Expand All @@ -13,13 +14,21 @@ async function route(fastify: FastifyInstance) {
schema: getSchemaForEndpoint('/governance/dreps/{drep_id}'),

handler: async (request: FastifyRequest<QueryTypes.RequestDRepID>, reply) => {
let drepHex;

try {
drepHex = drepIdToRaw(request.params.drep_id);
} catch {
return handle400Custom(reply, 'Invalid or malformed drep id.');
}

const clientDbSync = await getDbSync(fastify);

try {
const { rows }: { rows: ResponseTypes.DRepsDrepID[] } =
await clientDbSync.query<QueryTypes.DRepsDrepID>(
SQLQuery.get('governance_dreps_drep_id'),
[request.params.drep_id],
[drepHex],
);

clientDbSync.release();
Expand Down
13 changes: 11 additions & 2 deletions src/routes/governance/dreps/drep-id/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { FastifyInstance, FastifyRequest } from 'fastify';
import * as QueryTypes from '../../../../types/queries/governance.js';
import * as ResponseTypes from '../../../../types/responses/governance.js';
import { getDbSync } from '../../../../utils/database.js';
import { handle404 } from '../../../../utils/error-handler.js';
import { handle400Custom, handle404 } from '../../../../utils/error-handler.js';
import { SQLQuery } from '../../../../sql/index.js';
import { getSchemaForEndpoint } from '@blockfrost/openapi';
import { drepIdToRaw } from '../../../../utils/bech32.js';

async function route(fastify: FastifyInstance) {
fastify.route({
Expand All @@ -13,13 +14,21 @@ async function route(fastify: FastifyInstance) {
schema: getSchemaForEndpoint('/governance/dreps/{drep_id}/metadata'),

handler: async (request: FastifyRequest<QueryTypes.RequestDRepID>, reply) => {
let drepHex;

try {
drepHex = drepIdToRaw(request.params.drep_id);
} catch {
return handle400Custom(reply, 'Invalid or malformed drep id.');
}

const clientDbSync = await getDbSync(fastify);

try {
const { rows }: { rows: ResponseTypes.DRepsDrepIDMetadata[] } =
await clientDbSync.query<QueryTypes.DRepsDrepIDMetadata>(
SQLQuery.get('governance_dreps_drep_id_metadata'),
[request.params.drep_id],
[drepHex],
);

clientDbSync.release();
Expand Down
17 changes: 11 additions & 6 deletions src/routes/governance/dreps/drep-id/updates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { getDbSync } from '../../../../utils/database.js';
import { SQLQuery } from '../../../../sql/index.js';
import { getSchemaForEndpoint } from '@blockfrost/openapi';
import { isUnpaged } from '../../../../utils/routes.js';
import { handle400Custom } from '@blockfrost/blockfrost-utils/lib/fastify.js';
import { drepIdToRaw } from '../../../../utils/bech32.js';

async function route(fastify: FastifyInstance) {
fastify.route({
Expand All @@ -13,6 +15,14 @@ async function route(fastify: FastifyInstance) {
schema: getSchemaForEndpoint('/governance/dreps/{drep_id}/updates'),

handler: async (request: FastifyRequest<QueryTypes.RequestParametersDRepID>, reply) => {
let drepHex;

try {
drepHex = drepIdToRaw(request.params.drep_id);
} catch {
return handle400Custom(reply, 'Invalid or malformed drep id.');
}

const clientDbSync = await getDbSync(fastify);

try {
Expand All @@ -24,12 +34,7 @@ async function route(fastify: FastifyInstance) {
)
: await clientDbSync.query<QueryTypes.DRepsDrepIDUpdates>(
SQLQuery.get('governance_dreps_drep_id_updates'),
[
request.query.order,
request.query.count,
request.query.page,
request.params.drep_id,
],
[request.query.order, request.query.count, request.query.page, drepHex],
);

clientDbSync.release();
Expand Down
17 changes: 11 additions & 6 deletions src/routes/governance/dreps/drep-id/votes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { getDbSync } from '../../../../utils/database.js';
import { SQLQuery } from '../../../../sql/index.js';
import { getSchemaForEndpoint } from '@blockfrost/openapi';
import { isUnpaged } from '../../../../utils/routes.js';
import { handle400Custom } from '@blockfrost/blockfrost-utils/lib/fastify.js';
import { drepIdToRaw } from '../../../../utils/bech32.js';

async function route(fastify: FastifyInstance) {
fastify.route({
Expand All @@ -13,6 +15,14 @@ async function route(fastify: FastifyInstance) {
schema: getSchemaForEndpoint('/governance/dreps/{drep_id}/votes'),

handler: async (request: FastifyRequest<QueryTypes.RequestParametersDRepID>, reply) => {
let drepHex;

try {
drepHex = drepIdToRaw(request.params.drep_id);
} catch {
return handle400Custom(reply, 'Invalid or malformed drep id.');
}

const clientDbSync = await getDbSync(fastify);

try {
Expand All @@ -24,12 +34,7 @@ async function route(fastify: FastifyInstance) {
)
: await clientDbSync.query<QueryTypes.DRepsDrepIDUpdates>(
SQLQuery.get('governance_dreps_drep_id_votes'),
[
request.query.order,
request.query.count,
request.query.page,
request.params.drep_id,
],
[request.query.order, request.query.count, request.query.page, drepHex],
);

clientDbSync.release();
Expand Down
2 changes: 1 addition & 1 deletion src/sql/governance/dreps_drep_id.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ FROM drep_hash dh
FROM queried_epoch
)
)
WHERE dh.view = $1
WHERE dh.raw = $1
ORDER BY (tx_id, cert_index) DESC
LIMIT 1
2 changes: 1 addition & 1 deletion src/sql/governance/dreps_drep_id_delegators.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ FROM (
FROM delegation_vote dv
JOIN drep_hash dh ON (dh.id = dv.drep_hash_id)
JOIN stake_address sa ON (sa.id = dv.addr_id)
WHERE dh.view = $4
WHERE dh.raw = $4
AND dv.id = (
SELECT MAX(id)
FROM delegation_vote
Expand Down
2 changes: 1 addition & 1 deletion src/sql/governance/dreps_drep_id_metadata.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ FROM drep_hash dh
JOIN drep_registration dr ON (dh.id = dr.drep_hash_id)
JOIN voting_anchor va ON (dr.voting_anchor_id = va.id)
JOIN off_chain_vote_data ocvd ON (ocvd.voting_anchor_id = va.id)
WHERE dh.view = $1
WHERE dh.raw = $1
ORDER BY (dr.tx_id, dr.cert_index) DESC
LIMIT 1
2 changes: 1 addition & 1 deletion src/sql/governance/dreps_drep_id_updates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ SELECT encode(tx.hash, 'hex') AS "tx_hash",
FROM drep_hash dh
JOIN drep_registration dr ON (dh.id = dr.drep_hash_id)
JOIN tx ON (dr.tx_id = tx.id)
WHERE dh.view = $4
WHERE dh.raw = $4
ORDER BY CASE
WHEN LOWER($1) = 'desc' THEN dr.id
END DESC,
Expand Down
2 changes: 1 addition & 1 deletion src/sql/governance/dreps_drep_id_votes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ SELECT encode(tx.hash, 'hex') AS "tx_hash",
FROM voting_procedure vp
JOIN drep_hash dh ON (vp.drep_voter = dh.id)
JOIN tx ON (vp.tx_id = tx.id)
WHERE dh.view = $4
WHERE dh.raw = $4
ORDER BY CASE
WHEN LOWER($1) = 'desc' THEN vp.id
END DESC,
Expand Down
14 changes: 14 additions & 0 deletions src/utils/bech32.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { bech32 } from 'bech32';

export const drepIdToRaw = (bechDrepId: string): string => {
const { prefix, words } = bech32.decode(bechDrepId);

console.log('prefix', prefix);
if (prefix !== 'drep' && prefix !== 'drep_script') {
throw new Error('Invalid drep id prefix');
}

const hashBuf = Buffer.from(bech32.fromWords(words));

return `\\x${Buffer.from(hashBuf).toString('hex')}`;
};
25 changes: 25 additions & 0 deletions test/unit/tests/utils/bech32.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { drepIdToRaw } from '../../../../src/utils/bech32.js';
import { describe, expect, test } from 'vitest';

describe('bech32', () => {
test('drepIdToRaw', () => {
expect(drepIdToRaw('drep1y3wylkrkyt3q6u078ajh8f2henflpsq5hrcqhfa3yfmlqx7z66n')).toEqual(
'\\x245c4fd87622e20d71fe3f6573a557ccd3f0c014b8f00ba7b12277f0',
);

expect(drepIdToRaw('drep1edu7a90eszdus0hguck2w3lxr5r0juvc9frrxv3d2e6fcnqte0e')).toEqual(
'\\xcb79ee95f9809bc83ee8e62ca747e61d06f971982a4633322d56749c',
);

expect(drepIdToRaw('drep_script1hmgwyt6zv89j5htlnwcttk95lr0x7r87sxzr9dumxnc3vadhlap')).toEqual(
'\\xbed0e22f4261cb2a5d7f9bb0b5d8b4f8de6f0cfe818432b79b34f116',
);

expect(drepIdToRaw('drep1hmgwyt6zv89j5htlnwcttk95lr0x7r87sxzr9dumxnc3vj02hpq')).toEqual(
'\\xbed0e22f4261cb2a5d7f9bb0b5d8b4f8de6f0cfe818432b79b34f116',
);
expect(drepIdToRaw('drep_script16pxnn38ykshfahwmkaqmke3kdqaksg4w935d7uztvh8y5sh6f6d')).toEqual(
'\\xd04d39c4e4b42e9edddbb741bb6636683b6822ae2c68df704b65ce4a',
);
});
});
Loading

0 comments on commit 82be016

Please sign in to comment.