From c05e2a5a0c920c1bcfa7b23fcc2575bea60a2023 Mon Sep 17 00:00:00 2001 From: srikanth716 Date: Fri, 16 Jan 2026 10:42:13 +0530 Subject: [PATCH] [INJIVER-1512] fix:decoded mapped data conversion Signed-off-by: srikanth716 --- js/package-lock.json | 13 +++++-------- js/package.json | 5 +++-- js/src/index.js | 15 ++++++++++----- js/src/utils/cborUtils.js | 13 +++++++++++-- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/js/package-lock.json b/js/package-lock.json index d6f5f9e..b151f92 100644 --- a/js/package-lock.json +++ b/js/package-lock.json @@ -1,12 +1,12 @@ { "name": "@injistack/pixelpass", - "version": "0.8.0-RC1", + "version": "0.8.0-RC2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@injistack/pixelpass", - "version": "0.8.0-RC1", + "version": "0.8.0-RC2", "license": "MPL-2.0", "dependencies": { "base45-web": "^1.0.2", @@ -5591,8 +5591,7 @@ "version": "7.21.0-placeholder-for-preset-env.2", "resolved": "https://registry.npmjs.org/@babel/plugin-proposal-private-property-in-object/-/plugin-proposal-private-property-in-object-7.21.0-placeholder-for-preset-env.2.tgz", "integrity": "sha512-SOSkfJDddaM7mak6cPEpswyTRnuRltl429hMraQEglW+OkovnCzsiszTmsrlY//qLFjCpQDFRvjdm2wA5pPm9w==", - "dev": true, - "requires": {} + "dev": true }, "@babel/plugin-syntax-async-generators": { "version": "7.8.4", @@ -7203,8 +7202,7 @@ "version": "1.5.1", "resolved": "https://registry.npmjs.org/dedent/-/dedent-1.5.1.tgz", "integrity": "sha512-+LxW+KLWxu3HW3M2w2ympwtqPrqYRzU8fqi6Fhd18fBALe15blJPI/I4+UHveMVG6lJqB4JNd4UG0S5cnVHwIg==", - "dev": true, - "requires": {} + "dev": true }, "deepmerge": { "version": "4.3.1", @@ -7838,8 +7836,7 @@ "version": "1.2.3", "resolved": "https://registry.npmjs.org/jest-pnp-resolver/-/jest-pnp-resolver-1.2.3.tgz", "integrity": "sha512-+3NpwQEnRoIBtx4fyhblQDPgJI0H1IEIkX7ShLUjPGA7TtUTvI1oiKi3SR4oBR0hQhQR80l4WAe5RrXBwWMA8w==", - "dev": true, - "requires": {} + "dev": true }, "jest-regex-util": { "version": "29.6.3", diff --git a/js/package.json b/js/package.json index f53e038..a1f4a56 100644 --- a/js/package.json +++ b/js/package.json @@ -1,6 +1,6 @@ { "name": "@injistack/pixelpass", - "version": "0.8.0-RC1", + "version": "0.8.0-RC2", "repository": "https://github.com/inji/pixelpass", "author": "INJI", "license": "MPL-2.0", @@ -9,7 +9,8 @@ "main": "src/index.js", "scripts": { "test": "jest --coverage", - "verify": "npm test" + "verify": "npm test", + "localPublish": "npm version patch && npm test && npm publish --access public --registry http://localhost:4873" }, "devDependencies": { "@babel/core": "^7.24.4", diff --git a/js/src/index.js b/js/src/index.js index 47a16f9..474bb93 100644 --- a/js/src/index.js +++ b/js/src/index.js @@ -18,6 +18,7 @@ const pako = require("pako"); const cbor = require("cbor-web"); const JSZip = require("jszip"); const { + hexToBytes, translateToJson, replaceKeysAtDepth, replaceValuesForClaim169, @@ -147,19 +148,23 @@ function decodeMappedData( } let jsonData; - + let decoded; try { - const bytes = Buffer.from(data, "hex"); - const decoded = cbor.decodeFirstSync(bytes); + const bytes = hexToBytes(data); + + decoded = cbor.decodeFirstSync(bytes); jsonData = translateToJson(decoded); } catch (error) { + console.warn("Failed to decode as CBOR, trying JSON...", error); try { jsonData = JSON.parse(data); } catch (parseError) { - throw new Error(`Failed to decode data as CBOR or JSON: ${parseError.message}`); + console.error("Failed to decode as JSON:", parseError); + throw new Error( + `Failed to decode data as CBOR or JSON: ${parseError.message}` + ); } } - if (keyMapper) { if (!Array.isArray(keyMapper)) { throw new TypeError( diff --git a/js/src/utils/cborUtils.js b/js/src/utils/cborUtils.js index f9f9c31..e0b191f 100644 --- a/js/src/utils/cborUtils.js +++ b/js/src/utils/cborUtils.js @@ -7,6 +7,14 @@ const { CLAIM_169_ROOT_REVERSE_VALUE_MAPPER, } = require("../shared/Constants"); +function hexToBytes(hex) { + const bytes = new Uint8Array(hex.length / 2); + for (let i = 0; i < hex.length; i += 2) { + bytes[i / 2] = parseInt(hex.substr(i, 2), 16); + } + return bytes; +} + function translateToJson(value) { if (value instanceof Map) { const data = {}; @@ -167,12 +175,13 @@ function replaceValuesForClaim169(jsonData) { } function decodeFromBase64UrlFormat(content) { - return Buffer.from(content, 'base64url'); + return Buffer.from(content, "base64url"); } module.exports = { + hexToBytes, translateToJson, replaceKeysAtDepth, replaceValuesForClaim169, - decodeFromBase64UrlFormat + decodeFromBase64UrlFormat, };