lib/bitcoin/bech32.rb
# Utility for converting bytes of data between bases. These is used for
# BIP 173 address encoding/decoding to convert between sequences of bytes
# representing 8-bit values and groups of 5 bits. Conversions may be padded
# with trailing 0 bits to the nearest byte boundary. Returns nil if
# conversion requires padding and pad is false.
#
# For example:
#
#   convert_bits("\xFF\xFF", from_bits: 8, to_bits: 5, pad: true)
#     => "\x1F\x1F\x1F\10"
#
# See https://github.com/bitcoin/bitcoin/blob/595a7bab23bc21049526229054ea1fff1a29c0bf/src/utilstrencodings.h#L154
Utility for converting bytes of data between bases. These is used for
BIP 173 address encoding/decoding to convert between sequences of bytes
representing 8-bit values and groups of 5 bits. Conversions may be padded
with trailing 0 bits to the nearest byte boundary. Returns nil if
conversion requires padding and pad is false.

For example.




Utility for converting bytes of data between bases . These is used for BIP 173 address encoding / decoding to convert between sequences of bytes representing 8 - bit values and groups of 5 bits . Conversions may be padded with trailing 0 bits to the nearest byte boundary . Returns nil if conversion requires padding and pad is false . For example .
def convert_bits(chunks, from_bits:, to_bits:, pad:)
        output_mask = (1 << to_bits) - 1
        buffer_mask = (1 << (from_bits + to_bits - 1)) - 1
        buffer = 0
        bits = 0
        output = []
        chunks.each do |chunk|
          buffer = ((buffer << from_bits) | chunk) & buffer_mask
          bits += from_bits
          while bits >= to_bits
            bits -= to_bits
            output << ((buffer >> bits) & output_mask)
          end
        end
        output << ((buffer << (to_bits - bits)) & output_mask) if pad && bits > 0
        if !pad && (bits >= from_bits || ((buffer << (to_bits - bits)) & output_mask) != 0)
          return nil
        end
        output
      end
def convert_bits ( chunks , from_bits : , to_bits : , pad : ) output_mask = ( 1 << to_bits ) - 1 buffer_mask = ( 1 << ( from_bits + to_bits - 1 ) ) - 1 buffer = 0 bits = 0 output = [ ] chunks . each do | chunk | buffer = ( ( buffer << from_bits ) | chunk ) & buffer_mask bits += from_bits while bits >= to_bits bits -= to_bits output << ( ( buffer >> bits ) & output_mask ) end end output << ( ( buffer << ( to_bits - bits ) ) & output_mask ) if pad && bits > 0 if ! pad && ( bits >= from_bits || ( ( buffer << ( to_bits - bits ) ) & output_mask ) != 0 ) return nil end output end
Utility for converting bytes of data between bases.
Utility for converting bytes of data between bases .
chunks
<not_specific>
from_bits
<not_specific>
to_bits
<not_specific>
pad
<not_specific>