Closed tcharding closed 1 year ago
Unrelated to the length checks, while working on this I found data_iter
to be annoying because of the generic. If we assume that users will do validity checks on the checksum before iterating the data perhaps we should remove the checksum during validation so that iterating does not require the generic (we can save it in another slice field of Parsed
) .
If we assume that users will do validity checks on the checksum before iterating the data
How are they going to check the checksum without iterating through the data? Are you suggesting that they iterate twice? What API should they use to iterate the first time?
After constructing the Parsed
type we are expecting them to call parsed.validate_checksum
before calling data_iter
.
/// When constructing the iterator we do some preliminary checks on the length
/// of the checksummed data, hence the `Result` return type. But **this function
/// does not validate the checksum**. Before using it, you should first call
/// [`Self::validate_checksum`].
Ah! I see what you're saying. You need to use the same generic twice, once in validate_checksum
and once in data_iter
. I agree this is annoying.
The existing API is there so that you don't need to take self
by mutable reference. I am tempted to change validate_checksum
to take ownership of self
, reduce the slice internally, and mark it #[must_use]
.
We should also probably make Parsed
be Copy
.
What would happen if you forgot to do this, and called data_iter
anyway?
Superseded by #113
Throwing this up for you to look at @apoelstra when/if you get time.
Draft because the current implementation fails to parse a valid bech32 address from mainnet but AFAICT the code correctly implements BIP-173 - where is the bug?
Failing test is at the bottom of
hrpstring.rs
It fails with
InvalidDataLength
from the call tochecksum_length_checks
:And the relevant part of the bip is in bold:
Decoding
Software interpreting a segwit address: