blocker / bip39
PHP的BIP39实现,易于使用,支持多语言。
0.10.0
2018-08-23 02:36 UTC
Requires
- php: >=7.1.0
- ext-gmp: *
- illuminate/support: ^5.6
This package is not auto-updated.
Last update: 2024-09-13 10:48:50 UTC
README
PHP的BIP39提案的易于使用、多语言、独立和区块链无关的实现。
此库允许将128-256位范围内的数据进行编码和解码,生成助记词列表。
您是否使用过本地比特币钱包?记得您备份私钥时输入的12个单词吗?此库允许在PHP上实现这一功能。
1. 背景。
此库旨在同时考虑生产使用准备和概念学习。
这意味着所有代码都有详细的注释,描述了编码和解码BIP39词序列的每个细节。
2. 安装
标准composer安装
composer require blocker/bip39
3. 概念
在您的项目中实现此库时,您需要了解一些关键因素
3.1. 熵
在此库实现中,我们将熵定义为128位到256位之间的数据集。
这意味着,例如,您可以生成一个256位的ECDSA私钥,并允许用户将这个随机、难以记忆的密钥编码成一组单词。
获取熵的方法
如果您已经有了想要编码的值
<?php // aliases. use Blocker\Bip39\Util\Entropy; $entropy = new Entropy($dataInHexadecimal);
或者,如果您想生成一些数据然后创建私钥,可以这样做
<?php // aliases. use Blocker\Bip39\Util\Entropy; // the parameter here is the size, in bits, of the random data to be generated. // values can be between 128 and 256, and must be multiples of 32. $entropy = Entropy::random(128);
3.2. 编码和解码。
与使用熵一样简单,从熵解析到单词序列以及反之亦然都很容易
// aliases. use Blocker\Bip39\Bip39; use Blocker\Bip39\Util\Entropy; // a word sequence provided by the user. $some128bitValueAlreadyEncoded = 'walnut antenna forward shuffle invest legal confirm polar hope timber pear cover'; // create a bip39 instance. $bip39 = new Bip39('en'); // decode the given word list into an entropy instance. $entropy = $bip39->decode($some128bitValueAlreadyEncoded); // decode the provided word sequence into a hexadecimal encoded entropy. echo (string) $entropy; // "f6c1396f63b75efecbbd3b6d7c468818"
并且,与编码一样简单
// aliases. use Blocker\Bip39\Bip39; use Blocker\Bip39\Util\Entropy; // some entropy value to be encoded with BIP39. $previousGeneratedEntropyHex = 'f6c1396f63b75efecbbd3b6d7c468818'; //$some128bitValueAlreadyEncoded = 'walnut antenna forward shuffle invest legal confirm polar hope timber pear cover'; // create a bip39 instance. $bip39 = new Bip39('en'); // create an entropy instance from it's hex representation. $entropy = new Entropy($previousGeneratedEntropyHex); echo (string) $bip39->setEntropy($entropy)->encode(); // 'walnut antenna forward shuffle invest legal confirm polar hope timber pear cover'
很简单,对吧?
3.3. 支持的语言
- [en] 英语
- [es] 西班牙语
- [fr] 法语
- [it] 意大利语
- [ja] 日语
- [ko] 韩语
- [zh] 中文(简体)。
只需在Bip39构造函数中使用语言区域即可
$bip39 = new Bip39('en'); $bip39 = new Bip39('es'); $bip39 = new Bip39('fr'); // ...
3.4. 特殊功能。
如前所述,此库还考虑了教育,因此有一个缓冲二进制操作类BitBuffer
和一些帮助您理解许多不同概念的实用工具。
学习愉快!