Skip to content

Commit

Permalink
Merge b9b06a9 into b03d8ed
Browse files Browse the repository at this point in the history
  • Loading branch information
aantonop committed Nov 20, 2019
2 parents b03d8ed + b9b06a9 commit 0b1d149
Showing 1 changed file with 96 additions and 29 deletions.
125 changes: 96 additions & 29 deletions cmd/lncli/commands.go
Expand Up @@ -1401,6 +1401,101 @@ func create(ctx *cli.Context) error {
client, cleanUp := getWalletUnlockerClient(ctx)
defer cleanUp()

// We'll check to see if the user provided any static channel backups (SCB),
// if so, we will warn the user that SCB recovery closes all open channels
// and ask them to confirm their intention.
//
// If the user agrees, we'll add the SCB recovery onto the final init wallet
// request.

var (
chanBackups *lnrpc.ChanBackupSnapshot

// We use var restoreSCB to track if we will be including an SCB
// recovery in the init wallet request.
//
// By default, we do not include the SCB recovery.
restoreSCB = false
)

// parseChanBackups returns an errMissingBackup error if there is no
// (optional) SCB backup file in the command.
//
// We can therefore ignore the errMissingChanBackup error as it's an
// optional field.
//
// Try to load an SCB file.
backups, err := parseChanBackups(ctx)

// Evaluate result (err) from parseChanBackups.
switch {
case err == errMissingChanBackup:
// SCB recovery is an optional parameter of the create command.
// We ignore errMissingChanBackup.

case err != nil:
// Passed an invalid channel backup file.
return fmt.Errorf("unable to parse chan backups: %v", err)

default:
// We have an SCB recovery option with a valid backup file.
//
// We must warn user that restoring from a static channel backup will
// CLOSE all existing open channels, and confirm this is what the user
// wants to do.
warningLoop:
for {

fmt.Println()
fmt.Printf("WARNING: You are attempting to restore from a " +
"static channel backup (SCB) file.\nThis action will CLOSE " +
"all currently open channels, and you will pay on-chain fees.\n\n" +
"Are you sure you want to recover funds from " +
" static channel backup? (Enter y/n): ")

reader := bufio.NewReader(os.Stdin)
answer, err := reader.ReadString('\n')
if err != nil {
return err
}

answer = strings.TrimSpace(answer)
answer = strings.ToLower(answer)

switch answer {
case "y":
restoreSCB = true
break warningLoop
case "n":
restoreSCB = false
break warningLoop
}
}
}

if restoreSCB {
// Proceed with SCB recovery.
fmt.Println("Static Channel Backup (SCB) recovery selected!")
if backups != nil {
switch {
case backups.GetChanBackups() != nil:
singleBackup := backups.GetChanBackups()
chanBackups = &lnrpc.ChanBackupSnapshot{
SingleChanBackups: singleBackup,
}

case backups.GetMultiChanBackup() != nil:
multiBackup := backups.GetMultiChanBackup()
chanBackups = &lnrpc.ChanBackupSnapshot{
MultiChanBackup: &lnrpc.MultiChanBackup{
MultiChanBackup: multiBackup,
},
}
}
}

}

walletPassword, err := capturePassword(
"Input wallet password: ", false, walletunlocker.ValidatePassword,
)
Expand Down Expand Up @@ -1569,34 +1664,6 @@ mnemonicCheck:
fmt.Println("\n!!!YOU MUST WRITE DOWN THIS SEED TO BE ABLE TO " +
"RESTORE THE WALLET!!!")

// We'll also check to see if they provided any static channel backups,
// if so, then we'll also tack these onto the final init wallet request.
// We can ignore the errMissingChanBackup error as it's an optional
// field.
backups, err := parseChanBackups(ctx)
if err != nil && err != errMissingChanBackup {
return fmt.Errorf("unable to parse chan backups: %v", err)
}

var chanBackups *lnrpc.ChanBackupSnapshot
if backups != nil {
switch {
case backups.GetChanBackups() != nil:
singleBackup := backups.GetChanBackups()
chanBackups = &lnrpc.ChanBackupSnapshot{
SingleChanBackups: singleBackup,
}

case backups.GetMultiChanBackup() != nil:
multiBackup := backups.GetMultiChanBackup()
chanBackups = &lnrpc.ChanBackupSnapshot{
MultiChanBackup: &lnrpc.MultiChanBackup{
MultiChanBackup: multiBackup,
},
}
}
}

// With either the user's prior cipher seed, or a newly generated one,
// we'll go ahead and initialize the wallet.
req := &lnrpc.InitWalletRequest{
Expand Down Expand Up @@ -2404,7 +2471,7 @@ var sendToRouteCommand = cli.Command{
Send a payment over Lightning using a specific route. One must specify
the route to attempt and the payment hash. This command can even
be chained with the response to queryroutes or buildroute. This command
can be used to implement channel rebalancing by crafting a self-route,
can be used to implement channel rebalancing by crafting a self-route,
or even atomic swaps using a self-route that crosses multiple chains.
There are three ways to specify a route:
Expand Down

0 comments on commit 0b1d149

Please sign in to comment.