Lightning Integration in Electrum

Thomas Voegtlin

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

Slides: https://electrum.org/talks/lightning/tlc.html

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 channel_db.py | wc -l
4 7844

Presenter Notes

Code overview

 1 ~/electrum/electrum$ ls *.py
 2 address_synchronizer.py  json_db.py       paymentrequest_pb2.py
 3 base_crash_reporter.py   keystore.py      paymentrequest.py
 4 base_wizard.py           lnaddr.py        pem.py
 5 bip32.py                 lnchannel.py     plot.py
 6 bitcoin.py               lnhtlc.py        plugin.py
 7 blockchain.py            lnmsg.py         qrscanner.py
 8 channel_db.py            lnonion.py       ripemd.py
 9 coinchooser.py           lnpeer.py        rsakey.py
10 commands.py              lnrouter.py      segwit_addr.py
11 constants.py             lnsweep.py       simple_config.py
12 contacts.py              lntransport.py   sql_db.py
13 crypto.py                lnutil.py        storage.py
14 daemon.py                lnverifier.py    synchronizer.py
15 dnssec.py                lnwatcher.py     transaction.py
16 ecc_fast.py              lnworker.py      util.py
17 ecc.py                   logging.py       verifier.py
18 exchange_rate.py         mnemonic.py      version.py
19 i18n.py                  msqr.py          wallet.py
20 __init__.py              network.py       x509.py
21 interface.py             old_mnemonic.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

Watchtower

  • 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

CLI

setup Regtest

1 ./electrum/tests/regtest/start_bitcoind.sh
2 ./electrum/tests/regtest/start_electrumx.sh
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 127.0.0.1:51001:t -d
2 $bob load_wallet
3 $alice daemon --server 127.0.0.1:51001:t -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 127.0.0.1
4 $carol setconfig -o watchtower_port 12345
5 $alice setconfig -o watchtower_url http://127.0.0.1:12345
6 $carol daemon --server 127.0.0.1:51001:t -d

Presenter Notes