UsdtWalletService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\MemberModel;
  13. use BitWasp\Bitcoin\Address\AddressCreator;
  14. use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress;
  15. use BitWasp\Bitcoin\Crypto\Random\Random;
  16. use BitWasp\Bitcoin\Key\Factory\PrivateKeyFactory;
  17. use BitWasp\Bitcoin\Network\NetworkInterface;
  18. /**
  19. * USDT链管理-服务类
  20. * Class UsdtWalletService
  21. * @package App\Services
  22. */
  23. class UsdtWalletService extends BaseService
  24. {
  25. protected $config = [];
  26. /**
  27. * 构造函数
  28. * UsdtWalletService constructor.
  29. */
  30. public function __construct()
  31. {
  32. $this->config = ConfigService::make()->getConfigByGroup(2);
  33. if (empty($this->config)) {
  34. return false;
  35. }
  36. }
  37. /**
  38. * 静态入口
  39. * @return UsdtWalletService|null
  40. */
  41. public static function make()
  42. {
  43. return parent::make(); // TODO: Change the autogenerated stub
  44. }
  45. /**
  46. * 获取
  47. */
  48. public function getLink()
  49. {
  50. }
  51. /**
  52. * 创建转入TRC2.0转账交易
  53. * api: https://services.tokenview.com/vipapi/onchainwallet/{币种简称小写}?apikey={apikey}
  54. */
  55. public function transferTrc20()
  56. {
  57. //
  58. }
  59. /**
  60. * 获取钱包地址
  61. * @param string $type
  62. * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
  63. */
  64. public function getWalletAddress()
  65. {
  66. $random = new Random();
  67. $privateKeyFactory = new PrivateKeyFactory();
  68. $privateKey = $privateKeyFactory->generateCompressed($random);
  69. $publicKey = $privateKey->getPublicKey();
  70. // p2pkh 格式的地址
  71. $addressService = new PayToPubKeyHashAddress($publicKey->getPubKeyHash());
  72. // 将生成的钱包保存到数据库中
  73. $network = null;
  74. $wif = $privateKey->toWif($network);
  75. $address = $addressService->getAddress();
  76. return ['wif'=> $wif, 'address'=> $address];
  77. }
  78. /**
  79. * 获取HASH钱包地址
  80. * @param $address 钱包地址
  81. * @return \BitWasp\Buffertools\BufferInterface
  82. * @throws \BitWasp\Bitcoin\Exceptions\UnrecognizedAddressException
  83. */
  84. public function getHashAddress($address)
  85. {
  86. $data = (new AddressCreator())->fromString($address)->getHash();
  87. return isset($data['buffer'])? $data['buffer'] : '';
  88. }
  89. }