Class: Zilliqa::Util::Bech32
- Inherits:
-
Object
- Object
- Zilliqa::Util::Bech32
- Defined in:
- lib/zilliqa/util/bech32.rb
Class Method Summary collapse
- .from_bech32(address) ⇒ Object
- .to_bech32(address) ⇒ Object
-
.to_checksum_address(address) ⇒ Object
to_checksum_address.
Class Method Details
.from_bech32(address) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/zilliqa/util/bech32.rb', line 17 def self.from_bech32(address) data = Bitcoin::Bech32.decode(address) raise 'Expected hrp to be zil' unless data[0] == 'zil' ret = Bitcoin::Bech32.convert_bits(data[1], from_bits: 5, to_bits: 8, pad: false) to_checksum_address(Util.encode_hex(ret.pack('c*'))).sub('0x', '') end |
.to_bech32(address) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/zilliqa/util/bech32.rb', line 7 def self.to_bech32(address) raise 'Invalid address format.' unless Validator.address?(address) address = address.sub('0x','') ret = Bitcoin::Bech32.convert_bits(Util.decode_hex(address).bytes, from_bits: 8, to_bits: 5, pad: false) Bitcoin::Bech32.encode('zil', ret) end |
.to_checksum_address(address) ⇒ Object
to_checksum_address
takes hex-encoded string and returns the corresponding address
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/zilliqa/util/bech32.rb', line 33 def self.to_checksum_address(address) return from_bech32(address) if Validator.bech32?(address) address = address.downcase.gsub('0x', '') s1 = Digest::SHA256.hexdigest(Util.decode_hex(address)) v = s1.to_i(16) ret = ['0x'] address.each_char.each_with_index do |c, idx| if '1234567890'.include?(c) ret << c else ret << ((v & (2 ** (255 - 6 * idx))) < 1 ? c.downcase : c.upcase) end end ret.join end |