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

Add Bech32 Command Tree #9

Merged
merged 7 commits into from
Jul 26, 2019
Merged

Conversation

clarkmoody
Copy link
Contributor

@clarkmoody clarkmoody commented Jul 19, 2019

Wanted to add a quick interface to the Bech32 library. This will allow encoding and decoding of Bech32 strings and payloads.

Decode

decode bech32 format

USAGE:
    hal bech32 decode [FLAGS] <string>

FLAGS:
    -c, --convert-bits    Attempt to convert payload from 5-bit to 8-bit values.
                          NOTE: Does not work for BIP-173 addresses.
    -h, --help            Prints help information
    -v, --verbose         Print verbose logging output to stderr
    -y, --yaml            print output in YAML instead of JSON

ARGS:
    <string>    a bech32 string

Sample output:

$ hal bech32 decode bc1svu3ak479v954vrpxe0wst
{
  "bech32": "bc1svu3ak479v954vrpxe0wst",
  "hrp": "bc",
  "payload": "100c1c111d16151e050c0514150c0301"
}

# Convert 5-bit to 8-bit values
$ hal bech32 decode -c bc1svu3ak479v954vrpxe0wst
{
  "bech32": "bc1svu3ak479v954vrpxe0wst",
  "hrp": "bc",
  "payload": "100c1c111d16151e050c0514150c0301",
  "payload_base256": "83391edabe2b0b4ab061"
}

Encode

encode bech32 format

USAGE:
    hal bech32 encode [FLAGS] <hrp> <payload-hex>

FLAGS:
    -h, --help          Prints help information
        --no-convert    Do not convert payload to base32
    -v, --verbose       Print verbose logging output to stderr
    -y, --yaml          print output in YAML instead of JSON

ARGS:
    <hrp>            human-readable part
    <payload-hex>    hex-encoded payload bytes, 8-bit values
                     unless --no-convert is specified

Sample output:

$ hal bech32 encode bc 83391edabe2b0b4ab061
{
  "bech32": "bc1svu3ak479v954vrpxe0wst",
  "hrp": "bc",
  "payload": "100c1c111d16151e050c0514150c0301",
  "payload_base256": "83391edabe2b0b4ab061"
}

# Consume payload as 5-bit data directly with no conversion
hal bech32 encode --no-convert bc 100c1c111d16151e050c0514150c0301`
{
  "bech32": "bc1svu3ak479v954vrpxe0wst",
  "hrp": "bc",
  "payload": "100c1c111d16151e050c0514150c0301",
  "payload_base256": "83391edabe2b0b4ab061"
}

@stevenroose
Copy link
Owner

One thing I'm trying to think about is to avoid short flags if they could collide with global ones. -x for example could be one day. And since you're returning json anyway, you can just have two fields, one for bytes and one hex.

I didn't know it's possible to flag a PR as "draft", TIL!

@stevenroose
Copy link
Owner

We should be able to drop the bitcoin-bech32 dep as well once rust-bitcoin v0.19 lands. But I have some more branches ready related to addresses for when v0.19 lands.

@clarkmoody
Copy link
Contributor Author

And since you're returning json anyway, you can just have two fields, one for bytes and one hex.

Great point. We might as well just output both, if we're in structured data mode.

On another note, would it be possible to output a trailing newline after the JSON or YAML, so the command prompt is clean for the next input?

@clarkmoody
Copy link
Contributor Author

And since you're returning json anyway, you can just have two fields, one for bytes and one hex.

Great point. We might as well just output both, if we're in structured data mode.

On second thought, I'd like to move the "payload" output to non-default, since it displays each byte of a separate line, which is ultra-verbose.

@stevenroose
Copy link
Owner

On another note, would it be possible to output a trailing newline after the JSON or YAML, so the command prompt is clean for the next input?

Sure.

On second thought, I'd like to move the "payload" output to non-default, since it displays each byte of a separate line, which is ultra-verbose.

Yeah personally I don't really see much value in an interger-list-representation of bytes over hex.

For decoding, this echoes back the original input string, along with
decoded values. For encoding, the inputs are echoed back alongside the
bech32 field output.

Cargo fmt
Complete bech32 encode command with payload field output

Remove integer vector from output
@clarkmoody clarkmoody marked this pull request as ready for review July 23, 2019 03:16
@clarkmoody
Copy link
Contributor Author

Ok, ready for review here. I've tweaked the interface and options again. Please let me know if it makes any sense or if we can word the help text better for what's going on with 5-bit and 8-bit values.

Copy link
Owner

@stevenroose stevenroose left a comment

Choose a reason for hiding this comment

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

Nice! The first real contributor! I left some minor remarks!

src/bin/hal/cmd/bech32/decode.rs Outdated Show resolved Hide resolved
src/bin/hal/cmd/bech32/decode.rs Outdated Show resolved Hide resolved
src/bin/hal/cmd/bech32/decode.rs Outdated Show resolved Hide resolved
bech32: result.unwrap(),
hrp: hrp.to_string(),
payload: payload_bytes.into(),
payload_base256: match Vec::<u8>::from_base32(&payload_base32) {
Copy link
Owner

Choose a reason for hiding this comment

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

Just curious: what would this field be useful for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The payload field is the 5-bit values, and payload_base256 is the result of convert_bits from 5- to 8-bit, so we get back the original bytes. This does not work for BIP-173 addresses, since they abuse the encoding format with the version number taking the first 5-bit slot.

Copy link
Owner

Choose a reason for hiding this comment

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

Aaaah, lol, I was reading "base256" as 256-bit. The base thing confuses me, though. Do you think "payload_base256" is more clear than "payload_bytes"? By definition a "byte" is base256, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's go with payload_bytes and see how that feels.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, renamed.

src/bin/hal/cmd/bech32/encode.rs Outdated Show resolved Hide resolved
src/bin/hal/cmd/bech32/encode.rs Outdated Show resolved Hide resolved
Use .expect instead of explicit error checking. Lowercase expect error
messages.

Put payload_base256 assignment inline with struct definition.
@clarkmoody
Copy link
Contributor Author

Ok, first round of feedback addressed.

@stevenroose stevenroose merged commit 2503e24 into stevenroose:master Jul 26, 2019
@clarkmoody clarkmoody deleted the bech32 branch July 26, 2019 15:15
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.

2 participants