Repository: algorand/js-algorand-sdk (ts)

Issue:
Cannot encode ABI string with non-ASCII characters
## Problem

When I try to encode the value `"What’s your favorite tall building?"` as an ABI `string`, an error occurs.

However, `"What's your favorite tall building?"` works fine. Note the difference between `’` and `'` -- the first is not an ASCII character, while the second is, which leads me to believe non-ASCII characters are not supported.

Pull request:
Bug-Fix: encode ABI string with non-ASCII characters
Fixes #699 

Patch:
diff --git a/src/abi/abi_type.ts b/src/abi/abi_type.ts
index 7ca39c1..3ea89e6 100644
--- a/src/abi/abi_type.ts
+++ b/src/abi/abi_type.ts
@@ -380,8 +380,13 @@ export class ABIStringType extends ABIType {
       throw new Error(`Cannot encode value as string: ${value}`);
     }
     const encodedBytes = Buffer.from(value);
-    const encodedLength = bigIntToBytes(value.length, LENGTH_ENCODE_BYTE_SIZE);
-    const mergedBytes = new Uint8Array(value.length + LENGTH_ENCODE_BYTE_SIZE);
+    const encodedLength = bigIntToBytes(
+      encodedBytes.length,
+      LENGTH_ENCODE_BYTE_SIZE
+    );
+    const mergedBytes = new Uint8Array(
+      encodedBytes.length + LENGTH_ENCODE_BYTE_SIZE
+    );
     mergedBytes.set(encodedLength);
     mergedBytes.set(encodedBytes, LENGTH_ENCODE_BYTE_SIZE);
     return mergedBytes;


Test patch:
diff --git a/tests/10.ABI.ts b/tests/10.ABI.ts
index c2dd071..1eeb4f0 100644
--- a/tests/10.ABI.ts
+++ b/tests/10.ABI.ts
@@ -236,6 +236,29 @@ describe('ABI encoding', () => {
           'MO2H6ZU47Q36GJ6GVHUKGEBEQINN7ZWVACMWZQGIYUOE3RBSRVYHV4ACJI'
         ).publicKey,
       ],
+      [
+        new ABIStringType().encode('What’s new'),
+        new Uint8Array([
+          0,
+          12,
+          87,
+          104,
+          97,
+          116,
+          226,
+          128,
+          153,
+          115,
+          32,
+          110,
+          101,
+          119,
+        ]),
+      ],
+      [
+        new ABIStringType().encode('😅🔨'),
+        new Uint8Array([0, 8, 240, 159, 152, 133, 240, 159, 148, 168]),
+      ],
       [new ABIByteType().encode(10), new Uint8Array([10])],
       [new ABIByteType().encode(255), new Uint8Array([255])],
       [new ABIBoolType().encode(true), new Uint8Array([128])],
@@ -453,6 +476,33 @@ describe('ABI encoding', () => {
         ),
         'MO2H6ZU47Q36GJ6GVHUKGEBEQINN7ZWVACMWZQGIYUOE3RBSRVYHV4ACJI',
       ],
+      [
+        new ABIStringType().decode(
+          new Uint8Array([
+            0,
+            12,
+            87,
+            104,
+            97,
+            116,
+            226,
+            128,
+            153,
+            115,
+            32,
+            110,
+            101,
+            119,
+          ])
+        ),
+        'What’s new',
+      ],
+      [
+        new ABIStringType().decode(
+          new Uint8Array([0, 8, 240, 159, 152, 133, 240, 159, 148, 168])
+        ),
+        '😅🔨',
+      ],
       [new ABIByteType().decode(new Uint8Array([10])), 10],
       [new ABIByteType().decode(new Uint8Array([255])), 255],
       [new ABIBoolType().decode(new Uint8Array([128])), true],