DEV Community

Discussion on: JavaScript: How to decode the GreenPass QR code

Collapse
 
lgrauser profile image
lgrauser

Hi Lorenzo,

Thanks for your post.
I was trying your code using the string contained in my UE QR CODE but when I used decodeAllSync I got the following error :
[ERR_ENCODING_INVALID_ENCODED_DATA]: The encoded data was not valid for encoding utf-8

Any idea of what could have went wrong ?

Collapse
 
lmillucci profile image
Lorenzo Millucci

Have you decoded data from Base45 format and have you decompressed it using zlib before using cbor?

Collapse
 
Sloan, the sloth mascot
Comment deleted
 
lmillucci profile image
Lorenzo Millucci

I've decoded your QR

{
  "v": [
    {
      "ci": "urn:uvci:....",
      "co": "FR",
      "dn": 2,
      "dt": "2021-07-01",
      "is": "CNAM",
      "ma": "...",
      "mp": "...",
      "sd": 2,
      "tg": "840539006",
      "vp": "...."
    }
  ],
  "dob": "1980-...",
  "nam": {
    "fn": "GR....",
    "gn": "LUD...",
    "fnt": "GR...",
    "gnt": "LUD..."
  },
  "ver": "1.3.0"
}
Enter fullscreen mode Exit fullscreen mode

PS: I suggest you to remove your data from the post above as anyone can decode it

Thread Thread
 
lgrauser profile image
lgrauser

Oh yes thank you. (gonna remove the QR data)

Here's my code

const greenpassJpeg = fs.readFileSync('qrcodelgr.jpg');
const greenpassImageData = jpeg.decode(greenpassJpeg, {useTArray: true});
const decodedGreenpass = jsQR (greenpassImageData.data, greenpassImageData.width, greenpassImageData.height);
console.log('pass decode');
console.log(decodedGreenpass.data);
const greenpassBody = decodedGreenpass.data.substr(4);

const decodedData = base45.decode(greenpassBody);

console.log('Post B45 decode');
console.log(decodedData);

const output = pako.deflate(decodedData);
console.log('apres zlib');
console.log(output);
const results = cbor.decodeAllSync(output);
consol.log('apres decodeallsync');
[headers1, headers2, cbor_data, signature] = results [0] .value;

const greenpassData = cbor.decodeAllSync(cbor_data);

console.log (JSON.stringify(greenpassData[0].get(-260).get (1), null, 2));

Thread Thread
 
lmillucci profile image
Lorenzo Millucci

you are using pako.deflate(decodedData) instead of const output = pako.inflate(decodedData);

Thread Thread
 
lgrauser profile image
lgrauser

oh yes . Sorry and once again thank you so much

Collapse
 
lgrauser profile image
lgrauser

with the upload

Collapse
 
lgrauser profile image
lgrauser

And the image :-)
Thank you so much again

Collapse
 
andrea_guido profile image
andrea guido

Hi Igrauser, Are you abe to help me in to create a valid gren pass from an existing valid one??
Thanks

Collapse
 
andrea_guido profile image
andrea guido

Igrauser can you help me with that?