Skip to content
Merged
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
13 changes: 5 additions & 8 deletions js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions js/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down
15 changes: 10 additions & 5 deletions js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const pako = require("pako");
const cbor = require("cbor-web");
const JSZip = require("jszip");
const {
hexToBytes,
translateToJson,
replaceKeysAtDepth,
replaceValuesForClaim169,
Expand Down Expand Up @@ -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(
Expand Down
13 changes: 11 additions & 2 deletions js/src/utils/cborUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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,
};