UsdtWalletService_back.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\Address\SegwitAddress;
  16. use BitWasp\Bitcoin\Bitcoin;
  17. use BitWasp\Bitcoin\Crypto\Random\Random;
  18. use BitWasp\Bitcoin\Key\Factory\PrivateKeyFactory;
  19. use BitWasp\Bitcoin\Network\NetworkInterface;
  20. use BitWasp\Bitcoin\Script\ScriptFactory;
  21. use BitWasp\Bitcoin\Script\WitnessProgram;
  22. use BitWasp\Bitcoin\Script\WitnessScript;
  23. use BitWasp\Buffertools\Buffer;
  24. use Web3\Web3;
  25. /**
  26. * USDT链管理-服务类
  27. * Class UsdtWalletService
  28. * @package App\Services
  29. */
  30. class UsdtWalletService extends BaseService
  31. {
  32. protected $config = [];
  33. protected $apiUrl = [];
  34. /**
  35. * 构造函数
  36. * UsdtWalletService constructor.
  37. */
  38. public function __construct()
  39. {
  40. $this->config = ConfigService::make()->getConfigOptionByGroup(4);
  41. $this->apiUrl = isset($this->config['usdt_api_url'])? $this->config['usdt_api_url'] : '';
  42. if (empty($this->config) || empty($this->apiUrl)) {
  43. return false;
  44. }
  45. }
  46. /**
  47. * 静态入口
  48. * @return UsdtWalletService|null
  49. */
  50. public static function make()
  51. {
  52. return parent::make(); // TODO: Change the autogenerated stub
  53. }
  54. /**
  55. * 获取钱包地址
  56. * @param string $type
  57. * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
  58. */
  59. public function getWalletAddress($type = 'trc')
  60. {
  61. $random = new Random();
  62. $network = Bitcoin::getNetwork();
  63. $privateKeyFactory = new PrivateKeyFactory();
  64. $privateKey = $privateKeyFactory->generateCompressed($random);
  65. $publicKey = $privateKey->getPublicKey();
  66. // p2pkh 格式的地址
  67. $addressService = new PayToPubKeyHashAddress($publicKey->getPubKeyHash());
  68. // 将生成的钱包保存到数据库中
  69. $wif = $privateKey->toWif($network);
  70. $address = $addressService->getAddress();
  71. return ['wif'=> $wif, 'address'=> $address];
  72. }
  73. public function getWebAddress($label='1')
  74. {
  75. $web3 = new Web3();
  76. $web3->personal->newAccount($label, function ($err, $account) use (&$newAccount) {
  77. if ($err !== null) {
  78. echo 'Error: ' . $err->getMessage();
  79. return;
  80. }
  81. $newAccount = $account;
  82. echo 'New account: ' . $account . PHP_EOL;
  83. });
  84. }
  85. /**
  86. * 获取HASH钱包地址
  87. * @param $address 钱包地址
  88. * @return \BitWasp\Buffertools\BufferInterface
  89. * @throws \BitWasp\Bitcoin\Exceptions\UnrecognizedAddressException
  90. */
  91. public function getHash16Address($address)
  92. {
  93. $data = WitnessProgram::v0((new AddressCreator())->fromString($address)->getHash());
  94. $buffer = $data->getProgram()->getHex();
  95. return $buffer? '0x'.$buffer : '';
  96. }
  97. /**
  98. * 创建交易参数
  99. * @param $payWif
  100. * @param $address
  101. * @param $amount
  102. * @param string $coin
  103. * @throws \BitWasp\Bitcoin\Exceptions\Base58ChecksumFailure
  104. * @throws \BitWasp\Bitcoin\Exceptions\InvalidPrivateKey
  105. * @throws \BitWasp\Bitcoin\Exceptions\UnrecognizedAddressException
  106. * @throws \BitWasp\Bitcoin\Exceptions\WitnessScriptException
  107. * api: https://services.tokenview.com/vipapi/onchainwallet/{币种简称小写}?apikey={apikey}
  108. */
  109. public function createTrade($payAddress, $address, $amount, $coin='etc')
  110. {
  111. $data = [
  112. 'owner_address'=> $payAddress,
  113. 'to_address'=> $address,
  114. 'amount'=> $amount,
  115. 'method'=> 'createtransaction',
  116. 'visible'=> false,
  117. ];
  118. $this->apiUrl = $this->apiUrl."/onchainwallet/{$coin}?apikey=".$this->config['usdt_apikey'];
  119. var_dump($data);
  120. var_dump($this->apiUrl);
  121. $result = curl_api($this->apiUrl, $data, ["Content-Type: application/json"]);
  122. var_dump($result);
  123. }
  124. /**
  125. * 广播交易上链
  126. * api: https://services.tokenview.com/vipapi/onchainwallet/trx?apikey=R8UNuoal7yGtw32PlksD
  127. */
  128. public function broadcastTrade()
  129. {
  130. }
  131. }