Skip to content

Commit

Permalink
unified string to uint8array
Browse files Browse the repository at this point in the history
  • Loading branch information
ahangsu committed Nov 29, 2022
1 parent 9225675 commit 4c2afb1
Show file tree
Hide file tree
Showing 2 changed files with 26 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
19 changes: 19 additions & 0 deletions tests/10.ABI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,25 @@ 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 ABIByteType().encode(10), new Uint8Array([10])],
[new ABIByteType().encode(255), new Uint8Array([255])],
[new ABIBoolType().encode(true), new Uint8Array([128])],
Expand Down

0 comments on commit 4c2afb1

Please sign in to comment.