This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added support files for hashing. Credit to https://github.com/iexbase…
…/tron-api updated readme to come soon
- Loading branch information
Showing
9 changed files
with
462 additions
and
113 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 mattvb91\TronTrx\Support; | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
class Base58 | ||
{ | ||
const AVAILABLE_CHARS = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; | ||
|
||
public static function encode($num, $length = 58): string | ||
{ | ||
return Crypto::dec2base($num, $length, self::AVAILABLE_CHARS); | ||
} | ||
|
||
public static function decode(string $addr, int $length = 58): string | ||
{ | ||
return Crypto::base2dec($addr, $length, self::AVAILABLE_CHARS); | ||
} | ||
} |
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,73 @@ | ||
<?php | ||
|
||
namespace mattvb91\TronTrx\Support; | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
class Base58Check | ||
{ | ||
/** | ||
* Encode Base58Check | ||
* | ||
* @param string $string | ||
* @param int $prefix | ||
* @param bool $compressed | ||
* @return string | ||
*/ | ||
public static function encode(string $string, int $prefix = 128, bool $compressed = true) | ||
{ | ||
$string = hex2bin($string); | ||
|
||
if ($prefix) { | ||
$string = chr($prefix) . $string; | ||
} | ||
|
||
if ($compressed) { | ||
$string .= chr(0x01); | ||
} | ||
|
||
$string = $string . substr(Hash::SHA256(Hash::SHA256($string)), 0, 4); | ||
|
||
$base58 = Base58::encode(Crypto::bin2bc($string)); | ||
for ($i = 0; $i < strlen($string); $i++) { | ||
if ($string[$i] != "\x00") { | ||
break; | ||
} | ||
|
||
$base58 = '1' . $base58; | ||
} | ||
return $base58; | ||
} | ||
|
||
/** | ||
* Decoding from Base58Check | ||
* | ||
* @param string $string | ||
* @param int $removeLeadingBytes | ||
* @param int $removeTrailingBytes | ||
* @param bool $removeCompression | ||
* @return bool|string | ||
*/ | ||
public static function decode(string $string, int $removeLeadingBytes = 1, int $removeTrailingBytes = 4, bool $removeCompression = true) | ||
{ | ||
$string = bin2hex(Crypto::bc2bin(Base58::decode($string))); | ||
|
||
//If end bytes: Network type | ||
if ($removeLeadingBytes) { | ||
$string = substr($string, $removeLeadingBytes * 2); | ||
} | ||
|
||
//If the final bytes: Checksum | ||
if ($removeTrailingBytes) { | ||
$string = substr($string, 0, -($removeTrailingBytes * 2)); | ||
} | ||
|
||
//If end bytes: compressed byte | ||
if ($removeCompression) { | ||
$string = substr($string, 0, -2); | ||
} | ||
|
||
return $string; | ||
} | ||
} |
Oops, something went wrong.