Hogyan készítsünk Ethereum Wallet webalkalmazást

Az eth-hot-pénztárca legmenőbb részeinek áttekintése

Ez a cikk az eth-hot-wallet , egy erc20 token natív támogatással rendelkező Ethereum pénztárca webalkalmazás érdekes részeinek műszaki áttekintése . A forráskód megtalálható a GitHub-on (MIT License).

Tartalomjegyzék:

  • Ethereum pénztárca, mint webes alkalmazás
  • A verem
  • Az et-hot-pénztárca tartályai
  • Egységes tervezés az Ethereum pénztárcához
  • Redux és Redux-Saga
  • Biztonságos jelszógenerátor
  • eth-lightwallet és SignerProvider
  • Titkosított offline tárhely
  • Az Ethereum küldése a Web3.js használatával
  • Erc20 tokenek küldése a Web3.js használatával
  • Feliratkozás az Ethereum tranzakció életciklusára a Web3.js V1 és a Redux-Saga csatornák használatával
  • Az Ethereum blokklánc és az áradatok lekérdezése a Redux-Saga segítségével
  • Szemmel tartva a csomag méretét
  • Következtetés

Ethereum pénztárca, mint webes alkalmazás

Ha a szoftvert webalkalmazásként telepítik, akkor először a széles körű akadálymentesítés jut eszünkbe. Végül is a web a legszélesebb körben elérhető eszközökön keresztüli eszközök közötti platform. Az Eth-hot-wallet egy PWA (progresszív webalkalmazás), amely bármely modern webböngészőből használható.

Sőt, a PWA támogatás legújabb fejlesztései jelentősen javítják a mobil felhasználói élményt.

Előnyök:

  • Nincs szükség további szoftverekre
  • Semmiféle telepítés nem szükséges
  • Képesség a modern webfejlesztő eszközök használatára.
  • Könnyen telepíthető és frissíthető

Hátrányok:

  • Hajlamosabb az adathalász támadásokra.
  • A böngésző beépülő moduljai rosszindulatú kódot juttathatnak az oldalra.
  • Magas betöltési idő lassú internetkapcsolaton
  • Korlátozott hozzáférés az eszköz tárhelyéhez

Jelentős az a tény, hogy a rosszindulatú böngészőbővítmények JavaScript kódot juttathatnak a kulcsok kibontásának megkísérléséhez. A kockázat áthelyezéséhez ösztönözni kell a felhasználót, hogy kapcsolja ki a kiterjesztéseket (azaz inkognitómódban használja), vagy integrálja a webet egy külső web3 szolgáltatóval, például a MetaMask vagy a Trust böngészővel. A webalkalmazás konvertálása asztali alkalmazássá szintén életképes lehetőség.

Ami az adathalászatot illeti, arra kell ösztönözni a felhasználót, hogy könyvjelzővel töltse fel az oldalt, és a Google keresőn keresztül férjen hozzá. Nagyon valószínűtlen, hogy az adathalász webhely a valós webhely fölé kerüljön a keresési eredmények között.

Lényeg: egy internetes alkalmazás lehetővé teszi, hogy a lehető legszélesebb közönséget érje el minimális súrlódással . Véleményem szerint a web a legjobb célplatform az új alkalmazások számára.

A verem

A kód nagy részét a kezelőfelületnek szentelték :

A végső csomag sok csomagból áll, amint az a package.json fájlban is látható.

A legfelső szintű összetevők a következők:

  • Eth-lightwallet - Könnyű JS pénztárca a Node-hoz és a böngésző a kulcstárak kezeléséhez
  • React, Redux, saga.js, megváltoztathatatlan JS és újraválasztás az offline-első reakció-kazán által csomagolva
  • Hangya kialakítás - kiváló felhasználói felület-alkatrészek a reakcióhoz
  • Webpack - Csomagoló a JavaScript és a barátok számára.

És a háttérben:

Az utolsó csomag közvetlenül a GitHub-oldalakra kerül telepítésre a lerakat egy dedikált ágából. Nincs szükség háttérre a hagyományos jelenetben.

A Testnet Ethereum csaptelep létrehozásához egy Serverless keretrendszert fogunk használni. Jelentősen javítja a fejlesztői élményt az AWS Lambda használatakor. Ez egy nagyon költséghatékony megoldás, amely megsemmisíti az infrastruktúra fenntartásának szükségességét, különösen kis volumenű alkalmazásoknál.

Az et-hot-pénztárca tartályai

A React, a Redux, a Saga.js és a Reselect kombinációjának használata esetén minden tartály a következő összetevőkből állhat:

  • index.js - a grafikus felhasználói felület megjelenítéséhez
  • actions.js
  • reduktor.js
  • saga.js
  • selectors.js
  • konstansok.js

Mint Dan Abramov kijelentette, egynél több megközelítés létezik arra vonatkozóan, hogy alkatrészt vagy tartályt használjunk-e. Tapasztalatom szerint, ha egy komponensnek több mint ~ 8 attribútuma van az alkalmazás állapotán belül, akkor egy új tárolóba kell szétválasztani. Ez csak ökölszabály. Az attribútumok száma idővel felpuffadhat. Bonyolult összetevők esetén jobb, ha egyedi tárolóval rendelkezik, mintsem a fő konténer állapotát csoportosítsa.

Nem minden tartályban kell lennie az összes összetevőnek. Az eth-hot- sendTokenwalletnél a tároló nem használja a saját Saga.js-jét. Úgy választottuk el, hogy ne terheljük a kezdőlap komponensének állapotát.

A Honlap tároló

Az elsődleges tároló, ahol a művelet nagy része megtörténik, a kezdőlap tároló. A kezdőlap tárolóban a Saga.js felelős a mellékhatások kezeléséért. A GUI mellett fő feladata a kulcstár-műveletek kezelése lesz .

Az ETH-Lightwallet csomag biztosítja a kulcstárat. Minden kapcsolódó művelet, beleértve a kulcsokat, a magokat, a titkosítást, az importálást és az exportálást, ebben a szakaszban történik.

A Fejléc konténer

A fejléc azt a tényt szemlélteti, hogy a tároló nem csupán GUI-összetevő:

This container might look simple at first with only a logo and a network selector. Does it even need to be in its own container? The answer is that in eth-hot-wallet every network communication-related action and state management is done inside the header container. More than enough for any container.

The SendToken container

SendToken is a modal that appears while the user selects to send Ether/ tokens.

The modal includes some basic input verification, like amount and Ethereum address check. It does not use Saga.js to initiate side effects, but instead uses actions provided by the homepage and header containers.

We separated it into a new container to reduce clustering the state of the homepage container.

TokenChooser container

Token Chooser appears when the user wants to select what token the wallet will manage.

The TokenChooser name was selected in order not to be confused with the term “selector” which appears many times through the wallet code in a different context (reduxjs/reselect: Selector library for Redux).

Same as with the SendToken container, TokenChooser does not use its own Saga.js file but calls actions from the homepage container when needed.

A Unified design for Ethereum Wallet

Since the appearance of the ERC20 standard (EIP20), it was obvious that tokens were going to be an important part of the Ethereum ecosystem. The Ethereum wallet was designed with a unified design approach in mind. Ether and token should be treated equally from the user’s perspective.

Under the hood, the API for sending Ether and sending tokens is quite different. So is checking the balance, but they will appear the same on the GUI.

To send Ether, we need to use native functions provided by the web3.js library, while sending tokens and checking balances involves interaction with a smart contract. More on this issue later.

Redux and Redux-Saga

Using Redux store as a single source of truth greatly benefits the wallet. GUI actions and user-triggered flows can be relatively easily managed by actions and reducers provided by Redux.

Aside from keeping the UI state, the Redux store also holds the key-store object (a partially encrypted JavaScript object supplied by eth-lightwallet). This makes the keystore accessible throughout the app by using a selector.

Redux-Saga is what makes the entire setup shine.

redux-saga egy olyan könyvtár, amelynek célja, hogy az alkalmazás mellékhatásait (azaz az aszinkron dolgokat, például az adatgyűjtést és az olyan tisztátalanságokat, mint a böngésző gyorsítótárának elérése) könnyebben kezelhetővé, hatékonyabban végrehajthatóvá, könnyen tesztelhetővé és jobban kezelje a hibákat.

A Saga.js a Generátorokat használja arra, hogy az aszinkron folyamatok könnyen olvashatók és írhatók legyenek . Ezzel ezek az aszinkron folyamatok úgy néznek ki, mint a szokásos szinkron JavaScript kódok (hasonlóak async/ awaitde több testreszabási lehetőséggel).

Az Ethereum pénztárca esetében a Saga használatával kényelmes módszert kapunk az olyan aszinkron műveletek kezelésére, mint a többi API-hívás, kulcstároló-műveletek, az Ethereum blokklánc-hívások a web3.js-n keresztül és még sok más. Az összes kérést tisztán kezelik egy helyen, nincs visszahívás, és nagyon intuitív API.

Példa a redux-saga használatára:

Secure password generator

To adequately secure the user’s keystore, we need to encrypt it with a strong password. When using eth-lightwallet, the password needs to be provided during the initiation of the hd-wallet.

Let’s assume that we have a function called generateString, which can provide genuinely random strings at any length.

If the user wants to generate a new wallet, we will produce the following parameters:

We can ask the user to confirm the password and the seed or generate a new set on its behalf. Alternatively, we can ask the user for their own existing seed and password.

generateString implementation: We will use the relatively new window.crypto API to get random values (currently supported by all major browsers).

Eth-hot-wallet implementation is based on the following code to generate random but human-readable strings:

After the user has accepted the password and the seed, we can use the values and generate the new wallet.

eth-lightwallet and SignerProvider

  1. LightWallet is intended to be a signing provider for the Hooked Web3 provider.
  2. Hooked Web3 provider has been deprecated, and currently the author recommends the package ethjs-provider-signer as an alternative.
  3. At the moment of writing, there is a bug in ethjs-provider-signer that prevents the display of error messages. The bug was fixed but didn’t merge back into the main branch. Those error messages are critical for this setup to function correctly.

Bottom line: Use eth-lightwallet with this version of ethjs-provider-signer: //github.com/ethjs/ethjs-provider-signer/pull/3 to save time on trial and error.

Encrypted offline storage

The lightwallet keystore vault JSON object is encrypted, and it requires from us an external passwordProvider to safely keep the encryption key. The keystrore object is always encrypted. The app is responsible for safekeeping the password and provides it with any action.

eth-hot-wallet uses Store.js — Cross-browser storage for all use cases, used across the web. Store.js allows us to store the encrypted keystore easily and extract it back from storage when the webpage is accessed.

When the wallet is loaded for the first time, it will check if there is a keystore in local storage and will auto load it to Redux state if so.

At this point, we can read the public data of the keystore but not the keys. To display public data before the user enters the encryption password, we need an additional operation mode: loaded and locked. In this mode, the wallet will display the addresses and fetch the balances but will not be able to perform operations such as sending transactions or even generating new addresses. Triggering any of those actions will prompt for the user’s password.

Sending Ethereum using Web3.js

When using [email protected], the function sendTransaction is provided in the following form:

web3.eth.sendTransaction(transactionObject [, callback])

The callback will return the TX as a result in case of success.

However, to properly integrate it into our saga.js flow, we need to wrap sendTransaction function with a promise:

This way we continue regular Saga.js execution after sendTransaction is called.

Sending erc20 tokens using Web3.js

The Ethereum blockchain does not provide primitives that encapsulate token functionality, nor should it. Every token deployed on Ethereum is, in fact, a program that corresponds to the eip20 specification. Since the Ethereum virtual machine (EVM) is Turing complete (with some restrictions), every token might have a different implementation (even for the same functionality). What unifies all those programs under the term “token” is that they provide the same API as defined by the specification.

When we are sending a token on Ethereum, we are interacting with a smart contract. To communicate with a smart contract we need to know its API, the format for sharing contract’s API called Ethereum Contract ABI.

We will store the erc20 ABI as part of our JavaScript bundle and instantiate a contract during the program run-time:

const erc20Contract = web3.eth.contract(erc20Abi);

After contract setup, we can easily interact with it programmatically using the Web3.js contract API.

For each token we will need a dedicated contract instance:

const tokenContract = erc20Contract.at(tokenContractAddress);

After the creation of contract instance, we can access the contract functions by calling the desired function straight from JavaScript:

See Web3.js contract API for the full details.

We will promisify the tokenContract.transfer.sendTransaction to use it with our redux-saga flow:

It is possible to use es6-promisify or similar instead of writing the promise directly, but I prefer the direct approach in this case.

Subscribing to Ethereum transaction life-cycle using Web3.js V1 and redux-saga channels

eth-hot-wallet uses web3.js v0.2.x and does not support this feature at the moment. The example is provided by another project. It is an important feature and should be used extensively.

The new version of Web3.js (V1.0.0) is shipped with a new contract API that can inform us about transaction life-cycle changes.

Enter the PromiEvent: A promise combined event emitter.

web3.eth.sendTransaction({...}).once('transactionHash', function(hash){ ... }).once('receipt', function(receipt){ ... }).on('confirmation', function(number, receipt){ ... }).on('error', function(error){ ... }).then(function(receipt){ //fired once the receipt is mined});

web3.eth.sendTransaction() will return an object (a promise) that will resolve once the transaction is mined. The same object will allow us to subscribe to ‘transactionHash’, ‘receipt’, ‘confirmation’ and ‘error’ events.

This API is far more informative and elegant than the one provided with 0.2.x version of Web3.js. We will see how we can integrate it into our web app with the help of Saga.js channels. The motivation is to update the application state (Redux store) once a change to the transaction state is detected.

In the following example, we will create a ‘commit’ transaction to an arbitrary smart contract and update app state when we get ‘transactionHash’, ‘receipt’ and ‘error’ events.

We need to initialize the new channel and fork a handler:

The handler will catch all channel events and will call the appropriate Redux action creator.

Once the channel and the handler are both ready and the user initiates the transaction, we need to register to the generated events:

In fact, we don't need a new channel for each transaction and can use the same channel for all types of transactions.

The full source code of this example can be found here.

Polling Ethereum blockchain and price data using redux-saga

There are several ways to watch for blockchain changes. It is possible to use Web3.js to subscribe to events or we can poll the blockchain by ourselves and have more control over some aspects of polling.

In eth-hot-wallet, the wallet is polling the blockchain periodically for balance changes and Coinmarketcap API for price changes.

This redux-saga pattern will allow us to poll any data source or API:

After the CHECK_BALANCES action is seen by the default saga, the checkAllBalances function is called. It can end with one of two possible outcomes: CHECK_BALANCES_SUCCESS or CHECK_BALANCES_ERROR . Each one of them will be caught by watchPollData() to wait X seconds and call checkAllBalance again. This routine will continue until STOP_POLL_BALANCES is caught by watchPollData . After that, it is possible to resume the polling by submitting CHECK_BALANCES action again.

Keeping an eye on the bundle size

When building web apps using JavaScript and npm, it might be tempting to add new packages without analyzing the footprint increase. Eth-hot-wallet uses webpack-monitor to display a chart of all the dependencies and the differences between each build. It allows the developer to see the bundle size change clearly after each new package is added.

Webpack monitor also can help in finding the most demanding dependencies and might even surprise the developer by highlighting the dependencies that do little for the app but contribute a lot to the bundle size.

Webpack-monitor is easy to integrate and is definitely worth including in any webpack based web app.

Conclusion

The issues presented in this article are only part of the challenges that need to be solved when building an Ethereum wallet. However, overcoming those issues will create a solid foundation and will allow us to continue and create a successful wallet.

Building a wallet can also be a great introduction to the world of Ethereum since most distributed applications (DApps) require a similar set of capabilities both from the front-end and blockchain perspective.

ETH Hot Wallet - Ethereum Wallet with ERC20 support

ETH Hot wallet is an Ethereum wallet with ERC20 Support. The keys are generated inside the browser and never sent…eth-hot-wallet.com

In case you have any questions regarding eth-hot-wallet or any related subject, feel free to contact me via Twitter or open an issue on GitHub.