Skip to content

Commit 653acc4

Browse files
fix: byteArray encoding for less than 31 chars (#1011)
* fix: byteArray encoding for less than 31 chars * refactor: reuse byteArray utility for typed data hashing --------- Co-authored-by: Petar Penovic <pp@spaceshard.io>
1 parent 880b906 commit 653acc4

File tree

5 files changed

+25
-53
lines changed

5 files changed

+25
-53
lines changed

__tests__/cairo1v2.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ describe('Cairo 1', () => {
10311031
const callD2 = CallData.compile({ mess: message });
10321032
expect(callD2).toEqual(expectedResult);
10331033
const callD3 = CallData.compile({ mess: byteArray.byteArrayFromString('Take care.') });
1034-
expect(callD3).toEqual(['1', '0', '398475857363345939260718', '10']);
1034+
expect(callD3).toEqual(['0', '398475857363345939260718', '10']);
10351035
const str1 = await stringContract.get_string();
10361036
expect(str1).toBe(
10371037
"Cairo has become the most popular language for developers + charizards !@#$%^&*_+|:'<>?~`"

__tests__/cairo1v2_typed.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ describe('Cairo 1', () => {
973973
const callD2 = CallData.compile({ mess: message });
974974
expect(callD2).toEqual(expectedResult);
975975
const callD3 = CallData.compile({ mess: byteArray.byteArrayFromString('Take care.') });
976-
expect(callD3).toEqual(['1', '0', '398475857363345939260718', '10']);
976+
expect(callD3).toEqual(['0', '398475857363345939260718', '10']);
977977
const str1 = await stringContract.get_string();
978978
expect(str1).toBe(
979979
"Cairo has become the most popular language for developers + charizards !@#$%^&*_+|:'<>?~`"

__tests__/utils/shortString.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ describe('shortString', () => {
6565
pending_word_len: 0,
6666
});
6767
expect(byteArray.byteArrayFromString('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234')).toEqual({
68-
data: ['0x00'],
68+
data: [],
6969
pending_word: '0x4142434445464748494a4b4c4d4e4f505152535455565758595a31323334',
7070
pending_word_len: 30,
7171
});
7272
expect(byteArray.byteArrayFromString('')).toEqual({
73-
data: ['0x00'],
73+
data: [],
7474
pending_word: '0x00',
7575
pending_word_len: 0,
7676
});
@@ -90,14 +90,14 @@ describe('shortString', () => {
9090
});
9191
expect(
9292
byteArray.stringFromByteArray({
93-
data: ['0x00'],
93+
data: [],
9494
pending_word: '0x4142434445464748494a4b4c4d4e4f505152535455565758595a31323334',
9595
pending_word_len: 30,
9696
})
9797
).toBe('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234');
9898
expect(
9999
byteArray.stringFromByteArray({
100-
data: ['0x00'],
100+
data: [],
101101
pending_word: '0x00',
102102
pending_word_len: 0,
103103
})

src/utils/calldata/byteArray.ts

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { decodeShortString, encodeShortString, splitLongString } from '../shortS
99
* @example
1010
* ```typescript
1111
* const myByteArray = {
12-
* data: [ '0x00' ],
12+
* data: [],
1313
* pending_word: '0x414243444546474849',
1414
* pending_word_len: 9
1515
* }
@@ -36,39 +36,28 @@ export function stringFromByteArray(myByteArray: ByteArray): string {
3636
* @returns Cairo representation of a LongString
3737
* @example
3838
* ```typescript
39-
* const myByteArray: ByteArray = byteArrayFromStr("ABCDEFGHI");
39+
* const myByteArray: ByteArray = byteArrayFromString("ABCDEFGHI");
4040
* ```
4141
* Result is :
4242
* {
43-
* data: [ '0x00' ],
43+
* data: [],
4444
* pending_word: '0x414243444546474849',
4545
* pending_word_len: 9
4646
* }
4747
*/
48-
export function byteArrayFromString(myString: string): ByteArray {
49-
if (myString.length === 0) {
50-
return {
51-
data: ['0x00'],
52-
pending_word: '0x00',
53-
pending_word_len: 0,
54-
} as ByteArray;
55-
}
56-
const myShortStrings: string[] = splitLongString(myString);
57-
const remains: string = myShortStrings[myShortStrings.length - 1];
58-
const myShortStringsEncoded: BigNumberish[] = myShortStrings.map((shortStr) =>
59-
encodeShortString(shortStr)
60-
);
61-
if (remains.length === 31) {
62-
return {
63-
data: myShortStringsEncoded,
64-
pending_word: '0x00',
65-
pending_word_len: 0,
66-
} as ByteArray;
67-
}
68-
const pendingEncodedWord: BigNumberish = myShortStringsEncoded.pop()!;
48+
export function byteArrayFromString(targetString: string): ByteArray {
49+
const shortStrings: string[] = splitLongString(targetString);
50+
const remainder: string = shortStrings[shortStrings.length - 1];
51+
const shortStringsEncoded: BigNumberish[] = shortStrings.map(encodeShortString);
52+
53+
const [pendingWord, pendingWordLength] =
54+
remainder === undefined || remainder.length === 31
55+
? ['0x00', 0]
56+
: [shortStringsEncoded.pop()!, remainder.length];
57+
6958
return {
70-
data: myShortStringsEncoded.length === 0 ? ['0x00'] : myShortStringsEncoded,
71-
pending_word: pendingEncodedWord,
72-
pending_word_len: remains.length,
73-
} as ByteArray;
59+
data: shortStringsEncoded.length === 0 ? [] : shortStringsEncoded,
60+
pending_word: pendingWord,
61+
pending_word_len: pendingWordLength,
62+
};
7463
}

src/utils/typedData.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
StarkNetType,
88
TypedData,
99
} from '../types';
10+
import { byteArrayFromString } from './calldata/byteArray';
1011
import {
1112
computePedersenHash,
1213
computePedersenHashOnElements,
@@ -16,7 +17,7 @@ import {
1617
} from './hash';
1718
import { MerkleTree } from './merkle';
1819
import { isHex, toHex } from './num';
19-
import { encodeShortString, splitLongString } from './shortString';
20+
import { encodeShortString } from './shortString';
2021

2122
/** @deprecated prefer importing from 'types' over 'typedData' */
2223
export * from '../types/typedData';
@@ -61,24 +62,6 @@ const revisionConfiguration: Record<Revision, Configuration> = {
6162
},
6263
};
6364

64-
// TODO: replace with utils byteArrayFromString from PR#891 once it is available
65-
export function byteArrayFromString(targetString: string) {
66-
const shortStrings: string[] = splitLongString(targetString);
67-
const remainder: string = shortStrings[shortStrings.length - 1];
68-
const shortStringsEncoded: BigNumberish[] = shortStrings.map(encodeShortString);
69-
70-
const [pendingWord, pendingWordLength] =
71-
remainder === undefined || remainder.length === 31
72-
? ['0x00', 0]
73-
: [shortStringsEncoded.pop()!, remainder.length];
74-
75-
return {
76-
data: shortStringsEncoded.length === 0 ? ['0x00'] : shortStringsEncoded,
77-
pending_word: pendingWord,
78-
pending_word_len: pendingWordLength,
79-
};
80-
}
81-
8265
function identifyRevision({ types, domain }: TypedData) {
8366
if (revisionConfiguration[Revision.Active].domain in types && domain.revision === Revision.Active)
8467
return Revision.Active;

0 commit comments

Comments
 (0)