diff --git a/cmd_dumpbackup.go b/cmd_dumpbackup.go index 48286b5..27ef3d2 100644 --- a/cmd_dumpbackup.go +++ b/cmd_dumpbackup.go @@ -1,56 +1,38 @@ package chantools import ( - "net" - - "github.com/btcsuite/btcutil" "github.com/davecgh/go-spew/spew" + "github.com/guggero/chantools/dump" "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)) + dumpSingles := make([]dump.BackupSingle, 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( + dumpSingles[idx] = dump.BackupSingle{ + Version: single.Version, + IsInitiator: single.IsInitiator, + ChainHash: single.ChainHash.String(), + FundingOutpoint: single.FundingOutpoint.String(), + ShortChannelID: single.ShortChannelID, + RemoteNodePub: dump.PubKeyToString( + single.RemoteNodePub, + ), + Addresses: single.Addresses, + Capacity: single.Capacity, + LocalChanCfg: dump.ToChannelConfig( + single.LocalChanCfg, + ), + RemoteChanCfg: dump.ToChannelConfig( + single.RemoteChanCfg, + ), + ShaChainRootDesc: dump.ToKeyDescriptor( single.ShaChainRootDesc, ), } } - spew.Dump(dumpMulti{ + spew.Dump(dump.BackupMulti{ Version: multi.Version, StaticBackups: dumpSingles, }) diff --git a/cmd_dumpchannels.go b/cmd_dumpchannels.go index f25054c..74808f2 100644 --- a/cmd_dumpchannels.go +++ b/cmd_dumpchannels.go @@ -4,50 +4,19 @@ import ( "bytes" "encoding/hex" - "github.com/btcsuite/btcd/chaincfg/chainhash" - "github.com/btcsuite/btcutil" "github.com/davecgh/go-spew/spew" + "github.com/guggero/chantools/dump" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/input" - "github.com/lightningnetwork/lnd/lnwire" ) -// dumpInfo is the information we want to dump from an open channel in lnd's -// channel DB. See `channeldb.OpenChannel` for information about the fields. -type dumpInfo struct { - ChanType channeldb.ChannelType - ChainHash chainhash.Hash - FundingOutpoint string - ShortChannelID lnwire.ShortChannelID - IsPending bool - IsInitiator bool - ChanStatus channeldb.ChannelStatus - FundingBroadcastHeight uint32 - NumConfsRequired uint16 - ChannelFlags lnwire.FundingFlag - IdentityPub string - Capacity btcutil.Amount - TotalMSatSent lnwire.MilliSatoshi - TotalMSatReceived lnwire.MilliSatoshi - PerCommitPoint string - LocalChanCfg dumpChanCfg - RemoteChanCfg dumpChanCfg - LocalCommitment channeldb.ChannelCommitment - RemoteCommitment channeldb.ChannelCommitment - RemoteCurrentRevocation string - RemoteNextRevocation string - FundingTxn string - LocalShutdownScript lnwire.DeliveryAddress - RemoteShutdownScript lnwire.DeliveryAddress -} - func dumpChannelInfo(chanDb *channeldb.DB) error { channels, err := chanDb.FetchAllChannels() if err != nil { return err } - dumpChannels := make([]dumpInfo, len(channels)) + dumpChannels := make([]dump.OpenChannel, len(channels)) for idx, channel := range channels { var buf bytes.Buffer if channel.FundingTxn != nil { @@ -64,7 +33,7 @@ func dumpChannelInfo(chanDb *channeldb.DB) error { } perCommitPoint := input.ComputeCommitmentPoint(revPreimage[:]) - dumpChannels[idx] = dumpInfo{ + dumpChannels[idx] = dump.OpenChannel{ ChanType: channel.ChanType, ChainHash: channel.ChainHash, FundingOutpoint: channel.FundingOutpoint.String(), @@ -75,21 +44,25 @@ func dumpChannelInfo(chanDb *channeldb.DB) error { FundingBroadcastHeight: channel.FundingBroadcastHeight, NumConfsRequired: channel.NumConfsRequired, ChannelFlags: channel.ChannelFlags, - IdentityPub: pubKeyToString( + IdentityPub: dump.PubKeyToString( channel.IdentityPub, ), Capacity: channel.Capacity, TotalMSatSent: channel.TotalMSatSent, TotalMSatReceived: channel.TotalMSatReceived, - PerCommitPoint: pubKeyToString(perCommitPoint), - LocalChanCfg: toDumpChanCfg(channel.LocalChanCfg), - RemoteChanCfg: toDumpChanCfg(channel.RemoteChanCfg), - LocalCommitment: channel.LocalCommitment, - RemoteCommitment: channel.RemoteCommitment, - RemoteCurrentRevocation: pubKeyToString( + PerCommitPoint: dump.PubKeyToString(perCommitPoint), + LocalChanCfg: dump.ToChannelConfig( + channel.LocalChanCfg, + ), + RemoteChanCfg: dump.ToChannelConfig( + channel.RemoteChanCfg, + ), + LocalCommitment: channel.LocalCommitment, + RemoteCommitment: channel.RemoteCommitment, + RemoteCurrentRevocation: dump.PubKeyToString( channel.RemoteCurrentRevocation, ), - RemoteNextRevocation: pubKeyToString( + RemoteNextRevocation: dump.PubKeyToString( channel.RemoteNextRevocation, ), FundingTxn: hex.EncodeToString(buf.Bytes()), diff --git a/dump.go b/dump.go deleted file mode 100644 index 3b2a8cc..0000000 --- a/dump.go +++ /dev/null @@ -1,57 +0,0 @@ -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 "" - } - return hex.EncodeToString(pubkey.SerializeCompressed()) -} diff --git a/dump/dump.go b/dump/dump.go new file mode 100644 index 0000000..0b2c04e --- /dev/null +++ b/dump/dump.go @@ -0,0 +1,117 @@ +package dump + +import ( + "encoding/hex" + "fmt" + "net" + + "github.com/btcsuite/btcd/btcec" + "github.com/btcsuite/btcd/chaincfg/chainhash" + "github.com/btcsuite/btcutil" + "github.com/lightningnetwork/lnd/chanbackup" + "github.com/lightningnetwork/lnd/channeldb" + "github.com/lightningnetwork/lnd/keychain" + "github.com/lightningnetwork/lnd/lnwire" +) + +const ( + lndInternalDerivationPath = "m/1017'/0'/%d'/0/%d" +) + +// BackupSingle is the information we want to dump from an lnd channel backup +// multi file. See `chanbackup.Multi` for information about the fields. +type BackupMulti struct { + Version chanbackup.MultiBackupVersion + StaticBackups []BackupSingle +} + +// BackupSingle is the information we want to dump from an lnd channel backup. +// See `chanbackup.Single` for information about the fields. +type BackupSingle struct { + Version chanbackup.SingleBackupVersion + IsInitiator bool + ChainHash string + FundingOutpoint string + ShortChannelID lnwire.ShortChannelID + RemoteNodePub string + Addresses []net.Addr + Capacity btcutil.Amount + LocalChanCfg ChannelConfig + RemoteChanCfg ChannelConfig + ShaChainRootDesc KeyDescriptor +} + +// OpenChannel is the information we want to dump from an open channel in lnd's +// channel DB. See `channeldb.OpenChannel` for information about the fields. +type OpenChannel struct { + ChanType channeldb.ChannelType + ChainHash chainhash.Hash + FundingOutpoint string + ShortChannelID lnwire.ShortChannelID + IsPending bool + IsInitiator bool + ChanStatus channeldb.ChannelStatus + FundingBroadcastHeight uint32 + NumConfsRequired uint16 + ChannelFlags lnwire.FundingFlag + IdentityPub string + Capacity btcutil.Amount + TotalMSatSent lnwire.MilliSatoshi + TotalMSatReceived lnwire.MilliSatoshi + PerCommitPoint string + LocalChanCfg ChannelConfig + RemoteChanCfg ChannelConfig + LocalCommitment channeldb.ChannelCommitment + RemoteCommitment channeldb.ChannelCommitment + RemoteCurrentRevocation string + RemoteNextRevocation string + FundingTxn string + LocalShutdownScript lnwire.DeliveryAddress + RemoteShutdownScript lnwire.DeliveryAddress +} + +// ChannelConfig is the information we want to dump from a channel +// configuration. See `channeldb.ChannelConfig` for more information about the +// fields. +type ChannelConfig struct { + channeldb.ChannelConstraints + MultiSigKey KeyDescriptor + RevocationBasePoint KeyDescriptor + PaymentBasePoint KeyDescriptor + DelayBasePoint KeyDescriptor + HtlcBasePoint KeyDescriptor +} + +// KeyDescriptor is the information we want to dump from a key descriptor. See +// `keychain.KeyDescriptor` for more information about the fields. +type KeyDescriptor struct { + Path string + PubKey string +} + +func ToChannelConfig(cfg channeldb.ChannelConfig) ChannelConfig { + return ChannelConfig{ + ChannelConstraints: cfg.ChannelConstraints, + MultiSigKey: ToKeyDescriptor(cfg.MultiSigKey), + RevocationBasePoint: ToKeyDescriptor(cfg.RevocationBasePoint), + PaymentBasePoint: ToKeyDescriptor(cfg.PaymentBasePoint), + DelayBasePoint: ToKeyDescriptor(cfg.DelayBasePoint), + HtlcBasePoint: ToKeyDescriptor(cfg.HtlcBasePoint), + } +} + +func ToKeyDescriptor(desc keychain.KeyDescriptor) KeyDescriptor { + return KeyDescriptor{ + Path: fmt.Sprintf( + lndInternalDerivationPath, desc.Family, desc.Index, + ), + PubKey: PubKeyToString(desc.PubKey), + } +} + +func PubKeyToString(pubkey *btcec.PublicKey) string { + if pubkey == nil { + return "" + } + return hex.EncodeToString(pubkey.SerializeCompressed()) +}