diff --git a/api/handlers.go b/api/handlers.go index e1a6ca5..ceec6df 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -18,6 +18,9 @@ func sessionHandler(c *gin.Context) { session := sessions.Default(c) lastUp := session.Get(storage.SessLastUploadName) + session.Clear() + session.Save() + if lastUp != nil { // Get upload status diff --git a/api/routes.go b/api/routes.go index 4536873..a793863 100644 --- a/api/routes.go +++ b/api/routes.go @@ -51,7 +51,7 @@ func NewAPI() *API { panic(err) } - router.Use(sessions.Sessions("websocket-session", sessionStore)) + router.Use(sessions.Sessions("bit4sat-session", sessionStore)) // // diff --git a/btc/price_watcher.go b/btc/price_watcher.go new file mode 100644 index 0000000..cda8dd8 --- /dev/null +++ b/btc/price_watcher.go @@ -0,0 +1,2 @@ +//TODO: cache price rates for faster invoices +package btc diff --git a/bus/messages.go b/bus/messages.go index faa06eb..12c5832 100644 --- a/bus/messages.go +++ b/bus/messages.go @@ -5,7 +5,7 @@ const ( WatchInvoicesChannelName = "watch_invoices" WebsocketPubSubPrefix = "websocket_update" UploadUpdateChannelPrefix = "upload_update" - InvoicePaidChannelPrefix = "invoice:paid" + InvoicePaidChannelPrefix = "invoice@paid" ) // PubSub event types diff --git a/ln/api.go b/ln/api.go index 4866c66..a7d0a10 100644 --- a/ln/api.go +++ b/ln/api.go @@ -56,7 +56,7 @@ func PollPaidInvoice(invoiceId string, invoicePaidChan chan<- *Invoice, errorCha watchPaidChannel := make(chan radix.PubSubMessage) - busName := fmt.Sprintf("%s_*", bus.InvoicePaidChannelPrefix) + busName := fmt.Sprintf("%s:*", bus.InvoicePaidChannelPrefix) err := db.DB.RedisPubSub.PSubscribe(watchPaidChannel, busName) if err != nil { @@ -66,8 +66,14 @@ func PollPaidInvoice(invoiceId string, invoicePaidChan chan<- *Invoice, errorCha for { msg := <-watchPaidChannel + //log.Printf("received message on %s", msg.Channel) - targetInvoiceId := strings.Split(msg.Channel, "_")[1] + split := strings.Split(msg.Channel, ":") + if len(split) != 2 { + errorChan <- fmt.Errorf("error in pubsub channel parsing %s", msg.Channel) + } + targetInvoiceId := strings.Split(msg.Channel, ":")[1] + //log.Printf("target invoice is %s", targetInvoiceId) if targetInvoiceId == invoiceId { invoice := Invoice{} @@ -84,6 +90,7 @@ func PollPaidInvoice(invoiceId string, invoicePaidChan chan<- *Invoice, errorCha } } + } func PollPaidInvoiceTMP(invoiceId string, invoiceChan chan<- *Invoice, errorChan chan<- error) { diff --git a/ln/invoice.go b/ln/invoice.go index 5b9d8ed..9cf9322 100644 --- a/ln/invoice.go +++ b/ln/invoice.go @@ -4,6 +4,7 @@ import ( "encoding/hex" "encoding/json" "fmt" + "log" "strconv" "time" @@ -92,6 +93,7 @@ type Invoice struct { Msatoshi float64 `json:"msatoshi"` Payreq string `json:"payreq"` RHash string `json:"rhash"` + PreImage string `json:"-"` // used as secret admin token Status string `json:"status"` PaidAt timestamp `json:"paid_at"` CreatedAt timestamp `json:"created_at"` @@ -112,12 +114,14 @@ func (i *Invoice) UnmarshalBinary(b []byte) error { // Create new Invoice from lnrpc.Invoice func InvoiceFromLndIn(lndInvoice *lnrpc.Invoice) *Invoice { + log.Println(hex.EncodeToString(lndInvoice.RPreimage)) invoice := Invoice{ AddIndex: lndInvoice.AddIndex, Description: lndInvoice.GetMemo(), Msatoshi: float64(lndInvoice.Value * 1000), Payreq: lndInvoice.PaymentRequest, RHash: hex.EncodeToString(lndInvoice.RHash), + PreImage: hex.EncodeToString(lndInvoice.RPreimage), CreatedAt: timestamp(time.Unix(lndInvoice.CreationDate, 0)), Status: InvoiceStatus[UnPaid], } diff --git a/storage/upload_ctrl.go b/storage/upload_ctrl.go index 8a82a4d..7791ec7 100644 --- a/storage/upload_ctrl.go +++ b/storage/upload_ctrl.go @@ -81,6 +81,7 @@ func (ctrl UploadCtrl) New(c *gin.Context) { // Set the uploadId as last upload for session session.Set(SessLastUploadName, uploadId) + //session.AddFlash(SessLastUploadName, uploadId) session.Save() // If not free upload generate a an invoice @@ -94,6 +95,13 @@ func (ctrl UploadCtrl) New(c *gin.Context) { return } + // Save the secret admin token + err = SetUploadAdminToken(uploadId, invoice.PreImage) + if err != nil { + utils.JSONErrPriv(c, http.StatusInternalServerError, err) + return + } + // Update Upload Invoice err = SetUploadInvoice(uploadId, invoice) if err != nil { @@ -263,10 +271,23 @@ func (ctrl UploadCtrl) PollStatus(c *gin.Context) { return } + // get the secret admin token + token, err := GetUploadAdminToken(uploadId) + if err != nil { + utils.JSONErrPriv(c, http.StatusInternalServerError, err) + return + } + + // Clear the session + session := sessions.Default(c) + session.Clear() + session.Save() + // invoice was paid c.JSON(http.StatusAccepted, gin.H{ - "status": uploadStatus, - "invoice": invoice, + "status": uploadStatus, + "invoice": invoice, + "admin_token": token, }) return @@ -421,8 +442,8 @@ func (ctrl UploadCtrl) Upload(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "status": http.StatusOK, - "result": "uploaded", "store_status": UpStored, + "upload_id": uploadId, }) } diff --git a/storage/upload_model.go b/storage/upload_model.go index 7aa8599..aba78dc 100644 --- a/storage/upload_model.go +++ b/storage/upload_model.go @@ -261,6 +261,19 @@ func GetUploadStatus(id string) (status UpStatus, err error) { return } +func SetUploadAdminToken(uploadId, token string) error { + key := fmt.Sprintf("upload_admin_%s", uploadId) + return DB.Redis.Do(radix.FlatCmd(nil, "SET", key, token)) +} + +func GetUploadAdminToken(uploadId string) (string, error) { + var token string + key := fmt.Sprintf("upload_admin_%s", uploadId) + err := DB.Redis.Do(radix.FlatCmd(&token, "GET", key)) + return token, err + +} + func SetUploadInvoice(uploadId string, invoice *ln.Invoice) error { uploadInvoiceKey := fmt.Sprintf("upload_%s_invoice", uploadId) diff --git a/watchers/invoices.go b/watchers/invoices.go index 6d89355..35f1447 100644 --- a/watchers/invoices.go +++ b/watchers/invoices.go @@ -46,13 +46,14 @@ func WatchInvoice() { // Get the last invoice index cursor cursor := getLastSettledInvoiceIndex() - log.Println(cursor) ctxb := context.Background() client, cleanUp := lndrpc.GetClient() defer cleanUp() - subscription := &lnrpc.InvoiceSubscription{} + subscription := &lnrpc.InvoiceSubscription{ + SettleIndex: cursor, + } invoiceStream, err := client.SubscribeInvoices(ctxb, subscription) if err != nil { @@ -155,7 +156,7 @@ func handleSettledInvoice(invoice *lnrpc.Invoice) { // publish invoice was updated to upload_id_paid channel log.Printf("Notifying invoice paid %s", uploadId) - key := fmt.Sprintf("%s_%s", bus.InvoicePaidChannelPrefix, + key := fmt.Sprintf("%s:%s", bus.InvoicePaidChannelPrefix, newInvoice.RHash) err = db.DB.Redis.Do(radix.FlatCmd(nil, "PUBLISH",