2
0
mirror of https://github.com/guggero/chantools synced 2024-11-15 06:12:49 +00:00
chantools/lnd/chanbackup.go
2020-03-30 18:33:00 +02:00

36 lines
940 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)
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
}