Skip to content

Commit

Permalink
fix: reuse multiasset value validation for input operations
Browse files Browse the repository at this point in the history
  • Loading branch information
guido-ta committed Jan 26, 2021
1 parent 0eed17e commit 8fd1ede
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
Expand Up @@ -17,7 +17,7 @@ import { isPolicyIdValid, isTokenNameValid } from '../validations';
import { generateRewardAddress } from './addresses';
import { getStakingCredentialFromHex } from './staking-credentials';

const isNumeric = (value: string): boolean => /^\d+$/.test(value);
const isPositiveNumber = (value: string): boolean => /^\+?\d+/.test(value);

/**
* This function validates and parses token bundles that might be attached to unspents
Expand Down Expand Up @@ -58,7 +58,7 @@ const validateAndParseTokenBundle = (
`Token with name ${tokenName} for policy ${multiAsset.policyId} has no value or is empty`
);
}
if (!isNumeric(asset.value)) {
if (!isPositiveNumber(asset.value)) {
logger.error(`[validateAndParseTokenBundle] Asset ${tokenName} has negative or invalid value '${asset.value}'`);
throw ErrorFactory.transactionOutputsParametersMissingError(
`Asset ${tokenName} has negative or invalid value '${asset.value}'`
Expand Down Expand Up @@ -91,7 +91,7 @@ const validateAndParseTransactionOutput = (
logger.error('[validateAndParseTransactionOutput] Output has missing amount value field');
throw ErrorFactory.transactionOutputsParametersMissingError('Output has missing amount value field');
}
if (!isNumeric(outputValue)) {
if (!isPositiveNumber(outputValue)) {
logger.error(`[validateAndParseTransactionOutput] Output has negative or invalid value '${outputValue}'`);
throw ErrorFactory.transactionOutputsParametersMissingError('Output has negative amount value');
}
Expand Down Expand Up @@ -123,7 +123,7 @@ const validateAndParseTransactionInput = (
logger.error('[validateAndParseTransactionInput] Input has missing amount value field');
throw ErrorFactory.transactionInputsParametersMissingError('Input has missing amount value field');
}
if (/^\+?\d+/.test(value)) {
if (isPositiveNumber(value)) {
logger.error('[validateAndParseTransactionInput] Input has positive value');
throw ErrorFactory.transactionInputsParametersMissingError('Input has positive amount value');
}
Expand Down
Expand Up @@ -418,7 +418,7 @@ describe(CONSTRUCTION_PREPROCESS_ENDPOINT, () => {
});
});

test('Should fail if MultiAsset value is negative', async () => {
test('Should fail if MultiAsset value for output operation is negative', async () => {
const value = '-10000';
const operations = mod(
1,
Expand Down

0 comments on commit 8fd1ede

Please sign in to comment.