-
Notifications
You must be signed in to change notification settings - Fork 21
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
Support Uint8Array when applicable #42
Comments
Isn't In any case, technically for "bytes" fields it makes sense to be Uint8Array because:
For others you need to rely on, for example Why handle Vec specifically as bytes in the IDL and decode into Buffer in borsh-ts and others as Array? IDK ask Armani. Could this be done? Sure it could. Do we want this? IDK maybe, but I think probably not. Uint32 is more difficult to work with than Array. Calling IDK TBH. You might as well ask why Vec gets a special encoding as "bytes" in the IDL and not as vec of u8? This might be a valid question for the anchor guys. But what is your use case? What do you need "native array types" for that you cant do with just arrays? As for
This makes sense to do.
Isn't this already done automatically when encoding the layout with the serum borsh package? |
Correct Vec is already encoded as bytes, hence Uint8Array. I'm currently looking at fixed length u8 arrays: I'm mainly concerned with input validation for overflows, when assigning to
How are these currently handled? |
First case, would return obscure errors like this one, which make it really hard to trace, which field actually caused the error:
I'm concerned that the second case will trigger an equally obscure error message, while choosing a more explicit type for the generated structure / interfaces will make the issue more transparent to the programmer using the generated code as the type checker can identify which argument potentially causes an overflow. |
The above error comes from passing an Uint8array for a blob(32) struct field, apparently we won't be able to support Uint8Array at all and always need to pass Buffer when interacting with borsh due to this check https://github.com/pabigot/buffer-layout/blob/main/lib/Layout.js#L2319 I'll send a new PR that first updates all usage of Uint8Array to Buffer, so we don't run into this issue anywhere |
We're not sending Uint8Array anywhere to borsh. Buffer is sent. |
They're handled as array of numbers both in IDL, borsh layout, and the generator.
Javascript is not a typed language. Typescript helps a lot but it's not perfect, the typing is only present at compile time. Fixed sized arrays don't exist neither in javascript nor typescript, so not sure what we can expect there. The only thing we can maybe do is check for array length when constructing types or call instructions. Keep in mind that the code generated by anchor-client-gen is a bit lower level-ish and you will probably want to create a lib that wraps around it and is more semantic and include your checks there. See #5 (comment) |
Yeah, so the idl does preserve array length and type bounds, so it should be possible. I'll follow your advise and create a nice lib around the generated code to solve some of those issues on our end so that it is very integration friendly |
Also dropped the constant array size binding to Buffer approach, turned out to cause some nasty issues when trying to use Anchor's fetch helpers |
Hey @kklas , thank you for your help with the bytes type. I am currently working on a project, where I would like to use this for more data types, specifically:
Vec<u8>
[u8; 32]
I had a look at the code already and was able to get it to work on a local branch: https://github.com/mschneider/anchor-client-gen/tree/max/uint8array2 but curious to hear your thoughts as this somewhat breaks with the current abstraction of the library on a few levels.
My ideal outcome would be:
Let me know what you think about these changes.
The text was updated successfully, but these errors were encountered: