From a5f56342df2c925a1ad3d96ac6db0e2b1afebf11 Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Fri, 31 Jan 2020 12:41:18 -0800 Subject: [PATCH] lndclient: expose retrieval of channel backups through LightningClient --- lndclient/lightning_client.go | 50 +++++++++++++++++++++++++++++++++++ test/lightning_client_mock.go | 13 +++++++++ 2 files changed, 63 insertions(+) diff --git a/lndclient/lightning_client.go b/lndclient/lightning_client.go index 6eeb3fb..32dfffe 100644 --- a/lndclient/lightning_client.go +++ b/lndclient/lightning_client.go @@ -41,6 +41,15 @@ type LightningClient interface { // ListTransactions returns all known transactions of the backing lnd // node. ListTransactions(ctx context.Context) ([]*wire.MsgTx, error) + + // ChannelBackup retrieves the backup for a particular channel. The + // backup is returned as an encrypted chanbackup.Single payload. + ChannelBackup(context.Context, wire.OutPoint) ([]byte, error) + + // ChannelBackups retrieves backups for all existing pending open and + // open channels. The backups are returned as an encrypted + // chanbackup.Multi payload. + ChannelBackups(ctx context.Context) ([]byte, error) } // Info contains info about the connected lnd node. @@ -384,3 +393,44 @@ func (s *lightningClient) ListTransactions(ctx context.Context) ([]*wire.MsgTx, return txs, nil } + +// ChannelBackup retrieves the backup for a particular channel. The backup is +// returned as an encrypted chanbackup.Single payload. +func (s *lightningClient) ChannelBackup(ctx context.Context, + channelPoint wire.OutPoint) ([]byte, error) { + + rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout) + defer cancel() + + rpcCtx = s.adminMac.WithMacaroonAuth(rpcCtx) + req := &lnrpc.ExportChannelBackupRequest{ + ChanPoint: &lnrpc.ChannelPoint{ + FundingTxid: &lnrpc.ChannelPoint_FundingTxidBytes{ + FundingTxidBytes: channelPoint.Hash[:], + }, + OutputIndex: channelPoint.Index, + }, + } + resp, err := s.client.ExportChannelBackup(rpcCtx, req) + if err != nil { + return nil, err + } + + return resp.ChanBackup, nil +} + +// ChannelBackups retrieves backups for all existing pending open and open +// channels. The backups are returned as an encrypted chanbackup.Multi payload. +func (s *lightningClient) ChannelBackups(ctx context.Context) ([]byte, error) { + rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout) + defer cancel() + + rpcCtx = s.adminMac.WithMacaroonAuth(rpcCtx) + req := &lnrpc.ChanBackupExportRequest{} + resp, err := s.client.ExportAllChannelBackups(rpcCtx, req) + if err != nil { + return nil, err + } + + return resp.MultiChanBackup.MultiChanBackup, nil +} diff --git a/test/lightning_client_mock.go b/test/lightning_client_mock.go index ec030e0..2801bf9 100644 --- a/test/lightning_client_mock.go +++ b/test/lightning_client_mock.go @@ -137,3 +137,16 @@ func (h *mockLightningClient) ListTransactions( h.lnd.lock.Unlock() return txs, nil } + +// ChannelBackup retrieves the backup for a particular channel. The +// backup is returned as an encrypted chanbackup.Single payload. +func (h *mockLightningClient) ChannelBackup(context.Context, wire.OutPoint) ([]byte, error) { + return nil, nil +} + +// ChannelBackups retrieves backups for all existing pending open and +// open channels. The backups are returned as an encrypted +// chanbackup.Multi payload. +func (h *mockLightningClient) ChannelBackups(ctx context.Context) ([]byte, error) { + return nil, nil +}