From 592606ed753e19724a5c0806dc51d1da01ff2fcd Mon Sep 17 00:00:00 2001 From: AlexanderBuzz <102560752+AlexanderBuzz@users.noreply.github.com> Date: Mon, 25 Sep 2023 17:15:52 +0200 Subject: [PATCH] - Improved code quality in BinaryCodec --- src/Core/RippleBinaryCodec/Binary.php | 13 ++- src/Core/RippleBinaryCodec/BinaryCodec.php | 25 ++++- .../RippleBinaryCodec/Serdes/BinaryParser.php | 10 +- .../Serdes/BinarySerializer.php | 10 +- .../RippleBinaryCodec/Serdes/BytesList.php | 22 ++--- .../RippleBinaryCodec/Types/AccountId.php | 8 ++ src/Core/RippleBinaryCodec/Types/Amount.php | 94 ++++++++++++++----- src/Core/RippleBinaryCodec/Types/Blob.php | 8 ++ src/Core/RippleBinaryCodec/Types/Currency.php | 8 ++ 9 files changed, 153 insertions(+), 45 deletions(-) diff --git a/src/Core/RippleBinaryCodec/Binary.php b/src/Core/RippleBinaryCodec/Binary.php index 2d4902a..5fea749 100644 --- a/src/Core/RippleBinaryCodec/Binary.php +++ b/src/Core/RippleBinaryCodec/Binary.php @@ -1,13 +1,16 @@ $value) { @@ -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(); diff --git a/src/Core/RippleBinaryCodec/Serdes/BinaryParser.php b/src/Core/RippleBinaryCodec/Serdes/BinaryParser.php index b7ddd6c..6c0ca3d 100644 --- a/src/Core/RippleBinaryCodec/Serdes/BinaryParser.php +++ b/src/Core/RippleBinaryCodec/Serdes/BinaryParser.php @@ -1,4 +1,12 @@ -bufferArray[$index] =$newElement; } - /* - public function toBytesSink(BytesList $list): Buffer - { - $list->toBytesSink($list); //TODO: Is this necessary? - } - */ - public function toBytes(): Buffer { $tempArray = []; diff --git a/src/Core/RippleBinaryCodec/Types/AccountId.php b/src/Core/RippleBinaryCodec/Types/AccountId.php index 1e2bb5c..abc72ed 100644 --- a/src/Core/RippleBinaryCodec/Types/AccountId.php +++ b/src/Core/RippleBinaryCodec/Types/AccountId.php @@ -1,4 +1,12 @@ 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; @@ -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; @@ -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 @@ -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); @@ -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 @@ -210,6 +259,9 @@ private function isNative(array $bytes): bool } /** + * + * + * @param array $bytes * @return bool */ private function isPositive(array $bytes): bool diff --git a/src/Core/RippleBinaryCodec/Types/Blob.php b/src/Core/RippleBinaryCodec/Types/Blob.php index a6e5d84..a8b528b 100644 --- a/src/Core/RippleBinaryCodec/Types/Blob.php +++ b/src/Core/RippleBinaryCodec/Types/Blob.php @@ -1,4 +1,12 @@