mirror of
https://github.com/guggero/chantools
synced 2024-11-01 15:40:11 +00:00
38 lines
967 B
Go
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: %w", 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: %w", err)
|
|
}
|
|
err = multiFile.UpdateAndSwap(b.Bytes())
|
|
if err != nil {
|
|
return fmt.Errorf("unable to write backup file: %w", err)
|
|
}
|
|
return nil
|
|
}
|