8000 sdk: Add functions for type-safe conversions from integers to ChainID by johnsaigle · Pull Request #4271 · wormhole-foundation/wormhole · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

sdk: Add functions for type-safe conversions from integers to ChainID #4271

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

Merged
merged 3 commits into from
Mar 21, 2025

Conversation

johnsaigle
Copy link
Contributor
@johnsaigle johnsaigle commented Feb 20, 2025

In many places in the Guardian, the following is done:

// equivalent to `cid := uint16(x)`
cid := vaa.ChainID(x)

As ChainID() is just a custom type around uint16, this is equivalent to doing an unchecked type-conversion to uint16, which could result in edge-case truncation/overflow/sign-change issues.

The motivation for this change is to provide a clear way to go from any integer type to a ChainID in a type-safe way. With this PR, someone could instead write the following:

// just type-safe
cid, err := vaa.ChainIDFromNumber(x)
// type-safe + corresponds to a real chain
cid, err := vaa.KnownChainIDFromNumber(x)

If the general idea seems good, I will merge this work with #4239 in the future so that the function introduced in that PR will use KnownChainIDFromNumber in the numeric string context.

@johnsaigle johnsaigle added the enhancement New feature or request label Feb 20, 2025
@johnsaigle johnsaigle marked this pull request as ready for review February 20, 2025 17:07
@johnsaigle johnsaigle marked this pull request as draft February 21, 2025 17:18
@johnsaigle johnsaigle marked this pull request as ready for review February 21, 2025 18:40
@johnsaigle johnsaigle force-pushed the chainid-conversions branch from df9b417 to cd5bfe8 Compare March 20, 2025 13:36
@johnsaigle johnsaigle force-pushed the chainid-conversions branch from cd5bfe8 to 94fdb2c Compare March 21, 2025 12:48
@johnsaigle johnsaigle merged commit 868f201 into wormhole-foundation:main Mar 21, 2025
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0