depolymerization

wire gateway for Bitcoin/Ethereum
Log | Files | Refs | Submodules | README | LICENSE

report.tex (35496B)


      1 \documentclass[12pt]{article}
      2 \usepackage[tmargin=1in,bmargin=1in,lmargin=1.25in,rmargin=1.25in]{geometry}
      3 \usepackage[utf8]{inputenc}
      4 \usepackage{bytefield}
      5 \usepackage{graphics}
      6 \usepackage{parskip}
      7 \usepackage{tikz}
      8 \usepackage{float}
      9 \usepackage{authblk}
     10 \usepackage{acro}
     11 
     12 \usetikzlibrary{positioning,fit}
     13 
     14 \title{Depolymerization \\ Integrating GNU Taler with blockchain-based cryptocurrencies}
     15 \author{Antoine d'Aligny}
     16 \affil{Bern University of Applied Sciences}
     17 \date{\today}
     18 
     19 \DeclareAcronym{DLT}{
     20   short=DLT,
     21   long=Distributed Ledger,
     22 }
     23 \DeclareAcronym{DOS}{
     24   short=DOS,
     25   long=Denial of service,
     26 }
     27 \DeclareAcronym{BIP}{
     28   short=BIP,
     29   long=Bitcoin Improvement Proposal,
     30 }
     31 \DeclareAcronym{RPC}{
     32   short=RPC,
     33   long=Remote Procedure Call
     34 }
     35 \DeclareAcronym{API}{
     36   short=API,
     37   long=Application programming interface
     38 }
     39 \DeclareAcronym{HTTP}{
     40   short=HTTP,
     41   long=Hypertext Transfer Protocol
     42 }
     43 
     44 \begin{document}
     45 
     46 \maketitle
     47 
     48 \clearpage
     49 
     50 \subsection*{Abstract}
     51 
     52 GNU Taler is an electronic payment system implemented as free software. The goal of this project is to enable payment with blockchain-based cryptocurrencies. 
     53 
     54 To achieve this goal, we need to understand how the distributed ledgers of blockchain-based cryptocurrencies work, what their limitations are, and how to mitigate them so that they can serve as a settlement layer. Key challenges include finding a reliable way to write metadata to the blockchain, handling blockchain reorganization, and resolving stuck transactions. 
     55 
     56 Blockchain reorganization prevents the finality of blockchain transactions, which is unacceptable for a settlement layer. By using a confirmation delay, we can create a semblance of finality. However, we still have to handle extreme cases where our delay is insufficient by suspending operations and gracefully recovering when possible.
     57 
     58 We have created a depolymerizer, which acts as an adapter between GNU Taler and a \ac{DLT}, allowing their use as a settlement layer. By using official DLT clients, and separating Taler specific components from \ac{DLT} specific components, we achieved a simple and maintainable design. 
     59 
     60 By proving that blockchains can be used as a settlement layer for GNU Taler, we show that it is not only capable of handling bank money, but also widely used cryptocurrencies.
     61 
     62 For cryptocurrencies owners, this integration offers a new solution for instant and low-cost payments that can scale beyond \ac{DLT} limitations while preserving or enhancing privacy.
     63 
     64 \subsection*{Acknowledgement}
     65 
     66 I would like to kindly thank Christian Grothoff and Emmanuel Benoist (Bern University of Applied Sciences) for their extensive guidance and support during this project.
     67 
     68 \clearpage
     69 
     70 \tableofcontents
     71 
     72 \clearpage
     73 
     74 \section{Blockchain-based cryptocurrencies}
     75 
     76 A cryptocurrency is a digital currency that relies on code and especially cryptography to secure transactions without a central authority. It is important to understand the basis of how they work and their limitations to understand the integration challenges they pose. For this project, only the two most used one, Bitcoin and Ethereum, were integrated. While other blockchain-based cryptocurrencies may work differently, we are only interested in the specific similarities between these two.
     77 
     78 \subsubsection*{Bitcoin}
     79 
     80 Bitcoin is the first cryptocurrency to achieve a successful public rollout. Invented by an unknown person or group of people working under the name of Satoshi Nakamoto, it was first mentioned in his white paper published the 28 October 2008 \cite{nakamoto2008bitcoin}. Bitcoin assembled the technological foundation for many of the cryptocurrencies in use today.
     81 
     82 \subsubsection*{Ethereum}
     83 
     84 Bitcoin is focused on currency transfer transactions and some people wanted to do more with cryptocurrencies, to be able to run smart contracts (programs) with the same guarantee as a currency transfer transaction. Ethereum is built to meet this expectation. After eighteen months of crowdfunding-funded development, Ethereum was launched in July 2015.
     85 
     86 \subsection{Blockchain}
     87 
     88 At the heart of these currencies is a blockchain. A blockchain is an append-only database composed of a list of linked records called blocks. The content of each block, except the genesis block (the first block), depends on the content of its parent block. This chained dependency enforces the immutability of the database, preventing retroactive modification without altering all subsequent blocks. Cryptocurrency transactions take effect when they are stored inside those blocks.
     89 
     90 \subsection{Consensus}
     91 
     92 The blockchain itself is just a storage system. To make it a \ac{DLT}, it needs a peer-to-peer network to share its changes. But also a way for participants (nodes) to agree on a single state of the chain, to reach consensus in a network where nodes can be malicious and have an economic interest in deceiving others. There are many ways to create such consensus, but only two of them interest us: proof of work and proof of stake.
     93 
     94 \subsubsection*{Proof of work}
     95 
     96 This mechanism consists in making the process of appending a block to the blockchain heavy in computation (mining) by requiring brute force hashing for example. Nodes willing to invest their computation power (miners), work to extend the chain they consider the current state. Over time, the longest chain will be the one where the majority of computing power has been invested, which means that it is the one followed by the majority of miners.
     97 
     98 This falls short as soon as one node control significantly more computation power than others. This node will not be able to create invalid blocks, because the other nodes will reject them, but will be able to revise transaction history and control the mining of future transactions. This is a majority attack also known as the 51\% attack.
     99 
    100 Another problem is that miners are incentivized to invest more and more computing power, which leads to ever-increasing energy consumption and ecological impact. These problems have motivated the search for another mechanism: proof of stake.
    101 
    102 \subsubsection*{Proof of stake}
    103 
    104 This mechanism replaces the use of computing power with the stacking of money. Validators (who replace miners) stake large sums of money to be allowed to verify blocks and be randomly selected to propose them. In case of malicious behaviour, the stacked money is burned by other validators. 
    105 
    106 The effectiveness and security of this mechanism have yet to be proven, but Ethereum plans to adopt it in the summer of 2022.
    107 
    108 \subsubsection*{Block time}
    109 
    110 Achieving consensus within the peer-to-peer network requires broadcasting the state of the blockchain to most nodes. This coordination takes some time, and we do not want blocks to be mined faster than the network can keep up. An adaptive difficulty algorithm is used to keep the block time close to a constant, by changing the amount of computation required to mine a block. On Ethereum, the block time is about 12 to 14 seconds\footnote{https://ethereum.org/en/developers/docs/blocks/\#block-time}. On Bitcoin, the block time is about 10 minutes. This limitation combined with a block size limit, creates a transaction rate and a blockchain size growth limit that is difficult to change without disrupting the security of the system. 
    111 
    112 \subsubsection*{Reorganization}
    113 
    114 These decentralized consensus mechanisms lead to the creation of competing blockchain states. When two miners broadcast a new valid block in a short period of time, one part of the network may receive them in a different order than another part. As nodes will follow the first valid block they found, we have a blockchain fork where two different blockchain states are followed in the network (Figure~\ref{fig:reorg}). 
    115 
    116 \begin{figure}[H]
    117     \begin{center}
    118         \input{figures/reorg.tex}
    119     \end{center}
    120     \caption{Blockchain fork}
    121     \label{fig:reorg}
    122 \end{figure}
    123 
    124 Over time, one fork will become longer than the other, and nodes will follow the longest chain. They will replace recent blocks as necessary during a reorganization of the blockchain. Those reorganizations can cause a transaction previously considered mined by a node to no longer be mined. Therefore, blockchain transactions lack finality.
    125 
    126 \subsection{Mining incentive}
    127 
    128 A minimum amount of mining power is required for the cryptocurrency to work. Without new blocks, there can be no new transaction. The more mining power and diversity (different miners), the harder it is to attack the \ac{DLT}. Since mining is an expensive process, cryptocurrencies must incentivize it by offering rewards to miners.
    129 
    130 Rewards are of two kinds:
    131 \begin{itemize}
    132     \item \textbf{Coin generation} While mining a block, the miner creates money in it that he can use as he pleases. Bitcoins use this type of reward but to reach a constant amount of 21 million bitcoins, this reward decreases over time.
    133     \item \textbf{Transaction fee} Transactions may contain a tip for the miner to encourage mining. Miners will not mine transactions that cost them money and will mine the most profitable transaction first. Transaction fees are then market-based values, the more transactions there are waiting to be extracted, the higher the fee to be paid to be included in the next block. When a transaction is sent with a fee too small, it may not be mined in a timely fashion. Since transaction fees are unpredictable, transactions sometimes end being stuck for a long time.
    134 \end{itemize}
    135 
    136 \subsection{Blockchain-based cryptocurrencies limitations}
    137 
    138 As we have seen, blockchain-based cryptocurrencies have certain limitations by design. Block time adds a significant delay to each transaction, and the block size limits the number of possible transactions per second. High transaction fees are required to cover the cost of an expensive mining process. Because the transaction fee is an unpredictable market-based value, transactions can get stuck. Finally, the redundant computational work of all miners has a significant ecological impact. 
    139 
    140 \clearpage
    141 
    142 \section{GNU Taler}
    143 
    144 GNU Taler is an anonymous, taxable payment system implemented as free software and inspired by David Chaum's Ecash \cite{burdges2016enabling} \cite{dold2019gnu}. Taler does not rely on a \ac{DLT}. Taler payments are made using digital form of traditional money anonymized using blind signatures. We provide only a superficial overview of GNU Taler necessary to understand how Depolymerization fits into the system. More information can be found in the project documentation and repository.
    145 
    146 \subsection{Overview}
    147 
    148 \begin{figure}[hb]
    149     \begin{center}
    150         \scalebox{1.2}{\input{figures/taler_arch.tex}}
    151     \end{center}
    152     \caption{GNU Taler overview}
    153 \end{figure}
    154 
    155 
    156 \paragraph*{Exchange}
    157 The exchange is the payment service provider for financial transactions between customers and merchants. The exchange holds money as a reserve for anonymous digital coins.
    158 
    159 \paragraph*{Customer}
    160 Customers can withdraw coins from the exchange and store them in their electronic wallets. These coins can then be spent at a merchant.
    161 
    162 \paragraph*{Merchant}
    163 Merchants accept coins in exchange for goods and services. They can then deposit these coins at the exchange to receive money in return.
    164 
    165 \paragraph*{Auditor}
    166 Auditors monitor the behaviour of the exchanges to ensure that exchanges operate correctly. They are typically operated by financial regulators.
    167 
    168 \paragraph*{Settlement layer}
    169 The settlement layer provides finality for wire transfers that allow customers to deposit money and merchant to withdraw money from Taler. It is typically provided by banks. The goal of this project is to use DLT as a settlement layer to support blockchain-based cryptocurrencies.
    170 
    171 \subsection{DLT settlement layer}
    172 
    173 \begin{figure}[h]
    174     \begin{center}
    175         \vspace{0.5em}
    176         \input{figures/settlement_layer.tex}
    177     \end{center}
    178     \caption{DLT settlement layer with Depolymerizer}
    179     \label{fig:DltSettlement}
    180 \end{figure}
    181 
    182 Depolymerizer serves as a middleware between GNU Taler and cryptocurrencies \ac{DLT} (Figure \ref{fig:DltSettlement}). Customers can send money to the Depolymerizer via a credit transaction, to obtain coins that they can use in GNU Taler transactions. Using the Depolymerizer, Taler exchanges can materialize the coins back into the DLT via a debit transaction.
    183 
    184 Off-chain transactions have many advantages over on-chain transactions. At the cost of putting trust in exchange operators or auditors, you can have fast and low cost transactions with instant confirmation (ms). GNU Taler offers linear scalability that can solve DLT throughput limitation and, by not relying on Proof of Work, has a much lower ecological impact. GNU Taler does not sacrifice privacy either; it provides privacy when it can and transparency when it has to (regulation: avoid tax evasion and money laundering). 
    185 
    186 \clearpage
    187 
    188 \section{Resolving blockchain challenges}
    189 
    190 Some properties of blockchain-based cryptocurrencies are problematic for their use as a Taler settlement layer. The two main issues are blockchain reorganizations and stuck transactions.
    191 
    192 \subsection{Solving chain reorganization}
    193 
    194 Taler expects credits to be final. If a credit disappears from the blockchain, an irrevocable debit would no longer be backed by credit. A malicious actor able to rearrange the blockchain at will could use his Taler coins while removing the credit that created those coins and thus get his cryptocurrencies coins back, allowing for double spending. This problem is well known and already mentioned in the Bitcoin white paper \cite{nakamoto2008bitcoin}. The deeper a block is in the blockchain, the harder it is to replace. The depth of a block determines its probability of persistence. The solution is therefore to wait until a block has a certain number of blocks linked to it before considering its transactions as final (Figure \ref{fig:conf_delay}).
    195 
    196 \begin{figure}[H]
    197     \begin{center}
    198         \vspace{0.5em}
    199         \input{figures/conf_delay.tex}
    200     \end{center}
    201     \caption{Reorganization mitigation using confirmation delay}
    202     \label{fig:conf_delay}
    203 \end{figure}
    204 
    205 The choice of this confirmation delay is a compromise between speed and security. As small reorganizations are common by design, a minimal delay is necessary for proper operation. To be resistant to an adversary with $30\%$ of the total mining power, we need to wait for 6 blocks in Bitcoin ($\approx$ 1h) and 37 blocks in Ethereum ($\approx$ 8min) \cite{gervais2016security}.
    206 
    207 Using a confirmation delay does not solve the problem, it only mitigates it. We still have to deal with a potential reorganization that is longer than our delay. When this happens, we look for any missing confirmed credits, if there are none, we can ignore that reorganization (Figure \ref{fig:harmless_reorg}).
    208 
    209 \begin{figure}[H]
    210     \begin{center}
    211         \vspace{0.5em}
    212         \input{figures/harmless_reorg.tex}
    213     \end{center}
    214     \caption{Harmless reorganization}
    215     \label{fig:harmless_reorg}
    216 \end{figure}
    217 
    218 If confirmed credits have been removed, we will suspend operations. If we are not targeted by an attack and the DLT network is behaving well, the credits will be mined again, and we can wait for them to be confirmed again to resume operations. In the case where the missing credits have been maliciously replaced (Figure \ref{fig:conflict}) we never resume operation.
    219 
    220 \begin{figure}[H]
    221     \begin{center}
    222         \vspace{0.5em}
    223         \input{figures/conflict.tex}
    224     \end{center}
    225     \caption{Reorganization with conflicting transaction}
    226     \label{fig:conflict}
    227 \end{figure}
    228 
    229 \subsection{Adaptive confirmation}
    230 
    231 If we experience a reorganization once, it is dangerously likely that another one of the same magnitudes will occur again. Depolymerizer learns from reorganizations by increasing its confirmation time (Figure \ref{fig:analysis}). Since we understand that a longer delay can also be counterproductive, we also want to limit the delay adjustment.
    232 
    233 \begin{figure}[H]
    234     \begin{center}
    235         \vspace{0.5em}
    236         \input{figures/analysis.tex}
    237     \end{center}
    238     \caption{Adaptive confirmation}
    239     \label{fig:analysis}
    240 \end{figure}
    241 
    242 \subsection{Solving stuck transaction}
    243 
    244 We know that transactions can get stuck for a long time, which can be problematic when we are expecting transactions to be executed in a timely manner. Depolymerizer keeps track of pending transactions and finds those that are taking an excessive amount of time to mine. Then, it increases the fee for these transactions to bump their mining priority. Since the process of replacing transactions is expensive, this feature is optional and configurable.
    245 
    246 \clearpage
    247 
    248 \section{Metadata} \label{metadata}
    249 
    250 GNU Taler need additional metadata to link a wallet to credits and allow merchants to link deposits to debits. Metadata need to be stored alongside transactions in the blockchain, so it is possible to recover the full transaction history of any depolymerizer from it.
    251 
    252 \subsection{Metadata format}
    253 
    254 The goal of our metadata format is to be simple to parse and versioned for future extensions.
    255 
    256 \subsubsection*{Incoming transaction}
    257 
    258 Incoming transaction metadata contains a reserve public key, which is a 32B hash of a public key. We just prepend a versioning byte to allow future extension.
    259 
    260 \begin{figure}[h]
    261     \begin{center}
    262         \begin{bytefield}{33}
    263             \bitheader{0,1,32} \\
    264             \bitbox{1}{\tiny 0} & \bitbox{32}{Key Hash}
    265         \end{bytefield}
    266     \end{center}
    267     \caption{Incoming metadata format}
    268 \end{figure}
    269 
    270 \subsubsection*{Outgoing transaction}
    271 
    272 Outgoing transactions can be of two types: credit or bounce. Credit metadata contains the wire transfer ID which is a 32B hash and the exchange base URL which is of variable size and encoded using uri-packing (Section \ref{uri-packing}). Bounce metadata contains the bounced transaction ID which is implementation-dependent but is 32B for Bitcoin and Ethereum. A prepended versioning byte differentiates the two types, 0 being a credit and 254 a bounce.
    273 
    274 
    275 \begin{figure}[h]
    276     \begin{center}
    277         \begin{bytefield}[rightcurly=., rightcurlyspace=0pt]{33}
    278             \bitheader{0,1,32,33}  \\
    279             \begin{rightwordgroup}{Credit}
    280                 \bitbox{1}{\tiny 0} & \bitbox{32}{Transfer ID} & \bitbox{10}{Base URL}
    281             \end{rightwordgroup} \\ \\
    282             \begin{rightwordgroup}{Bounce}
    283                 \bitbox{1}{\tiny \rotatebox{90}{254}} & \bitbox{32}{Transaction ID}
    284             \end{rightwordgroup}
    285         \end{bytefield}
    286     \end{center}
    287     \caption{Outgoing metadata format}
    288 \end{figure}
    289 
    290 \subsection{Bitcoin}
    291 
    292 There are many documented ways to encode metadata in a bitcoin transaction \cite{bartoletti2019journey}. In the early days of this cryptocurrency, users abused the transaction format to store metadata, and some of these techniques result in ecosystem pollution. For example, bitcoin clients keep a list of all addresses that currently have money to spend (UTXO), with fake addresses wasting space there indefinitely. To allow storage of a reasonable amount of metadata without polluting the blockchain, a new opcode script OP\_RETURN was created, allowing up to 80B of storage per transaction.
    293 
    294 Debits are performed from our code using OP\_RETURN to store metadata, but credits are done from common wallet clients, and they do not yet support custom metadata. We had to find another format using fake addresses.
    295 
    296 We use the latest address type, segwit addresses \cite{BIP173}, where we can embed 20B of chosen data. Since the reserve pub key is a 32B hash, we need two addresses. Therefore, we use two fake addresses each containing one half of the key prepended by a common random prefix, except for the first bit, which must be 0 for the first half and 1 for the second. We then send a single transaction with three addresses (the exchange address and the two fake) as recipients.
    297 
    298 \begin{figure}[h]
    299     \centering
    300     \begin{tikzpicture}
    301         \draw[dotted,thick] (-6.33,1.13) -- (-6.33,0.185);
    302         \draw[dotted,thick] (-5,1.13) -- (4.3,0.185);
    303         \node {
    304             \begin{bytefield}[rightcurly=., rightcurlyspace=0pt]{32}
    305                 \bitheader{0,3,4,19} \\
    306                 \begin{rightwordgroup}{Address}
    307                     \bitbox{4}{ID} & \bitbox{16}{Half}
    308                 \end{rightwordgroup} \\ \\
    309                 \bitheader{0,1,31} \\
    310                 \begin{rightwordgroup}{First ID}
    311                     \bitbox{1}{\tiny 0} & \bitbox{31}{Common random prefix}
    312                 \end{rightwordgroup} \\ 
    313                 \bitbox[]{32}{or} \\
    314                 \begin{rightwordgroup}{Second ID}
    315                     \bitbox{1}{\tiny 1} & \bitbox{31}{Common random prefix}
    316                 \end{rightwordgroup}
    317             \end{bytefield}
    318         };
    319     \end{tikzpicture}
    320     \caption{Outgoing metadata format}
    321 \end{figure}
    322 
    323 Having a common random prefix allows us to distinguish real addresses from fake ones, since it is very unlikely that two addresses share the same 32 bits. Since the Bitcoin client randomizes the order of the addresses for privacy reasons, the first bit allows us to distinguish the first half of the key from the second.
    324 
    325 \subsection{Ethereum}
    326 
    327 Ethereum is designed around the concept of smart contracts. Logging inside a smart contract is the recommended way to add metadata\footnote{https://ethereum.org/en/developers/docs/smart-contracts/anatomy/\#events-and-logs}, but it is expensive (additional storage and execution costs) and adds an avoidable attack surface. We chose to use the transaction field typically used to call smart contracts to store our raw metadata.
    328 
    329 \subsection{Friendly behaviour on format error}
    330 
    331 When we receive a transaction without any metadata or with an incompatible format (bogus wallet), we want to return the money to its owner (bounce). However, this can be dangerous because it creates a potential attack loophole as anyone can now make Depolymerizer do a transaction, by sending a malformed transaction. Depolymerizer takes a bounce fee to make a potential \ac{DOS} attack too costly. There is another issue, making a transaction has a cost, and if the transaction fee is higher than our bounce fee, malicious bounces could bankrupt us. We also need to charge the recipient the transaction fee to ensure we never lose money on a bounce.
    332 
    333 \clearpage
    334 
    335 \section{Architecture}
    336 
    337 Each cryptocurrency uses a different \ac{DLT} with its own format and rules, which evolve over time. We do not want to manage the \ac{DLT} logic ourselves, nor do we want to rely on third-party dependencies to implement their support properly and be maintained. The simplest solution is to rely on the official clients and communicate with them via \ac{RPC}.
    338 
    339 \begin{figure}[h]
    340     \begin{center}
    341         \input{figures/depolymerizer_arch.tex}
    342     \end{center}
    343     \caption{Depolymerizer architecture}
    344 \end{figure}
    345 
    346 While some parts of Depolymerizer are \ac{DLT} specific, much of the logic is common, and we want to reuse it. We have a Wire Gateway component that implements the Taler \ac{HTTP} \ac{API} to enable communication with Taler exchanges. Each supported cryptocurrency has its specific adapter to communicate with the official full node client via \ac{RPC}. The Wire Gateway module and the \ac{DLT} adapter use a common database to store transactions and communicate with notifications.
    347 
    348 \subsection{DLT adapter}
    349 
    350 The DTL adapter uses an event-based architecture with three distinct loops.
    351 
    352 \paragraph*{Block watcher}
    353 
    354 The watcher loop looks for new incoming blocks and notifies the other loops of their arrival.
    355 
    356 \paragraph*{Analysis}
    357 
    358 The analysis loop waits for new blocks and then analyzes the behavior of the DLT network. If a dangerous reorganization occurs, it is responsible for updating the confirmation delay. 
    359 
    360 \paragraph*{Worker}
    361 
    362 The worker loop waits for new blocks or transaction requests (from the Wire Gateway \ac{API}). When one of these events occurs, it first reconciles the local database with the \ac{DLT}, then triggers requested debits, re-issues blocked debits and bounces malformed credits.
    363 
    364 \subsection{Worker loop in detail}
    365 
    366 \subsubsection*{DLT reconciliation}
    367 
    368 During a \ac{DLT} reconciliation, we first list all new transactions and any transactions that have been removed in a reorganization since the last reconciliation. If any previously confirmed debits have been removed without being reinserted into another block, we notify the Wire Gateway to cease activity and wait for the next block in hopes of recovering them. All newly confirmed debits and successful credits are registered in the database.
    369 
    370 \subsubsection*{Reconciliation inconsistency}
    371 
    372 When we issue a transaction (debit or bounce), it is possible for the database or \ac{DLT} \ac{RPC} request to fail. Since a \ac{DLT} \ac{RPC} request error does not mean that the cryptocurrency transaction was not successful, and since the database may not record a successful transaction, it is possible to have an inconsistency between the DLT and the database where a successful transaction is not recorded as such. This is very problematic because we must perform each transaction only once.
    373 
    374 This is also problematic because, even if we used a status machine state in the database to detect this inconsistency, the only way to resolve it is to make another \ac{DLT} reconciliation, which is slow and does not play well with database locking.
    375 
    376 Since we know that blockchain-based cryptocurrencies have low throughput, we do not need parallel worker loops to stay synchronized. Therefore, we can use a cross-process locking mechanism to ensure that only one working loop is running at a time. Then, when a database or \ac{DLT} request error occurs, we can restart the loop, which will start by performing a \ac{DLT} reconciliation that will recover all successful unregistered transactions.
    377 
    378 \clearpage
    379 
    380 \section{Implementation specific issues}
    381 
    382 \subsection*{Ethereum amount precision}
    383 
    384 The Taler amount format comes from RFC 8905 \cite{RFC8905}. It allows up to $2^{53}$ unit and 8 decimal digits. This format is perfectly suited for Bitcoin where the maximal amount is 21 million bitcoins and the minimum amount is the satoshi, one satoshi being worth $10^{8}$ bitcoin. However, the minimum amount of Ethereum is the wei, with one ether being worth $10^{18}$ wei. The amount of ether in circulation continues to grow without a cap, with over 119,000,000 ether in circulation at the time of writing those lines. Therefore, it is not possible to represent all Ethereum amounts with the current format.
    385 
    386 A standard Ethereum transaction requires 21 000 units of gas\footnote{https://ethereum.org/en/developers/docs/gas/\#post-london}. The average gas price is currently around 30 Gwei. Therefore, a standard transaction cost about $63.10^{18}$ wei in transaction fees. Since the transaction fee is so high, even if we truncate Ethereum value to $10^{-8}$ eth ($10^{10}$ wei), we can still represent any amount you can send without losing money on the transaction fee. In Depolymerizer, all Ethereum amounts are truncated as such.
    387 
    388 \subsection*{Replaceable bitcoin transaction}
    389 
    390 When some merchants wanted to allow instant payments with Bitcoin, they chose to consider a transaction final when it is announced. Although wrong, this choice works most of the time because many nodes do not accept conflicting transactions in their mempool, making it difficult to replace or cancel a transaction that has already been announced.
    391 
    392 This becomes problematic when you want to make a legitimate replacement, to unstuck a transaction by increasing its transaction fee for example. At the same time, it is dangerous to give an easy way to attackers and scammers to change the content of announced transaction.
    393 
    394 We use the solution has been adopted in \ac{BIP} 125 \cite{BIP125}, adding the possibility to encode the replaceability of a bitcoin transaction at its creation. It can thus be replaced by a new transaction within certain rules: you cannot send less money to existing recipients, and you must pay a replacement fee as a countermeasure to a \ac{DOS} attack.
    395 
    396 \clearpage
    397 
    398 \section{URI packing, a compression side quest}\label{uri-packing}
    399 
    400 \subsection*{The need for compact URI}
    401 
    402 As discussed previously in section \ref{metadata}, storing metadata in blockchain is
    403 expensive and limited. Therefore, we want our metadata to be as small as possible.
    404 
    405 \noindent
    406 Transactions metadata are composed of three parts:
    407 \begin{itemize}
    408     \item Version and identity metadata ($\sim$ 1B)
    409     \item Reserve public key or wire transfer ID (32B)
    410     \item Base URL (debit only, variable)
    411 \end{itemize}
    412 
    413 The only variable, and so problematic, part is the base URL. Those URLs have some
    414 property in common, they always use a few different scheme (http or https) and
    415 are composed of a domain and a small path.
    416 
    417 We would normally encode the URL using ASCII, but we knew only a few ASCII characters are actually used, and we can take advantage of that.
    418 
    419 \subsection*{5 or 11 encoding}
    420 
    421 Our idea is to encode the most commonly used characters using five bits, and the remaining characters using eleven bits. As ASCII characters are seven bits wide and are commonly encoded using height, we gain on size if more than half of the characters composing the URI are encodable using fewer bits (Table~\ref{table:uri-packing}). You can find the detailed encoding table in appending \ref{5-11}.
    422 
    423 \begin{table}[h]
    424     \centering
    425     \begin{tabular}{ll}
    426         value    & encoding                                    \\
    427         \hline
    428         0..30    & common character: a-z . / - \%              \\
    429         30 0..64 & extended character, remaining graphic ascii \\
    430         31       & end of encoded string                       \\
    431     \end{tabular}
    432     \caption{URI packing encoding}
    433     \label{table:uri-packing}
    434 \end{table}
    435 
    436 Using this encoding format on all domains on the
    437 majestic-million\footnote{https://majestic.com/reports/majestic-million}
    438 database, $98.77\%$ of the domain name where smaller, going from an average encoded size of 14B in ASCII to 10B using our format.
    439 
    440 \subsection*{Uri in metadata}
    441 
    442 To further optimize metadata size we chose to encode the URI scheme into the
    443 version and identity metadata byte and the remaining domain and path using our
    444 custom format.
    445 
    446 For example, for bitcoin the maximum amount of data than is accepted in
    447 OP\_RETURN is currently 80 bytes, leaving us 47 bytes to store the URI. With our
    448 encoding we can encode in the best case 74 characters instead of 47 which is more than enough for our use case.
    449 
    450 \clearpage
    451 
    452 \section{Taler Wire Gateway HTTP API}
    453 
    454 Taler is a modular project where each module communicates through \ac{HTTP} \ac{API}. The Wire Gateway \ac{API} allows the exchange to communicate to wire adaptors. The Wire Gateway module allow Depolymerizer to communicate with Taler exchanges. As the \ac{API} can be exposed on the Internet it has to be resistant to most of the known attacks.
    455 
    456 \subsection*{HTTP Authentication}
    457 
    458 The wire \ac{API} only supports the Basic \ac{HTTP} Authentication method and it has to be optional. Making it optional can lead to security issues by misconfiguration. If the default behavior in case of missing configuration is to deactivate authentication, a typo could lead to an exposed \ac{API}. We made the authentication method configuration mandatory to make its deactivation explicit.
    459 
    460 \subsection*{OOM DOS}
    461 
    462 A common Denial Of Service attack consists of sending many requests with huge bodies to saturate a server memory and, in the worst case, create an Out Of Memory error. To be resilient against such attacks we only read body after request authentication, to prevent any person without authorization to access the \ac{API} to perform such attacks.
    463 
    464 Then we chose an aggressive memory budget of 4kB, as all request bodies should be very small, and we only read and parse them under this budget. In the case of compressed bodies, we also apply this budget to the decompression process to protect ourselves against decompression bombs.
    465 
    466 \subsection*{Testing}
    467 
    468 The Taler exchange has a taler-exchange-wire-gateway-client CLI that allowed me to test that my implementation not only conforms to the \ac{API} documentation but also with how the official client handles it. I found confusion in the documentation where it was specified that timestamp should consist of time in milliseconds since epoch, but the client will reject timestamps that are not rounded to second.
    469 
    470 \clearpage
    471 
    472 \section{Conclusion}
    473 
    474 \subsection*{Summary}
    475 
    476 We have shown that is it possible to use Bitcoin and Ethereum \ac{DLT} as a settlement layer for GNU Taler enabling payment with blockchain-based cryptocurrencies using Depolymerization. Depolymerization act as a middleware between Taler exchange and a \ac{DLT} mitigating their inconsistency especially chain reorganization and stuck transactions.
    477 
    478 In this project, we have not addressed the legal challenges of running GNU Taler with Depolymerization. In some countries, the use of cryptocurrencies is prohibited or restricted, and in most countries, payment systems are regulated and may require a licence. We therefore advise against deploying it without checking that you are authorized to do so.
    479 
    480 Depolymerization has many attractive features for cryptocurrency users at one cost, the centralization of trust in the exchange. This problem is partially solved with the use of auditors that federate trust. In contrast, blockchain-based cryptocurrencies are generally public, with their entire state subject to public scrutiny. For GNU Taler to convince users who expect this level of openness, we would have to find a way to make enough information public that any client could do the auditors' job.
    481 
    482 
    483 \subsection*{Future work}
    484 
    485 \subsubsection*{Paying for reorganization loss}
    486 
    487 When a reorganization removes a confirmed credit indefinitely (conflicting transaction) we suspend operation indefinitely. We could allow exchange administrators to pay for the missing transactions to resume operations, as sometimes the loss caused by the non-functioning Depolymerizer exceeds the cost of the missing transactions.
    488 
    489 \subsubsection*{Smarter analysis}
    490 
    491 More intelligent analysis of network behaviors can be performed to tailor the confirmation time to an estimated risk factor. Brute force attacks on a DLT are very expensive, and one can expect that attackers have an economic incentive to attack through double spending for example. When we receive a large credit or when a considerable amount of currency is exchanged on the network, this can be the preparation for such attacks. We could monitor these indicators, and we could apply a longer temporary delay to make the attacks harder to sustain.
    492 
    493 \clearpage
    494 
    495 \bibliographystyle{alpha}
    496 \bibliography{literature}
    497 
    498 \clearpage
    499 
    500 \printacronyms
    501 
    502 \clearpage
    503 
    504 \appendix
    505 
    506 \section*{5-11 encoding table}\label{5-11}
    507 
    508 \input{tables/5-11.tex}
    509 
    510 \end{document}