Fix render of AssetCode12 to JSON when shorter than 5 chars #358
+29
−18
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What
Change render of AssetCode12 to JSON to always output at least 5 chars and show escaped zeros for any missing.
Why
AssetCode12's that have less than 5 characters are not supported by the Stellar network. However, they can be encoded in XDR which means they can be rendered to JSON. There's no pubnet usage of this because they are rejected being invalid, but it has been noted in SEP-11 that such cases can exist for testing invalid data. In any case this is one stand out place that the round trip encoding of asset codes is somewhat buggy and is worth fixing.
Today the AssetCode12 when encoded to JSON renders itself to a string stopping at the last sequential zero from the end meaning it can render itself as ABC, or AB, or A, or even an empty string "" if it is all zeroes. When an AssetCode12 has been wrapped in an AssetCode and is then decoded it is reinterpreted as an AssetCode4 because the length is equal or less than four.
For the sake of preserving the round-trippable guarantees of the XDR <> JSON conversions, this is worth fixing.
This is a breaking change to the XDR<>JSON conversion, but it is a bug fix, and any AssetCode12 already encoded using this function would never have decoded correctly anyway, so this change can be included in a patch or minor release if the opportunity arises or it is deemed necessary.
Close #356