Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoding fails if the data's first byte is 0 #23

Open
nazariyg opened this issue Mar 29, 2022 · 2 comments
Open

Decoding fails if the data's first byte is 0 #23

nazariyg opened this issue Mar 29, 2022 · 2 comments

Comments

@nazariyg
Copy link

nazariyg commented Mar 29, 2022

Example:

let bytes: [UInt8] = [0, 128, 128, 128]
let encodedString = Base58.base58CheckEncode(bytes)
let decodedBytes = Base58.base58CheckDecode(encodedString)!  // <- fails here
@nazariyg nazariyg changed the title Decoding fails if the encoded data's first byte is 0 Decoding fails if the data's first byte is 0 Mar 29, 2022
@asloup
Copy link

asloup commented Apr 13, 2023

Yeah, I think the bug is in base58Decode because it's handling things as a big int, which considers leading 0s as insignificant - for example, 000123456 is the same as 123456. But for our case, those leading 0s represent 0x00 bytes, thus they are significant. So we just need to prefix them back to the decoded bytes before returning.

Bug
return Array(byteString.prefix { i in i == alphabet[0] }) + bytes

Fix

let zeroBytesToAdd = byteString.prefix { i in i == alphabet[0] }.count
return [UInt8](repeating: 0, count: zeroBytesToAdd) + bytes

@asloup
Copy link

asloup commented Apr 13, 2023

Just checked the PRs too. Looks like this one would fix it: #21

Repository owner deleted a comment from D-inspiration Feb 23, 2024
Repository owner deleted a comment from Blue-pill-786 Feb 23, 2024
Repository owner deleted a comment from rookiecdn Mar 4, 2024
@github-staff github-staff deleted a comment Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants