|
|
@@ -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()
|
|
|
+ {
|
|
|
+
|
|
|
}
|
|
|
}
|