-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathsigntx.go
More file actions
472 lines (395 loc) · 13.4 KB
/
Copy pathsigntx.go
File metadata and controls
472 lines (395 loc) · 13.4 KB
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
471
472
package qln
import (
"bytes"
"fmt"
"github.com/mit-dci/lit/btcutil/txscript"
"github.com/mit-dci/lit/crypto/koblitz"
"github.com/mit-dci/lit/lnutil"
"github.com/mit-dci/lit/logging"
"github.com/mit-dci/lit/sig64"
"github.com/mit-dci/lit/wire"
)
// SignBreak signs YOUR tx, which you already have a sig for
func (nd *LitNode) SignBreakTx(q *Qchan) (*wire.MsgTx, error) {
// TODO: we probably have to do something with the HTLCs here
tx, _, _, err := q.BuildStateTxs(true)
if err != nil {
return nil, err
}
// make hash cache for this tx
hCache := txscript.NewTxSigHashes(tx)
// generate script preimage (keep track of key order)
pre, swap, err := lnutil.FundTxScript(q.MyPub, q.TheirPub)
if err != nil {
return nil, err
}
// get private signing key
priv, err := nd.SubWallet[q.Coin()].GetPriv(q.KeyGen)
if err != nil {
return nil, err
}
// generate sig.
mySig, err := txscript.RawTxInWitnessSignature(
tx, hCache, 0, q.Value, pre, txscript.SigHashAll, priv)
if err != nil {
return nil, err
}
theirSig := sig64.SigDecompress(q.State.Sig)
// put the sighash all byte on the end of their signature
theirSig = append(theirSig, byte(txscript.SigHashAll))
logging.Infof("made mysig: %x theirsig: %x\n", mySig, theirSig)
// add sigs to the witness stack
if swap {
tx.TxIn[0].Witness = SpendMultiSigWitStack(pre, theirSig, mySig)
} else {
tx.TxIn[0].Witness = SpendMultiSigWitStack(pre, mySig, theirSig)
}
// save channel state as closed
// Removed - this is already done in the calling function - and is killing
// the ability to just print the TX.
// q.CloseData.Closed = true
// q.CloseData.CloseTxid = tx.TxHash()
// err = nd.SaveQchanUtxoData(q)
// if err != nil {
// return nil, err
// }
return tx, nil
}
// SignSimpleClose signs the given simpleClose tx, given the other signature
// Tx is modified in place.
func (nd *LitNode) SignSimpleClose(q *Qchan, tx *wire.MsgTx) ([64]byte, error) {
var sig [64]byte
// make hash cache
hCache := txscript.NewTxSigHashes(tx)
// generate script preimage for signing (ignore key order)
pre, _, err := lnutil.FundTxScript(q.MyPub, q.TheirPub)
if err != nil {
return sig, err
}
// get private signing key
priv, err := nd.SubWallet[q.Coin()].GetPriv(q.KeyGen)
if err != nil {
return sig, err
}
// generate sig
mySig, err := txscript.RawTxInWitnessSignature(
tx, hCache, 0, q.Value, pre, txscript.SigHashAll, priv)
if err != nil {
return sig, err
}
// truncate sig (last byte is sighash type, always sighashAll)
mySig = mySig[:len(mySig)-1]
return sig64.SigCompress(mySig)
}
// SignSettlementTx signs the given settlement tx based on the passed contract
// using the passed private key. Tx is modified in place.
func (nd *LitNode) SignSettlementTx(c *lnutil.DlcContract, tx *wire.MsgTx,
priv *koblitz.PrivateKey) ([64]byte, error) {
var sig [64]byte
// make hash cache
hCache := txscript.NewTxSigHashes(tx)
// generate script preimage for signing (ignore key order)
pre, _, err := lnutil.FundTxScript(c.OurFundMultisigPub,
c.TheirFundMultisigPub)
if err != nil {
return sig, err
}
// generate sig
mySig, err := txscript.RawTxInWitnessSignature(
tx, hCache, 0, c.TheirFundingAmount+c.OurFundingAmount,
pre, txscript.SigHashAll, priv)
if err != nil {
return sig, err
}
// truncate sig (last byte is sighash type, always sighashAll)
mySig = mySig[:len(mySig)-1]
return sig64.SigCompress(mySig)
}
// SignClaimTx signs the given claim tx based on the passed preimage and value
// using the passed private key. Tx is modified in place. timeout=false means
// it's a regular claim, timeout=true means we're claiming an output that has
// expired (for instance if someone) published the wrong settlement TX, we can
// claim this output back to our wallet after the timelock expired.
func (nd *LitNode) SignClaimTx(claimTx *wire.MsgTx, value int64, pre []byte,
priv *koblitz.PrivateKey, timeout bool) error {
// make hash cache
hCache := txscript.NewTxSigHashes(claimTx)
// generate sig
mySig, err := txscript.RawTxInWitnessSignature(
claimTx, hCache, 0, value, pre, txscript.SigHashAll, priv)
if err != nil {
return err
}
witStash := make([][]byte, 3)
witStash[0] = mySig
if timeout {
witStash[1] = nil
} else {
witStash[1] = []byte{0x01}
}
witStash[2] = pre
claimTx.TxIn[0].Witness = witStash
return nil
}
// SignNextState generates your signature for their state.
func (nd *LitNode) SignState(q *Qchan) ([64]byte, [][64]byte, error) {
var sig [64]byte
// make sure channel exists, and wallet is present on node
if q == nil {
return sig, nil, fmt.Errorf("SignState nil channel")
}
_, ok := nd.SubWallet[q.Coin()]
if !ok {
return sig, nil, fmt.Errorf("SignState no wallet for cointype %d", q.Coin())
}
// build transaction for next state
commitmentTx, spendHTLCTxs, HTLCTxOuts, err := q.BuildStateTxs(false) // their tx, as I'm signing
if err != nil {
return sig, nil, err
}
logging.Infof("Signing state with Elk [%x] NextElk [%x] N2Elk [%x]\n", q.State.ElkPoint, q.State.NextElkPoint, q.State.N2ElkPoint)
// make hash cache for this tx
hCache := txscript.NewTxSigHashes(commitmentTx)
// generate script preimage (ignore key order)
pre, _, err := lnutil.FundTxScript(q.MyPub, q.TheirPub)
if err != nil {
return sig, nil, err
}
// get private signing key
priv, err := nd.SubWallet[q.Coin()].GetPriv(q.KeyGen)
if err != nil {
return sig, nil, err
}
// generate sig.
bigSig, err := txscript.RawTxInWitnessSignature(
commitmentTx, hCache, 0, q.Value, pre, txscript.SigHashAll, priv)
if err != nil {
return sig, nil, err
}
// truncate sig (last byte is sighash type, always sighashAll)
bigSig = bigSig[:len(bigSig)-1]
sig, err = sig64.SigCompress(bigSig)
if err != nil {
return sig, nil, err
}
logging.Infof("____ sig creation for channel (%d,%d):\n", q.Peer(), q.Idx())
logging.Infof("\tinput %s\n", commitmentTx.TxIn[0].PreviousOutPoint.String())
for i, txout := range commitmentTx.TxOut {
logging.Infof("\toutput %d: %x %d\n", i, txout.PkScript, txout.Value)
}
logging.Infof("\tstate %d myamt: %d theiramt: %d\n", q.State.StateIdx, q.State.MyAmt, q.Value-q.State.MyAmt)
// Generate signatures for HTLC-success/failure transactions
spendHTLCSigs := map[int][64]byte{}
curElk, err := q.ElkSnd.AtIndex(q.State.StateIdx)
if err != nil {
return sig, nil, err
}
elkScalar := lnutil.ElkScalar(curElk)
ep := lnutil.ElkPointFromHash(curElk)
logging.Infof("Using elkpoint %x to sign HTLC txs", ep)
for idx, h := range HTLCTxOuts {
// Find out which vout this HTLC is in the commitment tx since BIP69
// potentially reordered them
var where uint32
for i, o := range commitmentTx.TxOut {
if bytes.Compare(o.PkScript, h.PkScript) == 0 {
where = uint32(i)
break
}
}
var HTLCPrivBase *koblitz.PrivateKey
if idx == len(q.State.HTLCs) {
HTLCPrivBase, err = nd.SubWallet[q.Coin()].GetPriv(q.State.InProgHTLC.KeyGen)
} else if idx == len(q.State.HTLCs)+1 {
HTLCPrivBase, err = nd.SubWallet[q.Coin()].GetPriv(q.State.CollidingHTLC.KeyGen)
} else {
HTLCPrivBase, err = nd.SubWallet[q.Coin()].GetPriv(q.State.HTLCs[idx].KeyGen)
}
if err != nil {
return sig, nil, err
}
HTLCPriv := lnutil.CombinePrivKeyWithBytes(HTLCPrivBase, elkScalar[:])
// Find the tx we need to sign. (this would all be much easier if we
// didn't use BIP69)
var spendTx *wire.MsgTx
var which int
for i, t := range spendHTLCTxs {
if t.TxIn[0].PreviousOutPoint.Index == where {
spendTx = t
which = i
break
}
}
hc := txscript.NewTxSigHashes(spendTx)
var HTLCScript []byte
if idx == len(q.State.HTLCs) {
HTLCScript, err = q.GenHTLCScript(*q.State.InProgHTLC, false)
} else if idx == len(q.State.HTLCs)+1 {
HTLCScript, err = q.GenHTLCScript(*q.State.CollidingHTLC, false)
} else {
HTLCScript, err = q.GenHTLCScript(q.State.HTLCs[idx], false)
}
if err != nil {
return sig, nil, err
}
HTLCparsed, err := txscript.ParseScript(HTLCScript)
if err != nil {
return sig, nil, err
}
spendHTLCHash := txscript.CalcWitnessSignatureHash(
HTLCparsed, hc, txscript.SigHashAll, spendTx, 0, h.Value)
logging.Infof("Signing HTLC hash: %x, with pubkey: %x", spendHTLCHash, HTLCPriv.PubKey().SerializeCompressed())
mySig, err := HTLCPriv.Sign(spendHTLCHash)
if err != nil {
return sig, nil, err
}
HTLCSig := mySig.Serialize()
s, err := sig64.SigCompress(HTLCSig)
if err != nil {
return sig, nil, err
}
spendHTLCSigs[which] = s
}
// Get the sigs in the same order as the HTLCs in the tx
var spendHTLCSigsArr [][64]byte
for i := 0; i < len(spendHTLCSigs)+2; i++ {
if s, ok := spendHTLCSigs[i]; ok {
spendHTLCSigsArr = append(spendHTLCSigsArr, s)
}
}
return sig, spendHTLCSigsArr, err
}
// VerifySig verifies their signature for your next state.
// it also saves the sig if it's good.
// do bool, error or just error? Bad sig is an error I guess.
// for verifying signature, always use theirHAKDpub, so generate & populate within
// this function.
func (q *Qchan) VerifySigs(sig [64]byte, HTLCSigs [][64]byte) error {
bigSig := sig64.SigDecompress(sig)
// my tx when I'm verifying.
commitmentTx, spendHTLCTxs, HTLCTxOuts, err := q.BuildStateTxs(true)
if err != nil {
return err
}
logging.Infof("Verifying signatures with Elk [%x] NextElk [%x] N2Elk [%x]\n", q.State.ElkPoint, q.State.NextElkPoint, q.State.N2ElkPoint)
// generate fund output script preimage (ignore key order)
pre, _, err := lnutil.FundTxScript(q.MyPub, q.TheirPub)
if err != nil {
return err
}
hCache := txscript.NewTxSigHashes(commitmentTx)
parsed, err := txscript.ParseScript(pre)
if err != nil {
return err
}
// always sighash all
hash := txscript.CalcWitnessSignatureHash(
parsed, hCache, txscript.SigHashAll, commitmentTx, 0, q.Value)
// sig is pre-truncated; last byte for sighashtype is always sighashAll
pSig, err := koblitz.ParseDERSignature(bigSig, koblitz.S256())
if err != nil {
return err
}
theirPubKey, err := koblitz.ParsePubKey(q.TheirPub[:], koblitz.S256())
if err != nil {
return err
}
logging.Infof("____ sig verification for channel (%d,%d):\n", q.Peer(), q.Idx())
logging.Infof("\tinput %s\n", commitmentTx.TxIn[0].PreviousOutPoint.String())
for i, txout := range commitmentTx.TxOut {
logging.Infof("\toutput %d: %x %d\n", i, txout.PkScript, txout.Value)
}
logging.Infof("\tstate %d myamt: %d theiramt: %d\n", q.State.StateIdx, q.State.MyAmt, q.Value-q.State.MyAmt)
logging.Infof("\tsig: %x\n", sig)
worked := pSig.Verify(hash, theirPubKey)
if !worked {
return fmt.Errorf("Invalid signature on chan %d state %d",
q.Idx(), q.State.StateIdx)
}
// Verify HTLC-success/failure signatures
if len(HTLCSigs) != len(spendHTLCTxs) {
return fmt.Errorf("Wrong number of signatures provided for HTLCs in channel. Got %d expected %d.",
len(HTLCSigs), len(spendHTLCTxs))
}
// Map HTLC index to signature index
sigIndex := map[uint32]uint32{}
logging.Infof("Using elkpoint %x to verify HTLC txs", q.State.NextElkPoint)
for idx, h := range HTLCTxOuts {
// Find out which vout this HTLC is in the commitment tx since BIP69
// potentially reordered them
var where uint32
for i, o := range commitmentTx.TxOut {
if bytes.Compare(o.PkScript, h.PkScript) == 0 {
where = uint32(i)
break
}
}
// Find the tx we need to verify. (this would all be much easier if we
// didn't use BIP69)
var spendTx *wire.MsgTx
var which int
for i, t := range spendHTLCTxs {
if t.TxIn[0].PreviousOutPoint.Index == where {
spendTx = t
which = i
sigIndex[uint32(idx)] = uint32(which)
break
}
}
hc := txscript.NewTxSigHashes(spendTx)
var HTLCScript []byte
if idx == len(q.State.HTLCs) {
HTLCScript, err = q.GenHTLCScript(*q.State.InProgHTLC, true)
} else if idx == len(q.State.HTLCs)+1 {
HTLCScript, err = q.GenHTLCScript(*q.State.CollidingHTLC, true)
} else {
HTLCScript, err = q.GenHTLCScript(q.State.HTLCs[idx], true)
}
if err != nil {
return err
}
HTLCparsed, err := txscript.ParseScript(HTLCScript)
if err != nil {
return err
}
// always sighash all
spendHTLCHash := txscript.CalcWitnessSignatureHash(
HTLCparsed, hc, txscript.SigHashAll, spendTx, 0, h.Value)
// sig is pre-truncated; last byte for sighashtype is always sighashAll
HTLCSig, err := koblitz.ParseDERSignature(sig64.SigDecompress(HTLCSigs[which]), koblitz.S256())
if err != nil {
return err
}
var theirHTLCPub [33]byte
if idx == len(q.State.HTLCs) {
theirHTLCPub = lnutil.CombinePubs(q.State.InProgHTLC.TheirHTLCBase, q.State.NextElkPoint)
} else if idx == len(q.State.HTLCs)+1 {
theirHTLCPub = lnutil.CombinePubs(q.State.CollidingHTLC.TheirHTLCBase, q.State.NextElkPoint)
} else {
theirHTLCPub = lnutil.CombinePubs(q.State.HTLCs[idx].TheirHTLCBase, q.State.NextElkPoint)
}
theirHTLCPubKey, err := koblitz.ParsePubKey(theirHTLCPub[:], koblitz.S256())
if err != nil {
return err
}
logging.Infof("Verifying HTLC hash: %x, with pubkey: %x", spendHTLCHash, theirHTLCPub)
sigValid := HTLCSig.Verify(spendHTLCHash, theirHTLCPubKey)
if !sigValid {
return fmt.Errorf("Invalid signature HTLC on chan %d state %d HTLC %d",
q.Idx(), q.State.StateIdx, idx)
}
}
// copy signature, overwriting old signature.
q.State.Sig = sig
// copy HTLC-success/failure signatures
for i, s := range sigIndex {
if int(i) == len(q.State.HTLCs) {
q.State.InProgHTLC.Sig = HTLCSigs[s]
} else if int(i) == len(q.State.HTLCs)+1 {
q.State.CollidingHTLC.Sig = HTLCSigs[s]
} else {
q.State.HTLCs[i].Sig = HTLCSigs[s]
}
}
return nil
}