Module: AdequateCryptoAddress::Utils::Bch
- Included in:
- Bch
- Defined in:
- lib/adequate_crypto_address/utils/bch.rb
Constant Summary collapse
- CHARSET =
'qpzry9x8gf2tvdw0s3jn54khce6mua7l'
Class Method Summary collapse
- .b32decode(inputs) ⇒ Object
- .b32encode(inputs) ⇒ Object
- .calculate_cash_checksum(payload) ⇒ Object
- .code_list_to_string(code_list) ⇒ Object
- .convertbits(data, frombits, tobits, pad = true) ⇒ Object
- .expanded_prefix ⇒ Object
- .polymod(values) ⇒ Object
- .verify_cash_checksum(payload) ⇒ Object
Class Method Details
.b32decode(inputs) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/adequate_crypto_address/utils/bch.rb', line 14 def b32decode(inputs) out = [] return out unless inputs inputs.split('').each do |letter| out.push(CHARSET.index(letter)) end out end |
.b32encode(inputs) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/adequate_crypto_address/utils/bch.rb', line 70 def b32encode(inputs) out = '' inputs.each do |char_code| out += CHARSET[char_code].to_s end out end |
.calculate_cash_checksum(payload) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/adequate_crypto_address/utils/bch.rb', line 55 def calculate_cash_checksum(payload) poly = polymod( + payload + [0, 0, 0, 0, 0, 0, 0, 0]) out = [] 8.times do |i| out.push((poly >> 5 * (7 - i)) & 0x1f) end out end |
.code_list_to_string(code_list) ⇒ Object
10 11 12 |
# File 'lib/adequate_crypto_address/utils/bch.rb', line 10 def code_list_to_string(code_list) code_list.map { |i| Array(i).pack('C*') }.flatten.join end |
.convertbits(data, frombits, tobits, pad = true) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/adequate_crypto_address/utils/bch.rb', line 78 def convertbits(data, frombits, tobits, pad = true) acc = 0 bits = 0 ret = [] maxv = (1 << tobits) - 1 max_acc = (1 << (frombits + tobits - 1)) - 1 data.each do |value| return nil if value < 0 || ((value >> frombits) != 0) acc = ((acc << frombits) | value) & max_acc bits += frombits while bits >= tobits bits -= tobits ret.push((acc >> bits) & maxv) end end if pad ret.push((acc << (tobits - bits)) & maxv) if bits != 0 elsif bits >= frombits || (((acc << (tobits - bits)) & maxv) != 0) return nil end ret end |
.expanded_prefix ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/adequate_crypto_address/utils/bch.rb', line 43 def val = if prefix prefix.to_s.split('').map do |i| i.ord & 0x1f end else [] end val + [0] end |
.polymod(values) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/adequate_crypto_address/utils/bch.rb', line 24 def polymod(values) chk = 1 generator = [ [0x01, 0x98f2bc8e61], [0x02, 0x79b76d99e2], [0x04, 0xf33e5fb3c4], [0x08, 0xae2eabe2a8], [0x10, 0x1e4f43e470] ] values.each do |value| top = chk >> 35 chk = ((chk & 0x07ffffffff) << 5) ^ value generator.each do |i| chk ^= i[1] if (top & i[0]) != 0 end end chk ^ 1 end |
.verify_cash_checksum(payload) ⇒ Object
64 65 66 67 68 |
# File 'lib/adequate_crypto_address/utils/bch.rb', line 64 def verify_cash_checksum(payload) polymod( + payload) == 0 rescue TypeError raise AdequateCryptoAddress::InvalidAddress end |