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

nativeToScVal did not handle numbers correctly. #683

Closed
overcat opened this issue Aug 24, 2023 · 6 comments
Closed

nativeToScVal did not handle numbers correctly. #683

overcat opened this issue Aug 24, 2023 · 6 comments
Labels

Comments

@overcat
Copy link
Contributor

overcat commented Aug 24, 2023

Describe the bug

The following code should output false, but we got true, 2 ** 127 - 1 was not converted correctly.

const sorobanClient = require("soroban-client")

const a = sorobanClient.nativeToScVal(-(2 ** 127), {
    type: "i128",
}).toXDR("base64")

const b = sorobanClient.nativeToScVal(2 ** 127 - 1, {
    type: "i128",
}).toXDR("base64")

console.log(a === b)

What version are you on?
Check yarn.lock or package-lock.json to find out precisely what version of stellar-base you're running.

soroban-client: 0.11.0
stellar-base 10.0.0-soroban.7

To Reproduce
Check the code above.

Expected behavior
Check the code above.

Additional context
i256, u128, and u256 also have this issue. I speculate that it is due to the incorrect handling of the sign bit?

@overcat overcat added the bug label Aug 24, 2023
@willemneal
Copy link
Member

This is a JS issue. You went outside the bounds of the number type in JS, 2**53 - 1 So you need bigint, e.g. 2n ** 127 - 1n. However, I do think that it's a bug that the method doesn't throw if you are trying to use a number instead of a bigint.

@Shaptic
Copy link
Contributor

Shaptic commented Aug 24, 2023

Allowing number is intentional, but @willemneal is right that this is because JavaScript can't represent values that large using its Number type. You should either use Strings or BigInts for these. Perhaps you're right - maybe we shouldn't allow number?

@willemneal
Copy link
Member

Yeah for ints larger than u/i32, I think it should only allow bigints as it will prevent issues like this and draw attention to the issue. The bindings currently type them as bigints, but also doesn't ensure that the JS uses bigint arguments.

@overcat
Copy link
Contributor Author

overcat commented Aug 25, 2023

You should either use Strings or BigInts for these.

String is not supported, right?

const b = sorobanClient.nativeToScVal("170141183460469231731687303715884105727", {
    type: "i128",
}).toXDR("base64")

          throw new TypeError("invalid type (".concat(opts.type, ") specified for string value"));
          ^

TypeError: invalid type (i128) specified for string value

Furthermore, personally I tend not to support the use of number here as it may introduce implicit bugs.

@Shaptic
Copy link
Contributor

Shaptic commented Aug 25, 2023

Ah sure, that's right. If you know you have an integer, you should use ScInt rather than nativeToScVal which has more flexibility. But we can add support for that in this converter, too! I'll look into this.

@Shaptic
Copy link
Contributor

Shaptic commented Sep 14, 2023

Closed by #690!

@Shaptic Shaptic closed this as completed Sep 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants