123456789101112131415161718192021222324252627282930313233343536 |
- <?php
- namespace App\Servers\Icon;
- use Elliptic\EC;
- use kornrunner\Keccak;
- class Address
- {
- public $address;
- public $key;
- /**
- * @return array
- * @throws
- */
- public static function generate()
- {
- $ec = new EC('secp256k1');
- $pair = $ec->genKeyPair(); // 键值对
- $priv = $pair->getPrivate('hex'); // 导出私钥
- $pub = $pair->getPublic(false, 'hex'); // 导出公钥
- $pub = substr($pub, 2); // 移除开头的04
- $address = Keccak::hash(hex2bin($pub), 256); // 先转换为二进制, 再hash
- $address = Utils::fixHex(substr($address, -40)); // hash之后, 取后40位, 开头填充0x
- return ['key' => $priv, 'address' => $address];
- }
- }
|