Skip to content

Parse JSON numbers properly to BigNumbers #578

@manuelwedler

Description

@manuelwedler

During the review of #573 @andrevmatos found a potential problem: our losslessParse() function, which is used for parsing JSON API responses, only converts numbers of the type string into BigNumber. This is because the Raiden API does return all numbers as strings. However the PFS API uses the JSON number type for numbers. This is no problem at the moment, because we don't use the numbers from the PFS API, but could be one in the future. We should use a library for parsing.

André's comment from #573:

This losslessParse may work only if the BNs are stringified on the JSONs (since this revive function from JSON.parse is called only after the number already got parsed as such), while it seems the current PFS implementation encodes the capacity and other BNs as JSON number. This may only be working now if the numbers on your test were small (i.e. < 2^53).
We had this issue on the LC, and ended up using json-bigint lib to parse these JSONs. Check here for our implementation.

Notice you need to use a proper decoder/schema validator if you go the way we did, since it'll ensure these values are kept as strings, or use it with your reviver function after using the lib to ensure the BNs are kept as strings or bigint or whatever.. We already had the schemas in place, so went with it, but you can use the reviver as well if you don't care that much with the typesafety there.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions