aeternity / ae_mdw

Aeternity Middleware in Elixir
26 stars 11 forks source link

chore: add default padding to base64 oracles response/queries #1937

Closed sborrazas closed 2 months ago

sborrazas commented 2 months ago

refs #1931

vatanasov commented 2 months ago

I don't think this will solve the issue. I think the problem is, some of the responses are in Chinese/Japanese (e.g. in here: https://mainnet.aeternity.io/mdw/v3/oracles/ok_qJZPXvWPC7G9kFVEqNjj9NAmwMsQcpRu6E3SSCvCQuwfqpMtN/queries ) and if my findings are correct, aescan is using atob which doesn't decode directly encoded Chinese/Japanese correctly. It seems that a working solution would be to first use URI.encode and then Base.encode64 and in turn aescan could use decodeURIComponent(window.atob(<response>)) which seems to return the correct response. We could also do that for the query, so that everything is uniform.

sborrazas commented 2 months ago

@vatanasov I'm pretty sure you don't need any of that, we don't even need the base64 padding. I simply added the padding because it works with all base64 libraries, but it works with atob regardless:

Screen Shot 2024-09-11 at 8 08 06 AM

@janmichek can you describe what you are using to decode base64 strings?

janmichek commented 2 months ago
image

This is the frontend function depends if its server (nodejs) or browser rendered, that's what is the condition for

sborrazas commented 2 months ago

image This is the frontend function depends if its server (nodejs) or browser rendered, that's what is the condition for

@janmichek can we switch to using decodeURIComponent(escape(atob(STRING))) in the browser and Buffer.from(STRING, "base64").toString("utf8") in the server? Works both with and without padding

console.log(Buffer.from("5oiQ5Yqf5LqGIQ==", "base64").toString('utf8')); // 成功了!
console.log(Buffer.from("5oiQ5Yqf5LqGIQ", "base64").toString('utf8')); // 成功了!
janmichek commented 2 months ago

decodeURIComponent

Yea I guess that should work in browser

janmichek commented 1 month ago

@sborrazas There seems to be some issue with it, can you check it please?

https://aescan.io/oracles/ok_2fVpBb8j58mNQcVjY7bwUVpBWZv6AySGDjqepNofU8XGGmEN53

image

The decoding you suggested says its a malformed string, or am I doing something wrong?

sborrazas commented 1 month ago

@janmichek The response/query values for oracles are just binaries, meaning not all binaries have valid utf8 or ascii representations. Do you know what the utf8 representation for that example you gave should be?

janmichek commented 1 month ago

@janmichek The response/query values for oracles are just binaries, meaning not all binaries have valid utf8 or ascii representations. Do you know what the utf8 representation for that example you gave should be?

No, I don't know what the result should be. In that case the current solution is ok, no? I will just sanitize the empty string for N/A. The solution you suggested decodeURIComponent will cause error.

sborrazas commented 1 month ago

That's right, I would display something for when the binary is not a UTF-8/ASCII representation

janmichek commented 1 month ago

It will be fixed in here https://github.com/aeternity/aescan/pull/948

You can close this PR