-
Notifications
You must be signed in to change notification settings - Fork 2
/
lnurl.go
79 lines (69 loc) · 2.19 KB
/
lnurl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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 (
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
)
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.
func LNURLWithdraw(invoiceBuilder *InvoiceBuilder, qr string, listener LNURLListener) {
createInvoiceFunc := func(amt lnwire.MilliSatoshi, desc string, host string) (string, error) {
metadata := &OperationMetadata{
LnurlSender: host,
}
return invoiceBuilder.AmountMSat(int64(amt)).
Description(desc).
Metadata(metadata).
Build()
}
allowUnsafe := invoiceBuilder.net != Mainnet()
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)
}
})
}