src/utils/bech32.js
function serialize(hrp, data) {
  let chk = 1;
  let i;

  for (i = 0; i < hrp.length; i++) {
    const ch = hrp.charCodeAt(i);

    if ((ch >> 5) === 0)
      throw new Error('Invalid bech32 character.');

    chk = polymod(chk) ^ (ch >> 5);
  }

  if (i + 7 + data.length > 90)
    throw new Error('Invalid bech32 data length.');

  chk = polymod(chk);

  let str = '';

  for (let i = 0; i < hrp.length; i++) {
    const ch = hrp.charCodeAt(i);
    chk = polymod(chk) ^ (ch & 0x1f);
    str += hrp[i];
  }

  str += '1';

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

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

    chk = polymod(chk) ^ ch;
    str += CHARSET[ch];
  }

  for (let i = 0; i < 6; i++)
    chk = polymod(chk);

  chk ^= 1;

  for (let i = 0; i < 6; i++)
    str += CHARSET[(chk >>> ((5 - i) * 5)) & 0x1f];

  return str;
}
function serialize(hrp, data) {
  let chk = 1;
  let i;

  for (i = 0; i < hrp.length; i++) {
    const ch = hrp.charCodeAt(i);

    if ((ch >> 5) === 0)
      throw new Error('Invalid bech32 character.');

    chk = polymod(chk) ^ (ch >> 5);
  }

  if (i + 7 + data.length > 90)
    throw new Error('Invalid bech32 data length.');

  chk = polymod(chk);

  let str = '';

  for (let i = 0; i < hrp.length; i++) {
    const ch = hrp.charCodeAt(i);
    chk = polymod(chk) ^ (ch & 0x1f);
    str += hrp[i];
  }

  str += '1';

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

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

    chk = polymod(chk) ^ ch;
    str += CHARSET[ch];
  }

  for (let i = 0; i < 6; i++)
    chk = polymod(chk);

  chk ^= 1;

  for (let i = 0; i < 6; i++)
    str += CHARSET[(chk >>> ((5 - i) * 5)) & 0x1f];

  return str;
}
function serialize ( hrp , data ) { let chk = 1 ; let i ; for ( i = 0 ; i < hrp . length ; i ++ ) { const ch = hrp . charCodeAt ( i ) ; if ( ( ch >> 5 ) === 0 ) throw new Error ( 'Invalid bech32 character.' ) ; chk = polymod ( chk ) ^ ( ch >> 5 ) ; } if ( i + 7 + data . length > 90 ) throw new Error ( 'Invalid bech32 data length.' ) ; chk = polymod ( chk ) ; let str = '' ; for ( let i = 0 ; i < hrp . length ; i ++ ) { const ch = hrp . charCodeAt ( i ) ; chk = polymod ( chk ) ^ ( ch & 0x1f ) ; str += hrp [ i ] ; } str += '1' ; for ( let i = 0 ; i < data . length ; i ++ ) { const ch = data [ i ] ; if ( ( ch >> 5 ) !== 0 ) throw new Error ( 'Invalid bech32 value.' ) ; chk = polymod ( chk ) ^ ch ; str += CHARSET [ ch ] ; } for ( let i = 0 ; i < 6 ; i ++ ) chk = polymod ( chk ) ; chk ^= 1 ; for ( let i = 0 ; i < 6 ; i ++ ) str += CHARSET [ ( chk >>> ( ( 5 - i ) * 5 ) ) & 0x1f ] ; return str ; }
Encode hrp and data as a bech32 string.
@ignore
@param {String} hrp
@param {Buffer} data
@returns {String}
Encode hrp and data as a bech32 string .