paulmillr / scure-base

Secure, audited & 0-deps implementation of bech32, base64, base32, base16 & base58
https://paulmillr.com/noble/#scure
MIT License
106 stars 13 forks source link

Unable to decode a bech32 encoded lightning invoice. #18

Closed cmdruid closed 1 year ago

cmdruid commented 1 year ago

I am running the following code in nodejs (version 19+):

import { bech32 } from '@scure/base'

const test_enc  = 'lnbc1u1pjvy84epp5zrapr3w7tqelvjzwm0rwsac2ga79m982uruducydr2u6zwlhpasqhp5fe47lwjexge0lff7ru2g6757g35qajscy39hsz4dvqe97gnt3d3scqzzsxqyz5vqsp5ptv9dz544r5pxd3gkulqelakrtmx4nf47xw4mmm8a0u8j2up7mqs9qyyssqumxjespzkuwzdppw3hzkawgdedjyu2e0wnsk3t3y8g7mkpz49nn9rlrzsj07tz3hjnld80j749069puz9uanhr55p9ngw46cy2w295qpktsz9y'

const { bytes } = bech32.decodeToBytes(test_enc)
const decoded   = new TextDecoder().decode(bytes)

console.log('decoded:', decoded)

I get the following error:

Error: Non-zero padding: 32
    at convertRadix2 (./node_modules/@scure/base/lib/esm/index.js:175:15)
    at decode (./node_modules/@scure/base/lib/esm/index.js:210:36)
    at Object.decodeToBytes (./node_modules/@scure/base/lib/esm/index.js:360:40)

The invoice decodes normally using other decoding tools.

I'm not sure if this is a bug, or that I am using the library incorrectly. The above code works for smaller encoded strings.

paulmillr commented 1 year ago

can you give an example code of decoding it using a different lib?

cmdruid commented 1 year ago

Sure, here is an online decoder that I use for testing:

https://lightningdecoder.com/lnbc1u1pjvy84epp5zrapr3w7tqelvjzwm0rwsac2ga79m982uruducydr2u6zwlhpasqhp5fe47lwjexge0lff7ru2g6757g35qajscy39hsz4dvqe97gnt3d3scqzzsxqyz5vqsp5ptv9dz544r5pxd3gkulqelakrtmx4nf47xw4mmm8a0u8j2up7mqs9qyyssqumxjespzkuwzdppw3hzkawgdedjyu2e0wnsk3t3y8g7mkpz49nn9rlrzsj07tz3hjnld80j749069puz9uanhr55p9ngw46cy2w295qpktsz9y

Also, here is the github for the project. It appears they use the bech32 library in package.json:

https://github.com/andrerfneves/lightning-decoder

paulmillr commented 1 year ago

You are using the library in a wrong way. All tests actually pass.

Please take a look at the new README section: https://github.com/paulmillr/scure-base#bech32-bech32m-and-bitcoin

cmdruid commented 1 year ago

Thank you for the clarification and update. I will take a look at the new readme.