-
Notifications
You must be signed in to change notification settings - Fork 2
/
incoming_swap.go
470 lines (403 loc) · 12.9 KB
/
incoming_swap.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package libwallet
import (
"bytes"
"crypto/sha256"
"errors"
"fmt"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/muun/libwallet/btcsuitew/txscriptw"
"github.com/muun/libwallet/hdpath"
"github.com/muun/libwallet/sphinx"
"github.com/muun/libwallet/walletdb"
)
type IncomingSwap struct {
Htlc *IncomingSwapHtlc
SphinxPacket []byte
PaymentHash []byte
PaymentAmountSat int64
CollectSat int64
}
type IncomingSwapHtlc struct {
HtlcTx []byte
ExpirationHeight int64
SwapServerPublicKey []byte
}
type IncomingSwapFulfillmentData struct {
FulfillmentTx []byte
MuunSignature []byte
OutputVersion int // unused
OutputPath string // unused
MerkleTree []byte // unused
HtlcBlock []byte // unused
BlockHeight int64 // unused
ConfirmationTarget int64 // to validate fee rate, unused for now
}
type IncomingSwapFulfillmentResult struct {
FulfillmentTx []byte
Preimage []byte
}
func (s *IncomingSwap) getInvoice() (*walletdb.Invoice, error) {
db, err := openDB()
if err != nil {
return nil, err
}
defer db.Close()
return db.FindByPaymentHash(s.PaymentHash)
}
// VerifyFulfillable checks that an incoming swap is fulfillable.
func (s *IncomingSwap) VerifyFulfillable(userKey *HDPrivateKey, net *Network) error {
paymentHash := s.PaymentHash
if len(paymentHash) != 32 {
return fmt.Errorf("VerifyFulfillable: received invalid hash len %v", len(paymentHash))
}
// Lookup invoice data matching this HTLC using the payment hash
invoice, err := s.getInvoice()
if err != nil {
return fmt.Errorf("VerifyFulfillable: could not find invoice data for payment hash: %w", err)
}
parentPath, err := hdpath.Parse(invoice.KeyPath)
if err != nil {
return fmt.Errorf("VerifyFulfillable: invoice key path is not valid: %v", invoice.KeyPath)
}
identityKeyPath := parentPath.Child(identityKeyChildIndex)
nodeHDKey, err := userKey.DeriveTo(identityKeyPath.String())
if err != nil {
return fmt.Errorf("VerifyFulfillable: failed to derive key: %w", err)
}
nodeKey, err := nodeHDKey.key.ECPrivKey()
if err != nil {
return fmt.Errorf("VerifyFulfillable: failed to get priv key: %w", err)
}
// implementation is allowed to send a few extra sats
if invoice.AmountSat != 0 && invoice.AmountSat > s.PaymentAmountSat {
return fmt.Errorf("VerifyFulfillable: payment amount (%v) does not match invoice amount (%v)",
s.PaymentAmountSat, invoice.AmountSat)
}
if len(s.SphinxPacket) == 0 {
return nil
}
err = sphinx.Validate(
s.SphinxPacket,
paymentHash,
invoice.PaymentSecret,
nodeKey,
0, // This is used internally by the sphinx decoder but it's not needed
lnwire.MilliSatoshi(uint64(s.PaymentAmountSat)*1000),
net.network,
)
if err != nil {
return fmt.Errorf("VerifyFulfillable: invalid sphinx: %w", err)
}
return nil
}
// Fulfill validates and creates a fulfillment tx for the incoming swap.
// It returns the fullfillment tx and the preimage.
func (s *IncomingSwap) Fulfill(
data *IncomingSwapFulfillmentData,
userKey *HDPrivateKey, muunKey *HDPublicKey,
net *Network) (*IncomingSwapFulfillmentResult, error) {
if s.Htlc == nil {
return nil, fmt.Errorf("Fulfill: missing swap htlc data")
}
err := s.VerifyFulfillable(userKey, net)
if err != nil {
return nil, err
}
// Validate the fullfillment tx proposed by Muun.
tx := wire.MsgTx{}
err = tx.DeserializeNoWitness(bytes.NewReader(data.FulfillmentTx))
if err != nil {
return nil, fmt.Errorf("Fulfill: could not deserialize fulfillment tx: %w", err)
}
if len(tx.TxIn) != 1 {
return nil, fmt.Errorf("Fulfill: expected fulfillment tx to have exactly 1 input, found %d", len(tx.TxIn))
}
if len(tx.TxOut) != 1 {
return nil, fmt.Errorf("Fulfill: expected fulfillment tx to have exactly 1 output, found %d", len(tx.TxOut))
}
// Lookup invoice data matching this HTLC using the payment hash
invoice, err := s.getInvoice()
if err != nil {
return nil, fmt.Errorf("Fulfill: could not find invoice data for payment hash: %w", err)
}
// Sign the htlc input (there is only one, at index 0)
coin := coinIncomingSwap{
Network: net.network,
MuunSignature: data.MuunSignature,
Sphinx: s.SphinxPacket,
HtlcTx: s.Htlc.HtlcTx,
PaymentHash256: s.PaymentHash,
SwapServerPublicKey: []byte(s.Htlc.SwapServerPublicKey),
ExpirationHeight: s.Htlc.ExpirationHeight,
VerifyOutputAmount: true,
Collect: btcutil.Amount(s.CollectSat),
}
err = coin.SignInput(0, &tx, userKey, muunKey)
if err != nil {
return nil, err
}
// Serialize and return the signed fulfillment tx
var buf bytes.Buffer
err = tx.Serialize(&buf)
if err != nil {
return nil, fmt.Errorf("Fulfill: could not serialize fulfillment tx: %w", err)
}
return &IncomingSwapFulfillmentResult{
FulfillmentTx: buf.Bytes(),
Preimage: invoice.Preimage,
}, nil
}
// FulfillFullDebt gives the preimage matching a payment hash if we have it
func (s *IncomingSwap) FulfillFullDebt() (*IncomingSwapFulfillmentResult, error) {
// Lookup invoice data matching this HTLC using the payment hash
db, err := openDB()
if err != nil {
return nil, err
}
defer db.Close()
secrets, err := db.FindByPaymentHash(s.PaymentHash)
if err != nil {
return nil, fmt.Errorf("FulfillFullDebt: could not find invoice data for payment hash: %w", err)
}
return &IncomingSwapFulfillmentResult{
FulfillmentTx: nil,
Preimage: secrets.Preimage,
}, nil
}
type coinIncomingSwap struct {
Network *chaincfg.Params
MuunSignature []byte
Sphinx []byte
HtlcTx []byte
PaymentHash256 []byte
SwapServerPublicKey []byte
ExpirationHeight int64
VerifyOutputAmount bool // used only for fulfilling swaps through IncomingSwap
Collect btcutil.Amount
}
func (c *coinIncomingSwap) SignInput(index int, tx *wire.MsgTx, userKey *HDPrivateKey, muunKey *HDPublicKey) error {
// Deserialize the HTLC transaction
htlcTx := wire.MsgTx{}
err := htlcTx.Deserialize(bytes.NewReader(c.HtlcTx))
if err != nil {
return fmt.Errorf("could not deserialize htlc tx: %w", err)
}
// Lookup invoice data matching this HTLC using the payment hash
db, err := openDB()
if err != nil {
return err
}
defer db.Close()
secrets, err := db.FindByPaymentHash(c.PaymentHash256)
if err != nil {
return fmt.Errorf("could not find invoice data for payment hash: %w", err)
}
parentPath, err := hdpath.Parse(secrets.KeyPath)
if err != nil {
return fmt.Errorf("invalid invoice key path: %w", err)
}
// Recreate the HTLC script to verify it matches the transaction. For this
// we must derive the keys used in the HTLC script
htlcKeyPath := parentPath.Child(htlcKeyChildIndex)
// Derive first the private key, which we are going to use for signing later
userPrivateKey, err := userKey.DeriveTo(htlcKeyPath.String())
if err != nil {
return err
}
userPublicKey := userPrivateKey.PublicKey()
muunPublicKey, err := muunKey.DeriveTo(htlcKeyPath.String())
if err != nil {
return err
}
htlcScript, err := c.createHtlcScript(userPublicKey, muunPublicKey)
if err != nil {
return fmt.Errorf("could not create htlc script: %w", err)
}
// Try to find the script we just built inside the HTLC output scripts
htlcOutputIndex, err := c.findHtlcOutputIndex(&htlcTx, htlcScript)
if err != nil {
return err
}
// Next, we must validate the sphinx data. We derive the client identity
// key used by this invoice with the key path stored in the db.
identityKeyPath := parentPath.Child(identityKeyChildIndex)
nodeHDKey, err := userKey.DeriveTo(identityKeyPath.String())
if err != nil {
return err
}
nodeKey, err := nodeHDKey.key.ECPrivKey()
if err != nil {
return err
}
txInput := tx.TxIn[index]
if txInput.PreviousOutPoint.Hash != htlcTx.TxHash() {
return fmt.Errorf("expected fulfillment tx input to point to htlc tx")
}
if txInput.PreviousOutPoint.Index != uint32(htlcOutputIndex) {
return fmt.Errorf("expected fulfillment tx input to point to correct htlc output")
}
sigHashes := txscript.NewTxSigHashes(tx)
muunSigKey, err := muunPublicKey.key.ECPubKey()
if err != nil {
return err
}
// Verify Muun signature
htlcOutputAmount := htlcTx.TxOut[htlcOutputIndex].Value
err = verifyTxWitnessSignature(
tx,
sigHashes,
index,
htlcOutputAmount,
htlcScript,
c.MuunSignature,
muunSigKey,
)
if err != nil {
return fmt.Errorf("could not verify Muun signature for htlc: %w", err)
}
var outputAmount, expectedAmount lnwire.MilliSatoshi
if c.VerifyOutputAmount {
outputAmount = lnwire.MilliSatoshi(tx.TxOut[0].Value * 1000)
// This incoming swap might be collecting debt, which would be deducted from the outputAmount
// so we add it back up so the amount will match with the sphinx
expectedAmount = outputAmount + lnwire.NewMSatFromSatoshis(c.Collect)
}
// Now check the information we have against the sphinx created by the payer
if len(c.Sphinx) > 0 {
err = sphinx.Validate(
c.Sphinx,
c.PaymentHash256,
secrets.PaymentSecret,
nodeKey,
uint32(c.ExpirationHeight),
expectedAmount,
c.Network,
)
if err != nil {
return fmt.Errorf("could not verify sphinx blob: %w", err)
}
}
// Sign the fulfillment tx
sig, err := signNativeSegwitInput(
index,
tx,
userPrivateKey,
htlcScript,
btcutil.Amount(htlcOutputAmount),
)
if err != nil {
return fmt.Errorf("could not sign fulfillment tx: %w", err)
}
txInput.Witness = wire.TxWitness{
secrets.Preimage,
sig,
c.MuunSignature,
htlcScript,
}
return nil
}
func (c *coinIncomingSwap) FullySignInput(index int, tx *wire.MsgTx, userKey, muunKey *HDPrivateKey) error {
// Lookup invoice data matching this HTLC using the payment hash
db, err := openDB()
if err != nil {
return err
}
defer db.Close()
secrets, err := db.FindByPaymentHash(c.PaymentHash256)
if err != nil {
return fmt.Errorf("could not find invoice data for payment hash: %w", err)
}
derivedMuunKey, err := muunKey.DeriveTo(secrets.KeyPath)
if err != nil {
return fmt.Errorf("failed to derive muun key: %w", err)
}
muunSignature, err := c.signature(index, tx, userKey.PublicKey(), derivedMuunKey.PublicKey(), derivedMuunKey)
if err != nil {
return err
}
c.MuunSignature = muunSignature
return c.SignInput(index, tx, userKey, muunKey.PublicKey())
}
func (c *coinIncomingSwap) createHtlcScript(userPublicKey, muunPublicKey *HDPublicKey) ([]byte, error) {
return createHtlcScript(
userPublicKey.Raw(),
muunPublicKey.Raw(),
c.SwapServerPublicKey,
c.ExpirationHeight,
c.PaymentHash256,
)
}
func (c *coinIncomingSwap) signature(index int, tx *wire.MsgTx, userKey *HDPublicKey, muunKey *HDPublicKey,
signingKey *HDPrivateKey) ([]byte, error) {
htlcTx := wire.MsgTx{}
err := htlcTx.Deserialize(bytes.NewReader(c.HtlcTx))
if err != nil {
return nil, fmt.Errorf("could not deserialize htlc tx: %w", err)
}
htlcScript, err := c.createHtlcScript(userKey, muunKey)
if err != nil {
return nil, fmt.Errorf("could not create htlc script: %w", err)
}
htlcOutputIndex, err := c.findHtlcOutputIndex(&htlcTx, htlcScript)
if err != nil {
return nil, err
}
prevOutAmount := htlcTx.TxOut[htlcOutputIndex].Value
sig, err := signNativeSegwitInput(
index,
tx,
signingKey,
htlcScript,
btcutil.Amount(prevOutAmount),
)
if err != nil {
return nil, fmt.Errorf("could not sign fulfillment tx: %w", err)
}
return sig, nil
}
func (c *coinIncomingSwap) findHtlcOutputIndex(htlcTx *wire.MsgTx, htlcScript []byte) (int, error) {
witnessHash := sha256.Sum256(htlcScript)
address, err := btcutil.NewAddressWitnessScriptHash(witnessHash[:], c.Network)
if err != nil {
return 0, fmt.Errorf("could not create htlc address: %w", err)
}
pkScript, err := txscriptw.PayToAddrScript(address)
if err != nil {
return 0, fmt.Errorf("could not create pk script: %w", err)
}
// Try to find the script we just built inside the HTLC output scripts
for i, out := range htlcTx.TxOut {
if bytes.Equal(pkScript, out.PkScript) {
return i, nil
}
}
return 0, errors.New("could not find valid htlc output in htlc tx")
}
func createHtlcScript(userPublicKey, muunPublicKey, swapServerPublicKey []byte, expiry int64, paymentHash []byte) ([]byte, error) {
sb := txscript.NewScriptBuilder()
sb.AddData(muunPublicKey)
sb.AddOp(txscript.OP_CHECKSIG)
sb.AddOp(txscript.OP_NOTIF)
sb.AddOp(txscript.OP_DUP)
sb.AddOp(txscript.OP_HASH160)
sb.AddData(btcutil.Hash160(swapServerPublicKey))
sb.AddOp(txscript.OP_EQUALVERIFY)
sb.AddOp(txscript.OP_CHECKSIGVERIFY)
sb.AddInt64(expiry)
sb.AddOp(txscript.OP_CHECKLOCKTIMEVERIFY)
sb.AddOp(txscript.OP_ELSE)
sb.AddData(userPublicKey)
sb.AddOp(txscript.OP_CHECKSIGVERIFY)
sb.AddOp(txscript.OP_SIZE)
sb.AddInt64(32)
sb.AddOp(txscript.OP_EQUALVERIFY)
sb.AddOp(txscript.OP_HASH160)
sb.AddData(ripemd160(paymentHash))
sb.AddOp(txscript.OP_EQUAL)
sb.AddOp(txscript.OP_ENDIF)
return sb.Script()
}