From 5e2501c346e965f58bd3284a67875d8d49d0ffb6 Mon Sep 17 00:00:00 2001 From: Umair Sarfraz Date: Mon, 1 Jun 2020 20:42:37 +0000 Subject: [PATCH] fix: ignore null trytes during trytes to ascii conversion (#480) trytesToAscii() converts null trytes to non-breaking whitespaces. This (sometimes) lead to whitespaces at the end of ascii string. This commit fixes the issue by ignoring null trytes during trytes to ascii conversion. --- packages/converter/src/ascii.ts | 38 +++++++++++++---------- packages/converter/test/converter.test.ts | 7 +++++ 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/packages/converter/src/ascii.ts b/packages/converter/src/ascii.ts index 8b6f5095e..36c44002e 100644 --- a/packages/converter/src/ascii.ts +++ b/packages/converter/src/ascii.ts @@ -3,26 +3,26 @@ import * as errors from './errors' /** * This method converts ASCII characters to [trytes](https://docs.iota.org/docs/getting-started/0.1/introduction/ternary). - * + * * ## Related methods - * + * * To convert trytes to ASCII characters, use the [`trytesToAscii()`]{@link #module_converter.trytesToAscii} method. - * + * * @method asciiToTrytes - * + * * @summary Converts ASCII characters to trytes. - * + * * @memberof module:converter * * @param {string} input - ASCII input - * + * * @example * ```js * let trytes = Converter.asciiToTrytes('Hello, where is my coffee?'); * ``` - * + * * @return {string} Trytes - * + * * @throws {errors.INVALID_ASCII_CHARS}: Make sure that the `input` argument contains only valid ASCII characters. */ export const asciiToTrytes = (input: string): string => { @@ -45,28 +45,28 @@ export const asciiToTrytes = (input: string): string => { /** * This method converts trytes to ASCII characters. - * + * * Because each ASCII character is represented as 2 trytes, the given trytes must be of an even length. * * ## Related methods - * + * * To convert ASCII characters to trytes, use the [`asciiToTrytes()`]{@link #module_converter.asciiToTrytes} method. - * + * * @method trytesToAscii - * + * * @summary Converts trytes to ASCII characters. - * + * * @memberof module:converter * * @param {string} trytes - An even number of trytes - * + * * @example * ```js * let message = Converter.trytesToAscii('IOTA'); * ``` - * + * * @return {string} ASCII characters - * + * * @throws {errors.INVALID_TRYTES}: Make sure that the `trytes` argument contains only valid trytes (A-Z or 9). * @throws {errors.INVALID_ODD_LENGTH}: Make sure that the `trytes` argument contains an even number of trytes. */ @@ -82,7 +82,11 @@ export const trytesToAscii = (trytes: string): string => { let ascii = '' for (let i = 0; i < trytes.length; i += 2) { - ascii += String.fromCharCode(TRYTE_ALPHABET.indexOf(trytes[i]) + TRYTE_ALPHABET.indexOf(trytes[i + 1]) * 27) + const charCode = TRYTE_ALPHABET.indexOf(trytes[i]) + TRYTE_ALPHABET.indexOf(trytes[i + 1]) * 27 + + if (charCode) { + ascii += String.fromCharCode(charCode) + } } return ascii diff --git a/packages/converter/test/converter.test.ts b/packages/converter/test/converter.test.ts index 9e7c35c6f..550c8a760 100644 --- a/packages/converter/test/converter.test.ts +++ b/packages/converter/test/converter.test.ts @@ -27,6 +27,13 @@ describe('trytesToAscii(trytes)', async assert => { expected: 'IOTA', }) + assert({ + given: 'tryte-encoded ascii with 9s', + should: 'convert to ascii ignoring zero/null char code', + actual: trytesToAscii('SBYBCCKB9999'), + expected: 'IOTA', + }) + assert({ given: 'non-trytes', should: 'throw error',