Skip to content

Commit 5f4cfeb

Browse files
committed
Add to create an address, check the balance, initiate a transfer, and view transaction information
0 parents  commit 5f4cfeb

19 files changed

+1256
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
vendor

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Fenguoz
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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<h1 align="center">TRON-PHP</h1>
2+
3+
### 安装
4+
5+
```
6+
composer require fenguoz/tron-php
7+
```
8+
9+
### 待更新

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "fenguoz/tron-php",
3+
"description": "TRX TRX20",
4+
"keywords": [
5+
"php",
6+
"tron",
7+
"trx",
8+
"trx20"
9+
],
10+
"type": "library",
11+
"homepage": "https://github.com/Fenguoz/tron-php",
12+
"license": "MIT",
13+
"authors": [
14+
{
15+
"name": "Fenguoz",
16+
"email": "[email protected]"
17+
}
18+
],
19+
"require": {
20+
"guzzlehttp/guzzle": "^6.3",
21+
"ionux/phactor": "1.0.8",
22+
"kornrunner/keccak": "^1.0"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"Tron\\": "src/"
27+
}
28+
},
29+
"autoload-dev": {
30+
"psr-4": {
31+
"Test\\": "tests/"
32+
}
33+
}
34+
}

src/Address.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Tron;
4+
5+
use Tron\Support\Base58Check;
6+
use Tron\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, 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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
/**
43+
* Check if the response has an error and throw it.
44+
*
45+
* @param bool $returnAssoc
46+
* @param $body
47+
* @throws TronErrorException
48+
*/
49+
private function checkForErrorResponse(bool $returnAssoc, $body)
50+
{
51+
if ($returnAssoc) {
52+
if (isset($body['Error'])) {
53+
throw new TronErrorException($body['Error']);
54+
} elseif (isset($body['code']) && isset($body['message'])) {
55+
throw new TronErrorException($body['code'] . ': ' . hex2bin($body['message']));
56+
}
57+
}
58+
59+
if (isset($body->Error)) {
60+
throw new TronErrorException($body->Error);
61+
} elseif (isset($body->code) && isset($body->message)) {
62+
throw new TronErrorException($body->code . ': ' . hex2bin($body->message));
63+
}
64+
}
65+
}

src/Block.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Tron;
4+
5+
class Block
6+
{
7+
public $blockID;
8+
public $block_header;
9+
10+
public function __construct(string $blockID, array $block_header)
11+
{
12+
if (!strlen($blockID)) {
13+
throw new \Exception('blockID empty');
14+
}
15+
16+
$this->blockID = $blockID;
17+
$this->block_header = $block_header;
18+
}
19+
}
Lines changed: 7 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 balance(Address $address);
16+
17+
public function transfer(Address $from, Address $to, float $amount): Transaction;
18+
19+
public function blockNumber(): Block;
20+
21+
public function transactionReceipt(string $txHash);
22+
}

0 commit comments

Comments
 (0)