Browse Source

Weenier 168otc项目部署 0630

wesmiler 3 years ago
parent
commit
b1799f6845

+ 41 - 0
app/Helpers/function.php

@@ -107,3 +107,44 @@ if (!function_exists('xmlToArray')) {
         return $result;
     }
 }
+
+
+if (!function_exists('curl_api')) {
+
+    /**
+     * curl请求(POST)
+     * @param $url 请求地址
+     * @param array $data 请求参数
+     * @return bool|string 返回结果
+     * @author laravel开发员
+     * @date 2019/6/5
+     */
+    function curl_api($url, $data = [], $header=[], $timeout=20)
+    {
+        // 初始化
+        $ch = curl_init();
+        // 设置post方式提交
+        curl_setopt($ch, CURLOPT_POST, 1);
+        // 设置头文件的信息作为数据流输出
+        curl_setopt($ch, CURLOPT_HEADER, 0);
+        // 超时
+        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
+        // 是否要求返回数据
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+        // 请求头
+        if($header){
+            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
+        }
+        // 设置抓取的url
+        curl_setopt($ch, CURLOPT_URL, $url);
+        // 提交的数据
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
+        // 是否检测服务器的证书是否由正规浏览器认证过的授权CA颁发的
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
+        // 执行命令
+        $result = curl_exec($ch);
+        // 关闭URL请求(释放句柄)
+        curl_close($ch);
+        return $result;
+    }
+}

+ 13 - 1
app/Http/Controllers/Api/IndexController.php

@@ -82,8 +82,20 @@ class IndexController extends webApp
     }
 
     public function address(){
-        $result = UsdtWalletService::make()->getWalletAddress();
+        $address = request()->post('address','');
+        $result = UsdtWalletService::make()->getAddress();
 
+        var_dump($result);
+
+    }
+
+    public function transfer(){
+        $address1 = request()->post('payAddress','');
+        $address = request()->post('address','');
+        $num = request()->post('num',0);
+        $result = UsdtWalletService::make()->createTrade($address1, $address, $num);
+
+        var_dump($result);
 
     }
 }

+ 5 - 3
app/Services/Api/MemberService.php

@@ -57,7 +57,7 @@ class MemberService extends BaseService
      */
     public function getInfo($where, array $field=[])
     {
-        $field = $field? $field : ['id','username','realname','nickname','openid','idcard','source','idcard_check','safe_level','user_type','member_level','usdt_num','user_type','status','credit','avatar'];
+        $field = $field? $field : ['id','username','realname','nickname','openid','idcard','trc_address','erc_address','source','idcard_check','safe_level','user_type','member_level','usdt_num','user_type','status','credit','avatar'];
         if(is_array($where)){
             $info = $this->model->where($where)->select($field)->first();
         }else{
@@ -101,9 +101,10 @@ class MemberService extends BaseService
         ];
 
         // 生成trc2.0钱包地址
-        $trcAddress = UsdtWalletService::make()->getWalletAddress();
+        $trcAddress = UsdtWalletService::make()->getTrxAddress();
         if($trcAddress){
             $data['trc_wif'] = isset($trcAddress['wif'])? $trcAddress['wif'] : '';
+            $data['trc_hexaddress'] = isset($trcAddress['hexAddress'])? $trcAddress['hexAddress'] : '';
             $data['trc_address'] = isset($trcAddress['address'])? $trcAddress['address'] : '';
         }else{
             $this->error = 2201;
@@ -111,9 +112,10 @@ class MemberService extends BaseService
         }
 
         // 生erc2.0钱包地址
-        $ercAddress = UsdtWalletService::make()->getWalletAddress();
+        $ercAddress = UsdtWalletService::make()->getErcAddress();
         if($trcAddress){
             $data['erc_wif'] = isset($ercAddress['wif'])? $ercAddress['wif'] : '';
+            $data['erc_hexaddress'] = isset($ercAddress['hexAddress'])? $ercAddress['hexAddress'] : '';
             $data['erc_address'] = isset($ercAddress['address'])? $ercAddress['address'] : '';
         }else{
             $this->error = 2202;

+ 126 - 20
app/Services/UsdtWalletService.php

@@ -14,9 +14,25 @@ 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\Web3;
+use Web3p\EthereumUtil\Util;
 
 /**
  * USDT链管理-服务类
@@ -26,6 +42,7 @@ use BitWasp\Bitcoin\Network\NetworkInterface;
 class UsdtWalletService extends BaseService
 {
     protected $config = [];
+    protected $apiUrl = [];
 
     /**
      * 构造函数
@@ -33,8 +50,9 @@ class UsdtWalletService extends BaseService
      */
     public function __construct()
     {
-        $this->config = ConfigService::make()->getConfigByGroup(2);
-        if (empty($this->config)) {
+        $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;
         }
     }
@@ -49,30 +67,29 @@ class UsdtWalletService extends BaseService
     }
 
     /**
-     * 获取
+     * 获取TRC2.0钱包地址
+     * @throws \Tron\Exceptions\TronErrorException
      */
-    public function getLink()
+    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']];
     }
 
     /**
-     * 创建转入TRC2.0转账交易
-     * api: https://services.tokenview.com/vipapi/onchainwallet/{币种简称小写}?apikey={apikey}
-     */
-    public function transferTrc20()
-    {
-        //
-    }
-
-    /**
-     * 获取钱包地址
+     * 获取ERC2.0钱包地址
      * @param string $type
      * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
      */
-    public function getWalletAddress()
+    public function getErcAddress()
     {
         $random = new Random();
+        $network = Bitcoin::getNetwork();
         $privateKeyFactory = new PrivateKeyFactory();
         $privateKey = $privateKeyFactory->generateCompressed($random);
         $publicKey = $privateKey->getPublicKey();
@@ -81,11 +98,61 @@ class UsdtWalletService extends BaseService
         $addressService = new PayToPubKeyHashAddress($publicKey->getPubKeyHash());
 
         // 将生成的钱包保存到数据库中
-        $network = null;
         $wif = $privateKey->toWif($network);
         $address = $addressService->getAddress();
 
-        return ['wif'=> $wif, 'address'=> $address];
+        return ['wif'=> $wif,'hexAddress'=> $this->getHash16Address($address), 'address'=> $address];
+    }
+
+    /**
+     * 获取钱包地址
+     * @param string $type
+     * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
+     */
+    public function getWalletAddress1($type = 'trc')
+    {
+
+        $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();
+        $web3->personal->newAccount($label, function ($err, $account) use (&$newAccount) {
+            if ($err !== null) {
+                echo 'Error: ' . $err->getMessage();
+                return;
+            }
+            $newAccount = $account;
+            echo 'New account: ' . $account . PHP_EOL;
+        });
     }
 
     /**
@@ -94,9 +161,48 @@ class UsdtWalletService extends BaseService
      * @return \BitWasp\Buffertools\BufferInterface
      * @throws \BitWasp\Bitcoin\Exceptions\UnrecognizedAddressException
      */
-    public function getHashAddress($address)
+    public function getHash16Address($address)
     {
-        $data = (new AddressCreator())->fromString($address)->getHash();
-        return isset($data['buffer'])? $data['buffer'] : '';
+        $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()
+    {
+
     }
 }