mirror of https://github.com/guggero/chantools
Add dumpbackup command
parent
ffa06a0598
commit
8f2c8db310
@ -0,0 +1,58 @@
|
||||
package chantools
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/btcsuite/btcutil"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/lightningnetwork/lnd/chanbackup"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
)
|
||||
|
||||
type dumpMulti struct {
|
||||
Version chanbackup.MultiBackupVersion
|
||||
StaticBackups []dumpSingle
|
||||
}
|
||||
|
||||
// dumpSingle is the information we want to dump from an lnd channel backup.
|
||||
// See `chanbackup.Single` for information about the fields.
|
||||
type dumpSingle struct {
|
||||
Version chanbackup.SingleBackupVersion
|
||||
IsInitiator bool
|
||||
ChainHash string
|
||||
FundingOutpoint string
|
||||
ShortChannelID lnwire.ShortChannelID
|
||||
RemoteNodePub string
|
||||
Addresses []net.Addr
|
||||
Capacity btcutil.Amount
|
||||
LocalChanCfg dumpChanCfg
|
||||
RemoteChanCfg dumpChanCfg
|
||||
ShaChainRootDesc dumpDescriptor
|
||||
}
|
||||
|
||||
func dumpChannelBackup(multi *chanbackup.Multi) error {
|
||||
dumpSingles := make([]dumpSingle, len(multi.StaticBackups))
|
||||
for idx, single := range multi.StaticBackups {
|
||||
dumpSingles[idx] = dumpSingle{
|
||||
Version: single.Version,
|
||||
IsInitiator: single.IsInitiator,
|
||||
ChainHash: single.ChainHash.String(),
|
||||
FundingOutpoint: single.FundingOutpoint.String(),
|
||||
ShortChannelID: single.ShortChannelID,
|
||||
RemoteNodePub: pubKeyToString(single.RemoteNodePub),
|
||||
Addresses: single.Addresses,
|
||||
Capacity: single.Capacity,
|
||||
LocalChanCfg: toDumpChanCfg(single.LocalChanCfg),
|
||||
RemoteChanCfg: toDumpChanCfg(single.RemoteChanCfg),
|
||||
ShaChainRootDesc: toDumpDescriptor(
|
||||
single.ShaChainRootDesc,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
spew.Dump(dumpMulti{
|
||||
Version: multi.Version,
|
||||
StaticBackups: dumpSingles,
|
||||
})
|
||||
return nil
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package chantools
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/lightningnetwork/lnd/channeldb"
|
||||
"github.com/lightningnetwork/lnd/keychain"
|
||||
)
|
||||
|
||||
const (
|
||||
lndInternalDerivationPath = "m/1017'/0'/%d'/0/%d"
|
||||
)
|
||||
|
||||
// dumpChanCfg is the information we want to dump from a channel configuration.
|
||||
// See `channeldb.ChannelConfig` for more information about the fields.
|
||||
type dumpChanCfg struct {
|
||||
channeldb.ChannelConstraints
|
||||
MultiSigKey dumpDescriptor
|
||||
RevocationBasePoint dumpDescriptor
|
||||
PaymentBasePoint dumpDescriptor
|
||||
DelayBasePoint dumpDescriptor
|
||||
HtlcBasePoint dumpDescriptor
|
||||
}
|
||||
|
||||
type dumpDescriptor struct {
|
||||
Path string
|
||||
Pubkey string
|
||||
}
|
||||
|
||||
func toDumpChanCfg(cfg channeldb.ChannelConfig) dumpChanCfg {
|
||||
return dumpChanCfg{
|
||||
ChannelConstraints: cfg.ChannelConstraints,
|
||||
MultiSigKey: toDumpDescriptor(cfg.MultiSigKey),
|
||||
RevocationBasePoint: toDumpDescriptor(cfg.RevocationBasePoint),
|
||||
PaymentBasePoint: toDumpDescriptor(cfg.PaymentBasePoint),
|
||||
DelayBasePoint: toDumpDescriptor(cfg.DelayBasePoint),
|
||||
HtlcBasePoint: toDumpDescriptor(cfg.HtlcBasePoint),
|
||||
}
|
||||
}
|
||||
|
||||
func toDumpDescriptor(desc keychain.KeyDescriptor) dumpDescriptor {
|
||||
return dumpDescriptor{
|
||||
Path: fmt.Sprintf(
|
||||
lndInternalDerivationPath, desc.Family, desc.Index,
|
||||
),
|
||||
Pubkey: pubKeyToString(desc.PubKey),
|
||||
}
|
||||
}
|
||||
|
||||
func pubKeyToString(pubkey *btcec.PublicKey) string {
|
||||
if pubkey == nil {
|
||||
return "<nil>"
|
||||
}
|
||||
return hex.EncodeToString(pubkey.SerializeCompressed())
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package chantools
|
||||
|
||||
import (
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/btcsuite/btcutil/hdkeychain"
|
||||
"github.com/lightningnetwork/lnd/keychain"
|
||||
)
|
||||
|
||||
const (
|
||||
hardenedKeyStart = uint32(hdkeychain.HardenedKeyStart)
|
||||
)
|
||||
|
||||
func deriveChildren(key *hdkeychain.ExtendedKey, path []uint32) (
|
||||
*hdkeychain.ExtendedKey, error) {
|
||||
|
||||
var (
|
||||
currentKey = key
|
||||
err error
|
||||
)
|
||||
for _, pathPart := range path {
|
||||
currentKey, err = currentKey.Child(pathPart)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return currentKey, nil
|
||||
}
|
||||
|
||||
type channelBackupEncryptionRing struct {
|
||||
extendedKey *hdkeychain.ExtendedKey
|
||||
chainParams *chaincfg.Params
|
||||
}
|
||||
|
||||
func (r *channelBackupEncryptionRing) DeriveNextKey(_ keychain.KeyFamily) (
|
||||
keychain.KeyDescriptor, error) {
|
||||
|
||||
return keychain.KeyDescriptor{}, nil
|
||||
}
|
||||
|
||||
func (r *channelBackupEncryptionRing) DeriveKey(keyLoc keychain.KeyLocator) (
|
||||
keychain.KeyDescriptor, error) {
|
||||
|
||||
var empty = keychain.KeyDescriptor{}
|
||||
keyBackup, err := deriveChildren(r.extendedKey, []uint32{
|
||||
hardenedKeyStart + uint32(keychain.BIP0043Purpose),
|
||||
hardenedKeyStart + r.chainParams.HDCoinType,
|
||||
hardenedKeyStart + uint32(keyLoc.Family),
|
||||
0,
|
||||
keyLoc.Index,
|
||||
})
|
||||
if err != nil {
|
||||
return empty, err
|
||||
}
|
||||
|
||||
backupPubKey, err := keyBackup.ECPubKey()
|
||||
if err != nil {
|
||||
return empty, err
|
||||
}
|
||||
return keychain.KeyDescriptor{
|
||||
KeyLocator: keychain.KeyLocator{
|
||||
Family: keyLoc.Family,
|
||||
Index: keyLoc.Index,
|
||||
},
|
||||
PubKey: backupPubKey,
|
||||
}, nil
|
||||
}
|
Loading…
Reference in New Issue