本文主要内容为区块链钱包移动端(Android)开发,介绍比特币钱包和以太坊钱包的开发过程,包含钱包的主要功能:
创建钱包,钱包余额,导出钱包,钱包转账等。Demo地址
This paper, which is mainly developed by Android, describes the development of Bitcoin and Ether wallets and includes the main functions of wallets:
to create wallets, wallet balances, export wallets, transfer of wallets, etc. Demo address
在日常生活中,大家都会买个钱包用于存放政府机构发行的纸币,那么什么是数字资产世界的钱包呢?
In everyday life, everyone buys a wallet for banknotes issued by government agencies, so what is the wallet of the digital asset world?
以太坊钱包:Mist、Parity、MyEhterWallet、ImToken、MetaTask、Ledger(硬件钱包)
Mist, Paris, , ImToken, MetaTask, Ledger (hardware wallet)
- 助记词等价于私钥
- Keystore + 密码 等价于私钥
EOS钱包
EOS wallet.
NEO钱包
NEO Wallet
量子钱包
Quantum wallet
-
On-chain
给一个钱包地址发送数字货币, 这笔交易在全网广播、被确认、被打包进区块。这是发生在链上的,被称为on-chain交易。on-chain钱包需要自己保管私钥。
Sends a digital currency to a wallet address, the transaction is broadcast on the Internet, confirmed, and packed into blocks. This happens on the chain and is referred to as an-chain transaction. On-chain wallets need to keep their own private keys.
-
Off-chain
相对于on-chain交易是off-chain交易。通常,通过交易所进行的交易是off-chain的,本人并没有私钥。私钥在交易所,由交易所托管。所以交易所的钱包也是中心化的钱包。
The transaction is off-chain. Usually, the transaction is carried out by off-chain and does not have a private key. The private key is on the exchange and is held by the exchange. So the wallet of the exchange is also a central wallet.
-
冷钱包
Cold wallet.
冷即离线、断网,也就是说私钥存储的位置不能被网络所访问。例如纸钱包、脑钱包、硬件钱包等等。
Cold is offline, offline, or offline, i.e. the location where the private key is stored cannot be accessed by the network. For example, paper wallets, brain wallets, hardware wallets, etc.
-
热钱包
Hot wallet.
热即联网,也就是私钥存储在能被网络访问的位置。 例如存放在交易所的、在线钱包网站、手机App钱包都属于热钱包。通常而言,冷钱包更加安全,热钱包使用更加方便。
Hot, i.e., private keys are stored in places that can be accessed online. For example, online wallet sites, mobile phone App wallets, which are stored on the exchange, are all hot wallets.
非确定性钱包
钱包随机生成Undetermined wallet
确定性钱包(HD Wallets)
同一个种子,能够派生一样的密钥对集合HD Wallets
is the same seed, capable of deriving the same key pairs-
全节点钱包
Full Node Wallet
除了保存私钥外,全节点钱包还有保存了所有区块的数据,最为著名的是bitcoin-core。
In addition to keeping private keys, the full node wallet contains data on all blocks, most well known as bitcoin-core.
-
轻钱包
Light wallet.
它不必保存所有区块的数据,只保存跟自己相关的数据。基本可以实现去中心化。
It does not have to store data on all blocks, but only data about itself.
-
中心化钱包
Centralized Wallet
在交易所中的钱包,以及类似 OKLink 提供的保险柜服务。
Wallet in exchange and safe service similar to that provided by OKLink.
https://bitcoin.org/en/developer-guide#wallets
- 比特币钱包的组成
- 比特币钱包地址的创建过程
- BIP32
- BIP39
- BIP43
- BIP44
- BitcoinJ创建钱包
- Bitcoin钱包收款和转账
比特币钱包分为两部分:钱包程序和钱包文件
Bitcoin wallets are divided into two parts: wallet programs and wallet files.
保存私钥和转账记录
Keep private keys and transfer records
钱包程序,创建公钥来接受satoshi,使用私钥来使用satoshi。钱包程序可以拆分出3个独立的模块:公钥分发模块、签名模块、网络模块
Wallet program, creating a public key to accept satoshi, using private keys to use satoshi. The wallet program can split out three separate modules: public key distribution module, signature module, network module
根据三个模块的组合,可以分为全服务钱包、只签名钱包(离线钱包和硬件钱包)、只分发钱包。
The combination of the three modules can be divided into full-service wallets, signed wallets (offline wallets and hardware wallets) and distributed wallets only.
BIP32:定义了层级确定性钱包(hierarchical deterministic wallets),是一个系统可以从单一个 seed 产生一树状结构储存多组 keypairs(私钥和公钥)。好处是可以方便的备份、转移到其他相容装置(因为都只需要 seed),以及分层的权限控制等。
BIP32: Defines the hierarchical deterministic wallets, a system that can generate a single Seed with multiple sets of keypairs (private and public keys). The benefits are convenient backups, transfer to other compatible devices (because all need only seed) and stratification control.
作用:
Role:
1、备份更容易。按照比特币的原则,尽量不要使用同一个地址,一个地址只使用一次,这样会导致频繁备份钱包。HD钱包只需要在创建时保存主密钥,通过主密钥可以派生出所有的子密钥。
1 A backup is easier. In accordance with the Bitcoin principle, try not to use the same address and one address only once, which leads to frequent backup wallets. HD wallets only need to save the main key at the time of creation, and all subkeys can be derived from the primary key.
2、私钥离线更安全。主私钥离线存储,主公钥在线使用,通过主公钥可以派生出所有的子公钥。例如:给每个商品提供一个收款地址。
2. The private key is safer offline. The primary key is stored offline, the main public key is used online, and all sub-public keys are derived from the main public key. For example, a collection address is provided for each commodity.
3、利于管理,权限控制。树状结构类似于公司的组织架构,可以给各个部门指定一个密钥分支。
A tree structure is similar to the organizational structure of the company and assigns a key branch to each department.
4、记账。只使用公钥即可记账。
4. Booking. You can account for it only by using a public key.
BIP39:将seed 用方便记忆和书写的单字表示。一般由 12 个单字组成,称为 mnemonic code(phrase),中文称为助记词或助记码。例如:
average green proud remember advance trick estate oblige trouble when cube person
BIP43对BIP32树结构增加了子索引标识purpose的拓展m/purpose'/*
Extension m/purpose'/* of the BIP43 sub-index indicator added to the BIP32 tree structure
BIP32的索引:m/0'/*
Index to BIP32: m/0'/*
BIP44的索引:m/44'/*。
Index to BIP44: m/44'/*.
BIP44:基于BIP32和BIP43,赋予树状结构中的各层特殊的意义。让同一个 seed 可以支援多币种、多帐户等。各层定义如下:
- purporse': 固定值44', 代表是BIP44
- coin_type': 这个代表的是币种, 可以兼容很多种币, 比如BTC是0', ETH是60', 例如:btc一般是 m/44'/0'/0'/0, eth一般是 m/44'/60'/0'/0
- account':账号
- change': 0表示外部链(External Chain),用户接收比特币,1表示内部链(Internal Chain),用于接收找零
- address_index:钱包索引
- 使用助记词(BIP39)
- 使用层级确定性钱包(HD Wallets)(BIP32)
- 使用多目的HD Wallets(BIP43)
- 使用多币种,多账号的HD Wallets (BIP44)
1、生成128bit~256bit作为私钥
1. Generate 128bit ~256bit as private key
2、通过secp256k1椭圆曲线算法得到私钥对应的公钥
2. The private key corresponds to the public key obtained through setp256k1 elliptical algorithm
3、将公钥进行SHA-256,得到公钥Hash
3. Put the public key on SHA-256 and get the public key Hash
4、将3的结果进行RIMEMD-160
4. Conducting the results of 3 with RIMMED-160
5、将4中结果添加1个字节版本号
5. Add a byte version number for result 4
6、将5中结果进行两次SHA-256,取前4个字节作为checksum
6. Conduct two SHA-256 exercises with the first four bytes as checksum
7、将6中结果添加到5中结果的末尾
7. Add result 6 to end of result 5
8、将7中结果进行Base58,结果为比特币地址
8. Base58 for medium 7 results, with Bitcoin address
Bitcoinj是比特币协议Java版本实现的库。
bitcoinj is the repository for the realization of the Java version of the Bitcoin Agreement.
添加依赖:
Add dependency:
Android最大方法数的限制,60K
开启multiDexEnabled
Maximum number limit for Android, 60K
to open multiDexEnabled
钱包创建源码分析:
Wallet creation source analysis:
Wallet
KeyChainGroup
DeterministicKeyChain
-
DeterministicSeed
在获取地址的过程中会调用RIMEMD-160算法处理公钥hash:
The RIMMED-160 algorithm is used to handle the public key hash in the process of getting the address:
处理公钥hash后会进行Base58算法:
After processing the public key hash, the Base58 algorithm is performed:
比特币钱包余额需要统计所有钱包地址对应的UTXO
Bitcoin wallet balances need to count UTXO corresponding to all wallet addresses.
Simplified Payment Verification (SPV):节点无需下载所有的区块数据,而只需要加载所有区块头数据(block header的大小为80B),即可验证这笔交易是否曾经被比特币网络认证过。
Simplified Payment Verification (SPV): Nodes do not need to download all block data, but simply need to load all block head data (block header size 80B) to verify whether the transaction was ever certified by the Bitcoin network.
布隆过滤器(Bloom Filter):过滤掉那些不包含有目标地址的交易信息,这一步能避免掉大量不相关的数据下载。
Bloom Filter: Filtering off transactions that do not contain a targeted address avoids a large number of unrelated data downloads.
获取测试用比特币:https://testnet.manu.backend.hamburg/faucet
刚收到的币可能需要几分钟后才能使用
比特币测试链转账查询
创建一个Tx,对Tx进行签名,对Tx进行P2P网络广播
https://github.com/ethereumbook/ethereumbook/blob/develop/wallets.asciidoc
以太坊钱包功能与比特币钱包功能类似,获取用户余额,管理地址和密钥,转账、智能合约调用。以太坊钱包一般不用在本地维护区块链数据,只需要使用JSON-RPC访问
It is similar to the Bitcoin wallet function, which captures user balances, manages addresses and keys, transfers, calls for smart contracts.
KeyStore=私钥 + 密码
KeyStore= Private Key + Password
如果使用ImToken创建钱包,创建了助记词,密码用来加密钱包地址对应的子私钥,加密的结果就是Keystore.
If ImToken is used to create wallets, help words are created, passwords are used to encrypt sub-keys to the wallet address, and encryption results are Keystore.
1、使用Secp256k1创建公私钥
1. Create a public-private key using Secp256k1
2、通过Keccak算法得到公钥Hash值,进而得到长度为40的地址字符串
2. Get a public key Hash through the Keccak algorithm, and then get an address string with a length of 40
3、一般的,会在地址字符串签名加前缀"0x"
Three, normal, prefix "0x" for address string signing.
添加Web3j依赖
Add Web3j Reliance
这里不涉及BIP协议,为非确定性钱包
It's not about the BIP. It's a non-determinate wallet.
Wallet.createStandard() 出现OOM, Out of Memory
https://juejin.im/post/5b4b07f8e51d45199060fe1f
Wallet.createStandard() appears from OOM, Out of Memory
涉及BIP协议,但没有遵循bitcoin地址只使用一次的原则,钱包一般只使用派生出来第一个地址
In relation to the BIP agreement, the principle of one-time use of the bitcoin address was not followed, and the wallet usually used only the first address that came out of it.
可通过工具检查派生的地址是否正确
The correct address can be checked through the tool.
一般可以将助记词加密存储,导出时解密。注意无法从KeyStore或者私钥导出助记词。
It is usually possible to encrypt notes and decrypt them when they are exported. Note that they cannot be exported from KeyStore or from a private key.
例如:使用IMToken导入私钥或者KeyStore创建的钱包,没有导出助记词的功能
如果是通过助记词创建的,就会有导出助记词的功能
For example, if IMToken is used to import a private key or a wallet created by KeyStore,
does not have the function to export the word helper if it is created by the word helper, it has the function to export the word helper.
调用ERC20代币智能合约,获取当前地址的余额
Call an ERP20 smart contract to get the balance of the current address
注册有任何问题请添加 微信:MVIP619 拉你进入群
打开微信扫一扫
添加客服
进入交流群
发表评论