-
Notifications
You must be signed in to change notification settings - Fork 3
/
lsat.go
306 lines (290 loc) · 8.77 KB
/
lsat.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package lightning_go
import (
"bytes"
"context"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
lnrpc2 "github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
"gopkg.in/macaroon.v2"
"net/http"
"strings"
)
type LSAT struct {
ID TokenID
Preimage []byte
PayHash []byte
Invoice string
Value int64
Macaroon macaroon.Macaroon
}
func (ln *LightningClient) GenerateHodlLSAT(ip string) (LSAT, error) {
preimage, err := GenerateRandomBytes(32)
if err != nil {
return LSAT{}, err
}
hash := sha256.Sum256(preimage)
invoice, closeInvFunc, err := ln.GetInvoiceClient()
if err != nil {
return LSAT{}, err
}
defer closeInvFunc()
addInvoiceReq, err := invoice.AddHoldInvoice(context.Background(), &invoicesrpc.AddHoldInvoiceRequest{
Memo: fmt.Sprintf("HODL invoice payment from Chainpoint Core %s", ln.ServerHostPort),
Hash: hash[:],
Value: ln.HashPrice,
ValueMsat: 0,
DescriptionHash: nil,
Expiry: 0,
FallbackAddr: "",
CltvExpiry: 0,
RouteHints: nil,
Private: false,
})
if err != nil {
return LSAT{}, err
}
tID, err := MakeIDFromString(hex.EncodeToString(preimage))
if err != nil {
return LSAT{}, err
}
identifier := Identifier{
Version: 0,
PaymentHash: hash,
TokenID: tID,
}
secBytes, err := hex.DecodeString(ln.SessionSecret)
if err != nil {
return LSAT{}, err
}
var buf bytes.Buffer
EncodeIdentifier(&buf, &identifier)
mac, err := macaroon.New(secBytes, buf.Bytes(), "lsat", macaroon.LatestVersion)
if err != nil {
return LSAT{}, err
}
return LSAT{
ID: tID,
Preimage: preimage,
PayHash: hash[:],
Invoice: addInvoiceReq.PaymentRequest,
Value: ln.HashPrice,
Macaroon: *mac,
}, nil
}
// RespondLSAT : Use this in an http handler to issue an LSAT challenge
// Returns true to signal a required return from an http handler, false to fall through
func (ln *LightningClient) RespondLSAT(w http.ResponseWriter, r *http.Request) bool {
authorization := r.Header.Get("Authorization")
if len(authorization) == 0 {
lsat, err := ln.GenerateHodlLSAT(r.RemoteAddr)
if err != nil {
errorMessage := map[string]interface{}{"error": "Could not generate LSAT"}
respondJSON(w, http.StatusInternalServerError, errorMessage)
return true
}
challenge := lsat.ToChallenge()
w.Header().Set("www-authenticate", challenge)
errorMessage := map[string]interface{}{"error": "message: payment required"}
respondJSON(w, http.StatusPaymentRequired, errorMessage)
return true
} else {
lsat, err := FromHeader(&r.Header)
if err != nil {
errorMessage := map[string]interface{}{"error": "Invalid LSAT provided in Authorization header"}
respondJSON(w, http.StatusInternalServerError, errorMessage)
return true
}
invoice, err := ln.LookupInvoice(lsat.PayHash)
if err != nil {
errorMessage := map[string]interface{}{"error": fmt.Sprintf("No matching invoice found for payhash %s", lsat.PayHash)}
respondJSON(w, http.StatusNotFound, errorMessage)
return true
}
switch invoice.State {
case lnrpc2.Invoice_SETTLED:
errorMessage := map[string]interface{}{"error": "Unauthorized: Invoice has already been settled. Try again with a different LSAT"}
respondJSON(w, http.StatusUnauthorized, errorMessage)
return true
case lnrpc2.Invoice_OPEN:
lsat.Invoice = invoice.PaymentRequest
challenge := lsat.ToChallenge()
w.Header().Set("www-authenticate", challenge)
errorMessage := map[string]interface{}{"message": "Payment Required"}
respondJSON(w, http.StatusPaymentRequired, errorMessage)
return true
case lnrpc2.Invoice_CANCELED:
errorMessage := map[string]interface{}{"error": "Unauthorized: Invoice has been cancelled. Try again with a different LSAT"}
respondJSON(w, http.StatusUnauthorized, errorMessage)
return true
case lnrpc2.Invoice_ACCEPTED:
break
default:
errorMessage := map[string]interface{}{"error": "Unauthorized: Invoice in an unknown state"}
respondJSON(w, http.StatusUnauthorized, errorMessage)
return true
}
token := lsat.ToToken()
w.Header().Set("authorization", token)
return false // don't exit top level handler
}
}
func (lsat *LSAT) ToChallenge() string {
mac, _ := lsat.Macaroon.MarshalBinary()
macStr := base64.StdEncoding.EncodeToString(mac)
challenge := fmt.Sprintf("LSAT macaroon=\"%s\", invoice=\"%s\"", macStr, lsat.Invoice)
return challenge
}
func (lsat *LSAT) ToToken() string {
mac, _ := lsat.Macaroon.MarshalBinary()
macStr := base64.StdEncoding.EncodeToString(mac)
token := fmt.Sprintf("LSAT %s:%s", macStr, base64.StdEncoding.EncodeToString(lsat.Preimage))
return token
}
func FromHeader(header *http.Header) (LSAT, error) {
var authHeader string
HeaderMacaroonMD := "Grpc-Metadata-Macaroon"
HeaderMacaroon := "Macaroon"
switch {
// Header field 1 contains the macaroon and the preimage as distinct
// values separated by a colon.
case header.Get("Authorization") != "":
// Parse the content of the header field and check that it is in
// the correct format.
var macBase64 string
var preimageHex string
authHeader = header.Get("Authorization")
lsatAuth := strings.Split(authHeader, " ")
if len(lsatAuth) != 2 {
return LSAT{}, fmt.Errorf("invalid "+
"auth header format: %s", authHeader)
}
content := strings.Split(lsatAuth[1], ":")
if len(content) == 2 && content[0] != "" {
macBase64 = content[0]
}
if len(content) == 2 && content[1] != "" {
preimageHex = content[1]
}
macBytes, err := base64.StdEncoding.DecodeString(macBase64)
if err != nil {
return LSAT{}, fmt.Errorf("base64 "+
"decode of macaroon failed: %v\nrequest: %s", err, authHeader)
}
mac := &macaroon.Macaroon{}
err = mac.UnmarshalBinary(macBytes)
buf := bytes.NewReader(mac.Id())
id, err := DecodeIdentifier(buf)
if err != nil {
return LSAT{}, fmt.Errorf("unable to "+
"unmarshal macaroon: %v", err)
}
if preimageHex == "" {
return LSAT{
ID: id.TokenID,
Preimage: nil,
PayHash: id.PaymentHash[:],
Invoice: "",
Value: 2,
Macaroon: *mac,
}, nil
}
preimage, err := hex.DecodeString(preimageHex)
if err != nil {
return LSAT{}, fmt.Errorf("hex "+
"decode of preimage failed: %v", err)
}
tID, err := MakeIDFromString(preimageHex)
if err != nil {
return LSAT{}, fmt.Errorf("hex "+
"decode of preimage into TokenID failed: %v", err)
}
hash := sha256.Sum256(preimage)
// All done, we don't need to extract anything from the
// macaroon since the preimage was presented separately.
return LSAT{
ID: tID,
Preimage: preimage[:],
PayHash: hash[:],
Invoice: "",
Value: 2,
Macaroon: *mac,
}, nil
// Header field 2: Contains only the macaroon.
case header.Get(HeaderMacaroonMD) != "":
authHeader = header.Get(HeaderMacaroonMD)
// Header field 3: Contains only the macaroon.
case header.Get(HeaderMacaroon) != "":
authHeader = header.Get(HeaderMacaroon)
default:
return LSAT{}, fmt.Errorf("no auth header " +
"provided")
}
// For case 2 and 3, we need to actually unmarshal the macaroon to
// extract the preimage.
macBytes, err := hex.DecodeString(authHeader)
if err != nil {
return LSAT{}, fmt.Errorf("hex decode of "+
"macaroon failed: %v", err)
}
mac := &macaroon.Macaroon{}
err = mac.UnmarshalBinary(macBytes)
if err != nil {
return LSAT{}, fmt.Errorf("unable to "+
"unmarshal macaroon: %v", err)
}
preimageHex, ok := HasCaveat(mac, "preimage")
if !ok {
return LSAT{}, errors.New("preimage caveat " +
"not found")
}
preimage, err := hex.DecodeString(preimageHex)
if err != nil {
return LSAT{}, fmt.Errorf("hex decode of "+
"preimage failed: %v", err)
}
tID, err := MakeIDFromString(preimageHex)
if err != nil {
return LSAT{}, fmt.Errorf("hex "+
"decode of preimage into TokenID failed: %v", err)
}
hash := sha256.Sum256(preimage)
return LSAT{
ID: tID,
Preimage: preimage[:],
PayHash: hash[:],
Invoice: "",
Value: 2,
Macaroon: *mac,
}, nil
}
// GenerateRandomBytes returns securely generated random bytes.
// It will return an error if the system's secure random
// number generator fails to function correctly, in which
// case the caller should not continue.
func GenerateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
// Note that err == nil only if we read len(b) bytes.
if err != nil {
return nil, err
}
return b, nil
}
// respondJSON makes the response with payload as json format
func respondJSON(w http.ResponseWriter, status int, payload interface{}) {
response, err := json.Marshal(payload)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
w.Write([]byte(response))
}