-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 04b7222
Showing
18 changed files
with
1,313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
vendor | ||
*.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 ufado | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<h1 align="center">TRON-PHP</h1> | ||
|
||
## 概述 | ||
|
||
TRON-PHP 目前支持波场的 TRX 和 TRC20 中常用生成地址,发起转账,离线签名等功能。 | ||
|
||
## 特点 | ||
|
||
1. 一套写法兼容 TRON 网络中 TRX 货币和 TRC 系列所有通证 | ||
1. 接口方法可可灵活增减 | ||
|
||
## 支持方法 | ||
|
||
- 生成地址 `generateAddress()` | ||
- 验证地址 `validateAddress(Address $address)` | ||
- 根据私钥得到地址 `privateKeyToAddress(string $privateKeyHex)` | ||
- 查询余额 `balance(Address $address)` | ||
- 交易转账(离线签名) `transfer(Address $from, Address $to, float $amount)` | ||
- 查询最新区块 `blockNumber()` | ||
- 根据区块链查询信息 `blockByNumber(int $blockID)` | ||
- 根据交易哈希查询信息 `transactionReceipt(string $txHash)` | ||
|
||
## 快速开始 | ||
|
||
### 安装 | ||
|
||
``` php | ||
composer require fenguoz/tron-php | ||
``` | ||
|
||
### 接口调用 | ||
|
||
``` php | ||
use GuzzleHttp\Client; | ||
|
||
$uri = 'https://api.shasta.trongrid.io';// shasta testnet | ||
$api = new \Tron\Api(new Client(['base_uri' => $uri])); | ||
|
||
$trxWallet = new \Tron\TRX($api); | ||
$addressData = $trxWallet->generateAddress(); | ||
// $addressData->privateKey | ||
// $addressData->address | ||
|
||
$config = [ | ||
'contract_address' => 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t',// USDT TRC20 | ||
'decimals' => 6, | ||
]; | ||
$trc20Wallet = new \Tron\TRC20($api, $this->config); | ||
$addressData = $trc20Wallet->generateAddress(); | ||
``` | ||
|
||
## 计划 | ||
|
||
- 支持 TRC10 | ||
- 测试用例 | ||
- ... | ||
|
||
## 扩展包 | ||
|
||
| 扩展包名 | 描述 | 应用场景 | | ||
| :-----| :---- | :---- | | ||
| [iexbase/tron-api](https://github.com/iexbase/tron-api) | 波场官方文档推荐 PHP 扩展包 | 波场基础Api | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"name": "ufado/tron-php", | ||
"description": "Support TRON's TRX and TRC20, which include functions such as address creation, balance query, transaction transfer, query the latest blockchain, query information based on the blockchain, and query information based on the transaction hash", | ||
"keywords": [ | ||
"php", | ||
"tron", | ||
"trx", | ||
"trc20" | ||
], | ||
"type": "library", | ||
"homepage": "https://github.com/ufado/tron-php", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "ufado", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"iexbase/tron-api": "^2.0 || ^3.0 || ^3.1", | ||
"ionux/phactor": "1.0.8", | ||
"kornrunner/keccak": "^1.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^5.7 || ^7.5" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Tron\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Test\\": "tests/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Tron; | ||
|
||
use IEXBase\TronAPI\Support\Base58Check; | ||
use IEXBase\TronAPI\Support\Hash; | ||
|
||
class Address | ||
{ | ||
public $privateKey, | ||
$address, | ||
$hexAddress = ''; | ||
|
||
const ADDRESS_SIZE = 34; | ||
const ADDRESS_PREFIX = "41"; | ||
const ADDRESS_PREFIX_BYTE = 0x41; | ||
|
||
public function __construct(string $address = '', string $privateKey = '', string $hexAddress = '') | ||
{ | ||
if (strlen($address) === 0) { | ||
throw new \InvalidArgumentException('Address can not be empty'); | ||
} | ||
|
||
$this->privateKey = $privateKey; | ||
$this->address = $address; | ||
$this->hexAddress = $hexAddress; | ||
} | ||
|
||
/** | ||
* Dont rely on this. Always use Wallet::validateAddress to double check | ||
* against tronGrid. | ||
* | ||
* @return bool | ||
*/ | ||
public function isValid(): bool | ||
{ | ||
if (strlen($this->address) !== Address::ADDRESS_SIZE) { | ||
return false; | ||
} | ||
|
||
$address = Base58Check::decode($this->address, false, 0, false); | ||
$utf8 = hex2bin($address); | ||
|
||
if (strlen($utf8) !== 25) { | ||
return false; | ||
} | ||
|
||
if (strpos($utf8, chr(self::ADDRESS_PREFIX_BYTE)) !== 0) { | ||
return false; | ||
} | ||
|
||
$checkSum = substr($utf8, 21); | ||
$address = substr($utf8, 0, 21); | ||
|
||
$hash0 = Hash::SHA256($address); | ||
$hash1 = Hash::SHA256($hash0); | ||
$checkSum1 = substr($hash1, 0, 4); | ||
|
||
if ($checkSum === $checkSum1) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace Tron; | ||
|
||
use GuzzleHttp\Client; | ||
use Tron\Exceptions\TronErrorException; | ||
|
||
class Api | ||
{ | ||
private $_client; | ||
|
||
public function __construct(Client $client) | ||
{ | ||
$this->_client = $client; | ||
} | ||
|
||
public function getClient(): Client | ||
{ | ||
return $this->_client; | ||
} | ||
|
||
/** | ||
* Abstracts some common functionality like formatting the post data | ||
* along with error handling. | ||
* | ||
* @throws TronErrorException | ||
*/ | ||
public function post(string $endpoint, array $data = [], bool $returnAssoc = false) | ||
{ | ||
if (sizeof($data)) { | ||
$data = ['json' => $data]; | ||
} | ||
|
||
$stream = (string)$this->getClient()->post($endpoint, $data)->getBody(); | ||
$body = json_decode($stream, $returnAssoc); | ||
|
||
$this->checkForErrorResponse($returnAssoc, $body); | ||
|
||
return $body; | ||
} | ||
// /** | ||
// * Abstracts some common functionality like formatting the post data | ||
// * along with error handling. | ||
// * | ||
// * @throws TronErrorException | ||
// */ | ||
// public function post(string $endpoint, array $data = [], bool $returnAssoc = false) | ||
// { | ||
// if (sizeof($data)) { | ||
// $data = [ | ||
// 'headers' => [ | ||
// 'TRON-PRO-API-KEY' => 'your api key' | ||
// ], | ||
// 'json' => $data | ||
// ]; | ||
// } | ||
// $stream = (string)$this->getClient()->post($endpoint, $data)->getBody(); | ||
// $body = json_decode($stream, $returnAssoc); | ||
// $this->checkForErrorResponse($returnAssoc, $body); | ||
// return $body; | ||
// } | ||
|
||
|
||
/** | ||
* Check if the response has an error and throw it. | ||
* | ||
* @param bool $returnAssoc | ||
* @param $body | ||
* @throws TronErrorException | ||
*/ | ||
private function checkForErrorResponse(bool $returnAssoc, $body) | ||
{ | ||
if ($returnAssoc) { | ||
if (isset($body['Error'])) { | ||
throw new TronErrorException($body['Error']); | ||
} elseif (isset($body['code']) && isset($body['message'])) { | ||
throw new TronErrorException($body['code'] . ': ' . hex2bin($body['message'])); | ||
} | ||
} | ||
|
||
if (isset($body->Error)) { | ||
throw new TronErrorException($body->Error); | ||
} elseif (isset($body->code) && isset($body->message)) { | ||
throw new TronErrorException($body->code . ': ' . hex2bin($body->message)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Tron; | ||
|
||
class Block | ||
{ | ||
public $blockID; | ||
public $block_header; | ||
public $transactions; | ||
|
||
public function __construct(string $blockID, array $block_header, array $transactions = []) | ||
{ | ||
if (!strlen($blockID)) { | ||
throw new \Exception('blockID empty'); | ||
} | ||
|
||
$this->blockID = $blockID; | ||
$this->block_header = $block_header; | ||
$this->transactions = $transactions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Tron\Exceptions; | ||
|
||
class TransactionException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Tron\Exceptions; | ||
|
||
class TronErrorException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Tron\Interfaces; | ||
|
||
use Tron\Address; | ||
use Tron\Block; | ||
use Tron\Transaction; | ||
|
||
interface WalletInterface | ||
{ | ||
public function generateAddress(): Address; | ||
|
||
public function validateAddress(Address $address): bool; | ||
|
||
public function privateKeyToAddress(string $privateKeyHex): Address; | ||
|
||
public function balance(Address $address); | ||
|
||
public function transfer(Address $from, Address $to, float $amount): Transaction; | ||
|
||
public function blockNumber(): Block; | ||
|
||
public function blockByNumber(int $blockID): Block; | ||
|
||
public function transactionReceipt(string $txHash); | ||
} |
Oops, something went wrong.