src/utils/bech32.js
function convert(data, output, frombits, tobits, pad, off) {
  const maxv = (1 << tobits) - 1;
  let acc = 0;
  let bits = 0;
  let j = 0;

  if (pad !== -1)
    output[j++] = pad;

  for (let i = off; i < data.length; i++) {
    const value = data[i];

    if ((value >> frombits) !== 0)
      throw new Error('Invalid bech32 bits.');

    acc = (acc << frombits) | value;
    bits += frombits;

    while (bits >= tobits) {
      bits -= tobits;
      output[j++] = (acc >>> bits) & maxv;
    }
  }

  if (pad !== -1) {
    if (bits > 0)
      output[j++] = (acc << (tobits - bits)) & maxv;
  } else {
    if (bits >= frombits || ((acc << (tobits - bits)) & maxv))
      throw new Error('Invalid bech32 bits.');
  }

  return output.slice(0, j);
}
function convert(data, output, frombits, tobits, pad, off) {
  const maxv = (1 << tobits) - 1;
  let acc = 0;
  let bits = 0;
  let j = 0;

  if (pad !== -1)
    output[j++] = pad;

  for (let i = off; i < data.length; i++) {
    const value = data[i];

    if ((value >> frombits) !== 0)
      throw new Error('Invalid bech32 bits.');

    acc = (acc << frombits) | value;
    bits += frombits;

    while (bits >= tobits) {
      bits -= tobits;
      output[j++] = (acc >>> bits) & maxv;
    }
  }

  if (pad !== -1) {
    if (bits > 0)
      output[j++] = (acc << (tobits - bits)) & maxv;
  } else {
    if (bits >= frombits || ((acc << (tobits - bits)) & maxv))
      throw new Error('Invalid bech32 bits.');
  }

  return output.slice(0, j);
}
function convert ( data , output , frombits , tobits , pad , off ) { const maxv = ( 1 << tobits ) - 1 ; let acc = 0 ; let bits = 0 ; let j = 0 ; if ( pad !== - 1 ) output [ j ++ ] = pad ; for ( let i = off ; i < data . length ; i ++ ) { const value = data [ i ] ; if ( ( value >> frombits ) !== 0 ) throw new Error ( 'Invalid bech32 bits.' ) ; acc = ( acc << frombits ) | value ; bits += frombits ; while ( bits >= tobits ) { bits -= tobits ; output [ j ++ ] = ( acc >>> bits ) & maxv ; } } if ( pad !== - 1 ) { if ( bits > 0 ) output [ j ++ ] = ( acc << ( tobits - bits ) ) & maxv ; } else { if ( bits >= frombits || ( ( acc << ( tobits - bits ) ) & maxv ) ) throw new Error ( 'Invalid bech32 bits.' ) ; } return output . slice ( 0 , j ) ; }
Convert serialized data to bits,
suitable to be serialized as bech32.
@param {Buffer} data
@param {Buffer} output
@param {Number} frombits
@param {Number} tobits
@param {Number} pad
@param {Number} off
@returns {Buffer}
Convert serialized data to bits suitable to be serialized as bech32 .