cryptoishere / bip39
易于使用的多语言PHP BIP39实现。
1.0.0
2021-12-22 11:45 UTC
Requires
- php: >=8.0.0
- ext-gmp: *
- illuminate/support: ^8.77
This package is not auto-updated.
Last update: 2024-09-27 00:36:46 UTC
README
这是一个易于使用、多语言、独立且与区块链无关的PHP实现,用于**BIP39建议。
此库允许在128-256位范围内将数据编码和解码为助记词列表。
您是否曾经使用过本地比特币钱包?记得备份私钥时输入的12个单词吗?此库允许在PHP上实现此功能。
1. 背景。
此库旨在同时考虑生产使用准备和概念学习。
这意味着所有代码都经过了详细的注释,提供了关于BIP39编码和解码单词序列每个方面的详细信息。
2. 安装
标准composer安装
composer require cryptoishere/bip39
3. 概念
在您的项目中实现此库时,您需要了解一些关键因素
3.1. 熵
在此库实现中,我们将熵定义为给定的一组数据,其位在128位到256位之间。
这意味着,例如,您可以生成一个用于ECDSA
的256位私钥,并允许您的用户将这个随机、难以记住的密钥编码成一组单词。
获取熵的方法
如果您已经有了一个要编码的值
<?php // aliases. use Cryptoishere\Bip39\Util\Entropy; $entropy = new Entropy($dataInHexadecimal);
或者,如果您想生成一些数据以创建私钥,则可以这样操作
<?php // aliases. use Cryptoishere\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 Cryptoishere\Bip39\Bip39; use Cryptoishere\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 Cryptoishere\Bip39\Bip39; use Cryptoishere\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] English
- [es] Spanish
- [fr] French
- [it] Italian
- [ja] Japanese
- [ko] Korean
- [zh] Chinese (Simplified).
只需在Bip39构造函数中使用语言区域即可
$bip39 = new Bip39('en'); $bip39 = new Bip39('es'); $bip39 = new Bip39('fr'); // ...
3.4. 特殊功能。
如前所述,此库还考虑了教育,因此,有一个缓冲的二进制操作类BitBuffer
和一些帮助您理解许多不同概念的实用工具。
学得很好!