Skip to content

Commit

Permalink
Bug-Fix: encode ABI string with non-ASCII characters (#700)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahangsu authored Nov 30, 2022
1 parent 9225675 commit 4f0c9de
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/abi/abi_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
50 changes: 50 additions & 0 deletions tests/10.ABI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])],
Expand Down Expand Up @@ -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],
Expand Down

0 comments on commit 4f0c9de

Please sign in to comment.