Skip to content

Commit

Permalink
fix: from to errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirvolek committed Aug 30, 2023
1 parent 9ce31f8 commit eb3047c
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 170 deletions.
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"dependencies": {
"@blockfrost/blockfrost-js": "5.2.0",
"@blockfrost/blockfrost-utils": "2.7.1",
"@blockfrost/blockfrost-utils": "2.8.0",
"@blockfrost/openapi": "0.1.58",
"@emurgo/cardano-serialization-lib-nodejs": "11.3.0",
"@emurgo/cip14-js": "^3.0.1",
Expand Down
3 changes: 2 additions & 1 deletion src/routes/addresses/address/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import * as QueryTypes from '../../../types/queries/addresses.js';
import * as ResponseTypes from '../../../types/responses/addresses.js';
import { getDbSync } from '../../../utils/database.js';
import { handle400Custom, handle404, handleInvalidAddress } from '../../../utils/error-handler.js';
import { getAdditionalParametersFromRequest, toJSONStream } from '../../../utils/string-utils.js';
import { toJSONStream } from '../../../utils/string-utils.js';
import { getAdditionalParametersFromRequest } from '@blockfrost/blockfrost-utils/lib/fastify.js';
import { getAddressTypeAndPaymentCred } from '../../../utils/validation.js';

async function route(fastify: FastifyInstance) {
Expand Down
61 changes: 0 additions & 61 deletions src/utils/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,67 +10,6 @@ export const getEndpointFromUrl = (url: string): string => {
return endpoint;
};

export const getAdditionalParametersFromRequest = (
from?: string,
to?: string,
): unknown[] | 'outOfRangeOrMalformedErr' => {
// eslint-disable-next-line unicorn/no-useless-undefined
const parameterArray: unknown[] = Array.from({ length: 4 }).fill(undefined);

try {
const minInt = 0;
const maxInt = 2_147_483_647;

if (from !== undefined) {
const fromTokens = from.split(':');
const requestParameterIsOK = fromTokens.length <= 2;

if (requestParameterIsOK) {
const [heightString, indexString] = fromTokens;
const height = Number.parseInt(heightString, 10);
const index = Number.parseInt(indexString, 10); // NaN in case of missing index

if (
height >= minInt &&
height <= maxInt &&
(indexString === undefined || (index >= minInt && index <= maxInt))
) {
parameterArray[0] = heightString;
parameterArray[1] = indexString;
} else {
return 'outOfRangeOrMalformedErr';
}
}
}

if (to !== undefined) {
const toTokens = to.split(':');
const requestParameterIsOK = toTokens.length <= 2;

if (requestParameterIsOK) {
const [heightString, indexString] = toTokens;
const height = Number.parseInt(heightString, 10);
const index = Number.parseInt(indexString, 10); // NaN in case of missing index

if (
height >= minInt &&
height <= maxInt &&
(indexString === undefined || (index >= minInt && index <= maxInt))
) {
parameterArray[2] = heightString;
parameterArray[3] = indexString;
} else {
return 'outOfRangeOrMalformedErr';
}
}
}
} catch (error) {
console.error(error);
return 'outOfRangeOrMalformedErr';
}
return parameterArray;
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const sortKeysInObject = (object: any) => {
if (object === null || typeof object !== 'object') return object;
Expand Down
Loading

0 comments on commit eb3047c

Please sign in to comment.