bit4sat/ln/api.go

253 lines
5.1 KiB
Go
Raw Normal View History

2019-03-26 21:01:54 +00:00
package ln
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
2019-03-29 18:50:11 +00:00
"strconv"
2019-04-03 22:12:03 +00:00
"strings"
2019-03-26 21:01:54 +00:00
"git.sp4ke.com/sp4ke/bit4sat/btc"
2019-04-03 22:12:03 +00:00
"git.sp4ke.com/sp4ke/bit4sat/bus"
2019-04-01 10:12:29 +00:00
"git.sp4ke.com/sp4ke/bit4sat/config"
2019-04-03 22:12:03 +00:00
"git.sp4ke.com/sp4ke/bit4sat/db"
"git.sp4ke.com/sp4ke/bit4sat/lndrpc"
"git.sp4ke.com/sp4ke/bit4sat/utils"
2019-03-26 21:01:54 +00:00
"github.com/gin-gonic/gin"
2019-04-03 22:12:03 +00:00
"github.com/mediocregopher/radix/v3"
2019-03-26 21:01:54 +00:00
)
var (
2019-03-30 18:42:41 +00:00
Client *http.Client
2019-03-26 21:01:54 +00:00
)
2019-03-30 18:42:41 +00:00
func Info() (gin.H, error) {
2019-03-26 21:01:54 +00:00
result := make(gin.H)
2019-03-30 18:42:41 +00:00
resp, err := Client.Get(getUrl(InfoEndpoint))
2019-03-26 21:01:54 +00:00
if err != nil {
return nil, err
}
jsonDec := json.NewDecoder(resp.Body)
jsonDec.Decode(&result)
return result, nil
}
2019-04-03 22:12:03 +00:00
// Quick check if invoice was paid
2019-04-02 17:40:25 +00:00
func CheckInvoice(id string) (*Invoice, error) {
2019-04-03 22:12:03 +00:00
lnInvoice, err := lndrpc.LookupInvoiceRhashStr(id)
2019-04-02 17:40:25 +00:00
if err != nil {
return nil, err
}
2019-04-03 22:12:03 +00:00
invoice := InvoiceFromLndIn(lnInvoice)
2019-04-02 17:40:25 +00:00
2019-04-03 22:12:03 +00:00
return invoice, nil
2019-04-02 17:40:25 +00:00
}
2019-04-03 22:12:03 +00:00
// This will watch on the pubsub for paid invoices given an invoice id
func PollPaidInvoice(invoiceId string, invoicePaidChan chan<- *Invoice, errorChan chan<- error) {
watchPaidChannel := make(chan radix.PubSubMessage)
2019-04-04 00:27:09 +00:00
busName := fmt.Sprintf("%s:*", bus.InvoicePaidChannelPrefix)
2019-04-03 22:12:03 +00:00
err := db.DB.RedisPubSub.PSubscribe(watchPaidChannel, busName)
if err != nil {
errorChan <- err
return
}
for {
msg := <-watchPaidChannel
2019-04-04 00:27:09 +00:00
//log.Printf("received message on %s", msg.Channel)
2019-04-03 22:12:03 +00:00
2019-04-04 00:27:09 +00:00
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)
2019-04-03 22:12:03 +00:00
if targetInvoiceId == invoiceId {
invoice := Invoice{}
err := json.Unmarshal(msg.Message, &invoice)
if err != nil {
errorChan <- err
return
}
invoicePaidChan <- &invoice
return
}
}
2019-04-04 00:27:09 +00:00
}
func PollPaidInvoiceTMP(invoiceId string, invoiceChan chan<- *Invoice, errorChan chan<- error) {
invoice := Invoice{}
2019-03-29 18:50:11 +00:00
reqParams := url.Values{}
// Timeout in seconds
reqParams.Set("timeout", strconv.Itoa(60))
2019-03-29 18:50:11 +00:00
reqURI := fmt.Sprintf("%s/%s/wait?%s",
2019-04-02 17:40:25 +00:00
getUrl(InvoiceEndpoint),
invoiceId,
2019-03-29 18:50:11 +00:00
reqParams.Encode())
log.Printf("polling to %s", reqURI)
var err error
var jsonDec *json.Decoder
2019-03-29 18:50:11 +00:00
resp := &http.Response{}
for {
2019-03-31 20:53:53 +00:00
//log.Println("new poll")
2019-03-29 18:50:11 +00:00
resp, err = http.Get(reqURI)
if err != nil {
2019-03-30 18:42:41 +00:00
log.Printf("Error: ", err)
2019-03-29 18:50:11 +00:00
}
jsonDec = json.NewDecoder(resp.Body)
2019-03-29 18:50:11 +00:00
if resp.StatusCode == http.StatusPaymentRequired {
2019-04-02 17:40:25 +00:00
log.Printf("invoice %s not yet paid", invoiceId)
2019-03-29 18:50:11 +00:00
continue
}
if resp.StatusCode == http.StatusGone {
2019-04-02 17:40:25 +00:00
log.Printf("invoice expired %s", invoiceId)
invoice.Expired = true
break
2019-03-29 18:50:11 +00:00
}
if resp.StatusCode == http.StatusOK {
2019-04-02 17:40:25 +00:00
log.Printf("Invoice paid %s", invoice)
2019-03-29 18:50:11 +00:00
break
} else {
log.Println("else !")
// This section handles unknown answer from ln-charge
log.Printf("InvoicePoll Error: unknown resopnse %s", resp.Status)
jsonDec.Decode(&invoice)
2019-03-29 18:50:11 +00:00
close(invoiceChan)
errorChan <- fmt.Errorf("%s: %s", resp.Status, invoice)
return
}
2019-03-29 18:50:11 +00:00
}
jsonDec.Decode(&invoice)
invoiceChan <- &invoice
2019-04-02 17:40:25 +00:00
log.Printf("quit polling %s", invoiceId)
2019-03-29 18:50:11 +00:00
}
func NewInvoiceForUpload(amount float64, curr Currency, uploadId string) (*Invoice, error) {
var err error
var satValue int64
description := fmt.Sprintf("bit4sat upload: %s", uploadId)
if curr == CurMSat {
log.Println("cur is msat")
// Convert to MSAT, meaning values under 1000 MSAT are ignored
satValue = int64(amount / 1000)
// Other supported currecies
}
if curr != CurSat {
log.Println("cur is custom")
// Get Sat satValue for this invoice
msatVal, err := btc.ToMsat(CurrencyString[curr], amount)
if err != nil {
return nil, err
}
satValue = msatVal / 1000
} else {
satValue = int64(amount)
}
lndInvoice, err := lndrpc.AddInvoiceSat(description, satValue)
if err != nil {
return nil, err
}
invoice := InvoiceFromLndIn(lndInvoice)
invoice.QuotedCurrency = CurrencyString[curr]
invoice.QuotedAmount = amount
return invoice, nil
}
func NewInvoiceForUploadTMP(amount float32, curr Currency, uploadId string) (*Invoice, error) {
2019-03-26 21:01:54 +00:00
2019-04-01 10:12:29 +00:00
webhookUrl := fmt.Sprintf("http://%s:%s/%s", config.ApiHost, config.ApiPort,
config.ChargeCallbackEndpoint)
2019-03-26 21:01:54 +00:00
reqData := gin.H{
2019-03-29 18:50:11 +00:00
"description": fmt.Sprintf("bit4sat upload: %s", uploadId),
2019-04-01 10:12:29 +00:00
"webhook": webhookUrl,
2019-03-29 18:50:11 +00:00
}
// If Satoshi convert to millisat
if curr == CurSat {
reqData["msatoshi"] = amount * 1000
} else {
reqData["currency"] = CurrencyString[curr]
reqData["amount"] = amount
2019-03-26 21:01:54 +00:00
}
jsonEnc, err := json.Marshal(reqData)
if err != nil {
return nil, err
}
2019-03-31 20:53:53 +00:00
log.Printf("Asking for invoice with: %s", jsonEnc)
2019-03-30 18:42:41 +00:00
resp, err := Client.Post(getUrl(InvoiceEndpoint),
2019-03-26 21:01:54 +00:00
"application/json",
bytes.NewReader(jsonEnc))
if err != nil {
return nil, err
}
2019-03-29 18:50:11 +00:00
invoice := Invoice{}
2019-03-26 21:01:54 +00:00
jsonDec := json.NewDecoder(resp.Body)
2019-03-29 18:50:11 +00:00
jsonDec.Decode(&invoice)
2019-03-26 21:01:54 +00:00
2019-03-29 18:50:11 +00:00
return &invoice, nil
2019-03-26 21:01:54 +00:00
}
func getUrl(endpoint string) string {
url, err := url.Parse(fmt.Sprintf("http://api-token:%s@%s/%s",
2019-04-01 10:12:29 +00:00
config.LnChargeToken,
config.LnChargeApi,
2019-03-26 21:01:54 +00:00
endpoint))
if err != nil {
log.Fatal(err)
}
return url.String()
}
2019-03-30 18:42:41 +00:00
func init() {
Client = utils.NewHttpClient()
2019-03-30 18:42:41 +00:00
}