上周我开源了一款钱包,反映很好,一周时间不到已经快到100 Star。接下来我会几篇系列文章把开发以太坊钱包的核心要点写出来,也算是对代码的一个解读。
Last week I opened a , which is good, less than 100 Stars in a week. Next I will write a series of articles on development at the core of my wallet, which is a reading of the code.
钱包是使用Android安卓平台编写,使用的是原生代码Java 语言编写, 是基于Java 1.8 版本,也使用了Java 1.8 中一些较新的语言特性,如 Lambda表达式等;另外还较多使用了ReactiveX/RxAndroid响应式编程用法。
The wallet was prepared using the Android Andre platform, using the original code Java language, based on Java 1.8 version, and some of the newer linguistic features of Java 1.8, such as Lambda expressions, etc.; ReactiveX/RxAndroid.
在本系列文章中,重点是介绍以太坊钱包账号、交易等逻辑,有时可能会假定读者已经了解Android开发等相关知识,因为这些内容不是文章的重点,因此不会过多介绍,请海涵。
In this series, the focus is on the logic of e-Taiwan’s wallet accounts, transactions, etc., and it may sometimes be assumed that readers are already aware of relevant knowledge such as Android’s development, as these content is not the focus of the article and will therefore not be presented too much.
通常一个钱包会包含以下功能:
Usually a wallet contains the following functions:
- 支持通过生成助记词、Keystore文件、私钥 创建钱包账号。
- 支持导出钱包账号助记词、私钥、Keystore文件。
- 支持多个钱包账号管理
- 账户余额查询及转账功能(二维码扫描支持)。
- 支持ERC20 代币(余额显示、转账、代币币价显示)
- 支持用法币(美元和人民币)实时显示币价。
- 历史交易列表显示
我们先来介绍第一个功能:通过生成助记词、Keystore文件、私钥创建钱包账号。 本系列中,钱包都是指分层确定性钱包,(HD钱包 Hierarchical Deterministic Wallets), 之前博客有一篇文章分层钱包进行了详细的介绍,还不熟悉的可以读一下。 为了保持本文的完整,这里做一个总结性回顾:以太坊及比特币的地址是由随机生成的私钥经过椭圆曲线等算法单向推倒而来 ,BIP32及BIP44是为方便管理私钥提出的分层推倒方案,BIP39 定义助记词让分层种子的备份更方便。 而KeyStore文件是用来解密以太坊保存私钥的一种方式,大家可以阅读下这篇文章: 账号Keystore文件导入导出了解更多。
In this series, the wallet refers to the stratification wallet (HD wallet Hiarchical Deterministic Wallets), a former blog article 这是一张导入钱包账号的截图(导入和创建,其实原理一样),界面仿照ImToken,不过本文将不会介绍UI部分的编写。 This is a screenshot of an imported wallet account (import and creation, in fact, the same principle) with an ImToken interface, but this will not describe the preparation of the UI part. 为了完成创建账号功能,我们需要使用到两个库:Web3j 和 bitcoinj In order to complete the creation of the account, we need to use two libraries: Web3j and
Web3是一套和以太坊通信的封装库,Web3j是Java版本的实现,例如发起交易和智能合约进行交互,下图很好的表达了其作用。
Web3 is a set of envelopes for Etheraya communications, and Web3j is the realization of Java's version, such as the initiation of transactions and the interaction of smart contracts, whose role is well expressed in the figure below. 不过本文中的功能,主要是使用了web3j中椭圆曲线加密及KeyStore文件的生成与解密。 However, the features in this paper are mainly the encryption of elliptical curves in web3j and the generation and decryption of KeyStore files. bitcoinj 的功能和web3类似,它是比特币协议的Java实现,他实现了 BIP32、BIP44及BIP39 相关协议。 The function of bitcoinj, similar to that of web3, is the realization of Java under the Bitcoin Agreement, which fulfils the BIP32, BIP44 and BIP39 related agreements. Android使用Gradle来构建,直接在文件中加入: Android uses Gradle to construct it by adding directly to the document: 提示: 实践中遇到的一个问题,由于bitcoinj 中引入了 加密库, 它包含的文件,会导致在进行Android App Bundle 编译时会出现错误(好像也会导致某些机型没法安装),解决办法是在 build.gradle 加入一下语句,把这个文件在打包时排除掉。
packagingOptions {
exclude 'lib/x86_64/darwin/libscrypt.dylib'
} Note: A problem encountered in practice as a result of the introduction of encrypt libraries in bitcoinj, which contain documents that lead to errors in the preparation of Android App Bundle (as if some of the models could not be installed), the solution is to add a statement to the building. gradle and remove this file from the package. 这是目前钱包客户端,最常见的一种为用户常见账号的方式,这里会包含一下几个核心步骤: This is the current wallet client, the most common type of account for a user, which contains a few core steps: 大家可以在再次阅读分层钱包,理解为何这么做的原因。 You can read /a) understand the reasons for this. 理解了上面几点,那么代码就容易明白了,代码在代码库中的中,关键代码逻辑如下: In , the key code logic is as follows: 上述代码中,是入口函数,最终返回的是一个ETHWallet 自定义的钱包实体类,一个实例就对应一个钱包,ETHWallet保存了钱包相关的属性,后面会详细介绍,如果对它序列化保存钱包账号及多个钱包账号管理。 In the above code, which is an entry function, the final return is an ETHWallet self-defined wallet entity category, and one example corresponds to a wallet in which the ETHWallet retains the properties associated with the wallet, which is described in detail at a later stage, if the wallet account number and multiple wallet accounts are managed in a sequenced manner. 关于助记词及私钥的保存,有几点要特别注意,否则有可能和其他钱包无法兼容或导致私钥泄漏。 With regard to the preservation of notes and private keys, there are a number of points where pays particular attention to
这部分作为订阅者福利,发表在我的小专栏,趁还未涨价,赶紧订阅吧,超值的! This part of the subscription benefit is published in my , before the price rises, subscribe, overvalued! 加入知识星球,和一群优秀的区块链从业者一起学习。
深入浅出区块链 - 系统学习区块链,打造最好的区块链技术博客。 Join , with a group of excellent block operators. the best-learning section of the chain.
注册有任何问题请添加 微信:MVIP619 拉你进入群
打开微信扫一扫
添加客服
进入交流群
发表评论