-
Notifications
You must be signed in to change notification settings - Fork 104
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
Conversation
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 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 |
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.
Add a newline at end of file please.
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.
added.
ret | ||
end | ||
|
||
end |
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.
Newline here too.
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.
added.
ref/ruby/bech32.rb
Outdated
bech = bech.downcase | ||
# check data length | ||
pos = bech.rindex(SEPARATOR) | ||
return nil if pos.nil? || pos + 7 > bech.length || bech.length > 90 |
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.
It looks like the original Python disallows pos == 0 (a bech string starting with a separator) and this code does not?
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.
Added check to disallow pos == 0.
ref/ruby/segwit_addr.rb
Outdated
|
||
def addr | ||
Bech32.encode(hrp, [ver] + convert_bits(prog, 8, 5)) | ||
end |
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.
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 |
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.
I am not savvy enough to follow this but I trust it's correct if the tests pass.
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.
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 |
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.
Since this is a somewhat different interface than the Python code being ported, can you add a small comment or example demonstrating usage?
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.
Add usage to README.md
If human-readable part dose not found, Bech32#decode returns nil.
Awesome! utACK. (That is, I did not test this, but I give it a positive code review. I will leave the rest to @sipa .) |
Thanks for the review! |
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
Ruby implementation of Bech32 encoding and decoding for Segwit address.