Skip to content

Commit a399c2c

Browse files
committed
Start exporting library functions
1 parent 9c2b85c commit a399c2c

File tree

7 files changed

+174
-131
lines changed

7 files changed

+174
-131
lines changed

cmd/chantools/chanbackup.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package main
22

33
import (
4-
"bytes"
54
"fmt"
65
"path"
76

87
"github.com/btcsuite/btcutil/hdkeychain"
98
"github.com/guggero/chantools/lnd"
109
"github.com/lightningnetwork/lnd/chanbackup"
1110
"github.com/lightningnetwork/lnd/channeldb"
12-
"github.com/lightningnetwork/lnd/keychain"
1311
)
1412

1513
type chanBackupCommand struct {
@@ -59,28 +57,5 @@ func (c *chanBackupCommand) Execute(_ []string) error {
5957
ExtendedKey: extendedKey,
6058
ChainParams: chainParams,
6159
}
62-
return createChannelBackup(db, multiFile, keyRing)
63-
}
64-
65-
func createChannelBackup(db *channeldb.DB, multiFile *chanbackup.MultiFile,
66-
ring keychain.KeyRing) error {
67-
68-
singles, err := chanbackup.FetchStaticChanBackups(db)
69-
if err != nil {
70-
return fmt.Errorf("error extracting channel backup: %v", err)
71-
}
72-
multi := &chanbackup.Multi{
73-
Version: chanbackup.DefaultMultiVersion,
74-
StaticBackups: singles,
75-
}
76-
var b bytes.Buffer
77-
err = multi.PackToWriter(&b, ring)
78-
if err != nil {
79-
return fmt.Errorf("unable to pack backup: %v", err)
80-
}
81-
err = multiFile.UpdateAndSwap(b.Bytes())
82-
if err != nil {
83-
return fmt.Errorf("unable to write backup file: %v", err)
84-
}
85-
return nil
60+
return lnd.CreateChannelBackup(db, multiFile, keyRing)
8661
}

cmd/chantools/derivekey.go

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/btcsuite/btcutil"
76
"github.com/btcsuite/btcutil/hdkeychain"
87
"github.com/guggero/chantools/lnd"
98
)
@@ -41,33 +40,15 @@ func deriveKey(extendedKey *hdkeychain.ExtendedKey, path string,
4140
neuter bool) error {
4241

4342
fmt.Printf("Deriving path %s for network %s.\n", path, chainParams.Name)
44-
parsedPath, err := lnd.ParsePath(path)
43+
pubKey, wif, err := lnd.DeriveKey(extendedKey, path, chainParams)
4544
if err != nil {
46-
return fmt.Errorf("could not parse derivation path: %v", err)
47-
}
48-
derivedKey, err := lnd.DeriveChildren(extendedKey, parsedPath)
49-
if err != nil {
50-
return fmt.Errorf("could not derive children: %v", err)
51-
}
52-
pubKey, err := derivedKey.ECPubKey()
53-
if err != nil {
54-
return fmt.Errorf("could not derive public key: %v", err)
45+
return fmt.Errorf("could not derive keys: %v", err)
5546
}
5647
fmt.Printf("Public key: %x\n", pubKey.SerializeCompressed())
5748

58-
if neuter {
59-
return nil
60-
}
61-
62-
privKey, err := derivedKey.ECPrivKey()
63-
if err != nil {
64-
return fmt.Errorf("could not derive private key: %v", err)
65-
}
66-
wif, err := btcutil.NewWIF(privKey, chainParams, true)
67-
if err != nil {
68-
return fmt.Errorf("could not encode WIF: %v", err)
49+
if !neuter {
50+
fmt.Printf("Private key (WIF): %s\n", wif.String())
6951
}
70-
fmt.Printf("Private key (WIF): %s\n", wif.String())
7152

7253
return nil
7354
}

cmd/chantools/dumpbackup.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,34 +55,9 @@ func dumpChannelBackup(multiFile *chanbackup.MultiFile,
5555
if err != nil {
5656
return fmt.Errorf("could not extract multi file: %v", err)
5757
}
58-
dumpSingles := make([]dump.BackupSingle, len(multi.StaticBackups))
59-
for idx, single := range multi.StaticBackups {
60-
dumpSingles[idx] = dump.BackupSingle{
61-
Version: single.Version,
62-
IsInitiator: single.IsInitiator,
63-
ChainHash: single.ChainHash.String(),
64-
FundingOutpoint: single.FundingOutpoint.String(),
65-
ShortChannelID: single.ShortChannelID,
66-
RemoteNodePub: dump.PubKeyToString(
67-
single.RemoteNodePub,
68-
),
69-
Addresses: single.Addresses,
70-
Capacity: single.Capacity,
71-
LocalChanCfg: dump.ToChannelConfig(
72-
chainParams, single.LocalChanCfg,
73-
),
74-
RemoteChanCfg: dump.ToChannelConfig(
75-
chainParams, single.RemoteChanCfg,
76-
),
77-
ShaChainRootDesc: dump.ToKeyDescriptor(
78-
chainParams, single.ShaChainRootDesc,
79-
),
80-
}
81-
}
82-
8358
spew.Dump(dump.BackupMulti{
8459
Version: multi.Version,
85-
StaticBackups: dumpSingles,
60+
StaticBackups: dump.BackupDump(multi, chainParams),
8661
})
8762
return nil
8863
}

cmd/chantools/dumpchannels.go

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package main
22

33
import (
4-
"bytes"
5-
"encoding/hex"
64
"fmt"
75
"path"
86

97
"github.com/davecgh/go-spew/spew"
108
"github.com/guggero/chantools/dump"
119
"github.com/lightningnetwork/lnd/channeldb"
12-
"github.com/lightningnetwork/lnd/input"
1310
)
1411

1512
type dumpChannelsCommand struct {
@@ -39,59 +36,9 @@ func dumpChannelInfo(chanDb *channeldb.DB) error {
3936
return err
4037
}
4138

42-
dumpChannels := make([]dump.OpenChannel, len(channels))
43-
for idx, channel := range channels {
44-
var buf bytes.Buffer
45-
if channel.FundingTxn != nil {
46-
err = channel.FundingTxn.Serialize(&buf)
47-
if err != nil {
48-
return err
49-
}
50-
}
51-
revPreimage, err := channel.RevocationProducer.AtIndex(
52-
channel.LocalCommitment.CommitHeight,
53-
)
54-
if err != nil {
55-
return err
56-
}
57-
perCommitPoint := input.ComputeCommitmentPoint(revPreimage[:])
58-
59-
dumpChannels[idx] = dump.OpenChannel{
60-
ChanType: channel.ChanType,
61-
ChainHash: channel.ChainHash,
62-
FundingOutpoint: channel.FundingOutpoint.String(),
63-
ShortChannelID: channel.ShortChannelID,
64-
IsPending: channel.IsPending,
65-
IsInitiator: channel.IsInitiator,
66-
ChanStatus: channel.ChanStatus(),
67-
FundingBroadcastHeight: channel.FundingBroadcastHeight,
68-
NumConfsRequired: channel.NumConfsRequired,
69-
ChannelFlags: channel.ChannelFlags,
70-
IdentityPub: dump.PubKeyToString(
71-
channel.IdentityPub,
72-
),
73-
Capacity: channel.Capacity,
74-
TotalMSatSent: channel.TotalMSatSent,
75-
TotalMSatReceived: channel.TotalMSatReceived,
76-
PerCommitPoint: dump.PubKeyToString(perCommitPoint),
77-
LocalChanCfg: dump.ToChannelConfig(
78-
chainParams, channel.LocalChanCfg,
79-
),
80-
RemoteChanCfg: dump.ToChannelConfig(
81-
chainParams, channel.RemoteChanCfg,
82-
),
83-
LocalCommitment: channel.LocalCommitment,
84-
RemoteCommitment: channel.RemoteCommitment,
85-
RemoteCurrentRevocation: dump.PubKeyToString(
86-
channel.RemoteCurrentRevocation,
87-
),
88-
RemoteNextRevocation: dump.PubKeyToString(
89-
channel.RemoteNextRevocation,
90-
),
91-
FundingTxn: hex.EncodeToString(buf.Bytes()),
92-
LocalShutdownScript: channel.LocalShutdownScript,
93-
RemoteShutdownScript: channel.RemoteShutdownScript,
94-
}
39+
dumpChannels, err := dump.ChannelDump(channels, chainParams)
40+
if err != nil {
41+
return fmt.Errorf("error converting to dump format: %v", err)
9542
}
9643

9744
spew.Dump(dumpChannels)

dump/dump.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package dump
22

33
import (
4+
"bytes"
45
"encoding/hex"
56
"fmt"
7+
"github.com/lightningnetwork/lnd/input"
68
"net"
79

810
"github.com/btcsuite/btcd/btcec"
@@ -90,6 +92,98 @@ type KeyDescriptor struct {
9092
PubKey string
9193
}
9294

95+
// ChannelDump converts the channels in the given channel DB into a dumpable
96+
// format.
97+
func ChannelDump(channels []*channeldb.OpenChannel, params *chaincfg.Params) (
98+
[]OpenChannel, error) {
99+
100+
dumpChannels := make([]OpenChannel, len(channels))
101+
for idx, channel := range channels {
102+
var buf bytes.Buffer
103+
if channel.FundingTxn != nil {
104+
err := channel.FundingTxn.Serialize(&buf)
105+
if err != nil {
106+
return nil, err
107+
}
108+
}
109+
revPreimage, err := channel.RevocationProducer.AtIndex(
110+
channel.LocalCommitment.CommitHeight,
111+
)
112+
if err != nil {
113+
return nil, err
114+
}
115+
perCommitPoint := input.ComputeCommitmentPoint(revPreimage[:])
116+
117+
dumpChannels[idx] = OpenChannel{
118+
ChanType: channel.ChanType,
119+
ChainHash: channel.ChainHash,
120+
FundingOutpoint: channel.FundingOutpoint.String(),
121+
ShortChannelID: channel.ShortChannelID,
122+
IsPending: channel.IsPending,
123+
IsInitiator: channel.IsInitiator,
124+
ChanStatus: channel.ChanStatus(),
125+
FundingBroadcastHeight: channel.FundingBroadcastHeight,
126+
NumConfsRequired: channel.NumConfsRequired,
127+
ChannelFlags: channel.ChannelFlags,
128+
IdentityPub: PubKeyToString(
129+
channel.IdentityPub,
130+
),
131+
Capacity: channel.Capacity,
132+
TotalMSatSent: channel.TotalMSatSent,
133+
TotalMSatReceived: channel.TotalMSatReceived,
134+
PerCommitPoint: PubKeyToString(perCommitPoint),
135+
LocalChanCfg: ToChannelConfig(
136+
params, channel.LocalChanCfg,
137+
),
138+
RemoteChanCfg: ToChannelConfig(
139+
params, channel.RemoteChanCfg,
140+
),
141+
LocalCommitment: channel.LocalCommitment,
142+
RemoteCommitment: channel.RemoteCommitment,
143+
RemoteCurrentRevocation: PubKeyToString(
144+
channel.RemoteCurrentRevocation,
145+
),
146+
RemoteNextRevocation: PubKeyToString(
147+
channel.RemoteNextRevocation,
148+
),
149+
FundingTxn: hex.EncodeToString(buf.Bytes()),
150+
LocalShutdownScript: channel.LocalShutdownScript,
151+
RemoteShutdownScript: channel.RemoteShutdownScript,
152+
}
153+
}
154+
return dumpChannels, nil
155+
}
156+
157+
// BackupDump converts the given multi backup into a dumpable format.
158+
func BackupDump(multi *chanbackup.Multi, params *chaincfg.Params) []BackupSingle {
159+
160+
dumpSingles := make([]BackupSingle, len(multi.StaticBackups))
161+
for idx, single := range multi.StaticBackups {
162+
dumpSingles[idx] = BackupSingle{
163+
Version: single.Version,
164+
IsInitiator: single.IsInitiator,
165+
ChainHash: single.ChainHash.String(),
166+
FundingOutpoint: single.FundingOutpoint.String(),
167+
ShortChannelID: single.ShortChannelID,
168+
RemoteNodePub: PubKeyToString(
169+
single.RemoteNodePub,
170+
),
171+
Addresses: single.Addresses,
172+
Capacity: single.Capacity,
173+
LocalChanCfg: ToChannelConfig(
174+
params, single.LocalChanCfg,
175+
),
176+
RemoteChanCfg: ToChannelConfig(
177+
params, single.RemoteChanCfg,
178+
),
179+
ShaChainRootDesc: ToKeyDescriptor(
180+
params, single.ShaChainRootDesc,
181+
),
182+
}
183+
}
184+
return dumpSingles
185+
}
186+
93187
func ToChannelConfig(params *chaincfg.Params,
94188
cfg channeldb.ChannelConfig) ChannelConfig {
95189

lnd/chanbackup.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package lnd
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
7+
"github.com/lightningnetwork/lnd/chanbackup"
8+
"github.com/lightningnetwork/lnd/channeldb"
9+
"github.com/lightningnetwork/lnd/keychain"
10+
)
11+
12+
// CreateChannelBackup creates a channel backup file from all channels found in
13+
// the given DB file, encrypted with the key in the key ring.
14+
func CreateChannelBackup(db *channeldb.DB, multiFile *chanbackup.MultiFile,
15+
ring keychain.KeyRing) error {
16+
17+
singles, err := chanbackup.FetchStaticChanBackups(db)
18+
if err != nil {
19+
return fmt.Errorf("error extracting channel backup: %v", err)
20+
}
21+
multi := &chanbackup.Multi{
22+
Version: chanbackup.DefaultMultiVersion,
23+
StaticBackups: singles,
24+
}
25+
var b bytes.Buffer
26+
err = multi.PackToWriter(&b, ring)
27+
if err != nil {
28+
return fmt.Errorf("unable to pack backup: %v", err)
29+
}
30+
err = multiFile.UpdateAndSwap(b.Bytes())
31+
if err != nil {
32+
return fmt.Errorf("unable to write backup file: %v", err)
33+
}
34+
return nil
35+
}

0 commit comments

Comments
 (0)