github.com/decred/dcrlnd@v0.7.6/docs/recovery.md (about) 1 # Table of Contents 2 3 * [Recovering Funds From `lnd` (funds are safu!)](#recovering-funds-from-lnd-funds-are-safu) 4 * [On-Chain Recovery](#on-chain-recovery) 5 * [24-word Cipher Seeds](#24-word-cipher-seeds) 6 * [Wallet and Seed Passphrases](#wallet-and-seed-passphrases) 7 * [Starting On-Chain Recovery](#starting-on-chain-recovery) 8 * [Forced In-Place Rescan](#forced-in-place-rescan) 9 * [Off-Chain Recovery](#off-chain-recovery) 10 * [Obtaining SCBs](#obtaining-scbs) 11 * [On-Disk `channel.backup`](#on-disk-channelbackup) 12 * [Using the `ExportChanBackup` RPC](#using-the-exportchanbackup-rpc) 13 * [Streaming Updates via `SubscribeChannelBackups`.](#streaming-updates-via-subscribechannelbackups) 14 * [Recovering Using SCBs](#recovering-using-scbs) 15 16 # Recovering Funds From `lnd` (funds are safu!) 17 18 In this document, we'll go over the various built-in mechanisms for recovering 19 funds from `lnd` due to any sort of data loss, or malfunction. Coins in `lnd` 20 can exist in one of two pools: on-chain or off-chain. On-chain funds are 21 outputs under the control of `lnd` that can be spent immediately, and without 22 any auxiliary data. Off-chain funds on the other hand exist within a 2-of-2 23 multi-sig output typically referred to as a payment channel. Depending on the 24 exact nature of operation of a given `lnd` node, one of these pools of funds 25 may be empty. 26 27 Fund recovery for `lnd` will require two pieces of data: 28 1. Your 24-word cipher seed 29 2. Your encrypted Static Channel Backup file (or the raw data) 30 31 If one is only attempting to recover _on chain_ funds, then only the first item 32 is required. 33 34 The SCB file is encrypted using a key _derived_ from the user's seed. As a 35 result, it cannot be used in isolation. 36 37 ## On-Chain Recovery 38 39 ### 24-word Cipher Seeds 40 41 When a new `lnd` node is created, it's given a 24-word seed phrase, called an 42 [`cipher seed`](https://github.com/decred/dcrlnd/tree/master/aezeed). 43 The two seed formats look similar, but the only commonality they share are 44 using the same default English dictionary. A valid seed phrase obtained over 45 the CLI `lncli create` command looks something like: 46 ``` 47 !!!YOU MUST WRITE DOWN THIS SEED TO BE ABLE TO RESTORE THE WALLET!!! 48 49 ---------------BEGIN LND CIPHER SEED--------------- 50 1. ability 2. noise 3. lift 4. document 51 5. certain 6. month 7. shoot 8. perfect 52 9. matrix 10. mango 11. excess 12. turkey 53 13. river 14. pitch 15. fluid 16. rack 54 17. drill 18. text 19. buddy 20. pool 55 21. soul 22. fatal 23. ship 24. jelly 56 ---------------END LND CIPHER SEED----------------- 57 58 !!!YOU MUST WRITE DOWN THIS SEED TO BE ABLE TO RESTORE THE WALLET!!! 59 ``` 60 61 ### Wallet and Seed Passphrases 62 63 During the creation process, users are first prompted to enter a **wallet 64 password**: 65 ``` 66 Input wallet password: 67 Confirm wallet password: 68 ``` 69 70 This password is used to _encrypt_ the wallet on disk, which includes any 71 derived master private keys or public key data. 72 73 Users can also _optionally_ enter a second passphrase which we call the _cipher 74 seed passphrase_: 75 ``` 76 Your cipher seed can optionally be encrypted. 77 Input your passphrase if you wish to encrypt it (or press enter to proceed without a cipher seed passphrase): 78 ``` 79 80 If specified, then this will be used to encrypt the cipher seed itself. The 81 cipher seed format is unique in that the 24-word phrase is actually a 82 _ciphertext_. As a result, there's no standard word list as any arbitrary 83 encoding can be used. If a passphrase is specified, then the cipher seed you 84 write down is actually an _encryption_ of the entropy used to generate the BIP 85 32 root key for the wallet. Unlike a BIP 39 24-word phrase, the cipher seed is 86 able to _detect_ incorrect passphrase. BIP 39 on the other hand, will instead 87 silently decrypt to a new (likely empty) wallet. 88 89 ### Starting On-Chain Recovery 90 91 The initial entry point to trigger recovery of on-chain funds in the command 92 line is the `lncli create` command. 93 ``` 94 ⛰ lncli create 95 ``` 96 97 Next, one can enter a _new_ wallet password to encrypt any newly derived keys 98 as a result of the recovery process. 99 ``` 100 Input wallet password: 101 Confirm wallet password: 102 ``` 103 104 Once a new wallet password has been obtained, the user will be prompted for 105 their _existing_ cipher seed: 106 ``` 107 Input your 24-word mnemonic separated by spaces: ability noise lift document certain month shoot perfect matrix mango excess turkey river pitch fluid rack drill text buddy pool soul fatal ship jelly 108 ``` 109 110 If a _cipher seed passphrase_ was used when the seed was created, it MUST be entered now: 111 ``` 112 Input your cipher seed passphrase (press enter if your seed doesn't have a passphrase): 113 ``` 114 115 Finally, the user has an option to choose a _recovery window_: 116 ``` 117 Input an optional address look-ahead used to scan for used keys (default 2500): 118 ``` 119 120 The recovery window is a metric that the on-chain rescanner will use to 121 determine when all the "used" addresses have been found. If the recovery window 122 is two, lnd will fail to find funds in any addresses generated after the point 123 in which two consecutive addresses were generated but never used. If an `lnd` 124 on-chain wallet was extensively used, then users may want to _increase_ the 125 default value. 126 127 If all the information provided was valid, then you'll be presented with the 128 seed again: 129 ``` 130 131 !!!YOU MUST WRITE DOWN THIS SEED TO BE ABLE TO RESTORE THE WALLET!!! 132 133 ---------------BEGIN LND CIPHER SEED--------------- 134 1. ability 2. noise 3. lift 4. document 135 5. certain 6. month 7. shoot 8. perfect 136 9. matrix 10. mango 11. excess 12. turkey 137 13. river 14. pitch 15. fluid 16. rack 138 17. drill 18. text 19. buddy 20. pool 139 21. soul 22. fatal 23. ship 24. jelly 140 ---------------END LND CIPHER SEED----------------- 141 142 !!!YOU MUST WRITE DOWN THIS SEED TO BE ABLE TO RESTORE THE WALLET!!! 143 144 lnd successfully initialized! 145 ``` 146 147 In `lnd`'s logs, you should see something along the lines of (irrelevant lines skipped): 148 ``` 149 [INF] LNWL: Opened wallet 150 [INF] LTND: Wallet recovery mode enabled with address lookahead of 2500 addresses 151 [INF] LNWL: RECOVERY MODE ENABLED -- rescanning for used addresses with recovery_window=2500 152 [INF] CHBU: Updating backup file at test_lnd3/data/chain/bitcoin/simnet/channel.backup 153 [INF] CHBU: Swapping old multi backup file from test_lnd3/data/chain/bitcoin/simnet/temp-dont-use.backup to test_lnd3/data/chain/bitcoin/simnet/channel.backup 154 [INF] LNWL: Seed birthday surpassed, starting recovery of wallet from height=748 hash=3032830c812a4a6ea305d8ead13b52e9e69d6400ff3c997970b6f76fbc770920 with recovery-window=2500 155 [INF] LNWL: Scanning 1 blocks for recoverable addresses 156 [INF] LNWL: Recovered addresses from blocks 748-748 157 [INF] LNWL: Started rescan from block 3032830c812a4a6ea305d8ead13b52e9e69d6400ff3c997970b6f76fbc770920 (height 748) for 800 addresses 158 [INF] LNWL: Catching up block hashes to height 748, this might take a while 159 [INF] LNWL: Done catching up block hashes 160 [INF] LNWL: Finished rescan for 800 addresses (synced to block 3032830c812a4a6ea305d8ead13b52e9e69d6400ff3c997970b6f76fbc770920, height 748) 161 ``` 162 163 That final line indicates the rescan is complete! If not all funds have 164 appeared, then the user may need to _repeat_ the process with a higher recovery 165 window. Depending on how old the wallet is (the cipher seed stores the wallet's 166 birthday!) and how many addresses were used, the rescan may take anywhere from 167 a few minutes to a few hours. To track the recovery progress, one can use the 168 command `lncli getrecoveryinfo`. When finished, the following is returned, 169 ``` 170 { 171 "recovery_mode": true, 172 "recovery_finished": true, 173 "progress": 1 174 } 175 ``` 176 177 If the rescan wasn't able to complete fully (`lnd` was shutdown for example), 178 then from `lncli unlock`, it's possible to _restart_ the rescan from where it 179 left off with the `--recovery-window` argument: 180 ``` 181 ⛰ lncli unlock --recovery_window=2500 182 ``` 183 184 Note that if this argument is not specified, then the wallet will not 185 _re-enter_ the recovery mode and may miss funds during the portion of the 186 rescan. 187 188 ### Forced In-Place Rescan 189 190 The recovery methods described above assume a clean slate for a node, so 191 there's no existing UTXO or key data in the node's database. However, there're 192 times when an _existing_ node may want to _manually_ rescan the chain. We have 193 a tool for that! The tool is called 194 [`dropwtxmgr`](https://github.com/btcsuite/btcwallet/tree/master/cmd/dropwtxmgr). 195 It can be installed with the following command: 196 ``` 197 ⛰ go get -v -u github.com/btcsuite/btcwallet/cmd/dropwtxmgr 198 ``` 199 200 The `dropwtxmgr` tool will _reset_ the best synced height of the wallet back to 201 its birthday, or genesis if the birthday isn't known (for some older wallets). 202 In order to run the tool, you must first **shutdown `lnd`**. Once `lnd` is 203 shutdown, the rescan can be initiated with the following commands: 204 ``` 205 ⛰ cp $HOME/.lnd/data/chain/bitcoin/mainnet/wallet.db $HOME/wallet.db # Copy the existing databse just in case! 206 ⛰ dropwtxmgr --db=$HOME/.lnd/data/chain/bitcoin/mainnet/wallet.db 207 ``` 208 209 Once the above command returns (if it hangs for a while, then `lnd` may not 210 actually be shutdown, so double check!), `lnd` can be restarted. After it's 211 restarted, then the wallet should being rescanning. An entry resembling the 212 following will show up in the logs once it's complete: 213 ``` 214 [INF] LNWL: Finished rescan for 800 addresses (synced to block 3032830c812a4a6ea305d8ead13b52e9e69d6400ff3c997970b6f76fbc770920, height 748) 215 ``` 216 217 ## Off-Chain Recovery 218 219 After version `v0.6-beta` of `lnd`, the daemon now ships with a new feature 220 called Static Channel Backups (SCBs). We call these _static_ as they only need 221 to be obtained _once_: when the channel is created. From there on, a backup is 222 good until the channel is closed. The backup contains all the information we 223 need to initiate the Data Loss Protection (DLP) feature in the protocol, which 224 ultimately leads to us recovering the funds from the channel _on-chain_. This 225 is a foolproof _safe_ backup mechanism. 226 227 We say _safe_, as care has been taken to ensure that there are no foot guns in 228 this method of backing up channels, vs doing things like `rsync`ing or copying 229 the `channel.db` file periodically. Those methods can be dangerous as one never 230 knows if they have the latest state of a channel or not. Instead, we aim to 231 provide a simple, safe method to allow users to recover the settled funds in 232 their channels in the case of partial or complete data loss. The backups 233 themselves are encrypted using a key derived from the user's seed, this way we 234 protect privacy of the users channels in the back up state, and ensure that a 235 random node can't attempt to import another user's channels. 236 237 Given a valid SCB, the user will be able to recover funds that are fully 238 settled within their channels. By "fully settled" we mean funds that are in the 239 base commitment outputs, and not HTLCs. We can only restore these funds as 240 right after the channel is created, as we have all the data required to make a 241 backup, but lack information about the future HTLCs that the channel will 242 process. 243 244 ### Obtaining SCBs 245 246 #### On-Disk `channel.backup` 247 248 There are multiple ways of obtaining SCBs from `lnd`. The most commonly used 249 method will likely be via the `channel.backup` file that's stored on-disk 250 alongside the rest of the chain data. This is a special file that contains SCB 251 entries for _all_ currently open channels. Each time a channel is opened or 252 closed, this file is updated on disk in a safe manner (atomic file rename). As 253 a result, unlike the `channel.db` file, it's _always_ safe to copy this file 254 for backup at ones desired location. The default location on Linux is: 255 ``` 256 ~/.lnd/data/chain/bitcoin/mainnet/channel.backup 257 ``` 258 259 An example of using file system level notification to [copy the backup to a 260 distinct volume/partition/drive can be found 261 here](https://gist.github.com/alexbosworth/2c5e185aedbdac45a03655b709e255a3). 262 263 #### Using the `ExportChanBackup` RPC 264 265 Another way to obtain SCBS for all or a target channel is via the new 266 `exportchanbackup` `lncli` command: 267 ``` 268 ⛰ lncli --simnet exportchanbackup --chan_point=29be6d259dc71ebdf0a3a0e83b240eda78f9023d8aeaae13c89250c7e59467d5:0 269 { 270 "chan_point": "29be6d259dc71ebdf0a3a0e83b240eda78f9023d8aeaae13c89250c7e59467d5:0", 271 "chan_backup": "02e7b423c8cf11038354732e9696caff9d5ac9720440f70a50ca2b9fcef5d873c8e64d53bdadfe208a86c96c7f31dc4eb370a02631bb02dce6611c435753a0c1f86c9f5b99006457f0dc7ee4a1c19e0d31a1036941d65717a50136c877d66ec80bb8f3e67cee8d9a5cb3f4081c3817cd830a8d0cf851c1f1e03fee35d790e42d98df5b24e07e6d9d9a46a16352e9b44ad412571c903a532017a5bc1ffe1369c123e1e17e1e4d52cc32329aa205d73d57f846389a6e446f612eeb2dcc346e4590f59a4c533f216ee44f09c1d2298b7d6c" 272 } 273 274 ⛰ lncli --simnet exportchanbackup --all 275 { 276 "chan_points": [ 277 "29be6d259dc71ebdf0a3a0e83b240eda78f9023d8aeaae13c89250c7e59467d5:0" 278 ], 279 "multi_chan_backup": "fd73e992e5133aa085c8e45548e0189c411c8cfe42e902b0ee2dec528a18fb472c3375447868ffced0d4812125e4361d667b7e6a18b2357643e09bbe7e9110c6b28d74f4f55e7c29e92419b52509e5c367cf2d977b670a2ff7560f5fe24021d246abe30542e6c6e3aa52f903453c3a2389af918249dbdb5f1199aaecf4931c0366592165b10bdd58eaf706d6df02a39d9323a0c65260ffcc84776f2705e4942d89e4dbefa11c693027002c35582d56e295dcf74d27e90873699657337696b32c05c8014911a7ec8eb03bdbe526fe658be8abdf50ab12c4fec9ddeefc489cf817721c8e541d28fbe71e32137b5ea066a9f4e19814deedeb360def90eff2965570aab5fedd0ebfcd783ce3289360953680ac084b2e988c9cbd0912da400861467d7bb5ad4b42a95c2d541653e805cbfc84da401baf096fba43300358421ae1b43fd25f3289c8c73489977592f75bc9f73781f41718a752ab325b70c8eb2011c5d979f6efc7a76e16492566e43d94dbd42698eb06ff8ad4fd3f2baabafded" 280 } 281 282 ⛰ lncli --network=simnet exportchanbackup --all --output_file=channel.backup 283 ``` 284 285 As shown above, a user can either: specify a specific channel to backup, backup 286 all existing channels, or backup directly to an on-disk file. All backups use 287 the same format. 288 289 #### Streaming Updates via `SubscribeChannelBackups` 290 291 Using the gRPC interace directly, [a new call: 292 `SubscribeChannelBackups`](https://api.lightning.community/#subscribechannelbackups). 293 This call allows users to receive a new notification each time the underlying 294 SCB state changes. This can be used to implement more complex backup 295 schemes, compared to the file system notification based approach. 296 297 ### Recovering Using SCBs 298 299 If a node is being created from scratch, then it's possible to pass in an 300 existing SCB using the `lncli create` or `lncli unlock` commands: 301 ```shell 302 ⛰ lncli create -multi_file=channel.backup 303 ``` 304 305 Alternatively, the `restorechanbackup` command can be used if `lnd` has already 306 been created at the time of SCB restoration: 307 ``` 308 ⛰ lncli restorechanbackup -h 309 NAME: 310 lncli restorechanbackup - Restore an existing single or multi-channel static channel backup 311 312 USAGE: 313 lncli restorechanbackup [command options] [--single_backup] [--multi_backup] [--multi_file=] 314 315 CATEGORY: 316 Channels 317 318 DESCRIPTION: 319 320 Allows a user to restore a Static Channel Backup (SCB) that was 321 obtained either via the exportchanbackup command, or from lnd's 322 automatically managed channel.backup file. This command should be used 323 if a user is attempting to restore a channel due to data loss on a 324 running node restored with the same seed as the node that created the 325 channel. If successful, this command will allows the user to recover 326 the settled funds stored in the recovered channels. 327 328 The command will accept backups in one of three forms: 329 330 * A single channel packed SCB, which can be obtained from 331 exportchanbackup. This should be passed in hex encoded format. 332 333 * A packed multi-channel SCB, which couples several individual 334 static channel backups in single blob. 335 336 * A file path which points to a packed multi-channel backup within a 337 file, using the same format that lnd does in its channel.backup 338 file. 339 340 341 OPTIONS: 342 --single_backup value a hex encoded single channel backup obtained from exportchanbackup 343 --multi_backup value a hex encoded multi-channel backup obtained from exportchanbackup 344 --multi_file value the path to a multi-channel back up file 345 ``` 346 347 Once the process has been initiated, `lnd` will proceed to: 348 349 1. Given the set of channels to recover, the server will then will insert a 350 series of "channel shells" into the database. These contain only the 351 information required to initiate the DLP (data loss protection) protocol 352 and nothing more. As a result, they're marked as "recovered" channels in 353 the database, and we'll disallow trying to use them for any other process. 354 2. Once the channel shell is recovered, the 355 [chanbackup](https://github.com/decred/dcrlnd/tree/master/chanbackup) 356 package will attempt to insert a LinkNode that contains all prior 357 addresses that we were able to reach the peer at. During the process, 358 we'll also insert the edge for that channel (only in the outgoing 359 direction) into the database as well. 360 3. lnd will then start up, and as usual attempt to establish connections to 361 all peers that we have channels open with. If `lnd` is already running, 362 then a new persistent connection attempt will be initiated. 363 4. Once we connect with a peer, we'll then initiate the DLP protocol. The 364 remote peer will discover that we've lost data, and then immediately force 365 close their channel. Before they do though, they'll send over the channel 366 reestablishment handshake message which contains the unrevoked commitment 367 point which we need to derive keys (will be fixed in 368 BOLT 1.1 by making the key static) to sweep our funds. 369 5. Once the commitment transaction confirms, given information within the SCB 370 we'll re-derive all keys we need, and then sweep the funds.