forked from mit-dci/lit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signtx.go
193 lines (161 loc) · 5.31 KB
/
signtx.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
package qln
import (
"fmt"
"github.com/adiabat/btcd/btcec"
"github.com/adiabat/btcd/txscript"
"github.com/adiabat/btcd/wire"
"github.com/mit-dci/lit/lnutil"
"github.com/mit-dci/lit/sig64"
)
// SignBreak signs YOUR tx, which you already have a sig for
func (nd *LitNode) SignBreakTx(q *Qchan) (*wire.MsgTx, error) {
tx, err := q.BuildStateTx(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 := nd.SubWallet[q.Coin()].GetPriv(q.KeyGen)
// generate sig.
mySig, err := txscript.RawTxInWitnessSignature(
tx, hCache, 0, q.Value, pre, txscript.SigHashAll, priv)
theirSig := sig64.SigDecompress(q.State.sig)
// put the sighash all byte on the end of their signature
theirSig = append(theirSig, byte(txscript.SigHashAll))
fmt.Printf("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
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 := nd.SubWallet[q.Coin()].GetPriv(q.KeyGen)
// 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)
}
// SignNextState generates your signature for their state.
func (nd *LitNode) SignState(q *Qchan) ([64]byte, error) {
var sig [64]byte
// make sure channel exists, and wallet is present on node
if q == nil {
return sig, fmt.Errorf("SignState nil channel")
}
_, ok := nd.SubWallet[q.Coin()]
if !ok {
return sig, fmt.Errorf("SignState no wallet for cointype %d", q.Coin())
}
// build transaction for next state
tx, err := q.BuildStateTx(false) // their tx, as I'm signing
if err != nil {
return sig, err
}
// make hash cache for this tx
hCache := txscript.NewTxSigHashes(tx)
// generate script preimage (ignore key order)
pre, _, err := lnutil.FundTxScript(q.MyPub, q.TheirPub)
if err != nil {
return sig, err
}
// get private signing key
priv := nd.SubWallet[q.Coin()].GetPriv(q.KeyGen)
// generate sig.
bigSig, err := txscript.RawTxInWitnessSignature(
tx, hCache, 0, q.Value, pre, txscript.SigHashAll, priv)
// truncate sig (last byte is sighash type, always sighashAll)
bigSig = bigSig[:len(bigSig)-1]
sig, err = sig64.SigCompress(bigSig)
if err != nil {
return sig, err
}
fmt.Printf("____ sig creation for channel (%d,%d):\n", q.Peer(), q.Idx())
fmt.Printf("\tinput %s\n", tx.TxIn[0].PreviousOutPoint.String())
for i, txout := range tx.TxOut {
fmt.Printf("\toutput %d: %x %d\n", i, txout.PkScript, txout.Value)
}
fmt.Printf("\tstate %d myamt: %d theiramt: %d\n", q.State.StateIdx, q.State.MyAmt, q.Value-q.State.MyAmt)
return sig, nil
}
// 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) VerifySig(sig [64]byte) error {
bigSig := sig64.SigDecompress(sig)
// my tx when I'm verifying.
tx, err := q.BuildStateTx(true)
if err != nil {
return err
}
// generate fund output script preimage (ignore key order)
pre, _, err := lnutil.FundTxScript(q.MyPub, q.TheirPub)
if err != nil {
return err
}
hCache := txscript.NewTxSigHashes(tx)
parsed, err := txscript.ParseScript(pre)
if err != nil {
return err
}
// always sighash all
hash := txscript.CalcWitnessSignatureHash(
parsed, hCache, txscript.SigHashAll, tx, 0, q.Value)
// sig is pre-truncated; last byte for sighashtype is always sighashAll
pSig, err := btcec.ParseDERSignature(bigSig, btcec.S256())
if err != nil {
return err
}
theirPubKey, err := btcec.ParsePubKey(q.TheirPub[:], btcec.S256())
if err != nil {
return err
}
fmt.Printf("____ sig verification for channel (%d,%d):\n", q.Peer(), q.Idx())
fmt.Printf("\tinput %s\n", tx.TxIn[0].PreviousOutPoint.String())
for i, txout := range tx.TxOut {
fmt.Printf("\toutput %d: %x %d\n", i, txout.PkScript, txout.Value)
}
fmt.Printf("\tstate %d myamt: %d theiramt: %d\n", q.State.StateIdx, q.State.MyAmt, q.Value-q.State.MyAmt)
fmt.Printf("\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)
}
// copy signature, overwriting old signature.
q.State.sig = sig
return nil
}