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(tlv_conversion): Convert to Characters Rather than Hex #5

Merged
merged 1 commit into from
Nov 12, 2021

Conversation

obahareth
Copy link
Contributor

Description

The original implementation was converting to hex, whereas all we needed to do was to convert to characters and ensure ASCII-8BIT encoding.

ZATCA expects ASCII, the original implementation was assuming they were expecting hexadecimal versions of numbers, but the desired values are in ASCII.

This change ensures that the whole thing is forced to be ASCII-8BIT.

It was unclear from ZATCA if the value should be left as Unicode or not, but this presents a challenge because we would have to concatenate an ASCII string with a UTF-8 string and that's not possible.

We use String.force_encoding to ASCII-8BIT to get around those errors and avoid String#encode because it will throw an exception because you can't convert UTF-8 to ASCII-8BIT because there is no point of reference for what each character maps to.

This approach generates the same output as ZATCA's SDK does.

Summary

  • Get character values of tag ID and value length
  • Force TLV to be encoded in ASCII-8BIT (to work with Unicode strings)
  • Add Unicode tests to test suite
  • Validate output against ZATCA's SDK

The original implementation was converting to hex, whereas all we needed to do was to convert to characters and ensure ASCII-8BIT encoding.

ZATCA expects ASCII, the original implementation was assuming they were expecting hexadecimal versions of numbers, but the desired values are in ASCII.

This change ensures that the whole thing is forced to be ASCII-8BIT.

It was unclear from ZATCA if the `value` should be left as Unicode or not, but this presents a challenge because we would have to concatenate an ASCII string with a UTF-8 string and that's not possible.

We use `String.force_encoding` to ASCII-8BIT to get around those errors and avoid `String#encode` because it will throw an exception because you can't convert UTF-8 to ASCII-8BIT because there is no point of reference for what each character maps to.

This approach generates the same output as [ZATCA's SDK](https://zatca.gov.sa/en/E-Invoicing/SystemsDevelopers/ComplianceEnablementToolbox/Pages/DownloadSDK.aspx) does.
@obahareth obahareth merged commit 19c71c1 into main Nov 12, 2021
@obahareth obahareth deleted the fix/tlv-conversion branch November 12, 2021 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant