UsdtWalletService.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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\HierarchicalKeyFactory;
  19. use BitWasp\Bitcoin\Key\Factory\PrivateKeyFactory;
  20. use BitWasp\Bitcoin\Mnemonic\Bip39\Bip39Mnemonic;
  21. use BitWasp\Bitcoin\Mnemonic\Bip39\Bip39SeedGenerator;
  22. use BitWasp\Bitcoin\Mnemonic\MnemonicFactory;
  23. use BitWasp\Bitcoin\Network\NetworkInterface;
  24. use BitWasp\Bitcoin\Script\ScriptFactory;
  25. use BitWasp\Bitcoin\Script\WitnessProgram;
  26. use BitWasp\Bitcoin\Script\WitnessScript;
  27. use BitWasp\Buffertools\Buffer;
  28. use GuzzleHttp\Client;
  29. use Tron\Api;
  30. use Tron\TRC20;
  31. use Tron\TRX;
  32. use Web3\Providers\HttpProvider;
  33. use Web3\RequestManagers\RequestManager;
  34. use Web3\Web3;
  35. use Web3p\EthereumUtil\Util;
  36. /**
  37. * USDT链管理-服务类
  38. * Class UsdtWalletService
  39. * @package App\Services
  40. */
  41. class UsdtWalletService extends BaseService
  42. {
  43. protected $config = [];
  44. protected $apiUrl = [];
  45. /**
  46. * 构造函数
  47. * UsdtWalletService constructor.
  48. */
  49. public function __construct()
  50. {
  51. $this->config = ConfigService::make()->getConfigOptionByGroup(4);
  52. $this->apiUrl = isset($this->config['usdt_api_url'])? $this->config['usdt_api_url'] : '';
  53. if (empty($this->config) || empty($this->apiUrl)) {
  54. return false;
  55. }
  56. }
  57. /**
  58. * 静态入口
  59. * @return UsdtWalletService|null
  60. */
  61. public static function make()
  62. {
  63. return parent::make(); // TODO: Change the autogenerated stub
  64. }
  65. /**
  66. * 获取TRC2.0钱包地址
  67. * @throws \Tron\Exceptions\TronErrorException
  68. */
  69. public function getTrxAddress()
  70. {
  71. $uri = 'https://api.trongrid.io';
  72. $api = new Api(new Client(['base_uri' => $uri]));
  73. $trxWallet = new TRX($api);
  74. $addressData = $trxWallet->generateAddress();
  75. $addressData = (array) $addressData;
  76. return ['wif'=> $addressData['privateKey'],'hexAddress'=> $addressData['hexAddress'],'address'=> $addressData['address']];
  77. }
  78. /**
  79. * 获取ERC2.0钱包地址
  80. * @param string $type
  81. * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
  82. */
  83. public function getErcAddress()
  84. {
  85. $random = new Random();
  86. $network = Bitcoin::getNetwork();
  87. $privateKeyFactory = new PrivateKeyFactory();
  88. $privateKey = $privateKeyFactory->generateCompressed($random);
  89. $publicKey = $privateKey->getPublicKey();
  90. // p2pkh 格式的地址
  91. $addressService = new PayToPubKeyHashAddress($publicKey->getPubKeyHash());
  92. // 将生成的钱包保存到数据库中
  93. $wif = $privateKey->toWif($network);
  94. $address = $addressService->getAddress();
  95. return ['wif'=> $wif,'hexAddress'=> $this->getHash16Address($address), 'address'=> $address];
  96. }
  97. /**
  98. * 获取ERC钱包地址
  99. * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
  100. */
  101. public function getWalletAddress()
  102. {
  103. $math = Bitcoin::getMath();
  104. $network = Bitcoin::getNetwork();
  105. $random = new Random();
  106. // 生成随机数(initial entropy)
  107. $entropy = $random->bytes(Bip39Mnemonic::MIN_ENTROPY_BYTE_LEN);
  108. $bip39 = MnemonicFactory::bip39();
  109. // 通过随机数生成助记词
  110. $mnemonic = $bip39->entropyToMnemonic($entropy);
  111. echo "mnemonic: " . $mnemonic.PHP_EOL.PHP_EOL;// 助记词
  112. $seedGenerator = new Bip39SeedGenerator();
  113. // 通过助记词生成种子,传入可选加密串
  114. $seed = $seedGenerator->getSeed($mnemonic,'otc');
  115. echo "seed: " . $seed->getHex() . PHP_EOL;
  116. $hdFactory = new HierarchicalKeyFactory();
  117. $master = $hdFactory->fromEntropy($seed);
  118. $util = new Util();
  119. // 设置路径account
  120. $hardened = $master->derivePath("44'/60'/0'/0/0");
  121. echo " - m/44'/60'/0'/0/0 " .PHP_EOL;
  122. echo " public key: " . $hardened->getPublicKey()->getHex().PHP_EOL;
  123. echo " private key: " . $hardened->getPrivateKey()->getHex().PHP_EOL;// 可以导入到imtoken使用的私钥
  124. echo " address: " . $util->publicKeyToAddress($util->privateKeyToPublicKey($hardened->getPrivateKey()->getA())) . PHP_EOL;// 私钥导入imtoken后一样的地址
  125. echo " address: " . $util->publicKeyToAddress($util->privateKeyToPublicKey($hardened->getPrivateKey()->getHex())) . PHP_EOL;// 私钥导入imtoken后一样的地址
  126. }
  127. public function getWebAddress($label='1')
  128. {
  129. $web3 = new Web3("https://cloudflare-eth.com");
  130. $web3->eth->personal->newAccount($label, function ($err, $account) use (&$newAccount) {
  131. if ($err !== null) {
  132. echo 'Error: ' . $err->getMessage();
  133. return;
  134. }
  135. $newAccount = $account;
  136. echo 'New account: ' . $account . PHP_EOL;
  137. });
  138. var_dump($newAccount);
  139. }
  140. /**
  141. * 获取HASH钱包地址
  142. * @param $address 钱包地址
  143. * @return \BitWasp\Buffertools\BufferInterface
  144. * @throws \BitWasp\Bitcoin\Exceptions\UnrecognizedAddressException
  145. */
  146. public function getHash16Address($address)
  147. {
  148. $data = WitnessProgram::v0((new AddressCreator())->fromString($address)->getHash());
  149. $buffer = $data->getProgram()->getHex();
  150. return $buffer? '0x'.$buffer : '';
  151. }
  152. /**
  153. * 创建交易参数
  154. * @param $payWif
  155. * @param $address
  156. * @param $amount
  157. * @param string $coin
  158. * @throws \BitWasp\Bitcoin\Exceptions\Base58ChecksumFailure
  159. * @throws \BitWasp\Bitcoin\Exceptions\InvalidPrivateKey
  160. * @throws \BitWasp\Bitcoin\Exceptions\UnrecognizedAddressException
  161. * @throws \BitWasp\Bitcoin\Exceptions\WitnessScriptException
  162. * api: https://services.tokenview.com/vipapi/onchainwallet/{币种简称小写}?apikey={apikey}
  163. */
  164. public function createTrade($payAddress, $address, $amount, $coin='etc')
  165. {
  166. $data = [
  167. 'owner_address'=> $payAddress,
  168. 'to_address'=> $address,
  169. 'amount'=> $amount,
  170. 'method'=> 'createtransaction',
  171. 'visible'=> false,
  172. ];
  173. $this->apiUrl = $this->apiUrl."/onchainwallet/{$coin}?apikey=".$this->config['usdt_apikey'];
  174. var_dump($data);
  175. var_dump($this->apiUrl);
  176. $result = curl_api($this->apiUrl, $data, ["Content-Type: application/json"]);
  177. var_dump($result);
  178. }
  179. /**
  180. * 广播交易上链
  181. * api: https://services.tokenview.com/vipapi/onchainwallet/trx?apikey=R8UNuoal7yGtw32PlksD
  182. */
  183. public function broadcastTrade()
  184. {
  185. }
  186. }