stellar / js-stellar-base

The lowest-level stellar helper library. It consists of classes to read, write, hash, and sign Stellar xdr
https://stellar.github.io/js-stellar-base/
Apache License 2.0
106 stars 138 forks source link

Add support for `nativeToScVal` to convert strings to all number sizes. #763

Closed Shaptic closed 1 month ago

Shaptic commented 1 month ago

Previously, i32 and u32 could not automatically come from string values when using nativeToScVal. Namely, nativeToScVal("4", { type: "i32" }) was explicitly forbidden. This was my design: the rationale was that if it's a 32-bit integer, it should be passed as a Number.

However, this logic does not hold true for maps, because in JavaScript, all map keys are strings. Therefore, creating an ScVal representing an integer-to-integer map is impossible via nativeToScVal, e.g., a function signature like this one:

fn spin(env: Env, pairs: Map<u32, u64>) -> boolean;

cannot be executed by

nativeToScVal(
  { 1234: 5678 }, 
  { 
    type: { 
      "1234": [ "u32, "u64" ],
      "5678": [ "u32, "u64" ],
    }
);

because 1234 would be interpreted as a string, and thus could not be automatically converted to a number for a u32 inference.

github-actions[bot] commented 1 month ago

Size Change: +1.48 kB (+0.05%)

Total Size: 3.21 MB

Filename Size Change
dist/stellar-base.js 2.36 MB +1.08 kB (+0.05%)
dist/stellar-base.min.js 857 kB +400 B (+0.05%)

compressed-size-action