2021-11-12 19:06:13 -03:00
|
|
|
package libwallet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
"github.com/muun/libwallet/lnurl"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LNURLEvent struct {
|
|
|
|
Code int
|
|
|
|
Message string
|
|
|
|
Metadata *LNURLEventMetadata
|
|
|
|
}
|
|
|
|
|
|
|
|
type LNURLEventMetadata struct {
|
|
|
|
Host string
|
|
|
|
Invoice string
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2022-10-04 14:55:21 -03:00
|
|
|
LNURLErrDecode = lnurl.ErrDecode
|
|
|
|
LNURLErrUnsafeURL = lnurl.ErrUnsafeURL
|
|
|
|
LNURLErrUnreachable = lnurl.ErrUnreachable
|
|
|
|
LNURLErrInvalidResponse = lnurl.ErrInvalidResponse
|
|
|
|
LNURLErrResponse = lnurl.ErrResponse
|
|
|
|
LNURLErrUnknown = lnurl.ErrUnknown
|
|
|
|
LNURLErrWrongTag = lnurl.ErrWrongTag
|
|
|
|
LNURLErrNoAvailableBalance = lnurl.ErrNoAvailableBalance
|
|
|
|
LNURLErrRequestExpired = lnurl.ErrRequestExpired
|
|
|
|
LNURLErrNoRoute = lnurl.ErrNoRoute
|
|
|
|
LNURLErrTorNotSupported = lnurl.ErrTorNotSupported
|
|
|
|
LNURLErrAlreadyUsed = lnurl.ErrAlreadyUsed
|
|
|
|
LNURLErrForbidden = lnurl.ErrForbidden
|
|
|
|
LNURLErrCountryNotSupported = lnurl.ErrCountryNotSupported
|
|
|
|
LNURLStatusContacting = lnurl.StatusContacting
|
|
|
|
LNURLStatusInvoiceCreated = lnurl.StatusInvoiceCreated
|
|
|
|
LNURLStatusReceiving = lnurl.StatusReceiving
|
2021-11-12 19:06:13 -03:00
|
|
|
)
|
|
|
|
|
|
|
|
type LNURLListener interface {
|
|
|
|
OnUpdate(e *LNURLEvent)
|
|
|
|
OnError(e *LNURLEvent)
|
|
|
|
}
|
|
|
|
|
|
|
|
func LNURLValidate(qr string) bool {
|
|
|
|
return lnurl.Validate(qr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Withdraw will parse an LNURL withdraw QR and begin a withdraw process.
|
|
|
|
// Caller must wait for the actual payment after this function has notified success.
|
2022-10-04 14:55:21 -03:00
|
|
|
func LNURLWithdraw(invoiceBuilder *InvoiceBuilder, qr string, listener LNURLListener) {
|
2021-11-12 19:06:13 -03:00
|
|
|
createInvoiceFunc := func(amt lnwire.MilliSatoshi, desc string, host string) (string, error) {
|
2022-10-04 14:55:21 -03:00
|
|
|
metadata := &OperationMetadata{
|
|
|
|
LnurlSender: host,
|
2021-11-12 19:06:13 -03:00
|
|
|
}
|
2022-10-04 14:55:21 -03:00
|
|
|
|
|
|
|
return invoiceBuilder.AmountMSat(int64(amt)).
|
|
|
|
Description(desc).
|
|
|
|
Metadata(metadata).
|
|
|
|
Build()
|
2021-11-12 19:06:13 -03:00
|
|
|
}
|
|
|
|
|
2022-10-04 14:55:21 -03:00
|
|
|
allowUnsafe := invoiceBuilder.net != Mainnet()
|
2021-11-12 19:06:13 -03:00
|
|
|
|
|
|
|
go lnurl.Withdraw(qr, createInvoiceFunc, allowUnsafe, func(e *lnurl.Event) {
|
|
|
|
event := &LNURLEvent{
|
|
|
|
Code: e.Code,
|
|
|
|
Message: e.Message,
|
|
|
|
Metadata: &LNURLEventMetadata{
|
|
|
|
Host: e.Metadata.Host,
|
|
|
|
Invoice: e.Metadata.Invoice,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if event.Code < 100 {
|
|
|
|
listener.OnError(event)
|
|
|
|
} else {
|
|
|
|
listener.OnUpdate(event)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|