-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathinit.go
More file actions
279 lines (224 loc) · 6.53 KB
/
init.go
File metadata and controls
279 lines (224 loc) · 6.53 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
package qln
import (
"fmt"
"path/filepath"
"sync"
"github.com/boltdb/bolt"
"github.com/mit-dci/lit/btcutil"
"github.com/mit-dci/lit/btcutil/hdkeychain"
"github.com/mit-dci/lit/coinparam"
"github.com/mit-dci/lit/db/lnbolt" // TODO Abstract this more.
"github.com/mit-dci/lit/dlc"
"github.com/mit-dci/lit/eventbus"
"github.com/mit-dci/lit/lncore"
"github.com/mit-dci/lit/lnp2p"
"github.com/mit-dci/lit/logging"
"github.com/mit-dci/lit/portxo"
"github.com/mit-dci/lit/wallit"
"github.com/mit-dci/lit/watchtower"
)
// NewLitNode starts up a lit node. Needs priv key, and a path.
// Does not activate a subwallet; do that after init.
func NewLitNode(privKey *[32]byte, path string, trackerURL string, proxyURL string, nat string) (*LitNode, error) {
var err error
// Maybe make a new parameter set for "LN".. meh
// TODO change this to a non-coin
rootPrivKey, err := hdkeychain.NewMaster(privKey[:], &coinparam.TestNet3Params)
if err != nil {
return nil, err
}
nd := new(LitNode)
nd.LitFolder = path
litdbpath := filepath.Join(nd.LitFolder, "ln.db")
err = nd.OpenDB(litdbpath)
if err != nil {
return nil, err
}
db2 := &lnbolt.LitBoltDB{}
var dbx lncore.LitStorage
dbx = db2
err = dbx.Open(filepath.Join(nd.LitFolder, "db2"))
if err != nil {
return nil, err
}
nd.NewLitDB = dbx
err = nd.NewLitDB.Check()
if err != nil {
return nil, err
}
// Event system setup.
ebus := eventbus.NewEventBus()
nd.Events = &ebus
// Peer manager
nd.PeerMan, err = lnp2p.NewPeerManager(rootPrivKey, nd.NewLitDB.GetPeerDB(), trackerURL, &ebus, nil) // TODO proxy/nat stuff
if err != nil {
return nil, err
}
// Actually start the thread to send messages.
nd.PeerMan.StartSending()
// Register adapter event handlers. These are for hooking in the new peer management with the old one.
h1 := makeTmpNewPeerHandler(nd)
nd.Events.RegisterHandler("lnp2p.peer.new", h1)
h2 := makeTmpDisconnectPeerHandler(nd)
nd.Events.RegisterHandler("lnp2p.peer.disconnect", h2)
// Sets up handlers for all the messages we need to handle.
nd.registerHandlers()
var kg portxo.KeyGen
kg.Depth = 5
kg.Step[0] = 44 | 1<<31
kg.Step[1] = 513 | 1<<31
kg.Step[2] = 9 | 1<<31
kg.Step[3] = 0 | 1<<31
kg.Step[4] = 0 | 1<<31
nd.IdentityKey, err = kg.DerivePrivateKey(rootPrivKey)
if err != nil {
return nil, err
}
kg.Step[3] = 1 | 1<<31
rcPriv, err := kg.DerivePrivateKey(rootPrivKey)
nd.DefaultRemoteControlKey = rcPriv.PubKey()
rcPriv = nil
nd.TrackerURL = trackerURL
nd.ProxyURL = proxyURL
nd.Nat = nat
nd.InitRouting()
// optional tower activation
nd.Tower = new(watchtower.WatchTower)
// Create a new manager for the discreet log contracts
nd.DlcManager, err = dlc.NewManager(filepath.Join(nd.LitFolder, "dlc.db"))
if err != nil {
return nil, err
}
// make maps and channels
nd.UserMessageBox = make(chan string, 32)
nd.InProg = new(InFlightFund)
nd.InProg.done = make(chan uint32, 1)
nd.InProgDual = new(InFlightDualFund)
nd.InProgDual.done = make(chan *DualFundingResult, 1)
nd.InProgMultihop, err = nd.GetAllMultihopPayments()
if err != nil {
return nil, err
}
nd.RemoteMtx.Lock()
nd.RemoteCons = make(map[uint32]*RemotePeer)
nd.RemoteMtx.Unlock()
nd.SubWallet = make(map[uint32]UWallet)
// REFACTORING STUFF
nd.PeerMap = map[*lnp2p.Peer]*RemotePeer{}
nd.PeerMapMtx = &sync.Mutex{}
return nd, nil
}
// LinkBaseWallet activates a wallet and hooks it into the litnode.
func (nd *LitNode) LinkBaseWallet(
privKey *[32]byte, birthHeight int32, resync bool, tower bool,
host string, proxy string, param *coinparam.Params) error {
rootpriv, err := hdkeychain.NewMaster(privKey[:], param)
if err != nil {
return err
}
WallitIdx := param.HDCoinType
// see if we've already attached a wallet for this coin type
if nd.SubWallet[WallitIdx] != nil {
return fmt.Errorf("coin type %d already linked", WallitIdx)
}
// see if startheight is below allowed with coinparam
if birthHeight < param.StartHeight {
return fmt.Errorf("%s birth height give as %d, but parameters start at %d",
param.Name, birthHeight, param.StartHeight)
}
// see if there are other wallets already linked
if len(nd.SubWallet) != 0 {
// there are; assert multiwallet (may already be asserted)
nd.MultiWallet = true
}
// if there aren't, Multiwallet will still be false; set new wallit to
// be the first & default
var cointype int
nd.SubWallet[WallitIdx], cointype, err = wallit.NewWallit(
rootpriv, birthHeight, resync, host, nd.LitFolder, proxy, param)
if err != nil {
logging.Error(err)
return err
}
if nd.ConnectedCoinTypes == nil {
nd.ConnectedCoinTypes = make(map[uint32]bool)
nd.ConnectedCoinTypes[uint32(cointype)] = true
}
nd.ConnectedCoinTypes[uint32(cointype)] = true
// re-register channel addresses
qChans, err := nd.GetAllQchans()
if err != nil {
return err
}
for _, qChan := range qChans {
var pkh [20]byte
pkhSlice := btcutil.Hash160(qChan.MyRefundPub[:])
copy(pkh[:], pkhSlice)
nd.SubWallet[WallitIdx].ExportHook().RegisterAddress(pkh)
logging.Infof("Registering outpoint %v", qChan.PorTxo.Op)
nd.SubWallet[WallitIdx].WatchThis(qChan.PorTxo.Op)
}
go nd.OPEventHandler(nd.SubWallet[WallitIdx].LetMeKnow())
go nd.HeightEventHandler(nd.SubWallet[WallitIdx].LetMeKnowHeight())
if !nd.MultiWallet {
nd.DefaultCoin = param.HDCoinType
}
// if this node is running a watchtower, link the watchtower to the
// new wallet block events
if tower {
err = nd.Tower.HookLink(
nd.LitFolder, param, nd.SubWallet[WallitIdx].ExportHook())
if err != nil {
return err
}
}
return nil
}
// Opens the DB file for the LnNode
func (nd *LitNode) OpenDB(filename string) error {
var err error
nd.LitDB, err = bolt.Open(filename, 0644, nil)
if err != nil {
return err
}
// create buckets if they're not already there
err = nd.LitDB.Update(func(btx *bolt.Tx) error {
_, err := btx.CreateBucketIfNotExists(BKTChannelData)
if err != nil {
return err
}
_, err = btx.CreateBucketIfNotExists(BKTPeers)
if err != nil {
return err
}
_, err = btx.CreateBucketIfNotExists(BKTChanMap)
if err != nil {
return err
}
_, err = btx.CreateBucketIfNotExists(BKTPeerMap)
if err != nil {
return err
}
_, err = btx.CreateBucketIfNotExists(BKTWatch)
if err != nil {
return err
}
_, err = btx.CreateBucketIfNotExists(BKTHTLCOPs)
if err != nil {
return err
}
_, err = btx.CreateBucketIfNotExists(BKTPayments)
if err != nil {
return err
}
_, err = btx.CreateBucketIfNotExists(BKTRCAuth)
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
return nil
}