diff --git a/src/lib/decodeData.ts b/src/lib/decodeData.ts index 64e5f097..78750d09 100644 --- a/src/lib/decodeData.ts +++ b/src/lib/decodeData.ts @@ -19,7 +19,6 @@ * @author Callum Grindle <@CallumGrindle> * @date 2023 */ - import { isHexStrict } from 'web3-utils'; import { COMPACT_BYTES_ARRAY_STRING } from '../constants/constants'; @@ -48,17 +47,8 @@ const isValidTupleDefinition = (tupleContent: string): boolean => { return true; }; -const extractValueTypesFromTuple = (tupleContent: string): string[] => { - let valueTypeToDecode = tupleContent; - - if (tupleContent.includes(COMPACT_BYTES_ARRAY_STRING)) { - valueTypeToDecode = tupleContent.replace(COMPACT_BYTES_ARRAY_STRING, ''); - } - - return valueTypeToDecode - .substring(1, valueTypeToDecode.length - 1) - .split(','); -}; +const extractTupleElements = (tupleContent: string): string[] => + tupleContent.substring(1, tupleContent.length - 1).split(','); export const isValidTuple = (valueType: string, valueContent: string) => { if ( @@ -68,14 +58,14 @@ export const isValidTuple = (valueType: string, valueContent: string) => { return false; } - // At this stage, we can assume the user is trying to use a tuple, let's throw errors instead of returning - // false + // At this stage, we can assume the user is trying to use a tuple, + // let's throw errors instead of returning false - const valueTypeParts = extractValueTypesFromTuple(valueType); + // Sanitize the string to keep only the tuple, if we are dealing with `CompactBytesArray` + const valueTypeToDecode = valueType.replace(COMPACT_BYTES_ARRAY_STRING, ''); - const valueContentParts = valueContent - .substring(1, valueContent.length - 1) - .split(','); + const valueTypeParts = extractTupleElements(valueTypeToDecode); + const valueContentParts = extractTupleElements(valueContent); if (valueTypeParts.length !== valueContentParts.length) { throw new Error( @@ -143,20 +133,19 @@ export const decodeTupleKeyValue = ( ): Array => { // We assume data has already been validated at this stage - const valueTypeParts = extractValueTypesFromTuple(valueType); + // Sanitize the string to keep only the tuple, if we are dealing with `CompactBytesArray` + const valueTypeToDecode = valueType.replace(COMPACT_BYTES_ARRAY_STRING, ''); - const valueContentParts = valueContent - .substring(1, valueContent.length - 1) - .split(','); + const valueTypeParts = extractTupleElements(valueTypeToDecode); + const valueContentParts = extractTupleElements(valueContent); const bytesLengths: number[] = []; + valueTypeParts.forEach((valueTypePart) => { const regexMatch = valueTypePart.match(tupleValueTypesRegex); // if we are dealing with `bytesN` - if (regexMatch) { - bytesLengths.push(parseInt(regexMatch[1], 10)); - } + if (regexMatch) bytesLengths.push(parseInt(regexMatch[1], 10)); if (valueTypePart === 'address') bytesLengths.push(20); });