Re-write crate level API#128
Conversation
|
This PR more or less follows the proposal in #126 but I'm not auto-closing it because it does not include the suggested checksum example.
FTR, I did not work off of the proposal except for the docs stuff, I just hacked up what I thought was a useful API (on the plane). I rekon this needs a bit of thought to see if we like it or not? Once we are more-or-less happy with the API I'll go over the docs again and add examples etc. as needed. Note the |
92f020a to
c5bcd1e
Compare
|
The commented out |
b6c4fda to
d2623ec
Compare
|
This might need splitting up into separate PRs now because I've made some changes to the HRP constants to make them more readable. I've audited the two top level APIs (lib.rs and segwit.rs) and I'm happy they are uniform. Open to naming improvements still of course. I've improved docs and examples. Note please the new Now includes I changed the embedded tests so that one uses bech32 and one uses bech32m |
c036c7f to
d45d759
Compare
|
Ok, if CI goes green this is ready for review please @apoelstra, I edited the "what I did" post above, so in case you read a stale version please not this now adds |
d45d759 to
692bac7
Compare
|
FYI to check the embedded tests locally one must pin with I updated to latest nightly while debugging the |
692bac7 to
404a09d
Compare
|
In case its useful to you @clarkmoody I'm building the docs locally to review them using
|
404a09d to
4da0a7b
Compare
embedded/no-allocator/src/main.rs
Outdated
| let hrp = Hrp::parse("bech32").unwrap(); | ||
|
|
||
| bech32::encode_to_fmt_anycase(&mut encoded, hrp, &base32, Variant::Bech32).unwrap().unwrap(); | ||
| bech32::encode_to_fmt::<Bech32, ArrayString<30>>(&mut encoded, hrp, &data) |
There was a problem hiding this comment.
In 4da0a7b:
Could use <Bech32, _> rather than repeating ArrayString<30>.
src/primitives/mod.rs
Outdated
| /// The codex32 checksum algorithm, defined in [BIP-93]. | ||
| /// [BIP-350]: <https://github.com/bitcoin/bips/blob/master/bip-0093.mediawiki> | ||
| #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
| pub enum Codex32 {} |
There was a problem hiding this comment.
In 4da0a7b:
This can live in a doccomment or something but we probably shouldn't export it from this library.
|
In 4da0a7b:
Otherwise this looks great! |
clarkmoody
left a comment
There was a problem hiding this comment.
Nice top-level examples in the doc code!
| /// Re-exports the hrp types from [`primitives::hrp`] to make importing ergonomic for the top level APIs. | ||
| pub mod hrp; |
There was a problem hiding this comment.
Love the re-exports to the top-level hrp module.
Do the doc comments need to point out that these are re-exports?
There was a problem hiding this comment.
There is a stupid compiler warning now if re-exports don't have doccomments. Since they appear in the docs under Re-exports with a link to their definition, this isn't really sensible, so we've been putting "placeholder comments" like this in rust-bitcoin.
There was a problem hiding this comment.
Commented on this below.
src/primitives/mod.rs
Outdated
| pub enum Bech32m {} | ||
|
|
||
| /// The codex32 checksum algorithm, defined in [BIP-93]. | ||
| /// [BIP-350]: <https://github.com/bitcoin/bips/blob/master/bip-0093.mediawiki> |
There was a problem hiding this comment.
Should this be [BIP-93] here?
There was a problem hiding this comment.
Thanks, odd that -D rustdoc::broken-intra-doc-links didn't catch that. I've moved the codex32 stuff to a rustdoc comment and remove the link altogether.
| pub mod hrp; | ||
| /// All the primitive types and functionality used in encoding and decoding. | ||
| pub mod primitives; | ||
| /// API for encoding and decoding segwit addresses. |
There was a problem hiding this comment.
In the docs, this comment collides with the existing module-level docs in segwit.rs, giving us:
API for encoding and decoding segwit addresses. Segregated Witness API - enables typical usage for encoding and decoding segwit addresses.
We should remove one of the comments to avoid the redundant sentence.
There was a problem hiding this comment.
Ha, I saw this once and didn't fix it. Thanks for taking the time to build the docs and check them!
I've removed the docs from rust-bech32/src/hrp.rs and clippy seems to be ok with it, although I'm unsure why. Like @apoelstra said above we saw a new lint trigger for this situation in rust-bitcoin, will see how it goes in CI.
4da0a7b to
86c31d4
Compare
Ok, I removed them and added more docs about decoding with the checksum algorithm to the
Done!
Thanks man, this force push was the result of a pretty wild session, you can wait to review until I've had a chance to re-look at it with fresh eyes to catch my own mistakes if you like. |
ecf941f to
68c4dfd
Compare
src/segwit.rs
Outdated
| ) -> Result<(Hrp, Fe32, Vec<u8>), DecodeFromReaderError> { | ||
| let mut s = String::new(); | ||
| r.read_to_string(&mut s)?; | ||
| Ok(decode(&s)?) |
There was a problem hiding this comment.
In 68c4dfd:
I'll go ahead and ACK this since it's not API-visible, but the string allocation (and UTF-8 checks and everything that comes with it) are all unnecessary here. You should be able to just use the bytes() iterator on the Read, adapt it wiht char::from to get characters, then use the iterator API.
There was a problem hiding this comment.
I like it! I'll have go.
There was a problem hiding this comment.
I had a bit of a play with this, unless I'm missing something:
- The iterator API (you mean
iter.rs, right?) is for encoding not decoding - The API in
decode.rscould be changed to enable decoding from a reader but its non-trivial because all the decoding types (eg,UncheckedHrpstring) have lifetimes that expect a string somewhere to reference.
We could read into a buffer because a bech32 string is max 90 chars, then parse it out however we like.
Or am I totally missing something?
There was a problem hiding this comment.
No, you may be correct. Several iterations ago I had an idea for decoding through the iterator API and I guess that got dropped.
I think it's fine to just drop this function then. I don't have a clear usecase for it and it doesn't even work with nostd.
There was a problem hiding this comment.
Removed, both in lib.rs and segwit.rs
ea644f0 decode: Add unit test for parsing segwit address (Tobin C. Harding) 5d6e5c3 decode: Add empty data check (Tobin C. Harding) Pull request description: Noob bug here, I accessed an array without first checking that it was non-empty :( Adds unit test as a separate patch, re-arrange to verify the bug. Discovered while fuzzing #128. ACKs for top commit: apoelstra: ACK ea644f0 Tree-SHA512: dda0b91f8e2f94ba47b0d6f386e1edc17938bf4c71851ec387f922389ba384693655ed863e98bb95f60970bbf36844cc6c58b654c666b6ab1116cd6dea970ba2
68c4dfd to
9a90d64
Compare
Now we have all the new `primitives` pieces in place we can re-write the public API to take advantage of them - WIN!
9a90d64 to
79918f6
Compare
|
Rebaes only, no other changes. |
In rust-bitcoin#128 we dropped the benchmarks. They can be restore with only minor tweaks, so do so.
In rust-bitcoin#128 we dropped the benchmarks. They can be restore with only minor tweaks, so do so.
e0b95f1 benchmarks: add benchmarks for checksum param generation (Andrew Poelstra) cce8a7a benchmarks: restore benchmarks (Andrew Poelstra) 72421dc clippy: remove a couple redundant test imports (Andrew Poelstra) Pull request description: In #128 we dropped the benchmarks. They can be restored with only minor tweaks, so do so. ACKs for top commit: clarkmoody: ACK e0b95f1 Tree-SHA512: 6a23ba658ac8536ef6feb9400feddfbc4a561ff5ef1e10eb90bab6cf637c631f73901e7d8a738f8bad3389340fb532d74fe6129e2eb3d115ad01436c2ee298e5
Now includes updates to fuzzing and is on top of #130 (bug fix).
Now we have all the new
primitivespieces in place we can re-write the public API to take advantage of them - WIN!Close: #126