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

bech32: Add base256 conversion convenience funcs. #2025

Merged
merged 1 commit into from
Dec 27, 2019

Conversation

davecgh
Copy link
Member

@davecgh davecgh commented Dec 27, 2019

This requires PR #2024.

Since bech32 itself works with data encoded with 5 bits per byte (aka base32) padded out to the nearest byte boundary, the existing functions for Encode and Decode accept and return data encoded that way.

However, the most common way to use bech32 is to encode data that is already encoded with 8 bits per byte (aka base256) without padding which means it is up to the caller to use the ConvertBits function properly to convert between the two encodings.

Consequently, this introduces two convenience functions for working directly with base256-encoded data named EncodeFromBase256 and DecodeToBase256 along with a full set of tests to ensure they work as expected.

@davecgh davecgh added this to the 1.6.0 milestone Dec 27, 2019
Copy link
Member

@matheusd matheusd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only case I don't see covered in the tests is the "no hrp" case, though in the current code that ends up being triggered by the same logic as as "data part too short" case.

bech32/bech32_test.go Show resolved Hide resolved
@davecgh
Copy link
Member Author

davecgh commented Dec 27, 2019

@matheusd There is an "empty hrp" test. Are you referring to something else?

@matheusd
Copy link
Member

Sorry, should have been "no separator present" test (i.e., not a single "1" character in the string).

@davecgh
Copy link
Member Author

davecgh commented Dec 27, 2019

Added no separator test.

Since bech32 itself works with data encoded with 5 bits per byte (aka
base32) padded out to the nearest byte boundary, the existing functions
for Encode and Decode accept and return data encoded that way.

However, the most common way to use bech32 is to encode data that is
already encoded with 8 bits per byte (aka base256) without padding which
means it is up to the caller to use the ConvertBits function properly to
convert between the two encodings.

Consequently, this introduces two convenience functions for working
directly with base256-encoded data named EncodeFromBase256 and
DecodeToBase256 along with a full set of tests to ensure they work
expected.
@davecgh davecgh force-pushed the bech32_add_base256_convenience_funcs branch from da14459 to 3cfb088 Compare December 27, 2019 23:49
@davecgh davecgh merged commit 3cfb088 into decred:master Dec 27, 2019
@davecgh davecgh deleted the bech32_add_base256_convenience_funcs branch December 27, 2019 23:54
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

Successfully merging this pull request may close these issues.

3 participants