stakwork / sphinx-js

Web extension and PWA for Sphinx.chat and 2nd Brain
2 stars 2 forks source link

Implement buy L402 flow #4

Open ecurrencyhodler opened 9 months ago

ecurrencyhodler commented 9 months ago

L402 and Macaroons

When a user is funding their account, an L402 will be provisioned for them by secondbrain. An L402 will function as both an account identifier and balance for the user. This is stored inside the macaroon alongside the preimage in the chrome extension. The macaroon is also stored inside the second brain. These macaroons together is how the chrome extension and secondbrain communicate payments and account balances to one another.

Here is a visualization to help:

Screenshot 2023-12-05 at 10 59 56 AM

Testing environment

To run a testing environment locally, use sphinx-stack: https://github.com/stakwork/sphinx-stack

User story - Buys L402

There are two instances when this flow is triggers:

  1. When someone first adds a secondbrain
  2. When their account reaches 0.

The flow

  1. When a secondbrain has 0 sats, they will be prompted to fund their account.
  2. User types it amount they want and clicks on "generate invoice". An api call (/buy_lsat) is made to purchase an LSAT. The response returns a Macaroon and Invoice. You can specify which 2B endpoint to hit using the url by using this logic.
  3. The macaroon in the 2B encoded the preimage and send the invoice to the macaroon in the chrome extension.
  4. The macaroon in the chrome extension must decode the preimage and store it.
  5. The user then pays invoice.
  6. In the backend, invoices should be polled every 5 seconds during the duration of the time the invoice is displayed. API for this is /check_lsat.
  7. If status is paid, what will be returned is the the L402 along with the balance.
  8. Then success screen is then triggered and balance amount is displayed. The amount will be set in the frontend and by the amount the user had set previously. However later balance updates will be updated by an api call /balance.
  9. If timer expires, balance is not set.

Design

Fund your secondbrain 35 Type in an amount to deposit 27 Display Invoice 37 Success Screen This screen lasts for 3 seconds. The user can click anywhere inside the success screen to exit out. 28 Submit link After the success screen, they will be taken inside the 2B they funded and prompted to add a link. Make sure to note that the balance is correctly reflected. 29

Acceptance Criteria