bitcoinjs / bolt11

A library for encoding and decoding lightning network payment requests as defined in BOLT #11.
MIT License
93 stars 65 forks source link

Error: Invalid argument #75

Closed patrickReiis closed 3 months ago

patrickReiis commented 3 months ago

I'm trying to decode a valid invoice to get the amount and this error happens:

Error: Invalid argument
    at Object.ecdsaRecover (file:///home/me/.cache/deno/npm/registry.npmjs.org/secp256k1/4.0.3/lib/index.js:300:25)
    at Object.decode (file:///home/winlectro/.cache/deno/npm/registry.npmjs.org/bolt11/1.4.1/payreq.js:981:43)
    at getAmount (file:///home/me/Documents/Dev/ditto/src/utils/bolt11.ts:8:28)
    at file:///home/me/Documents/Dev/ditto/src/utils/bolt11.test.ts:6:5
    at innerWrapped (ext:cli/40_test.js:191:11)
    at exitSanitizer (ext:cli/40_test.js:107:33)
    at outerWrapped (ext:cli/40_test.js:134:20)

I am using Deno and running it as a test

Here's my code:

  1 import bolt11 from 'bolt11';                                                               
  2                                                                                                   
  3 /** Decodes the invoice and returns the amount in millisatoshis */                                                                                            
  4 function getAmount(invoice: string | undefined): string | undefined {
  5   if (!invoice) return;                                                                           
  6 
  7   try {                                                                                           
  8     const decoded = bolt11.decode(invoice);                                                                                                                                               
  9     return decoded?.millisatoshis ?? undefined;                                                   
 10   } catch (e) {                                                                                                                                           
 11     console.log(e);                                                                                                              
 12     return;                                                                                                                           
 13   }                                                                                                                                                                     
 14 }                                                                                                 
 15   
 16 export { getAmount };
patrickReiis commented 3 months ago

The test:

import { assertEquals } from '@std/assert';
import { getAmount } from '@/utils/bolt11.ts';

Deno.test('Amount is 200000', () => {
  assertEquals(
    getAmount(
      'lnbc2u1pn8qatypp5dweqaltlry2vgpxxyc0puxnc50335yznevj2g46wrhfm2694lhgqhp576ekte7lhhtsxdk6tfvkpyp8gdk2xccmuccdxwjd0fqdh34wfseqcqzzsxqyz5vqsp5n44zva7xndawg5l2r9d85v0tszwejtfzkc7v90d6c7d3nsdt0qds9qxpqysgqx2v2artsxmnfkpapdm9f5pahjs8etlpe7kcjue2kffhjg3jrtearstjvenr6lxzhpw3es4hpchzzeet7ul88elurfmvr9v94v0655rgpy7m7r5',
    ),
    '200000',
  );
});
junderw commented 3 months ago

I have never used deno, can you make a repository and in the README explain which version of deno I should use and what command I should run to get the deno test to run?

patrickReiis commented 3 months ago

Hi, I created a new project from scratch and it works, I don't really know what happened but it had to do with libsecp256k1

The project I just created, in case someone wants to see in the future: https://github.com/patrickReiis/deno-bolt11
The project where all of this problem happened: https://gitlab.com/soapbox-pub/ditto

To fix it, instead of using bolt11, I ended up using a fork: https://github.com/nbd-wtf/light-bolt11-decoder

junderw commented 3 months ago

Hi, I created a new project from scratch and it works

Glad to hear that it works!