tonkeeper / opentonapi

Opentonapi simplifies development of TON-based applications and provides an API centered around high-level concepts like Jettons, NFTs and so on keeping a way to access low-level details.
MIT License
224 stars 68 forks source link

Fix request body decoding mistake in SendRawMessage #355

Closed thedemons closed 3 months ago

thedemons commented 5 months ago

While trying to use the /v2/liteserver/send_message api as described on https://tonapi.io/api-v2, I ran into the issue where the server doesn't seem to accept any formats of the boc messages I sent, whether it's base64, hex string, hex string with 0x, or even integer. It returns status code 500 with the exception message "unknown magic prefix", which is thrown here.

After investigating, I found that the issue is at liteserver_handlers.go#L91

payload, err := json.Marshal(request.Body)

Firstly, json.Marshal will puts a 0x22 (or ") at the beginning and the end of payload, secondly, it can't handle bytes string this way.

For example json.Marshal("abc") outputs 22 61 62 63 22, and json.Marshal("\x00\x01\x02") outputs 22 5c 75 30 30 30 30 5c 75 30 30 30 31 5c 75 30 30 30 32 22.

This issue has been there since 6f41163788f40f8be960dc0d87b8fc7e4109fd45, that's 9 months ago. I'm not sure if this intentional or not. I suspect there were some changes in the way tongo handles the payload but opentonapi was not updated correctly.

The fix was to use base64.StdEncoding.DecodeString(request.Body). Now we're able to send boc's encoded in base64.

2Smart4My0wnG00d commented 5 months ago

BIGGG

thedemons commented 4 months ago

@aleksej-paschenko I’d appreciate your feedback on this PR.

If it's already working as intended, do you mind explaining how the API is supposed to be used? Thanks.