From ca92bc9da1e1bc27b5182a6c148a4209e908e109 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Przytu=C5=82a?= Date: Tue, 13 Dec 2022 15:50:58 +0100 Subject: [PATCH] fix: credential values encoding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Łukasz Przytuła --- .../credentials/formats/indy/IndyCredentialUtils.ts | 12 +++++++++++- .../indy/__tests__/IndyCredentialUtils.test.ts | 8 ++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/core/src/modules/credentials/formats/indy/IndyCredentialUtils.ts b/packages/core/src/modules/credentials/formats/indy/IndyCredentialUtils.ts index 0badb0c735..34042333e8 100644 --- a/packages/core/src/modules/credentials/formats/indy/IndyCredentialUtils.ts +++ b/packages/core/src/modules/credentials/formats/indy/IndyCredentialUtils.ts @@ -156,7 +156,13 @@ export class IndyCredentialUtils { } // If value is an int32 number string return as number string - if (isString(value) && !isEmpty(value) && !isNaN(Number(value)) && this.isInt32(Number(value))) { + if ( + isString(value) && + !isEmpty(value) && + !isNaN(Number(value)) && + this.isNumeric(value) && + this.isInt32(Number(value)) + ) { return Number(value).toString() } @@ -194,4 +200,8 @@ export class IndyCredentialUtils { // Check if number is integer and in range of int32 return Number.isInteger(number) && number >= minI32 && number <= maxI32 } + + private static isNumeric(value: string) { + return /^-?\d+$/.test(value) + } } diff --git a/packages/core/src/modules/credentials/formats/indy/__tests__/IndyCredentialUtils.test.ts b/packages/core/src/modules/credentials/formats/indy/__tests__/IndyCredentialUtils.test.ts index 7d629d2f66..e89a849001 100644 --- a/packages/core/src/modules/credentials/formats/indy/__tests__/IndyCredentialUtils.test.ts +++ b/packages/core/src/modules/credentials/formats/indy/__tests__/IndyCredentialUtils.test.ts @@ -74,6 +74,14 @@ const testEncodings: { [key: string]: { raw: string | number | boolean | null; e raw: '0.1', encoded: '9382477430624249591204401974786823110077201914483282671737639310288175260432', }, + 'str 1.0': { + raw: '1.0', + encoded: '94532235908853478633102631881008651863941875830027892478278578250784387892726', + }, + 'str 1': { + raw: '1', + encoded: '1', + }, 'leading zero number string': { raw: '012345', encoded: '12345',