Address.php 743 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace App\Servers\Icon;
  3. use Elliptic\EC;
  4. use kornrunner\Keccak;
  5. class Address
  6. {
  7. public $address;
  8. public $key;
  9. /**
  10. * @return array
  11. * @throws
  12. */
  13. public static function generate()
  14. {
  15. $ec = new EC('secp256k1');
  16. $pair = $ec->genKeyPair(); // 键值对
  17. $priv = $pair->getPrivate('hex'); // 导出私钥
  18. $pub = $pair->getPublic(false, 'hex'); // 导出公钥
  19. $pub = substr($pub, 2); // 移除开头的04
  20. $address = Keccak::hash(hex2bin($pub), 256); // 先转换为二进制, 再hash
  21. $address = Utils::fixHex(substr($address, -40)); // hash之后, 取后40位, 开头填充0x
  22. return ['key' => $priv, 'address' => $address];
  23. }
  24. }