-
Notifications
You must be signed in to change notification settings - Fork 214
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
Fix Bech32 decoder + encoder #312
Conversation
a6f3751
to
9eea6d3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change itself looks good, a few remarks about style & infinite shrinkers 😅
9eea6d3
to
2639338
Compare
…h32 alphabet. Also add an accompanying `Read` instance for `DataPart` to accompany the existing `Show` instance. This change also adds a QuickCheck property for the following relationship: >>> read (show (dp :: DataPart)) == dp
1f6b14c
to
3e0ab16
Compare
Use `Text` as the internal representation for `HumanReadablePart` and `DataPart`. As a side-effect, we can discard the `Read` and `Show` instances for `DataPart`.
The `encode` function was mistakenly expected (by the specification) to produce an invalid Bech32 string, when given an upper-case human readable part. If this invalid string is passed to the `decode` function, it fails (unsurprisingly) to decode. This change updates the specification so that the `encode` function is now expected to produce a valid Bech32 string.
3e0ab16
to
d7e9762
Compare
In the `Arbitrary` instance for `HumanReadablePart`, we don't need to (and shouldn't) convert the generated string to lower-case before calling `humanReadablePartFromText`. The `humanReadablePartFromText` function itself already converts to lower case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Are you planning on making a PR upstream to fix the reference implementation ?
I'm happy to do that, as it's relatively simple to fix. But perhaps we should wait for upstream to confirm first? I've raised an issue here: sipa/bech32#49 |
Well, looking at the JavaScript implementation on bitcoinjs/bech32 We have the following behavior: > bech32.encode('test', [])
'test12hrzfj'
> bech32.encode('Test', [])
'test12hrzfj'
> bech32.encode('TEST', [])
'test12hrzfj'
> bech32.encode('TEsT', [])
'test12hrzfj'
> bech32.encode('TesT', [])
'test12hrzfj'
> bech32.decode("test12hrzfj")
{ prefix: 'test', words: [] }
> bech32.decode("test13jgcyw")
Error: Invalid checksum for test13jgcyw which is quite aligned to what we now expect. This seems like a fair indication that there's indeed a bug in the Haskell reference implementation. |
Issue Number
This PR fixes the following issues:
Overview
For #311:
DataPart
, which wraps[Word5]
.decode
andencode
so that they operate onDataPart
instead ofByteString
.encode
anddecode
is preserved.dataPartFromBytes
anddataPartToBytes
to convert to (and from)ByteString
andDataPart
:dataPartFromBytes
pads with trailing zeros where appropriate.dataPartToBytes
trims trailing zeros where appropriate.For #314:
HumanReadablePart
andDataPart
so that inputs are always converted to lower case.