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 Ruby implementation #17

Merged
merged 7 commits into from
May 27, 2017
Merged

Add Ruby implementation #17

merged 7 commits into from
May 27, 2017

Conversation

azuchi
Copy link
Contributor

@azuchi azuchi commented May 10, 2017

Ruby implementation of Bech32 encoding and decoding for Segwit address.

Copy link

@gwillen gwillen left a comment

Choose a reason for hiding this comment

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

Looks generally good, I did not run the tests (I'm assuming they pass for you), see my comments. Thanks for contributing this!


private_class_method :polymod, :expand_hrp

end
Copy link

Choose a reason for hiding this comment

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

Add a newline at end of file please.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added.

ret
end

end
Copy link

Choose a reason for hiding this comment

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

Newline here too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added.

bech = bech.downcase
# check data length
pos = bech.rindex(SEPARATOR)
return nil if pos.nil? || pos + 7 > bech.length || bech.length > 90
Copy link

Choose a reason for hiding this comment

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

It looks like the original Python disallows pos == 0 (a bech string starting with a separator) and this code does not?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added check to disallow pos == 0.


def addr
Bech32.encode(hrp, [ver] + convert_bits(prog, 8, 5))
end
Copy link

Choose a reason for hiding this comment

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

I note that the original Python code asserts that the result of decoding is valid here, which the port does not. Perhaps @sipa can comment on the circumstances under which such an assertion would or would not be valuable. (It seems like purely a check on the functioning of the underlying encode/decode functions, but I'm not 100% sure there's not some other work it's doing.)


def to_scriptpubkey
v = ver == 0 ? ver : ver + 0x80
([v, prog.length].pack("CC") + prog.map{|p|[p].pack("C")}.join).unpack('H*').first
Copy link

Choose a reason for hiding this comment

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

I am not savvy enough to follow this but I trust it's correct if the tests pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

pack("C") is packs contents into binary sequence.
All tests are cleared.


attr_accessor :hrp # human-readable part
attr_accessor :ver # witness version
attr_accessor :prog # witness program
Copy link

Choose a reason for hiding this comment

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

Since this is a somewhat different interface than the Python code being ported, can you add a small comment or example demonstrating usage?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add usage to README.md

@gwillen
Copy link

gwillen commented May 11, 2017

Awesome! utACK. (That is, I did not test this, but I give it a positive code review. I will leave the rest to @sipa .)

@azuchi
Copy link
Contributor Author

azuchi commented May 11, 2017

Thanks for the review!

@sipa sipa merged commit a06fd77 into sipa:master May 27, 2017
sipa added a commit that referenced this pull request May 27, 2017
a06fd77 Add usage document (azuchi)
88204c7 Remove unnecessary code (azuchi)
8e988e7 Add script pubkey parse logic to SegiwtAddr#scriptpubkey= (azuchi)
30d8816 Add encoded value check (azuchi)
590d898 Fix check data length logic (azuchi)
1274ee7 Add newline at end of file (azuchi)
8795117 Add Ruby implementation (azuchi)

Tree-SHA512: 3d60d8d749335bfeffe248e16d7cf4dad88d7f86d0207246c8e57d91472ffe74001759d6b333a2f9afc2b1ac8a1dc31d9834ec2b7572d43a0edb30ebabdcbabe
@azuchi azuchi deleted the add-ruby-impl branch May 31, 2017 13: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.

3 participants