Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Commit

Permalink
added support files for hashing. Credit to https://github.com/iexbase…
Browse files Browse the repository at this point in the history
…/tron-api

updated readme to come soon
  • Loading branch information
mattvb91 committed Oct 30, 2018
1 parent b71b5ab commit b7a0292
Show file tree
Hide file tree
Showing 9 changed files with 462 additions and 113 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"description": "Library for interacting with the Tron blockchain through Tron-Grid",
"type": "library",
"require": {
"ext-bcmath": "*",
"guzzlehttp/guzzle": "^6.3"
},
"require-dev": {
Expand Down
227 changes: 115 additions & 112 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions src/Support/Base58.php
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);
}
}
73 changes: 73 additions & 0 deletions src/Support/Base58Check.php
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;
}
}
Loading

0 comments on commit b7a0292

Please sign in to comment.