| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services;
- use App\Models\MemberModel;
- use BitWasp\Bitcoin\Address\AddressCreator;
- use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress;
- use BitWasp\Bitcoin\Address\SegwitAddress;
- use BitWasp\Bitcoin\Bitcoin;
- use BitWasp\Bitcoin\Crypto\Random\Random;
- use BitWasp\Bitcoin\Key\Factory\HierarchicalKeyFactory;
- use BitWasp\Bitcoin\Key\Factory\PrivateKeyFactory;
- use BitWasp\Bitcoin\Mnemonic\Bip39\Bip39Mnemonic;
- use BitWasp\Bitcoin\Mnemonic\Bip39\Bip39SeedGenerator;
- use BitWasp\Bitcoin\Mnemonic\MnemonicFactory;
- use BitWasp\Bitcoin\Network\NetworkInterface;
- use BitWasp\Bitcoin\Script\ScriptFactory;
- use BitWasp\Bitcoin\Script\WitnessProgram;
- use BitWasp\Bitcoin\Script\WitnessScript;
- use BitWasp\Buffertools\Buffer;
- use GuzzleHttp\Client;
- use Tron\Api;
- use Tron\TRC20;
- use Tron\TRX;
- use Web3\Providers\HttpProvider;
- use Web3\RequestManagers\RequestManager;
- use Web3\Web3;
- use Web3p\EthereumUtil\Util;
- /**
- * USDT链管理-服务类
- * Class UsdtWalletService
- * @package App\Services
- */
- class UsdtWalletService extends BaseService
- {
- protected $config = [];
- protected $apiUrl = [];
- /**
- * 构造函数
- * UsdtWalletService constructor.
- */
- public function __construct()
- {
- $this->config = ConfigService::make()->getConfigOptionByGroup(4);
- $this->apiUrl = isset($this->config['usdt_api_url'])? $this->config['usdt_api_url'] : '';
- if (empty($this->config) || empty($this->apiUrl)) {
- return false;
- }
- }
- /**
- * 静态入口
- * @return UsdtWalletService|null
- */
- public static function make()
- {
- return parent::make(); // TODO: Change the autogenerated stub
- }
- /**
- * 获取TRC2.0钱包地址
- * @throws \Tron\Exceptions\TronErrorException
- */
- public function getTrxAddress()
- {
- $uri = 'https://api.trongrid.io';
- $api = new Api(new Client(['base_uri' => $uri]));
- $trxWallet = new TRX($api);
- $addressData = $trxWallet->generateAddress();
- $addressData = (array) $addressData;
- return ['wif'=> $addressData['privateKey'],'hexAddress'=> $addressData['hexAddress'],'address'=> $addressData['address']];
- }
- /**
- * 获取ERC2.0钱包地址
- * @param string $type
- * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
- */
- public function getErcAddress()
- {
- $random = new Random();
- $network = Bitcoin::getNetwork();
- $privateKeyFactory = new PrivateKeyFactory();
- $privateKey = $privateKeyFactory->generateCompressed($random);
- $publicKey = $privateKey->getPublicKey();
- // p2pkh 格式的地址
- $addressService = new PayToPubKeyHashAddress($publicKey->getPubKeyHash());
- // 将生成的钱包保存到数据库中
- $wif = $privateKey->toWif($network);
- $address = $addressService->getAddress();
- return ['wif'=> $wif,'hexAddress'=> $this->getHash16Address($address), 'address'=> $address];
- }
- /**
- * 获取ERC钱包地址
- * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
- */
- public function getWalletAddress()
- {
- $math = Bitcoin::getMath();
- $network = Bitcoin::getNetwork();
- $random = new Random();
- // 生成随机数(initial entropy)
- $entropy = $random->bytes(Bip39Mnemonic::MIN_ENTROPY_BYTE_LEN);
- $bip39 = MnemonicFactory::bip39();
- // 通过随机数生成助记词
- $mnemonic = $bip39->entropyToMnemonic($entropy);
- echo "mnemonic: " . $mnemonic.PHP_EOL.PHP_EOL;// 助记词
- $seedGenerator = new Bip39SeedGenerator();
- // 通过助记词生成种子,传入可选加密串
- $seed = $seedGenerator->getSeed($mnemonic,'otc');
- echo "seed: " . $seed->getHex() . PHP_EOL;
- $hdFactory = new HierarchicalKeyFactory();
- $master = $hdFactory->fromEntropy($seed);
- $util = new Util();
- // 设置路径account
- $hardened = $master->derivePath("44'/60'/0'/0/0");
- echo " - m/44'/60'/0'/0/0 " .PHP_EOL;
- echo " public key: " . $hardened->getPublicKey()->getHex().PHP_EOL;
- echo " private key: " . $hardened->getPrivateKey()->getHex().PHP_EOL;// 可以导入到imtoken使用的私钥
- echo " address: " . $util->publicKeyToAddress($util->privateKeyToPublicKey($hardened->getPrivateKey()->getA())) . PHP_EOL;// 私钥导入imtoken后一样的地址
- echo " address: " . $util->publicKeyToAddress($util->privateKeyToPublicKey($hardened->getPrivateKey()->getHex())) . PHP_EOL;// 私钥导入imtoken后一样的地址
- }
- public function getWebAddress($label='1')
- {
- $web3 = new Web3("https://cloudflare-eth.com");
- $web3->eth->personal->newAccount($label, function ($err, $account) use (&$newAccount) {
- if ($err !== null) {
- echo 'Error: ' . $err->getMessage();
- return;
- }
- $newAccount = $account;
- echo 'New account: ' . $account . PHP_EOL;
- });
- var_dump($newAccount);
- }
- /**
- * 获取HASH钱包地址
- * @param $address 钱包地址
- * @return \BitWasp\Buffertools\BufferInterface
- * @throws \BitWasp\Bitcoin\Exceptions\UnrecognizedAddressException
- */
- public function getHash16Address($address)
- {
- $data = WitnessProgram::v0((new AddressCreator())->fromString($address)->getHash());
- $buffer = $data->getProgram()->getHex();
- return $buffer? '0x'.$buffer : '';
- }
- /**
- * 创建交易参数
- * @param $payWif
- * @param $address
- * @param $amount
- * @param string $coin
- * @throws \BitWasp\Bitcoin\Exceptions\Base58ChecksumFailure
- * @throws \BitWasp\Bitcoin\Exceptions\InvalidPrivateKey
- * @throws \BitWasp\Bitcoin\Exceptions\UnrecognizedAddressException
- * @throws \BitWasp\Bitcoin\Exceptions\WitnessScriptException
- * api: https://services.tokenview.com/vipapi/onchainwallet/{币种简称小写}?apikey={apikey}
- */
- public function createTrade($payAddress, $address, $amount, $coin='etc')
- {
- $data = [
- 'owner_address'=> $payAddress,
- 'to_address'=> $address,
- 'amount'=> $amount,
- 'method'=> 'createtransaction',
- 'visible'=> false,
- ];
- $this->apiUrl = $this->apiUrl."/onchainwallet/{$coin}?apikey=".$this->config['usdt_apikey'];
- var_dump($data);
- var_dump($this->apiUrl);
- $result = curl_api($this->apiUrl, $data, ["Content-Type: application/json"]);
- var_dump($result);
- }
- /**
- * 广播交易上链
- * api: https://services.tokenview.com/vipapi/onchainwallet/trx?apikey=R8UNuoal7yGtw32PlksD
- */
- public function broadcastTrade()
- {
- }
- }
|