slowli / bech32-buffer

Bech32 encoding for byte buffers

Home Page:https://slowli.github.io/bech32-buffer/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Doesn't decode bech32 Bitcoin addresses.

stl1988 opened this issue · comments

It always says "Error: Excessive padding: 5 (max 4 allowed)" I think this shouldn't happen.

Thanks for the report! Indeed, bech32.decode() does not support Bitcoin addresses out of the box (it's geared towards generic Bech32-encoded data). There is need for a bit more legwork, as can be seen in test vectors:

function decodeBitcoinAddress(address) {
  const decoded = bech32.decodeTo5BitArray(address);
  return {
    prefix: decoded.prefix, // should equal `bc` or `tb` for valid addresses
    version: decoded.data[0], // should equal 0 for currently supported addresses
    script: bech32.from5BitArray(decoded.data.subarray(1)), // should have length 20 or 32
  };
}

I'll add decodeBitcoinAddress / encodeBitcoinAddress to the library.