How to fund a Lightning channel from your Electrum wallet

Aug 27, 2019

c-lightning v0.7.0 from June 2019 ("The Unfailing Twitter Consensus Algorithm") introduced new commands that will improve your Lightning experience the most as soon as you learn how to use them: fundchannel_start, fundchannel_complete and fundchannel_cancel.

Now instead of having to send a transaction to your c-lightning internal on-chain wallet and then call the singleton fundchannel you can use fundchannel_start and fundchannel_complete to fund a channel from an external wallet and thus economize on transaction fees. Here we will show how to do it from Electrum.

The process is not simple: you must pay attention to not lose funds. The rule to remember is: never send/publish/broadcast the transaction from your Electrum wallet until you call fundchannel_complete and get a success response.

First step: get funding address

The first thing to do is always to connect from your c-lightning node to the node with which you want to open a channel:

~> lightning-cli connect 025a14b8ed40583d67aec92da19453e0b2d1fbbf75f96f85d3dd0ff61a51ee0490
  "id": "025a14b8ed40583d67aec92da19453e0b2d1fbbf75f96f85d3dd0ff61a51ee0490"

(Tip of the day: normally you don't have to specify an IP. Public node IP addresses are known from the gossip messages.)

Now that you're connected you can tell the other node you're intending to fund a channel with it, some magic will happen and you'll both come up with an address for the 2-of-2 multisig address that will hold the channel funds.

Don't send funds to that address!

~> lightning-cli fundchannel_start 025a14b8ed40583d67aec92da19453e0b2d1fbbf75f96f85d3dd0ff61a51ee0490 300000
  "funding_address": "bc1q757wc0dwl7kmkvwuus8p3ycf9eynexalt2yr2xee8gp8l9c2r42qshezsq"

Here we are telling the other node we're going to fund a channel with 300000 satoshis. We get the address back so we can build the funding transaction. If we send the transaction now, however, the other node can have our funds permanently stuck on that 2-of-2 UTXO (as they control one key and it's needed to spend the funds). So before we send the funds we must be sure our node has received a presigned transaction from the other node that spends the funds to ourselves, which we can use to force-close the channel if we want.

If none of the above makes sense to you, that's because you are a normal person. Try reading this.

Second step: prepare the funding transaction

On Electrum, use the "Send" function to create a transaction to the address given above, then click on "Preview" to see the transaction id, but DON'T CLICK on "Send" or "Broadcast".

Third step: tell others about the funding transaction id

Now that you have a transaction id the Lightning nodes can use it to do magic stuff they need (build chains of unpublished transactions) even before it's included in a block, because that id won't ever change (you know, this is what the entire talk about "transaction malleability" means).

~> lightning-cli fundchannel_complete 025a14b8ed40583d67aec92da19453e0b2d1fbbf75f96f85d3dd0ff61a51ee0490 2a1d2dff70d60c8686fb5678301a0af63a5ba5d4787945115e68f7b94b810951 1
  "channel_id": "5109814bb9f7685e11457978d4a55b3af60a1a307856fb86860cd670ff2d1d2b",
  "commitments\_secured": true

The command above takes the target node id (025a...), as the first command did, then the transaction id (2a1d...), taken from the Electrum "Preview" window, then the output index (1). This last one is also taken from the Electrum "Preview" window, but maybe a little more tricky: can you see the "Outputs" section in that window? There are two outputs listed there, one is for the "change" address, the part of the output that returns to the control of the Electrum wallet, the other is the address to which we are actually trying to send money, which is the Lightning channel funding address (bc1q757w...), since that is the second on that window (not necessarily will be for you) and the index starts at 0 (zero), then the output index we need to pass to c-lightning is 1 (one).

Fourth and last step: broadcast the transaction

Now that we've read "commitments_secured": true we're safe. Our node already has the presigned transaction from the other node so we can force-close the channel at our will and our funds won't get stuck forever (assuming we've passed the correct transaction id and output index).

We can now broadcast the transaction on Electrum (click "Sign" to sign it first if you haven't already, then "Broadcast" -- of course if you've read this up to this point you already know how to use Electrum, but that doesn't matter).

You don't have to do anything after that. As soon as the transaction is seen on the blockchain your node and the other node will react accordingly and you'll have a brand new live Lightning channel.