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

toHextruncates leading zeroes #1028

Closed
naorye opened this issue Mar 20, 2024 · 9 comments
Closed

toHextruncates leading zeroes #1028

naorye opened this issue Mar 20, 2024 · 9 comments
Assignees
Labels
OnlyDust Open for OnlyDust contributors Type: feature New feature or request

Comments

@naorye
Copy link

naorye commented Mar 20, 2024

toHex function truncates leading zeroes:

export function toHex(number: BigNumberish): string {

For example:

toHex('0x023A2aAc5d0fa69E3243994672822BA43E34E5C9') // === "0x23A2aAc5d0fa69E3243994672822BA43E34E5C9"

(zero after '0x' gets truncate)

@PhilippeR26
Copy link
Collaborator

Hello,
Seems to be a normal behavior...
If you want a leading 0 (if necessary) :

const hexNum = encode.sanitizeHex(hexString);

@tabaktoni
Copy link
Collaborator

In general, toHex is an unopinionated number conversion.

Maybe we should add a helper method

 toCallerAddress(number: BigNumberish): string;

That would ensure leading zero on every conversion based on https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/starknet-state/#address_0x0

@PhilippeR26
Copy link
Collaborator

Just use addAddressPadding(address: BigNumberish): string

@ivpavici
Copy link
Collaborator

We have 3 options:

  1. add additional explanations to the guides about this case
  2. implement Toni's suggestion
  3. do both :D

@ivpavici ivpavici added OnlyDust Open for OnlyDust contributors Type: feature New feature or request labels Mar 22, 2024
@PhilippeR26
Copy link
Collaborator

This function is already existing in Starknet.js : https://www.starknetjs.com/docs/next/api/modules/#addaddresspadding

If you want, I can add some JSdoc.

@ivpavici
Copy link
Collaborator

ok, we can do that also! thanks!

@PhilippeR26
Copy link
Collaborator

Made.

@naorye
Copy link
Author

naorye commented Mar 24, 2024

@PhilippeR26 @ivpavici @tabaktoni

Sorry, I didn't explain the actual issue.

The following method

from_address: toHex(from_address),
uses toHex in order to make sure address is hex. By doing so padding zeroes are getting removed and the RPC throws an exception about incorrect address length.

@PhilippeR26
Copy link
Collaborator

This time I understood.
The node is expecting a string representing an hex number coded with 20 bytes.
I will have a look.
@ivpavici , could you assign me?

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

4 participants