APPENDIX A

Books and Papers

Books

Economics

Economics in One Lesson, Henry Hazlitt, ISBN-13: 978-0517548233

Basic Economics, Thomas Sowell, ISBN-13: 978-­0465060733

Crashed: How a Decade of Financial Crises Changed the World, Adam Tooze, ISBN-13: 978-­0670024933 The Ascent of Money, Niall Ferguson, ISBN-13: 978-­0143116172

The Ethics of Money Production, Jörg Guido Hülsmann, ASIN: B003NX6Z3W

Denationalisation of Money, F. A. Hayek, ASIN: B005DTKORM Society

The Sovereign Individual, James Dale Davidson and Lord William Rees-Mogg, ISBN-13: 978-0684832722­

Exit, Voice, and Loyalty, Albert O. Hirschman, ASIN: 0674276604

© Harris Brakmić 2019 349 H. Brakmić, and on Raspberry Pi, https://doi.org/10.1007/978-1-4842-5522-3 Appendix A Books and Papers

Technology

Mastering Bitcoin 2nd Edition, , ASIN: B071K7FCD4

Programming Bitcoin, Jimmy Song, ISBN-13: 978-­1492031499

A Dissection of Bitcoin, Paul Huang, ASIN: B0198LXI5K Bitcoin Internals, Chris Clark, ASIN: B00DG8EPT0 Applied , Bruce Schneier, ISBN-13: 978-1119096726

Essays and Papers

Bitcoin: A Peer-to-Peer Electronic Cash System, , https://bitcoin.org/en/bitcoin-paper Bitcoin’s Academic Pedigree, Arvind Narayanan and Jeremy Clark, https://queue.acm.org/detail.cfm?id=3136559 Proof-of-Work is a Decentralized Clock, Gregory Trubetskoy, https://grisha.org/blog/2018/01/23/explaining-proof-­ of-work/ Shelling Out: The Origins of Money, Nick Szabo, https://nakamoto institute.org/shelling-out/ Money, , and social scalability, Nick Szabo, https://unenumerated.blogspot.com/2017/02/money-blockchains- and-social-scalability.html The many traditions of non-governmental money (part I), Nick Szabo, ­https://unenumerated.blogspot.com/2018/03/the-many-­ traditions-of-non-governmental.html Smart Contracts: Building Blocks for Digital Markets, Nick Szabo, https://web.archive.org/web/20151222144315/szabo.best.vwh.net/­ smart_contracts_2.html

350 APPENDIX B

Problems and Solutions

Bitcoin Core Wallet

Problem: The configuration script could throw the error: Configure could not find version of library. Solution: Append flag --with-boost-libdir=/usr/lib/x86_64-­linux-­ gnu to the configure script, for example:

./configure --with-boost-libdir=/usr/lib/x86_64-linux-gnu [... other flags follow..] On 32-bit Raspbian you should use /usr/lib/arm-linux-­ gnueabihf instead.

LND

Problem: At start, LND throws the error: cannot unmarshal object into Go struct field GetBlockChainInfoResult.softforks Solution: Check out one of the v0.18.x branches from bitcoin’s repository (e.g., git checkout v0.18.1) and recompile and reinstall your local Bitcoin version. As of now (September 2019), LND only supports v0.18.x versions of Bitcoin Core reference client.

© Harris Brakmić 2019 351 H. Brakmić, Bitcoin and Lightning Network on Raspberry Pi, https://doi.org/10.1007/978-1-4842-5522-3 Appendix B Problems and Solutions

Problem: How to create backups of channels and funds located in them? Solution: Since v0.6 LND offers the static channel backups scheme. There is a file in .lnd/data/chain/bitcoin/testnet (mainnet), called channel.backup, that is used to restore the channels. This file is encrypted with your wallet seed. The official guide can be found here: https://github.com/ lightningnetwork/lnd/blob/master/docs/recovery.md However, take into account that by default channel.backup is located on the same disk where LND runs, so it’s always a good strategy to copy it to another disk. Here is a script that automatizes this task: https://gist.github.com/alexbosworth/2c5e185aedbdac45a03655b709 e255a3

More info on Static Channel Backups: https://github.com/ lightningnetwork/lnd/releases/tag/v0.6-beta

c-lightning

Problem: How to create backups of channels and funds located in them? Solution: Copy hsm_secret from .lightning to a safe place first. Later, in case your node has crashed, you should follow these instructions to get your funds back:

• Start lightningd --port=0 (this disables any communication)

• Run lightning-cli newaddr a few times • Stop lightning daemon with lightning-cli stop

352 Appendix B Problems and Solutions

• Run sqlite3 with these options

sqlite3 .lightning/lightningd.sqlite3 "UPDATE vars SET val= 500000 WHERE name='last_processed_block';"

• Start lightningd --port=0 again

• Initiate fund recovery with lightning-cli withdraw all It will now depend on your peers how long it will take, until all channels are closed and your funds available again. Meanwhile you will have to use another c-lightning instance as this node wouldn’t be available for any new channels or any other operations.

Important take care of not reusing or copying the lightnind. sqlite3 database file. Publishing an old state in Lightning Network will provoke revocations of transactions, which ultimately lead to loss of funds.

Networking Problems

Problem: The UFW (firewall) is blocking UPnPC port mapping. Solution: You should allow UDP packets for UPnPC inside your local network. Add this rule to your node’s UFW filter rules (replace the IP range with your own):

sudo ufw allow from 192.168.0.0/24 port 1900 to any proto udp

353 Index

A blockchain explorer, 255 compilation, 120–122 Addresses configuration file, 165–169 Base58, 51, 56 consensus rules, 7, 8 Bech32-type, 55 JavaScript language, 233 Bitcoin address, generation, 53 asynchronous command getnewaddress, 54 functions, 235, 236 command line tool bitcoin-cli, 54 Bech32, 246–248 double-SHA256 function, 52 bitcoin-core, 234 dumpwallet, 55 bitcoinlib-js package, 238 key types, 54 multisig script, 238, 240 P2SH-type, 54 P2PKH, 238 public key, 52 P2SH, 238 testnet and regtest types, 56 package.json file, 233 walletpassphrase, 55 scriptPubKey, 244 Asymmetric cryptography, 60, 74 SegWit address, 241–244 transaction data, 237 transaction malleability B problem, 244 Back-linked list of blocks, 17 WIF keys, 249 Base58Check format, 52 JSON format, 168 Basis of lightning technology monetary systems, 9 (BOLT), 267 money, 3–6 Bech32-type address, 55 public blockchain, 11–13 Bitcoin raw transaction access, 169 createrawtransaction bitcoin-rpc-explorer, 256, 258 command, 179 blockchain (see Blockchain) getblock command, 185

© Harris Brakmić 2019 355 H. Brakmić, Bitcoin and Lightning Network on Raspberry Pi, https://doi.org/10.1007/978-1-4842-5522-3 INDEX

Bitcoin (cont.) Bitcoin client, 58, 131 getnewaddress bitcoin.conf, 166 command, 178 Bitcoin core wallet, 49, 123, 130 gettransaction command, 183 client settings, 160–163 hexadecimal form, 179 encryption, 141–146 MY_RAW_TX, 180 main window, 133 sendrawtransaction menu entry, 131 command, 182 message signing and signatures, 48–51 verification, 157–159 source code, Git, 113–116 sending and receiving funds systemd scripts, 225–229 (see Sending and receiving transaction funds) data, 173 transaction entries, 138 gettransaction Bitcoin log entries, 127 command, 170, 171 Bitcoin script hexadecimal string, 173 bloated, 73 regtest address, 170 browser console output, 204 serialized data, 174 browser warning, 205 structure, 174–177 CHECKSIG operation, 73, 77 transactions (see Transactions) constraints, 72 UFW convert, 260 execute DUP, 75 Using Python language, EQUAL, 77 229–232 EQUALVERIFY, 73, 77 ZeroMQ hashing, public key, 76 block hash format, 195 JavaScript testing install, 191 OP_MUL command, 218 message, 192 parsed structure, 216 NodeJS download, 189 visual script execution, 218 npm package, 193 LIFO, 202 publish–subscribe locking script, 203 method, 185, 186 OP_ADD, 72 ZMQ interface, 188 Op-Codes, 78 zmqtest directory, 191 OP_DUP and OP_HASH160, 73 Bitcoin CLI, 54, 58 parameters, 206

356 INDEX

script notation, 205–208 file config, 290–293 scriptPubKey, 73 funding transaction, 302 signature and public key, 73 lightning node, 293–297 stack calculation, 207 payment channel stack structure, 202 creation, 300–305 turing-complete language, 204 port forwarding unlocking script, 203 (upnpc), 297–299 Bitcoin’s source code, 113 peer nodes, 303 Bitcoin Wallet GUI, 130–132 regtest, 304, 305 Bitcoin white paper, 27 testnet explorer, 297 Blockchain, 12, 27 testnet faucet, 294 back-linked list, 33 demo coffee shop, 305 back-linked stack, 35 experimental features, 310 confirmed member, 36 JSON object data, 308, 309 child block, 36 payment request, 307 decentralized and distributed plugins databases, 33 lightning-cli, 311–314 distributed over many nodes, 33 system configuration, 313, 314 Emergent Consensus, 37, 38 systemd, 320–321 forks, 36, 37 parameters, 309 Google’s LevelDB, 34 payment request, 306 parent block, 35 project’s sources, 289 predecessor, 34 QR codes, 307 replicated, 33 status of payment, 307 structure, 34 , 21, 64, 65 transactions, 34 Collision-free, 14 variants, 36 Command line interface (CLI), 58 Commitment transactions Alice’s Private Key and Bob’s C Key, 277 Candidate blocks, 10 blockchain, 272 c-lightning, 289 changing state (payment), 272 compilation, 289, 290 definition, 272 configuration half-valid, 273

357 INDEX

Commitment transactions (cont.) Environment Setup mirrored transactions, 273 hardware, 87–92 output transaction, 275 setting up, 116–120 revocation key, 275 EQUALVERIFY, 76 state creation, 275 Configuration F bitcoin-cli stop, 125 bitcoin.conf, 123 Fast Internet Bitcoin Relay daemon binary, 125 Network (FIBRE), 47 directory, creation, 124 Full Bitcoin Node, 22 HTTP-based API, 126 Full node, 10, 23, 43, 44, 47, log entries, 126 48, 130 non-mainnet Functioning node, 44 settings, 124 RPC server, 126 G regtest, 125, 126 git tool, 114 testnet, 124–126 git pull command, 116 Consensus rules, 7, 8 global UTXO set, 63, 66 Console, 126–130 Glue language, 233 Graphical interface, 123 D GUI, 130–132 Decentralized network, 10, 25 Deserialization, 74 H Digital keys, 48 Digital signatures, 48, 50, 51, 60 Hardware Distributed timestamp server, 25 Bitcoin software, 91 Double-SHA256 function, 52 operating system, 88 dumpprivkey, 55 RAM capacity, 88 Raspberry Pi configuration, 90 requirements, 88 E VNC servers, 91 Elliptic curve multiplication, 55 Hardware wallets, 78 Emergent consensus, 7, 27 Hash functions, 14, 50

358 INDEX Hash time-lock contracts (HTLC’s) L Alice’s key, 280 Lightning network, 265 bitcoin script, 279 bitcoin DROP command, 280 automated integrity, 267 half-signed transaction, 283 BOLT, 267 hash H from preimage R, 281 definition, 265 multi-hop payments, 285 nodes, 268 network state, 284 3rd-party payment Op-Code, 280 processors, 266 transaction of Alice’s, 282 security holes, 266 HD wallets, 82, 85 socially scalable, 265 HTTP-based API, 126 TCP-/IP-based protocols, 268 I technological scalability, 266 value-transfer layer, 267 IRC chat protocol, 43 commitment (see Commitment transactions) J fees, 287, 288 Joule Browser extension hash time-lock chrome web store, 342 contracts, 279–285 confirm node settings, 346 money transmission, 279 home page, 341 multihop payments, 278, 279 installation completed, 343 pathfinding, 285–287 node information window, 347 payment channels node type selection, 343 closingsettlement permissions window, 344 transaction, 270 REST interface, 344 commitment self-signed certificate, 345 transactions, 270 uploading macaroons, 345 different states of, 271 initialfunding transaction, 269 K off-chain transactions, 269 Key-pairs, 49 transaction revocation, 271

359 INDEX

Lightning nodes, 43 LND lightning Linux advantages of, 325 configuration compilation process, 325 activating interfaces, 107 configuration file, 326–330 black-border setting, 104 Go development language and time zone environment, 323 settings, 103 GOPATH directory, 325 password settings, 103 malloc()/free() function, 323 Raspbian user adding, 104 node creation, 331 rebooting, 108 option-n testnet, 330 software update, 105 payments and SSH server, 106, 109 invoices, 331, 332 VNC service, 108, 109 signing and verifying WLAN setting, 105 messages, 333 installation source directories, 324 adapter, 94 systemd, 332 BITCOIN partition, 97 Log file, 127 diskutil tool, 96 Etcher, 97, 98 graphical tools, 94 M HDMI adapter, 99 Mainnet, 124 NOOBS package, 93 mainnet blockchain, 128, 133 package selection, 100 Merkle Tree, 17 progress update, 101 Mining node Raspberry OS, 96 structure, 45 Raspbian, 101 operators, 47 SD-Card Formatter, 95 mnemonics, 82 SD-Card insertion, 98 Mobile nodes, 44, 46 Torrent client, 93 Mobile wallet applications, 43 WLAN connection, 100 Monetary systems, 5, 9 Raspbian updating, 113 Money, 3–6 VNC remote multisig, 65 desktop, 110–112 Multi-signature transactions, 66

360 INDEX N Peer-to-peer networking addr-messages, 40 Node types finding nodes, 42 full, 44, 48 flat structure, 38 functioning, 44 getaddr-messages, 40 lightning, 43 getblocks message, 41, 42 mining node, 44 getdata, 42 mobile, 44, 46 getpeerinfo command, 40 pruned blockchain, 48 hard-coded IP addresses and solo-mining node, 46 DNS, 38 trusted nodes, 44 IRC chat protocol, 43 Nonce values, 14 querying and updating, procedure, 42 O seed-nodes, 38, 39 OP-Code, 75 verack, 39 bitwise logic, 212 Polish Reverse Notation, 71 cryptographic operations, 213 Private keys, 49, 50, 61, 79 pushing data, 209 Proof-of-Work reserved words, 214 back-linked list of blocks, 17 script execution Bitcoin block structure, 15, 16 control, 210 bitcoin reward, 21 scriptPubKey, 209 blockchain, 17 stack management, 211 blocks, creation, 20 Optimization techniques, 14 calculating Nonces, 15 coinbases and transaction fees, 21, 23 P, Q collision-free, 14 P2SH-type addresses, 54 Consensus Rules, 23 Pay-to-public-key-hash, 238 difficulty algorithm, 22 Pay-to-script-hash (P2SH), 65, 238 exchanges, 19 Pay-to-witness-public-key-hash fingerprint of data, 14 (P2WPKH), 242 Genesis Block, 19 Pay-with-Bitcoin option, 80 hashing function, 15

361 INDEX

Proof-of-Work (cont.) tooltip, incoming legacy payment system, 22 transactions, 137 Merkle Tree, 17, 18 transaction entries, 138 mining blocks, 22 reqSigs key, 65 mining node, 21, 22 Reverse Polish Notation, 71 Nonce values, 14 nonstandard blocks, 23 powerful competitors, 21 S predecessor, 20 scriptPubKey, 69, 70, 75 racing game, 21 scriptSig, 69 rewards, incentives, and Sending and receiving funds punishments, 23 address book, 150 scripting language, 15 coin selection window, 149 security, 21, 22 confirmation window, 152 SHA-256 function, 17 control features, 147 small Raspberry Pi Node, 22 generatetoadress command, 154 solving puzzles, 19 multiple recipients, 151 transactions, 17, 18, 21, 23 payment history, 157 trial-and-error approach, 14 receive tab, 155 Pruned nodes, 129 replace-by-fee strategy, 152 pubkey, 65 request payment window, QR Public blockchain, 11–13 code, 156 Public key, 49–51, 74 send tab, 147 Public Key Cryptography, 49 tooltip transaction confirmation, 153 transaction tab, 153, 154 R unlock window, 151 Regtest, 125 Serialization, 74 Regtest-blocks, 136 SHA-256, 14 Regtest mode, running Signatures, 48–51, 74 debug window with console, 136 Solo mining, 45 execution command, 136 Spark Wallet Shutdown window, 134 administrator login, 336 splash screen, 135 channel list, 338

362 INDEX

channel opening, 338 change address, 64 confirming payment, 340 coinbase, 64, 65 console-based tools, 339 digital signatures, 62 decoding request, 339 exception, 64 header information, 340 fees, 64 installation, 335 command getrawtransaction, 58 main window, 337 getrawtransaction and management features, 337 decoderawtransaction notifications, 336 commands, 68 payment requests, 339 global UTXO set, 63 systemd, 340, 341 indexing, 129 SPV nodes, 43 input transaction, 66 key-value pairs, 58, 59 T land metaphor, 62 Time multi-sig, 65, 66 Bitcoin demands, 26 network signals and blockchain Bitcoin white paper, 26 entries, 57 blockchain, 27 output transaction, 64 decentralized network, 24 private keys, 62, 67 Emergent Consensus, 27 public keys, 65 memorylessness, 29 reqSigs key, 65 node, 30 scriptPubKey, 70 notion of, 25 scriptSig, 67, 69 optimal, 29 unlocking logic, 69 points, 24 unlocking scripts, 70 progress-free, 29 UTXO set, 61, 62 proof-of-work, 25 value and addresses, 64 SHA-256 function, 28, 29 Transaction scripts, 24 timestamp server, 26 Trusted nodes, 44 transaction, 24 Turing complete programming Timestamp server, 26 language, 78 Transactions Type-1 deterministic blockchain.info web explorer, 57 wallet, 80 block reward, 65 Type-2 deterministic wallet, 81

363 INDEX U, V warning message, 142 window menu, 144 UTXO set, 62 Wallets keeping and managing key pairs, 78 W, X, Y, Z master private and public keys, 84 Wallet data, 129 nondeterministic and Wallet encryption deterministic, 78 console window, 145 password, 84 menu entry, 141 private key, 83 new password, 142 software and hardware tools, 79 private key, 146 type-1 deterministic, 80 transactions tab, 143, 144 type-2 deterministic, 81 walletpassphrase command variants, 84 execution, 145 Web-based wallet, 335

364