forked from fiatjaf/etleneum
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.go
199 lines (176 loc) · 5 KB
/
helpers.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"bytes"
"context"
_ "embed"
"encoding/base64"
"encoding/json"
"fmt"
"image/png"
"net/http"
"regexp"
"strconv"
"time"
"github.com/fiatjaf/hashbow"
"github.com/fogleman/gg"
"github.com/golang/freetype/truetype"
"github.com/itchyny/gojq"
"github.com/yudai/gojsondiff"
)
//go:embed static/lnurlpayicon.png
var lnurlpayicon []byte
//go:embed static/Inconsolata-Bold.ttf
var inconsolatabold []byte
var wordMatcher *regexp.Regexp = regexp.MustCompile(`\b\w+\b`)
type Result struct {
Ok bool `json:"ok"`
Value interface{} `json:"value"`
Error string `json:"error,omitempty"`
}
func jsonError(w http.ResponseWriter, message string, code int) {
w.WriteHeader(code)
json.NewEncoder(w).Encode(Result{
Ok: false,
Error: message,
})
}
func diffDeltaOneliner(prefix string, idelta gojsondiff.Delta) (lines []string) {
key := prefix
if key != "" {
key += "."
}
switch pdelta := idelta.(type) {
case gojsondiff.PreDelta:
switch delta := pdelta.(type) {
case *gojsondiff.Moved:
key = key + delta.PrePosition().String()
lines = append(lines, fmt.Sprintf("- %s", key))
case *gojsondiff.Deleted:
key = key + delta.PrePosition().String()
lines = append(lines, fmt.Sprintf("- %s", key[:len(key)]))
}
}
switch pdelta := idelta.(type) {
case gojsondiff.PostDelta:
switch delta := pdelta.(type) {
case *gojsondiff.TextDiff:
key = key + delta.PostPosition().String()
lines = append(lines, fmt.Sprintf("= %s %v", key, delta.NewValue))
case *gojsondiff.Modified:
key = key + delta.PostPosition().String()
value, _ := json.Marshal(delta.NewValue)
lines = append(lines, fmt.Sprintf("= %s %s", key, value))
case *gojsondiff.Added:
key = key + delta.PostPosition().String()
value, _ := json.Marshal(delta.Value)
lines = append(lines, fmt.Sprintf("+ %s %s", key, value))
case *gojsondiff.Object:
key = key + delta.PostPosition().String()
for _, nextdelta := range delta.Deltas {
lines = append(lines, diffDeltaOneliner(key, nextdelta)...)
}
case *gojsondiff.Array:
key = key + delta.PostPosition().String()
for _, nextdelta := range delta.Deltas {
lines = append(lines, diffDeltaOneliner(key, nextdelta)...)
}
case *gojsondiff.Moved:
key = key + delta.PostPosition().String()
value, _ := json.Marshal(delta.Value)
lines = append(lines, fmt.Sprintf("+ %s %s", key, value))
if delta.Delta != nil {
if d, ok := delta.Delta.(gojsondiff.Delta); ok {
lines = append(lines, diffDeltaOneliner(key, d)...)
}
}
}
}
return
}
func runJQ(
ctx context.Context,
input []byte,
filter string,
) (result interface{}, err error) {
ctx, cancel := context.WithTimeout(ctx, time.Second*2)
defer cancel()
query, err := gojq.Parse(filter)
if err != nil {
return
}
var object map[string]interface{}
err = json.Unmarshal(input, &object)
if err != nil {
return nil, err
}
iter := query.RunWithContext(ctx, object)
v, ok := iter.Next()
if !ok {
return nil, nil
}
if err, ok := v.(error); ok {
return nil, err
}
return v, nil
}
func generateLnurlImage(contractId string, method string) (b64 string, err error) {
// load existing image
img, err := png.Decode(bytes.NewBuffer(lnurlpayicon))
if err != nil {
return
}
// load font to write
f, err := truetype.Parse(inconsolatabold)
if err != nil {
return
}
face := truetype.NewFace(f, &truetype.Options{Size: 20})
// create new image with gg
bounds := img.Bounds()
dc := gg.NewContext(bounds.Max.X, bounds.Max.Y)
dc.DrawImage(img, 0, 0)
// apply filters
// contract filter
hexcolor := hashbow.Hashbow(contractId)
r, _ := strconv.ParseInt(hexcolor[1:3], 16, 64)
g, _ := strconv.ParseInt(hexcolor[3:5], 16, 64)
b, _ := strconv.ParseInt(hexcolor[5:7], 16, 64)
dc.SetRGBA255(int(r), int(g), int(b), 120)
dc.MoveTo(0, float64(bounds.Max.Y)*0.63)
dc.CubicTo(
float64(bounds.Max.X)*0.33, float64(bounds.Max.Y)*0.9,
float64(bounds.Max.X)*0.80, float64(bounds.Max.Y)*0.25,
float64(bounds.Max.X), float64(bounds.Max.Y)*0.3,
)
dc.LineTo(float64(bounds.Max.X), 0)
dc.LineTo(0, 0)
dc.Fill()
// method filter
hexcolor = hashbow.Hashbow(method)
r, _ = strconv.ParseInt(hexcolor[1:3], 16, 64)
g, _ = strconv.ParseInt(hexcolor[3:5], 16, 64)
b, _ = strconv.ParseInt(hexcolor[5:7], 16, 64)
dc.SetRGBA255(int(r), int(g), int(b), 120)
dc.MoveTo(0, float64(bounds.Max.Y)*0.63)
dc.CubicTo(
float64(bounds.Max.X)*0.33, float64(bounds.Max.Y)*0.9,
float64(bounds.Max.X)*0.80, float64(bounds.Max.Y)*0.25,
float64(bounds.Max.X), float64(bounds.Max.Y)*0.3,
)
dc.LineTo(float64(bounds.Max.X), float64(bounds.Max.Y))
dc.LineTo(0, float64(bounds.Max.Y))
dc.Fill()
// write contract id and method
dc.SetFontFace(face)
dc.SetRGB255(255, 255, 255)
w, _ := dc.MeasureString(contractId)
dc.DrawString(contractId, float64(bounds.Max.X)-8-w, 19)
dc.DrawString(method+"()", 8, float64(bounds.Max.Y-9))
// encode to base64 png and return
out := bytes.Buffer{}
err = dc.EncodePNG(&out)
if err != nil {
return
}
return base64.StdEncoding.EncodeToString(out.Bytes()), nil
}