// +---------------------------------------------------------------------- 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\PrivateKeyFactory; 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 Web3\Web3; /** * 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 } /** * 获取钱包地址 * @param string $type * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure */ public function getWalletAddress($type = 'trc') { $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, 'address'=> $address]; } 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; }); } /** * 获取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() { } }