Lightning Integration in Electrum

Thomas Voegtlin

PGP: 6694 D8DE 7BE8 EE56 31BE D950 2BD5 824B 7F94 70E6


Presenter Notes

Electrum (since 2011)

  • Fast and light: Client-server architecture
  • You own the keys.
  • Deterministic
  • SPV: Do not trust, verify

what made the success of Electrum is the ability to restore from seed

Presenter Notes

Light clients are needed

  • Bitcoin's hashrate depends on the price.
  • A high price requires a large userbase.
  • Users want instant access

Our goal: make light clients as safe as possible

  • Compete with websites (custodian or non-custodian)
  • Same user experience, risk reduction

Presenter Notes

Lightning wallets

New constraints

  • Channels cannot be restored from seed.
  • Routing, liquidity
  • You need to watch your channels
  • You need to be online to receive payments

Custodian wallets:

  • can address all that
  • we have to compete with that

not saying it's bad, but bitcoin cannot exist if all users use these types of services. it makes bitcoin unstable, the consequence of hacks are bigger

Presenter Notes

Implementing Lightning in Python

  • Pure python implementation (uses libsecp256k1)
  • Uses asyncio
  • Development started 18 months ago.
  • Lightning branch was merged 1 month ago

Approx. 25% of Electrum's core code is lightning:

1 ~/electrum/electrum$ cat *.py | wc -l
2 31081
3 ~/electrum/electrum$ cat ln*.py | wc -l
4 7844

Presenter Notes

Code overview

 1 ~/electrum/electrum$ ls *.py

Presenter Notes

Model Overview

similar to eclair

Electrum servers

  • Are used only for on-chain requests
  • Can lie by omisssion
  • Cannot make things up (SPV)

Lightning protocol

  • One node ID per wallet
  • Private channels, no routing
  • Gossip is independent from wallet


  • We cannot expect users to be online
  • Delegate breach remedy to 3rd party
  • Uses pre-signed transactions

Presenter Notes

Desktop GUI: History

Presenter Notes

Desktop GUI: Send

Presenter Notes

Desktop GUI: Receive

Presenter Notes

Desktop GUI: Preferences

Presenter Notes

Desktop GUI: Channels

Presenter Notes

Mobile GUI: History

Presenter Notes

Mobile GUI: Send

Presenter Notes

Mobile GUI: Receive

Presenter Notes

Mobile GUI: Channels

Presenter Notes

Watchtowers: models and incentives

Public WT

  • Does not know who you are until there is a breach
  • Must store data forever, or pretend to do so
  • Can be paid on breach, or using anonymous tokens

Private WT

  • Knows about your channels
  • can delete data
  • Could charge a monthly subscription

Presenter Notes

Redeeming CTX outputs

Our latest CTX

- to_local : after CSV
- to_remote : for them
- sent HTLCs : htlctx -> after CSV
- received HTLCs : if preimage then htlctx -> after CSV

Their latest CTX

- to_local : for them
- to_remote : for us
- sent HTLCs  : we need preimagee
- received HTLCs : after CLTV

Their breach CTX

- to_local : before CSV
- to_remote : for us
- sent HTLCs:
    - unspent : for us
    - spent : before CSV
- received HTLCs:
    - unspent : for us
    - spent : before CSV

Presenter Notes


setup Regtest

1 ./electrum/tests/regtest/
2 ./electrum/tests/regtest/
3 bitcoin_cli="bitcoin-cli -rpcuser=doggman -rpcpassword=donkey -rpcport=18554 -regtest"

setup Alice and Bob

 1 alice="./run_electrum --regtest -D /tmp/alice"
 2 bob="./run_electrum --regtest -D /tmp/bob"
 3 $alice -o create
 4 $alice -o init_lightning
 5 $bob -o create
 6 $bob -o init_lightning
 7 $bob -o setconfig lightning_listen localhost:9735
 8 # fund Bob and mine a block
 9 $bitcoin_cli sendtoaddress $($bob -o getunusedaddress) 1
10 $bitcoin_cli generatetoaddress 1 $($bitcoin_cli getnewaddress)

Start daemons

1 $bob daemon --server -d
2 $bob load_wallet
3 $alice daemon --server -d
4 $alice load_wallet

Presenter Notes

CLI: Alice Pays Bob

Alice opens channel

1 bob_node=$($bob nodeid)
2 channel=$($alice open_channel $bob_node 0.15)
3 # mine a few blocks
4 $bitcoin_cli generatetoaddress 3 $($bitcoin_cli getnewaddress)

Alice pays Bob

1 request=$($bob add_lightning_request 0.01 -m "test")
2 $alice lnpay $request

Presenter Notes

Breach Remedy

Alice saves CTX

1 ctx=$($alice get_channel_ctx $channel | jq '.hex' | tr -d '"')

Alice pays Bob again

1 request2=$($alice add_lightning_request 0.01 -m "test2")
2 $alice lnpay $request2

Alice broadcasts old ctx

1 $bitcoin_cli sendrawtransaction $ctx

What will happen next?

1 python3 -m unittest electrum.tests.regtest.TestLightning.test_breach

Presenter Notes

CLI: Watchtower setup

1 carol="./run_electrum --regtest -D /tmp/carol"
2 $carol -o create
3 $carol setconfig -o watchtower_host
4 $carol setconfig -o watchtower_port 12345
5 $alice setconfig -o watchtower_url
6 $carol daemon --server -d

Presenter Notes