bit4sat/ln/api.go

185 lines
3.5 KiB
Go
Raw Normal View History

2019-03-26 21:01:54 +00:00
package ln
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"net/url"
2019-03-29 18:50:11 +00:00
"strconv"
2019-03-26 21:01:54 +00:00
"time"
2019-04-01 10:12:29 +00:00
"git.sp4ke.com/sp4ke/bit4sat/config"
2019-03-26 21:01:54 +00:00
"github.com/gin-gonic/gin"
)
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-03-30 18:42:41 +00:00
// Shoudl be called in goroutine
2019-04-02 17:40:25 +00:00
func CheckInvoice(id string) (*Invoice, error) {
invoice := Invoice{}
reqUri := fmt.Sprintf("%s/%s", getUrl(InvoiceEndpoint), id)
log.Printf("checking invoice %s", reqUri)
resp, err := http.Get(reqUri)
if err != nil {
return nil, err
}
jsonDec := json.NewDecoder(resp.Body)
err = jsonDec.Decode(&invoice)
return &invoice, err
}
func PollPaidInvoice(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
}
2019-04-02 17:40:25 +00:00
func NewInvoiceForUpload(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 newClient() *http.Client {
netTransport := &http.Transport{
Dial: (&net.Dialer{
Timeout: 5 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
}
c := &http.Client{
Timeout: time.Second * 10,
Transport: netTransport,
}
return c
}
func init() {
Client = newClient()
}