Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cairo 2.4.0 new types : bytes31 & ByteArray #886

Closed
PhilippeR26 opened this issue Dec 10, 2023 · 1 comment
Closed

Cairo 2.4.0 new types : bytes31 & ByteArray #886

PhilippeR26 opened this issue Dec 10, 2023 · 1 comment
Assignees
Labels
OnlyDust Open for OnlyDust contributors Type: feature New feature or request

Comments

@PhilippeR26
Copy link
Collaborator

Is your feature request related to a problem? Please describe.
I have analyzed the new Cairo v2.4.0 release, and searched what could be the impacts on Starknet.js.
I have found 2 new types that have been added in the abi :

  • bytes31
  • ByteArray

Describe the solution you'd like
Cairo team explained me how they are serialized :

bytes31

is just a Bigint limited to 31 bytes, representing a ShortString.

"type": "core::bytes_31::bytes31"

It will be straightforward to implement.

ByteArray

Here, it's more complicated. This type is an object representing a LongString.
In Cairo, it's used as a normal string ; nobody see the low level object.
But this complex object is used in the abi (similar to enums, but slightly more complicated).
I have coded/compiled a small cairo example. The result in the abi is :

{
      "type": "struct",
      "name": "core::byte_array::ByteArray",
      "members": [
        {
          "name": "data",
          "type": "core::array::Array::<core::bytes_31::bytes31>"
        },
        {
          "name": "pending_word",
          "type": "core::felt252"
        },
        {
          "name": "pending_word_len",
          "type": "core::integer::u32"
        }
      ]
    },

I think that on Starknet.js side, users want just to provide/receive a LongString in the calldata.
It means that we will not be in accordance with the abi for data input.
Will probably be interesting also for @haroune-mohammedi .
Cairo team has explained me how to read/write this struct ; I coded something that is conform.

One question is remaining : without Abi, what should be the behavior of CallData.compile in case of a string in the input data?
If it's a short string, deliver a Bigint, an array of Bigint, or a serialized ByteArray?
If it's a long string, deliver an array of Bigint, or a serialized ByteArray?

Describe alternatives you've considered
N/A

Additional context
As Cairo v2.4.0 is implemented for the first time in Starknet v0.13.0, it could be a good thing to implement these 2 new types in Starknet.js v6.0.

@PhilippeR26 PhilippeR26 added the Type: feature New feature or request label Dec 10, 2023
@ivpavici ivpavici added the OnlyDust Open for OnlyDust contributors label Dec 11, 2023
@PhilippeR26
Copy link
Collaborator Author

PhilippeR26 commented Dec 11, 2023

My code for CallData.compile() is working this way :

  • If it's a short string, deliver a Bigint.
  • If it's a long string, deliver a serialized ByteArray.
  • If it's a short string, and if I want absolutely a ByteArray, deliver a serialized ByteArray with const myCalldata= CallData.compile({mess:byteArray.byteArrayFromString("Take care.")});
  • If it's a long string, and if I want absolutely an array of shortString (for Cairo 0), deliver an array of felt with const myCalldata= CallData.compile({mess:shortString.splitLongString('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore')})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OnlyDust Open for OnlyDust contributors Type: feature New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants