Skip to content

Commit

Permalink
fix: byteArray encoding for less than 31 chars (#1011)
Browse files Browse the repository at this point in the history
* fix: byteArray encoding for less than 31 chars

* refactor: reuse byteArray utility for typed data hashing

---------

Co-authored-by: Petar Penovic <pp@spaceshard.io>
  • Loading branch information
PhilippeR26 and penovicp authored Mar 14, 2024
1 parent 880b906 commit 653acc4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 53 deletions.
2 changes: 1 addition & 1 deletion __tests__/cairo1v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ describe('Cairo 1', () => {
const callD2 = CallData.compile({ mess: message });
expect(callD2).toEqual(expectedResult);
const callD3 = CallData.compile({ mess: byteArray.byteArrayFromString('Take care.') });
expect(callD3).toEqual(['1', '0', '398475857363345939260718', '10']);
expect(callD3).toEqual(['0', '398475857363345939260718', '10']);
const str1 = await stringContract.get_string();
expect(str1).toBe(
"Cairo has become the most popular language for developers + charizards !@#$%^&*_+|:'<>?~`"
Expand Down
2 changes: 1 addition & 1 deletion __tests__/cairo1v2_typed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ describe('Cairo 1', () => {
const callD2 = CallData.compile({ mess: message });
expect(callD2).toEqual(expectedResult);
const callD3 = CallData.compile({ mess: byteArray.byteArrayFromString('Take care.') });
expect(callD3).toEqual(['1', '0', '398475857363345939260718', '10']);
expect(callD3).toEqual(['0', '398475857363345939260718', '10']);
const str1 = await stringContract.get_string();
expect(str1).toBe(
"Cairo has become the most popular language for developers + charizards !@#$%^&*_+|:'<>?~`"
Expand Down
8 changes: 4 additions & 4 deletions __tests__/utils/shortString.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ describe('shortString', () => {
pending_word_len: 0,
});
expect(byteArray.byteArrayFromString('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234')).toEqual({
data: ['0x00'],
data: [],
pending_word: '0x4142434445464748494a4b4c4d4e4f505152535455565758595a31323334',
pending_word_len: 30,
});
expect(byteArray.byteArrayFromString('')).toEqual({
data: ['0x00'],
data: [],
pending_word: '0x00',
pending_word_len: 0,
});
Expand All @@ -90,14 +90,14 @@ describe('shortString', () => {
});
expect(
byteArray.stringFromByteArray({
data: ['0x00'],
data: [],
pending_word: '0x4142434445464748494a4b4c4d4e4f505152535455565758595a31323334',
pending_word_len: 30,
})
).toBe('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234');
expect(
byteArray.stringFromByteArray({
data: ['0x00'],
data: [],
pending_word: '0x00',
pending_word_len: 0,
})
Expand Down
45 changes: 17 additions & 28 deletions src/utils/calldata/byteArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { decodeShortString, encodeShortString, splitLongString } from '../shortS
* @example
* ```typescript
* const myByteArray = {
* data: [ '0x00' ],
* data: [],
* pending_word: '0x414243444546474849',
* pending_word_len: 9
* }
Expand All @@ -36,39 +36,28 @@ export function stringFromByteArray(myByteArray: ByteArray): string {
* @returns Cairo representation of a LongString
* @example
* ```typescript
* const myByteArray: ByteArray = byteArrayFromStr("ABCDEFGHI");
* const myByteArray: ByteArray = byteArrayFromString("ABCDEFGHI");
* ```
* Result is :
* {
* data: [ '0x00' ],
* data: [],
* pending_word: '0x414243444546474849',
* pending_word_len: 9
* }
*/
export function byteArrayFromString(myString: string): ByteArray {
if (myString.length === 0) {
return {
data: ['0x00'],
pending_word: '0x00',
pending_word_len: 0,
} as ByteArray;
}
const myShortStrings: string[] = splitLongString(myString);
const remains: string = myShortStrings[myShortStrings.length - 1];
const myShortStringsEncoded: BigNumberish[] = myShortStrings.map((shortStr) =>
encodeShortString(shortStr)
);
if (remains.length === 31) {
return {
data: myShortStringsEncoded,
pending_word: '0x00',
pending_word_len: 0,
} as ByteArray;
}
const pendingEncodedWord: BigNumberish = myShortStringsEncoded.pop()!;
export function byteArrayFromString(targetString: string): ByteArray {
const shortStrings: string[] = splitLongString(targetString);
const remainder: string = shortStrings[shortStrings.length - 1];
const shortStringsEncoded: BigNumberish[] = shortStrings.map(encodeShortString);

const [pendingWord, pendingWordLength] =
remainder === undefined || remainder.length === 31
? ['0x00', 0]
: [shortStringsEncoded.pop()!, remainder.length];

return {
data: myShortStringsEncoded.length === 0 ? ['0x00'] : myShortStringsEncoded,
pending_word: pendingEncodedWord,
pending_word_len: remains.length,
} as ByteArray;
data: shortStringsEncoded.length === 0 ? [] : shortStringsEncoded,
pending_word: pendingWord,
pending_word_len: pendingWordLength,
};
}
21 changes: 2 additions & 19 deletions src/utils/typedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
StarkNetType,
TypedData,
} from '../types';
import { byteArrayFromString } from './calldata/byteArray';
import {
computePedersenHash,
computePedersenHashOnElements,
Expand All @@ -16,7 +17,7 @@ import {
} from './hash';
import { MerkleTree } from './merkle';
import { isHex, toHex } from './num';
import { encodeShortString, splitLongString } from './shortString';
import { encodeShortString } from './shortString';

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

// TODO: replace with utils byteArrayFromString from PR#891 once it is available
export function byteArrayFromString(targetString: string) {
const shortStrings: string[] = splitLongString(targetString);
const remainder: string = shortStrings[shortStrings.length - 1];
const shortStringsEncoded: BigNumberish[] = shortStrings.map(encodeShortString);

const [pendingWord, pendingWordLength] =
remainder === undefined || remainder.length === 31
? ['0x00', 0]
: [shortStringsEncoded.pop()!, remainder.length];

return {
data: shortStringsEncoded.length === 0 ? ['0x00'] : shortStringsEncoded,
pending_word: pendingWord,
pending_word_len: pendingWordLength,
};
}

function identifyRevision({ types, domain }: TypedData) {
if (revisionConfiguration[Revision.Active].domain in types && domain.revision === Revision.Active)
return Revision.Active;
Expand Down

0 comments on commit 653acc4

Please sign in to comment.