stellar / rs-stellar-xdr

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

cli: automatically remove a single trailing new-line character when decoding a base64 encoded input #375

Closed leighmcculloch closed 3 months ago

leighmcculloch commented 3 months ago

The CLI could automatically remove a single trailing new-line character when decoding a base64 encoded input, which would help in situations where someone has piped a base64 value and unknowingly included a new line character on the end.

This is really easy when piping the result from the echo command, which adds a new line character unless you also use the -n option. It's also really easy when piping the result from other applications that include a new line in the output at the end.

It would result in situations like this 🫨 :

❯ echo "AAAAAAAAAGQAAAAAAAAAAQAAAAAAAAAMAAAAAAAAAAAAAAABAAAAAG4cwu71zHNXx3jHCzRGOIthcnfwRgfN2f/AoHFLLMclAAAAAEySDkgAAAAAAAAAAkJVU0lORVNTAAAAAAAAAAC3JfDeo9vreItKNPoe74EkFIqWybeUQNFvLvURhHtskAAAAAAeQtHTR3+AgAAAiwkAAAAAAAAAAAAAAAA=" | stellar xdr d --type TransactionResult
{"fee_charged":100,"result":{"tx_success":[{"op_inner":{"manage_buy_offer":{"success":{"offers_claimed":[],"offer":{"updated":{"seller_id":"GBXBZQXO6XGHGV6HPDDQWNCGHCFWC4TX6BDAPTOZ77AKA4KLFTDSLROC","offer_id":1284640328,"selling":"native","buying":{"credit_alphanum12":{"asset_code":"BUSINESS","issuer":"GC3SL4G6UPN6W6ELJI2PUHXPQESBJCUWZG3ZIQGRN4XPKEMEPNWJBCRB"}},"amount":507695571,"price":{"n":1199538304,"d":35593},"flags":0,"ext":"v0"}}}}}}]},"ext":"v0"}
error: error reading file: error decoding XDR: Invalid byte 10, offset 220.
✘ exit status 1                                                                                                                                              

Becoming this 😄 :

❯ echo "AAAAAAAAAGQAAAAAAAAAAQAAAAAAAAAMAAAAAAAAAAAAAAABAAAAAG4cwu71zHNXx3jHCzRGOIthcnfwRgfN2f/AoHFLLMclAAAAAEySDkgAAAAAAAAAAkJVU0lORVNTAAAAAAAAAAC3JfDeo9vreItKNPoe74EkFIqWybeUQNFvLvURhHtskAAAAAAeQtHTR3+AgAAAiwkAAAAAAAAAAAAAAAA=" | stellar xdr d --type TransactionResult
{"fee_charged":100,"result":{"tx_success":[{"op_inner":{"manage_buy_offer":{"success":{"offers_claimed":[],"offer":{"updated":{"seller_id":"GBXBZQXO6XGHGV6HPDDQWNCGHCFWC4TX6BDAPTOZ77AKA4KLFTDSLROC","offer_id":1284640328,"selling":"native","buying":{"credit_alphanum12":{"asset_code":"BUSINESS","issuer":"GC3SL4G6UPN6W6ELJI2PUHXPQESBJCUWZG3ZIQGRN4XPKEMEPNWJBCRB"}},"amount":507695571,"price":{"n":1199538304,"d":35593},"flags":0,"ext":"v0"}}}}}}]},"ext":"v0"}
leighmcculloch commented 3 months ago

In general we should ignore whitespace with base64 entities since whitespace doesn't matter and sometimes folks have the base64 with hardwrap new lines within.