starknet.js icon indicating copy to clipboard operation
starknet.js copied to clipboard

Add ByteArray to UTF* decode utils

Open JorikSchellekens opened this issue 1 year ago • 2 comments

Is your feature request related to a problem? Please describe. String support is in Cairo but it's annoying for developers to have to think about how to deserialise the ByteArray struct.

Describe the solution you'd like Either provide a function to decode ByteArrays or just do it for them when the abi contains a ByteArray return type.

JorikSchellekens avatar Feb 27 '24 20:02 JorikSchellekens

Hello, ByteArray is already supported in Starknet.js, both for encoding and decoding : https://www.starknetjs.com/docs/next/guides/define_call_message#longstring-or-bytearray https://www.starknetjs.com/docs/next/guides/define_call_message#receive-data-from-a-cairo-contract

Conversion functions are available here : https://www.starknetjs.com/docs/next/API/namespaces/byteArray

PhilippeR26 avatar Feb 28 '24 17:02 PhilippeR26

@JorikSchellekens hi! Is https://www.starknetjs.com/docs/next/api/namespaces/bytearray/#stringfrombytearray what you are looking for?

ivpavici avatar Mar 05 '24 14:03 ivpavici

Neither that function nore it's associated namesapce is exported. Besides the abi compiler should just understand this type and compile the strings regardless.

Also the documentation around this is difficult to understand:

image

JorikSchellekens avatar Mar 08 '24 00:03 JorikSchellekens

Well, The guide is explaining all possible needs, including the complicated ones. Seems clear that in a normal case You can send to Starknet.js methods: string. . So just send a JS string. As a response you will receive just a JS string. It couldn't be simpler!

If you need to perform manually the JS <-> Cairo conversion (it's not necessary in normal situation), the doc is also clearly stating that the namespace is exposed here : https://www.starknetjs.com/docs/next/api/namespaces/bytearray/ (has already been explained above by @ivpavici ). You have a concrete example here : https://github.com/PhilippeR26/starknet.js-workshop-typescript/blob/da4431bf8b250d2503b008d0a7a997cda5923e95/src/scripts/shortstring/byteArrayTests.ts

PhilippeR26 avatar Mar 08 '24 08:03 PhilippeR26

Closed due to being stale

ivpavici avatar Apr 19 '24 13:04 ivpavici