src/utils/bech32.js
function deserialize(str) {
  let dlen = 0;

  if (str.length < 8 || str.length > 90)
    throw new Error('Invalid bech32 string length.');

  while (dlen < str.length && str[(str.length - 1) - dlen] !== '1')
    dlen++;

  const hlen = str.length - (1 + dlen);

  if (hlen < 1 || dlen < 6)
    throw new Error('Invalid bech32 data length.');

  dlen -= 6;

  const data = Buffer.allocUnsafe(dlen);

  let chk = 1;
  let lower = false;
  let upper = false;
  let hrp = '';

  for (let i = 0; i < hlen; i++) {
    let ch = str.charCodeAt(i);

    if (ch < 0x21 || ch > 0x7e)
      throw new Error('Invalid bech32 character.');

    if (ch >= 0x61 && ch <= 0x7a) {
      lower = true;
    } else if (ch >= 0x41 && ch <= 0x5a) {
      upper = true;
      ch = (ch - 0x41) + 0x61;
    }

    hrp += String.fromCharCode(ch);
    chk = polymod(chk) ^ (ch >> 5);
  }

  chk = polymod(chk);

  let i;
  for (i = 0; i < hlen; i++)
    chk = polymod(chk) ^ (str.charCodeAt(i) & 0x1f);

  i++;

  while (i < str.length) {
    const ch = str.charCodeAt(i);
    const v = (ch & 0x80) ? -1 : TABLE[ch];

    if (ch >= 0x61 && ch <= 0x7a)
      lower = true;
    else if (ch >= 0x41 && ch <= 0x5a)
      upper = true;

    if (v === -1)
      throw new Error('Invalid bech32 character.');

    chk = polymod(chk) ^ v;

    if (i + 6 < str.length)
      data[i - (1 + hlen)] = v;

    i++;
  }

  if (lower && upper)
    throw new Error('Invalid bech32 casing.');

  if (chk !== 1)
    throw new Error('Invalid bech32 checksum.');

  return [hrp, data.slice(0, dlen)];
}
function deserialize(str) {
  let dlen = 0;

  if (str.length < 8 || str.length > 90)
    throw new Error('Invalid bech32 string length.');

  while (dlen < str.length && str[(str.length - 1) - dlen] !== '1')
    dlen++;

  const hlen = str.length - (1 + dlen);

  if (hlen < 1 || dlen < 6)
    throw new Error('Invalid bech32 data length.');

  dlen -= 6;

  const data = Buffer.allocUnsafe(dlen);

  let chk = 1;
  let lower = false;
  let upper = false;
  let hrp = '';

  for (let i = 0; i < hlen; i++) {
    let ch = str.charCodeAt(i);

    if (ch < 0x21 || ch > 0x7e)
      throw new Error('Invalid bech32 character.');

    if (ch >= 0x61 && ch <= 0x7a) {
      lower = true;
    } else if (ch >= 0x41 && ch <= 0x5a) {
      upper = true;
      ch = (ch - 0x41) + 0x61;
    }

    hrp += String.fromCharCode(ch);
    chk = polymod(chk) ^ (ch >> 5);
  }

  chk = polymod(chk);

  let i;
  for (i = 0; i < hlen; i++)
    chk = polymod(chk) ^ (str.charCodeAt(i) & 0x1f);

  i++;

  while (i < str.length) {
    const ch = str.charCodeAt(i);
    const v = (ch & 0x80) ? -1 : TABLE[ch];

    if (ch >= 0x61 && ch <= 0x7a)
      lower = true;
    else if (ch >= 0x41 && ch <= 0x5a)
      upper = true;

    if (v === -1)
      throw new Error('Invalid bech32 character.');

    chk = polymod(chk) ^ v;

    if (i + 6 < str.length)
      data[i - (1 + hlen)] = v;

    i++;
  }

  if (lower && upper)
    throw new Error('Invalid bech32 casing.');

  if (chk !== 1)
    throw new Error('Invalid bech32 checksum.');

  return [hrp, data.slice(0, dlen)];
}
function deserialize ( str ) { let dlen = 0 ; if ( str . length < 8 || str . length > 90 ) throw new Error ( 'Invalid bech32 string length.' ) ; while ( dlen < str . length && str [ ( str . length - 1 ) - dlen ] !== '1' ) dlen ++ ; const hlen = str . length - ( 1 + dlen ) ; if ( hlen < 1 || dlen < 6 ) throw new Error ( 'Invalid bech32 data length.' ) ; dlen -= 6 ; const data = Buffer . allocUnsafe ( dlen ) ; let chk = 1 ; let lower = false ; let upper = false ; let hrp = '' ; for ( let i = 0 ; i < hlen ; i ++ ) { let ch = str . charCodeAt ( i ) ; if ( ch < 0x21 || ch > 0x7e ) throw new Error ( 'Invalid bech32 character.' ) ; if ( ch >= 0x61 && ch <= 0x7a ) { lower = true ; } else if ( ch >= 0x41 && ch <= 0x5a ) { upper = true ; ch = ( ch - 0x41 ) + 0x61 ; } hrp += String . fromCharCode ( ch ) ; chk = polymod ( chk ) ^ ( ch >> 5 ) ; } chk = polymod ( chk ) ; let i ; for ( i = 0 ; i < hlen ; i ++ ) chk = polymod ( chk ) ^ ( str . charCodeAt ( i ) & 0x1f ) ; i ++ ; while ( i < str . length ) { const ch = str . charCodeAt ( i ) ; const v = ( ch & 0x80 ) ? - 1 : TABLE [ ch ] ; if ( ch >= 0x61 && ch <= 0x7a ) lower = true ; else if ( ch >= 0x41 && ch <= 0x5a ) upper = true ; if ( v === - 1 ) throw new Error ( 'Invalid bech32 character.' ) ; chk = polymod ( chk ) ^ v ; if ( i + 6 < str . length ) data [ i - ( 1 + hlen ) ] = v ; i ++ ; } if ( lower && upper ) throw new Error ( 'Invalid bech32 casing.' ) ; if ( chk !== 1 ) throw new Error ( 'Invalid bech32 checksum.' ) ; return [ hrp , data . slice ( 0 , dlen ) ] ; }
Decode a bech32 string.
@param {String} str
@returns {Array} [hrp, data]
Decode a bech32 string .