-
Notifications
You must be signed in to change notification settings - Fork 9
/
invoice.go
215 lines (188 loc) · 5.46 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
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
package lightning
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"strings"
"time"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
"github.com/btcsuite/btcd/chaincfg"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/zpay32"
)
const DESCRIPTION_HASH_DESCRIPTION_PREFIX = "with description_hash: "
func (ln *Client) InvoiceWithDescriptionHash(
label string,
msatoshi int64,
descriptionHash []byte,
ppreimage *[]byte,
pexpiry *time.Duration,
) (bolt11 string, err error) {
var preimage []byte
if ppreimage != nil {
preimage = *ppreimage
} else {
preimage = make([]byte, 32)
_, err = rand.Read(preimage)
if err != nil {
return
}
}
description_hash := hex.EncodeToString(descriptionHash)
// create an invoice at the node so it expects for a payment at this hash
// we won't expose this, but it will still get paid
params := map[string]interface{}{
"label": label,
"msatoshi": msatoshi,
"preimage": hex.EncodeToString(preimage),
"description": DESCRIPTION_HASH_DESCRIPTION_PREFIX + description_hash,
}
if pexpiry != nil {
params["expiry"] = *pexpiry / time.Second
}
inv, err := ln.Call("invoice", params)
if err != nil {
return
}
// now create another invoice, this time with the desired description_hash instead
bolt11 = inv.Get("bolt11").String()
return ln.TranslateInvoiceWithDescriptionHash(bolt11)
}
func (ln *Client) TranslateInvoiceWithDescriptionHash(bolt11 string) (string, error) {
firstNumber := strings.IndexAny(bolt11, "1234567890")
chainPrefix := bolt11[2:firstNumber]
chain := &chaincfg.Params{
Bech32HRPSegwit: chainPrefix,
}
invoice, err := zpay32.Decode(bolt11, chain)
if err != nil {
return "", fmt.Errorf("failed to decode bolt11: %w", err)
}
// grab description_hash from description
if invoice.Description == nil {
return "", errors.New("given bolt11 doesn't have a text description")
}
descriptionHashHex := (*invoice.Description)[len(DESCRIPTION_HASH_DESCRIPTION_PREFIX):]
descriptionHash, err := hex.DecodeString(descriptionHashHex)
if err != nil {
return "", fmt.Errorf("given bolt11 doesn't have a valid description_hash in its text description: %w", err)
}
dhash32 := as32(descriptionHash)
invoice.Description = nil
invoice.Destination = nil
invoice.DescriptionHash = &dhash32
// finally sign this new invoice
privKey, err := ln.GetPrivateKey()
if err != nil {
return "", err
}
translatedBolt11, err := invoice.Encode(zpay32.MessageSigner{
SignCompact: func(hash []byte) ([]byte, error) {
return ecdsa.SignCompact(privKey, hash, true), nil
},
})
return translatedBolt11, err
}
func (ln *Client) InvoiceWithShadowRoute(
msatoshi int64,
descriptionOrHash interface{}, /* can be either a string (description) or a []byte (description_hash) */
ppreimage *[]byte,
pprivateKey **btcec.PrivateKey,
pexpiry *time.Duration,
baseFee uint32,
ppmFee uint32,
cltvExpiryDelta uint16,
channelId uint64,
) (bolt11 string, paymentHash string, err error) {
// create a random preimage if one is not given
var preimage []byte
if ppreimage != nil {
preimage = *ppreimage
} else {
preimage = make([]byte, 32)
_, err = rand.Read(preimage)
if err != nil {
return
}
}
hash := sha256.Sum256(preimage)
paymentHash = hex.EncodeToString(hash[:])
// params for invoice creation
params := make([]func(*zpay32.Invoice), 5, 6)
// payment secret can be anything
params[0] = zpay32.PaymentAddr([32]byte{
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,
})
// set expiry to 7 days if not given
if pexpiry != nil {
params[1] = zpay32.Expiry(*pexpiry)
} else {
params[1] = zpay32.Expiry(time.Duration(time.Hour * 24 * 7))
}
// set the description or description_hash
switch v := descriptionOrHash.(type) {
case string:
// it's a plain description (`d`)
params[2] = zpay32.Description(v)
case []byte:
// it's a description_hash (`h`)
descriptionHash := as32(v)
params[2] = zpay32.DescriptionHash(descriptionHash)
}
// set amount if not zero
if msatoshi > 0 {
params = append(params, zpay32.Amount(lnwire.MilliSatoshi(msatoshi)))
}
// set the shadow route hint with the public key of our real node
info, _ := ln.Call("getinfo")
nodeIdBytes, _ := hex.DecodeString(info.Get("id").String())
pubKey, err := btcec.ParsePubKey(nodeIdBytes)
if err != nil {
return
}
params[3] = zpay32.RouteHint([]zpay32.HopHint{
{
NodeID: pubKey,
ChannelID: channelId,
FeeBaseMSat: baseFee,
FeeProportionalMillionths: ppmFee,
CLTVExpiryDelta: cltvExpiryDelta,
},
})
params[4] = zpay32.Features(&lnwire.FeatureVector{
RawFeatureVector: lnwire.NewRawFeatureVector(
lnwire.PaymentAddrOptional,
lnwire.TLVOnionPayloadOptional,
),
})
// create the invoice
invoice, err := zpay32.NewInvoice(&chaincfg.MainNetParams,
hash, time.Now(), params...)
if err != nil {
return
}
// create a private key if one is not given, we need it to sign
var privateKey *btcec.PrivateKey
if pprivateKey != nil {
privateKey = *pprivateKey
} else {
randomBytes := make([]byte, 32)
_, err = rand.Read(randomBytes)
if err != nil {
return
}
privateKey, _ = btcec.PrivKeyFromBytes(randomBytes)
}
bolt11, err = invoice.Encode(zpay32.MessageSigner{
SignCompact: func(hash []byte) ([]byte, error) {
return ecdsa.SignCompact(privateKey, hash, true), nil
},
})
return
}