2
0
mirror of https://github.com/guggero/chantools synced 2024-11-07 03:20:43 +00:00
chantools/lnd/chanbackup.go
2021-12-13 10:34:26 +01:00

38 lines
967 B
Go

package lnd
import (
"bytes"
"fmt"
"github.com/lightningnetwork/lnd/chanbackup"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/keychain"
)
// CreateChannelBackup creates a channel backup file from all channels found in
// the given DB file, encrypted with the key in the key ring.
func CreateChannelBackup(db *channeldb.DB, multiFile *chanbackup.MultiFile,
ring keychain.KeyRing) error {
singles, err := chanbackup.FetchStaticChanBackups(
db.ChannelStateDB(), db,
)
if err != nil {
return fmt.Errorf("error extracting channel backup: %v", err)
}
multi := &chanbackup.Multi{
Version: chanbackup.DefaultMultiVersion,
StaticBackups: singles,
}
var b bytes.Buffer
err = multi.PackToWriter(&b, ring)
if err != nil {
return fmt.Errorf("unable to pack backup: %v", err)
}
err = multiFile.UpdateAndSwap(b.Bytes())
if err != nil {
return fmt.Errorf("unable to write backup file: %v", err)
}
return nil
}