Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: persist leading zero #1836

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/anoncreds/src/utils/__tests__/credential.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const testEncodings: { [key: string]: { raw: string | number | boolean | null; e
},
'leading zero number string': {
raw: '012345',
encoded: '12345',
encoded: '15442803653501515687133735982599955909858317553820932727345025407787217899333',
},
'chr 0': {
raw: String.fromCharCode(0),
Expand Down Expand Up @@ -119,6 +119,11 @@ describe('Utils | Credentials', () => {
mimeType: 'text/plain',
value: '1234',
}),
new CredentialPreviewAttribute({
name: 'id',
mimeType: 'text/plain',
value: '0678',
}),
]

expect(convertAttributesToCredentialValues(attributes)).toEqual({
Expand All @@ -127,6 +132,7 @@ describe('Utils | Credentials', () => {
encoded: '68086943237164982734333428280784300550565381723532936263016368251445461241953',
},
age: { raw: '1234', encoded: '1234' },
id: { raw: '0678', encoded: '77028439388269848458807170800403172154093562860161099407497319717275846030012' },
})
})
})
Expand Down
10 changes: 9 additions & 1 deletion packages/anoncreds/src/utils/credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const isString = (value: unknown): value is string => typeof value === 'string'
const isNumber = (value: unknown): value is number => typeof value === 'number'
const isBoolean = (value: unknown): value is boolean => typeof value === 'boolean'
const isNumeric = (value: string) => /^-?\d+$/.test(value)
const hasLeadingZero = (value: string) => value.length > 1 && value.startsWith('0')

const isInt32 = (number: number) => {
const minI32 = -2147483648
Expand Down Expand Up @@ -50,7 +51,14 @@ export function encodeCredentialValue(value: unknown) {
}

// If value is an int32 number string return as number string
if (isString(value) && !isEmpty(value) && !isNaN(Number(value)) && isNumeric(value) && isInt32(Number(value))) {
if (
isString(value) &&
!isEmpty(value) &&
!isNaN(Number(value)) &&
isNumeric(value) &&
isInt32(Number(value)) &&
!hasLeadingZero(value)
) {
return Number(value).toString()
}

Expand Down
Loading