forked from believethehype/nostdress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvoice.go
153 lines (130 loc) · 3.42 KB
/
invoice.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"bytes"
"fmt"
"image"
"image/jpeg"
"io/ioutil"
"net/http"
"strconv"
"time"
"github.com/nfnt/resize"
"github.com/fiatjaf/go-lnurl"
"github.com/fiatjaf/makeinvoice"
)
func metaData(params *Params) lnurl.Metadata {
metadata := lnurl.Metadata{
Description: fmt.Sprintf("Pay to %s@%s", params.Name, params.Domain),
LightningAddress: fmt.Sprintf("%s@%s", params.Name, params.Domain),
}
if params.Npub != "" && s.GetNostrProfile {
NostrProfile, err := GetNostrProfileMetaData(params.Npub)
if err == nil {
addImageToMetaData(&metadata, NostrProfile.Picture)
}
}
return metadata
}
// addImageMetaData add images an image to the LNURL metadata
func addImageToMetaData(metadata *lnurl.Metadata, imageurl string) {
picture, err := DownloadProfilePicture(imageurl)
if err != nil {
log.Debug().Str("Downloading profile picture", err.Error()).Msg("Error")
return
}
metadata.Image.Ext = "jpeg" //filepath.Ext(imageurl)
metadata.Image.DataURI = imageurl
metadata.Image.Bytes = picture
}
func DownloadProfilePicture(url string) ([]byte, error) {
res, err := http.Get(url)
if err != nil {
log.Debug().Str("http.Get ->", err.Error()).Msg("Error")
return nil, err
}
data, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Debug().Str("ioutil.ReadAll ->", err.Error()).Msg("Error")
return nil, err
}
res.Body.Close()
img, _, err := image.Decode(bytes.NewReader(data))
if err != nil {
return nil, err
}
// resize image
img = resize.Thumbnail(160, 160, img, resize.Lanczos3)
buf := new(bytes.Buffer)
_ = jpeg.Encode(buf, img, nil)
//ioutil.WriteFile("test.jpg", buf.Bytes(), 0666)
return buf.Bytes(), nil
}
func makeInvoice(
params *Params,
msat int,
pin *string,
zapEventSerializedStr string,
comment string,
) (bolt11 string, err error) {
// prepare params
var backend makeinvoice.BackendParams
switch params.Kind {
case "sparko":
backend = makeinvoice.SparkoParams{
Host: params.Host,
Key: params.Key,
}
case "lnd":
backend = makeinvoice.LNDParams{
Host: params.Host,
Macaroon: params.Key,
}
case "lnbits":
backend = makeinvoice.LNBitsParams{
Host: params.Host,
Key: params.Key,
}
case "lnpay":
backend = makeinvoice.LNPayParams{
PublicAccessKey: params.Pak,
WalletInvoiceKey: params.Waki,
}
case "eclair":
backend = makeinvoice.EclairParams{
Host: params.Host,
Password: "",
}
case "commando":
backend = makeinvoice.CommandoParams{
Host: params.Host,
NodeId: params.NodeId,
Rune: params.Rune,
}
}
mip := makeinvoice.Params{
Msatoshi: int64(msat),
Backend: backend,
Label: params.Domain + "/" + strconv.FormatInt(time.Now().Unix(), 16),
}
if pin != nil {
// use this as the description for new accounts
mip.UseDescriptionHash = false
mip.Description = fmt.Sprintf("%s's PIN for '%s@%s' lightning address: %s", params.Domain, params.Name, params.Domain, *pin)
} else {
//use zapEventSerializedStr if nip57,
mip.UseDescriptionHash = true
if zapEventSerializedStr != "" {
mip.Description = zapEventSerializedStr
} else {
//else build hash descriptionhash from params
mip.Description = metaData(params).Encode()
}
}
// actually generate the invoice
bolt11, err = makeinvoice.MakeInvoice(mip)
log.Debug().Int("msatoshi", msat).
Interface("backend", backend).
Str("bolt11", bolt11).Err(err).Str("Description", mip.Description).
Msg("invoice generation")
return bolt11, err
}