stellar / rs-stellar-xdr

Rust lib for Stellar XDR.
Apache License 2.0
20 stars 27 forks source link

Fix render of AssetCode12 to JSON when shorter than 5 chars #358

Closed leighmcculloch closed 7 months ago

leighmcculloch commented 7 months ago

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