Skip to content

Commit 04b7222

Browse files
committed
first commit
0 parents  commit 04b7222

18 files changed

+1313
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
vendor
3+
*.lock

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 ufado
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<h1 align="center">TRON-PHP</h1>
2+
3+
## 概述
4+
5+
TRON-PHP 目前支持波场的 TRX 和 TRC20 中常用生成地址,发起转账,离线签名等功能。
6+
7+
## 特点
8+
9+
1. 一套写法兼容 TRON 网络中 TRX 货币和 TRC 系列所有通证
10+
1. 接口方法可可灵活增减
11+
12+
## 支持方法
13+
14+
- 生成地址 `generateAddress()`
15+
- 验证地址 `validateAddress(Address $address)`
16+
- 根据私钥得到地址 `privateKeyToAddress(string $privateKeyHex)`
17+
- 查询余额 `balance(Address $address)`
18+
- 交易转账(离线签名) `transfer(Address $from, Address $to, float $amount)`
19+
- 查询最新区块 `blockNumber()`
20+
- 根据区块链查询信息 `blockByNumber(int $blockID)`
21+
- 根据交易哈希查询信息 `transactionReceipt(string $txHash)`
22+
23+
## 快速开始
24+
25+
### 安装
26+
27+
``` php
28+
composer require fenguoz/tron-php
29+
```
30+
31+
### 接口调用
32+
33+
``` php
34+
use GuzzleHttp\Client;
35+
36+
$uri = 'https://api.shasta.trongrid.io';// shasta testnet
37+
$api = new \Tron\Api(new Client(['base_uri' => $uri]));
38+
39+
$trxWallet = new \Tron\TRX($api);
40+
$addressData = $trxWallet->generateAddress();
41+
// $addressData->privateKey
42+
// $addressData->address
43+
44+
$config = [
45+
'contract_address' => 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t',// USDT TRC20
46+
'decimals' => 6,
47+
];
48+
$trc20Wallet = new \Tron\TRC20($api, $this->config);
49+
$addressData = $trc20Wallet->generateAddress();
50+
```
51+
52+
## 计划
53+
54+
- 支持 TRC10
55+
- 测试用例
56+
- ...
57+
58+
## 扩展包
59+
60+
| 扩展包名 | 描述 | 应用场景 |
61+
| :-----| :---- | :---- |
62+
| [iexbase/tron-api](https://github.com/iexbase/tron-api) | 波场官方文档推荐 PHP 扩展包 | 波场基础Api |

composer.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "ufado/tron-php",
3+
"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",
4+
"keywords": [
5+
"php",
6+
"tron",
7+
"trx",
8+
"trc20"
9+
],
10+
"type": "library",
11+
"homepage": "https://github.com/ufado/tron-php",
12+
"license": "MIT",
13+
"authors": [
14+
{
15+
"name": "ufado",
16+
"email": "[email protected]"
17+
}
18+
],
19+
"require": {
20+
"iexbase/tron-api": "^2.0 || ^3.0 || ^3.1",
21+
"ionux/phactor": "1.0.8",
22+
"kornrunner/keccak": "^1.0"
23+
},
24+
"require-dev": {
25+
"phpunit/phpunit": "^5.7 || ^7.5"
26+
},
27+
"autoload": {
28+
"psr-4": {
29+
"Tron\\": "src/"
30+
}
31+
},
32+
"autoload-dev": {
33+
"psr-4": {
34+
"Test\\": "tests/"
35+
}
36+
}
37+
}

src/Address.php

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Tron;
4+
5+
use IEXBase\TronAPI\Support\Base58Check;
6+
use IEXBase\TronAPI\Support\Hash;
7+
8+
class Address
9+
{
10+
public $privateKey,
11+
$address,
12+
$hexAddress = '';
13+
14+
const ADDRESS_SIZE = 34;
15+
const ADDRESS_PREFIX = "41";
16+
const ADDRESS_PREFIX_BYTE = 0x41;
17+
18+
public function __construct(string $address = '', string $privateKey = '', string $hexAddress = '')
19+
{
20+
if (strlen($address) === 0) {
21+
throw new \InvalidArgumentException('Address can not be empty');
22+
}
23+
24+
$this->privateKey = $privateKey;
25+
$this->address = $address;
26+
$this->hexAddress = $hexAddress;
27+
}
28+
29+
/**
30+
* Dont rely on this. Always use Wallet::validateAddress to double check
31+
* against tronGrid.
32+
*
33+
* @return bool
34+
*/
35+
public function isValid(): bool
36+
{
37+
if (strlen($this->address) !== Address::ADDRESS_SIZE) {
38+
return false;
39+
}
40+
41+
$address = Base58Check::decode($this->address, false, 0, false);
42+
$utf8 = hex2bin($address);
43+
44+
if (strlen($utf8) !== 25) {
45+
return false;
46+
}
47+
48+
if (strpos($utf8, chr(self::ADDRESS_PREFIX_BYTE)) !== 0) {
49+
return false;
50+
}
51+
52+
$checkSum = substr($utf8, 21);
53+
$address = substr($utf8, 0, 21);
54+
55+
$hash0 = Hash::SHA256($address);
56+
$hash1 = Hash::SHA256($hash0);
57+
$checkSum1 = substr($hash1, 0, 4);
58+
59+
if ($checkSum === $checkSum1) {
60+
return true;
61+
}
62+
63+
return false;
64+
}
65+
}

src/Api.php

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Tron;
4+
5+
use GuzzleHttp\Client;
6+
use Tron\Exceptions\TronErrorException;
7+
8+
class Api
9+
{
10+
private $_client;
11+
12+
public function __construct(Client $client)
13+
{
14+
$this->_client = $client;
15+
}
16+
17+
public function getClient(): Client
18+
{
19+
return $this->_client;
20+
}
21+
22+
/**
23+
* Abstracts some common functionality like formatting the post data
24+
* along with error handling.
25+
*
26+
* @throws TronErrorException
27+
*/
28+
public function post(string $endpoint, array $data = [], bool $returnAssoc = false)
29+
{
30+
if (sizeof($data)) {
31+
$data = ['json' => $data];
32+
}
33+
34+
$stream = (string)$this->getClient()->post($endpoint, $data)->getBody();
35+
$body = json_decode($stream, $returnAssoc);
36+
37+
$this->checkForErrorResponse($returnAssoc, $body);
38+
39+
return $body;
40+
}
41+
// /**
42+
// * Abstracts some common functionality like formatting the post data
43+
// * along with error handling.
44+
// *
45+
// * @throws TronErrorException
46+
// */
47+
// public function post(string $endpoint, array $data = [], bool $returnAssoc = false)
48+
// {
49+
// if (sizeof($data)) {
50+
// $data = [
51+
// 'headers' => [
52+
// 'TRON-PRO-API-KEY' => 'your api key'
53+
// ],
54+
// 'json' => $data
55+
// ];
56+
// }
57+
// $stream = (string)$this->getClient()->post($endpoint, $data)->getBody();
58+
// $body = json_decode($stream, $returnAssoc);
59+
// $this->checkForErrorResponse($returnAssoc, $body);
60+
// return $body;
61+
// }
62+
63+
64+
/**
65+
* Check if the response has an error and throw it.
66+
*
67+
* @param bool $returnAssoc
68+
* @param $body
69+
* @throws TronErrorException
70+
*/
71+
private function checkForErrorResponse(bool $returnAssoc, $body)
72+
{
73+
if ($returnAssoc) {
74+
if (isset($body['Error'])) {
75+
throw new TronErrorException($body['Error']);
76+
} elseif (isset($body['code']) && isset($body['message'])) {
77+
throw new TronErrorException($body['code'] . ': ' . hex2bin($body['message']));
78+
}
79+
}
80+
81+
if (isset($body->Error)) {
82+
throw new TronErrorException($body->Error);
83+
} elseif (isset($body->code) && isset($body->message)) {
84+
throw new TronErrorException($body->code . ': ' . hex2bin($body->message));
85+
}
86+
}
87+
}

src/Block.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Tron;
4+
5+
class Block
6+
{
7+
public $blockID;
8+
public $block_header;
9+
public $transactions;
10+
11+
public function __construct(string $blockID, array $block_header, array $transactions = [])
12+
{
13+
if (!strlen($blockID)) {
14+
throw new \Exception('blockID empty');
15+
}
16+
17+
$this->blockID = $blockID;
18+
$this->block_header = $block_header;
19+
$this->transactions = $transactions;
20+
}
21+
}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Tron\Exceptions;
4+
5+
class TransactionException extends \Exception
6+
{
7+
}

src/Exceptions/TronErrorException.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Tron\Exceptions;
4+
5+
class TronErrorException extends \Exception
6+
{
7+
}

src/Interfaces/WalletInterface.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Tron\Interfaces;
4+
5+
use Tron\Address;
6+
use Tron\Block;
7+
use Tron\Transaction;
8+
9+
interface WalletInterface
10+
{
11+
public function generateAddress(): Address;
12+
13+
public function validateAddress(Address $address): bool;
14+
15+
public function privateKeyToAddress(string $privateKeyHex): Address;
16+
17+
public function balance(Address $address);
18+
19+
public function transfer(Address $from, Address $to, float $amount): Transaction;
20+
21+
public function blockNumber(): Block;
22+
23+
public function blockByNumber(int $blockID): Block;
24+
25+
public function transactionReceipt(string $txHash);
26+
}

0 commit comments

Comments
 (0)