Skip to content

Commit

Permalink
- Improved code quality in BinaryCodec
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderBuzz committed Sep 25, 2023
1 parent bea7256 commit 592606e
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 45 deletions.
13 changes: 8 additions & 5 deletions src/Core/RippleBinaryCodec/Binary.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php declare(strict_types=1);
/**
* XRPL-PHP
*
* Copyright (c) Alexander Busse | Hardcastle Technologies
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace XRPL_PHP\Core\RippleBinaryCodec;

use XRPL_PHP\Core\Buffer;
use XRPL_PHP\Core\HashPrefix;
use XRPL_PHP\Core\RippleBinaryCodec\Serdes\BinaryParser;
use XRPL_PHP\Core\RippleBinaryCodec\Serdes\BytesList;
use XRPL_PHP\Core\RippleBinaryCodec\Types\JsonObject;
use XRPL_PHP\Core\RippleBinaryCodec\Types\StArray;
use XRPL_PHP\Core\RippleBinaryCodec\Types\StObject;

class Binary
Expand Down
25 changes: 20 additions & 5 deletions src/Core/RippleBinaryCodec/BinaryCodec.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ class BinaryCodec extends Binary
{
const TRANSACTION_SIGN = '53545800';

public function __construct()
{

}

/**
*
*
* @param string|array $jsonObject
* @return string
*/
public function encode(string|array $jsonObject): string
{
if (is_array($jsonObject)) {
Expand All @@ -27,6 +28,8 @@ public function encode(string|array $jsonObject): string
}

/**
*
*
* @param string $binaryString
* @return array
*/
Expand Down Expand Up @@ -107,6 +110,12 @@ public function decodeLedgerData()
}
*/

/**
*
*
* @param array $jsonObject
* @return array
*/
private function removeNonSigningFields(array $jsonObject): array
{
foreach ($jsonObject as $fieldName => $value) {
Expand All @@ -118,6 +127,12 @@ private function removeNonSigningFields(array $jsonObject): array
return $jsonObject;
}

/**
*
*
* @param string $fieldName
* @return bool
*/
private function isSigningField(string $fieldName): bool
{
return Definitions::getInstance()->getFieldInstance($fieldName)->isSigningField();
Expand Down
10 changes: 9 additions & 1 deletion src/Core/RippleBinaryCodec/Serdes/BinaryParser.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php
<?php declare(strict_types=1);
/**
* XRPL-PHP
*
* Copyright (c) Alexander Busse | Hardcastle Technologies
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace XRPL_PHP\Core\RippleBinaryCodec\Serdes;

Expand Down
10 changes: 9 additions & 1 deletion src/Core/RippleBinaryCodec/Serdes/BinarySerializer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php
<?php declare(strict_types=1);
/**
* XRPL-PHP
*
* Copyright (c) Alexander Busse | Hardcastle Technologies
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace XRPL_PHP\Core\RippleBinaryCodec\Serdes;

Expand Down
22 changes: 10 additions & 12 deletions src/Core/RippleBinaryCodec/Serdes/BytesList.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
<?php
<?php declare(strict_types=1);
/**
* XRPL-PHP
*
* Copyright (c) Alexander Busse | Hardcastle Technologies
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace XRPL_PHP\Core\RippleBinaryCodec\Serdes;

use ArrayAccess;
use XRPL_PHP\Core\Buffer;

/**
* https://github.com/XRPLF/xrpl.js/blob/main/packages/ripple-binary-codec/src/serdes/binary-serializer.ts
*
* Bytes list is a collection of buffer objects
* This class is a list is a collection of buffer objects ("array of byte arrays"))
*/
class BytesList
{
Expand Down Expand Up @@ -56,13 +61,6 @@ public function replace(int $index, Buffer $newElement): void
$this->bufferArray[$index] =$newElement;
}

/*
public function toBytesSink(BytesList $list): Buffer
{
$list->toBytesSink($list); //TODO: Is this necessary?
}
*/

public function toBytes(): Buffer
{
$tempArray = [];
Expand Down
8 changes: 8 additions & 0 deletions src/Core/RippleBinaryCodec/Types/AccountId.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php declare(strict_types=1);
/**
* XRPL-PHP
*
* Copyright (c) Alexander Busse | Hardcastle Technologies
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace XRPL_PHP\Core\RippleBinaryCodec\Types;

Expand Down
94 changes: 73 additions & 21 deletions src/Core/RippleBinaryCodec/Types/Amount.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
<?php
<?php declare(strict_types=1);
/**
* XRPL-PHP
*
* Copyright (c) Alexander Busse | Hardcastle Technologies
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace XRPL_PHP\Core\RippleBinaryCodec\Types;

use Brick\Math\BigDecimal;
use Brick\Math\BigInteger;
use Brick\Math\BigNumber;
use Exception;
use phpDocumentor\Reflection\Types\String_;
use XRPL_PHP\Core\Buffer;
use XRPL_PHP\Core\MathUtilities;
use XRPL_PHP\Core\RippleBinaryCodec\Serdes\BinaryParser;
use function MongoDB\BSON\fromJSON;

define('MAX_DROPS', BigDecimal::of("1e17"));
define('MIN_XRP', BigDecimal::of("1e-6"));

Expand All @@ -31,6 +35,12 @@ class Amount extends SerializedType

public const MAX_IOU_EXPONENT = 80;

/**
*
*
* @param Buffer|null $bytes
* @throws Exception
*/
public function __construct(?Buffer $bytes = null)
{
if (!$bytes) {
Expand All @@ -41,25 +51,34 @@ public function __construct(?Buffer $bytes = null)
}

/**
*
*
* @param BinaryParser $parser
* @param int|null $lengthHint
* @return SerializedType
* @throws Exception
*/
public static function fromParser(BinaryParser $parser, ?int $lengthHint = null): SerializedType
{
$isXRP = $parser->peek() & 0x80;
$numBytes = $isXRP ? 48 : 8; //NATIVE_AMOUNT_BYTE_LENGTH : CURRENCY_AMOUNT_BYTE_LENGTH;
$numBytes = $isXRP ? self::CURRENCY_AMOUNT_BYTE_LENGTH : self::NATIVE_AMOUNT_BYTE_LENGTH;

return new Amount($parser->read($numBytes));
}

/**
* @throws Exception
*
*
* @param string $serializedJson
* @return SerializedType
* @throws \Brick\Math\Exception\MathException
*/
public static function fromJson(string $serializedJson): SerializedType
{
$isScalar = preg_match('/^\d+$/', $serializedJson);
if ($isScalar) {
self::assertXrpIsValid($serializedJson);
$padded = str_pad(dechex($serializedJson), 16, 0, STR_PAD_LEFT);
$padded = str_pad(dechex((int)$serializedJson), 16, '0', STR_PAD_LEFT);
$bytes = Buffer::from($padded, 'hex'); //padding!
$rawBytes = $bytes->toArray();
$rawBytes[0] |= 0x40;
Expand All @@ -76,16 +95,16 @@ public static function fromJson(string $serializedJson): SerializedType
$amount = Buffer::alloc(8);

$number = BigDecimal::of($rawValue);
//self::assertIouIsValid($number);
self::assertIouIsValid($number);

if($number->isZero()) {
$amount[0] |= 0x80;
} else {
$intString = str_pad($number->getUnscaledValue()->abs(), 16, 0);
$intString = str_pad($number->getUnscaledValue()->abs()->jsonSerialize(), 16, '0');
$bigInteger = BigInteger::of($intString);

$hex1 = str_pad($bigInteger->shiftedRight(32)->toBase(16), 8, 0, STR_PAD_LEFT);
$hex2 = str_pad($bigInteger->and(0x00000000ffffffff)->toBase(16), 8, 0, STR_PAD_LEFT);
$hex1 = str_pad($bigInteger->shiftedRight(32)->toBase(16), 8, '0', STR_PAD_LEFT);
$hex2 = str_pad($bigInteger->and(0x00000000ffffffff)->toBase(16), 8, '0', STR_PAD_LEFT);
$amount = Buffer::from($hex1 . $hex2);

$amount[0] |= 0x80;
Expand Down Expand Up @@ -147,6 +166,36 @@ public function toJson(): string|array
}

/**
*
*
* @param mixed $amount
* @return bool
*/
public static function isAmountValid(mixed $amount): bool
{
try {
self::assertXrpIsValid($amount);
// If no exception is thrown, it's a valid XRP amount
return true;
} catch (Exception $exception) {
// Do nothing
}

try {
self::assertIouIsValid($amount);
// If no exception is thrown, it's a valid IOU/token amount
return true;
} catch (Exception $exception) {
// Do nothing
}

return false;
}

/**
*
* @param string $amount
* @return void
* @throws Exception
*/
private static function assertXrpIsValid(string $amount): void
Expand All @@ -164,11 +213,13 @@ private static function assertXrpIsValid(string $amount): void
}

/**
*
*
* @param BigDecimal $number
* @return void
* @throws Exception
*/
private function assertIouIsValid(BigDecimal $number): void
private static function assertIouIsValid(BigDecimal $number): void
{
if(!$number->isZero()) {
$precision = MathUtilities::getBigDecimalPrecision($number);
Expand All @@ -181,26 +232,24 @@ private function assertIouIsValid(BigDecimal $number): void
throw new Exception("Decimal precision out of range");
}

$this->verifyNoDecimal($number);
self::verifyNoDecimal($number);
}
}

private function verifyNoDecimal(BigDecimal $decimal): void
private static function verifyNoDecimal(BigDecimal $decimal): void
{
$intString = str_pad($decimal->getUnscaledValue()->abs(), 16, 0);
$intString = str_pad($decimal->getUnscaledValue()->abs()->jsonSerialize(), 16, '0');

if (str_contains($intString, '.')) {
throw new Exception("Decimal place found in integerNumberString");
}
}

private function getAmountBytes(BigDecimal $number): Buffer
{

}


/**
*
*
* @param array $bytes
* @return bool
*/
private function isNative(array $bytes): bool
Expand All @@ -210,6 +259,9 @@ private function isNative(array $bytes): bool
}

/**
*
*
* @param array $bytes
* @return bool
*/
private function isPositive(array $bytes): bool
Expand Down
8 changes: 8 additions & 0 deletions src/Core/RippleBinaryCodec/Types/Blob.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php declare(strict_types=1);
/**
* XRPL-PHP
*
* Copyright (c) Alexander Busse | Hardcastle Technologies
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace XRPL_PHP\Core\RippleBinaryCodec\Types;

Expand Down
8 changes: 8 additions & 0 deletions src/Core/RippleBinaryCodec/Types/Currency.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php declare(strict_types=1);
/**
* XRPL-PHP
*
* Copyright (c) Alexander Busse | Hardcastle Technologies
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace XRPL_PHP\Core\RippleBinaryCodec\Types;

Expand Down

0 comments on commit 592606e

Please sign in to comment.