Release 2.2.3

This commit is contained in:
Juan Pablo Civile
2022-10-04 14:55:21 -03:00
parent b6d596a0ad
commit f21f3a04b4
524 changed files with 28443 additions and 30000 deletions

View File

@@ -3,7 +3,6 @@ package lnurl
import (
"encoding/json"
"fmt"
"github.com/google/uuid"
"io/ioutil"
"net/http"
"net/url"
@@ -61,26 +60,32 @@ type WithdrawResponse struct {
// After adding new codes here, remember to export them in the root libwallet
// module so that the apps can consume them.
const (
ErrNone int = 0
ErrDecode int = 1
ErrUnsafeURL int = 2
ErrUnreachable int = 3
ErrInvalidResponse int = 4
ErrResponse int = 5
ErrUnknown int = 6
ErrWrongTag int = 7
ErrNoAvailableBalance int = 8
ErrRequestExpired int = 9
ErrNoRoute int = 10
ErrTorNotSupported int = 11
ErrAlreadyUsed int = 12
ErrForbidden int = 13
ErrNone int = 0
ErrDecode int = 1
ErrUnsafeURL int = 2
ErrUnreachable int = 3
ErrInvalidResponse int = 4
ErrResponse int = 5
ErrUnknown int = 6
ErrWrongTag int = 7
ErrNoAvailableBalance int = 8
ErrRequestExpired int = 9
ErrNoRoute int = 10
ErrTorNotSupported int = 11
ErrAlreadyUsed int = 12
ErrForbidden int = 13
ErrCountryNotSupported int = 14 // By LNURL Service Provider
StatusContacting int = 100
StatusInvoiceCreated int = 101
StatusReceiving int = 102
StatusContacting int = 100
StatusInvoiceCreated int = 101
StatusReceiving int = 102
)
const zebedeeHostConst = "api.zebedee.io"
// This should definitely be a const but to simplify testing we treat it as a "conf var"
var zebedeeHost = zebedeeHostConst
type Event struct {
Code int
Message string
@@ -88,9 +93,8 @@ type Event struct {
}
type EventMetadata struct {
Host string
Invoice string
RequestId string
Host string
Invoice string
}
var httpClient = http.Client{Timeout: 15 * time.Second}
@@ -132,11 +136,6 @@ func Withdraw(qr string, createInvoiceFunc CreateInvoiceFunction, allowUnsafe bo
// update contacting
notifier.Status(StatusContacting)
// add request id to enhance error reports and troubleshooting with LNURL service providers
requestId := uuid.New().String()
qrUrl.Query().Add("requestId", requestId)
notifier.SetRequestId(requestId)
// start withdraw with service
resp, err := httpClient.Get(qrUrl.String())
if err != nil {
@@ -187,11 +186,11 @@ func Withdraw(qr string, createInvoiceFunc CreateInvoiceFunction, allowUnsafe bo
notifier.Status(StatusInvoiceCreated)
// Mutate the query params so we keep those the original URL had
query := callbackURL.Query()
query.Add("k1", wr.K1)
query.Add("pr", invoice)
callbackQuery := callbackURL.Query()
callbackQuery.Add("k1", wr.K1)
callbackQuery.Add("pr", invoice)
callbackURL.RawQuery = callbackQuery.Encode()
callbackURL.RawQuery = query.Encode()
// Confirm withdraw with service
// Use an httpClient with a higher timeout for reliability with slow LNURL services
withdrawClient := http.Client{Timeout: 3 * time.Minute}
@@ -230,7 +229,11 @@ func validateHttpResponse(resp *http.Response) (int, string) {
if bytesBody, err := ioutil.ReadAll(resp.Body); err == nil {
code := ErrInvalidResponse
if resp.StatusCode == 403 {
code = ErrForbidden
if strings.Contains(resp.Request.URL.Host, zebedeeHost) {
code = ErrCountryNotSupported
} else {
code = ErrForbidden
}
}
return code, fmt.Sprintf("unexpected status code in response: %v, body: %s", resp.StatusCode, string(bytesBody))
@@ -273,8 +276,8 @@ func (fr *Response) Validate() (int, string) {
// reasons maps from parts of responses to the error code. The string can be in
// any part of the response, and has to be lowercased to simplify matching.
// Try to also document the original error string above the pattern.
var reasons = map[string]int {
"route": ErrNoRoute,
var reasons = map[string]int{
"route": ErrNoRoute,
"expired": ErrRequestExpired,
// This Withdrawal Request is already being processed by another wallet. (zebedee)
"already being processed": ErrAlreadyUsed,
@@ -304,23 +307,41 @@ func mapReasonToErrorCode(reason string) int {
func decode(qr string) (*url.URL, error) {
// handle fallback scheme
var toParse string
if strings.HasPrefix(qr, "http://") || strings.HasPrefix(qr, "https://") {
u, err := url.Parse(qr)
if err != nil {
return nil, err
}
qr = u.Query().Get("lightning")
toParse = u.Query().Get("lightning")
} else {
// remove lightning prefix
if strings.HasPrefix(strings.ToLower(qr), "lightning:") {
qr = qr[len("lightning:"):]
// Remove muun: prefix, including the :// version for iOS
qr = strings.Replace(qr, "muun://", "", 1)
qr = strings.Replace(qr, "muun:", "", 1)
// Use a consistent prefix
if !strings.HasPrefix(strings.ToLower(qr), "lightning:") {
qr = "lightning:" + qr
}
uri, err := url.Parse(qr)
if err != nil {
return nil, err
}
if len(uri.Opaque) > 0 {
// This catches lightning:LNURL
toParse = uri.Opaque
} else {
// And this catches lightning://LNURL which is needed for iOS
toParse = uri.Host
}
}
u, err := lnurl.LNURLDecode(qr)
u, err := lnurl.LNURLDecode(toParse)
if err != nil {
return nil, err
}
return url.Parse(string(u))
return url.Parse(u)
}
// We allow "withdraw" as a valid LNURL withdraw tag because, even though not in spec, there are
@@ -338,10 +359,6 @@ func (n *notifier) SetHost(host string) {
n.metadata.Host = host
}
func (n *notifier) SetRequestId(requestId string) {
n.metadata.RequestId = requestId
}
func (n *notifier) SetInvoice(invoice string) {
n.metadata.Invoice = invoice
}