jb55 / lnsocket

A minimal C & js library for sending messages to lightning nodes
Other
61 stars 9 forks source link

Decrypt Error #10

Open lnbc1QWFyb24 opened 2 years ago

lnbc1QWFyb24 commented 2 years ago

When sending multiple RPC requests at once I get the following errors:

Screen Shot 2022-07-27 at 8 38 06 pm

When the requests are made separately waiting for each one to complete they all work perfectly, but when fired off at the same time the above error occurs.

I could just wait for each request to receive a response before sending the next one except in the case of long running requests such as waitinvoice which would block other requests for long periods, so it would be nice to be able to send requests in parallel if possible.

jb55 commented 2 years ago

do you have code to reproduce this?

jb55 commented 2 years ago

This can happen if you re-use the lnsocket instance. Make sure you're only using one instance of lnsocket per request. If you're re-using an instance over a longer period, I'll need to support this case via the "request id" in commando which I'm currently not using. In either case there shouldn't be decryption errors unless you're re-generating the key for each request or something...

lnbc1QWFyb24 commented 2 years ago

Yeah I am sending multiple requests on the same instance of lnsocket, but not re-generating the key for each request though. I'm batching requests in a queue and then processing them on the same connection and then closing the connection once all requests have been sent and have received responses.

My goal was to use the same connection if there are a burst of requests (like when the app loads) and then close the connection when all requests have received responses rather than opening multiple websocket connections at once.

Can multiple simultaneous connections use the same public key? I am playing around with the idea of a "session" where the rune is limited to a specific public key, so will need to use the same public key for all requests. Not 100% sure if that will be useful or not, but the thinking is that since runes cannot be invalidated, it seems a bit scary to put a rune out into the world that has full access, but if it was limited to a particular public key then when the session is deleted, that corresponding private key is gone.

I'll put together some simple repro code and post here.

lnbc1QWFyb24 commented 2 years ago

Here's some simplified code without the processing queue to repro the decrypt error:

async function batchRequest(credentials, requests) {
  const LNSocket = await window.lnsocket_init()

  lnsocket = LNSocket()
  lnsocket.genkey()

  const { protocol, ip, port, proxy, publicKey, rune } = credentials
  const url = proxy ? `${protocol}//${proxy}/${ip}:${port}` : `${protocol}//${ip}:${port}`

  await lnsocket.connect_and_init(publicKey, url)

  const responses = await Promise.all(requests.map((request) => lnsocket.rpc({ ...request, rune })))

  return responses.map(({ result }) => result)
}

const credentials = {
  port: 4001,
  protocol: 'ws:',
  publicKey: '0284ddc8168cb2a18ee7d600a538089286f3efe0938bd1dc7abfbc8512b50c07c7',
  ip: 'localhost',
  rune: '8xDBM7iRP0xEFRb7Fx5XRKIPaaKQb_X-XVyc4viix7g9NA=='
}

batchRequest(credentials, [
  { method: 'getinfo' },
  { method: 'listinvoices' },
  { method: 'listpays' }
]).then(console.log)
lnbc1QWFyb24 commented 2 years ago

For the moment, I can easily just make a single request per connection though to keep it simple!

jb55 commented 2 years ago

thx for the code... I'll try to see what's up...