From 264ad69e9d1f80229980652938f8f574c84f249b Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Fri, 29 Dec 2017 15:32:09 +0800 Subject: [PATCH] inputFormat --- src/Contracts/Types/Address.php | 30 ++++++++++++++++++++++++++++ src/Contracts/Types/Boolean.php | 18 +++++++++++++++++ src/Contracts/Types/Bytes.php | 29 +++++++++++++++++++++++++++ src/Contracts/Types/DynamicBytes.php | 28 ++++++++++++++++++++++++++ src/Contracts/Types/IType.php | 9 +++++++++ src/Contracts/Types/Integer.php | 14 +++++++++++++ src/Contracts/Types/Str.php | 19 ++++++++++++++++++ src/Contracts/Types/Uinteger.php | 14 +++++++++++++ 8 files changed, 161 insertions(+) diff --git a/src/Contracts/Types/Address.php b/src/Contracts/Types/Address.php index 483edc1..125cc10 100644 --- a/src/Contracts/Types/Address.php +++ b/src/Contracts/Types/Address.php @@ -11,8 +11,11 @@ namespace Web3\Contracts\Types; +use InvalidArgumentException; use Web3\Contracts\SolidityType; use Web3\Contracts\Types\IType; +use Web3\Utils; +use Web3\Formatters\Integer as IntegerFormatter; class Address extends SolidityType implements IType { @@ -46,4 +49,31 @@ class Address extends SolidityType implements IType { return false; } + + /** + * inputFormat + * to do: iban + * + * @param mixed $value + * @param string $name + * @return string + */ + public function inputFormat($value, $name) + { + $value = (string) $value; + + if (Utils::isAddress($value)) { + $value = mb_strtolower($value); + + if (Utils::isZeroPrefixed($value)) { + // return '000000000000000000000000' . Utils::stripZero($value); + $value = Utils::stripZero($value); + } + // return '000000000000000000000000' . $value; + } + $value = IntegerFormatter::format($value); + + return $value; + // throw new InvalidArgumentException('The value to inputFormat must be string.'); + } } \ No newline at end of file diff --git a/src/Contracts/Types/Boolean.php b/src/Contracts/Types/Boolean.php index 9aeb833..6ec38c7 100644 --- a/src/Contracts/Types/Boolean.php +++ b/src/Contracts/Types/Boolean.php @@ -11,6 +11,7 @@ namespace Web3\Contracts\Types; +use InvalidArgumentException; use Web3\Contracts\SolidityType; use Web3\Contracts\Types\IType; @@ -46,4 +47,21 @@ class Boolean extends SolidityType implements IType { return false; } + + /** + * inputFormat + * + * @param mixed $value + * @param string $name + * @return string + */ + public function inputFormat($value, $name) + { + if (!is_bool($value)) { + throw new InvalidArgumentException('The value to inputFormat function must be boolean.'); + } + $value = (int) $value; + + return '000000000000000000000000000000000000000000000000000000000000000' . $value; + } } \ No newline at end of file diff --git a/src/Contracts/Types/Bytes.php b/src/Contracts/Types/Bytes.php index a5aded7..eba8a9c 100644 --- a/src/Contracts/Types/Bytes.php +++ b/src/Contracts/Types/Bytes.php @@ -11,6 +11,8 @@ namespace Web3\Contracts\Types; +use InvalidArgumentException; +use Web3\Utils; use Web3\Contracts\SolidityType; use Web3\Contracts\Types\IType; @@ -46,4 +48,31 @@ class Bytes extends SolidityType implements IType { return false; } + + /** + * inputFormat + * + * @param mixed $value + * @param string $name + * @return string + */ + public function inputFormat($value, $name) + { + if (!Utils::isHex($value)) { + throw new InvalidArgumentException('The value to inputFormat must be hex bytes.'); + } + $value = Utils::stripZero($value); + + // if (mb_strlen($value) % 2 !== 0) { + // throw new InvalidArgumentException('The value to inputFormat has invalid length. Value: ' . $value); + // } + + if (mb_strlen($value) > 64) { + throw new InvalidArgumentException('The value to inputFormat is too long.'); + } + $l = floor((mb_strlen($value) + 63) / 64); + $padding = (($l * 64 - mb_strlen($value) + 1) >= 0) ? $l * 64 - mb_strlen($value) : 0; + + return $value . implode('', array_fill(0, $padding, '0')); + } } \ No newline at end of file diff --git a/src/Contracts/Types/DynamicBytes.php b/src/Contracts/Types/DynamicBytes.php index 360070e..d95d50f 100644 --- a/src/Contracts/Types/DynamicBytes.php +++ b/src/Contracts/Types/DynamicBytes.php @@ -11,6 +11,8 @@ namespace Web3\Contracts\Types; +use InvalidArgumentException; +use Web3\Utils; use Web3\Contracts\SolidityType; use Web3\Contracts\Types\IType; @@ -46,4 +48,30 @@ class DynamicBytes extends SolidityType implements IType { return true; } + + /** + * inputFormat + * + * @param mixed $value + * @param string $name + * @return string + */ + public function inputFormat($value, $name) + { + if (!Utils::isHex($value)) { + throw new InvalidArgumentException('The value to inputFormat must be hex bytes.'); + } + $value = Utils::stripZero($value); + + // if (mb_strlen($value) % 2 !== 0) { + // throw new InvalidArgumentException('The value to inputFormat has invalid length.'); + // } + $bn = Utils::toBn(mb_strlen($value) / 2); + $bnHex = $bn->toHex(true); + $padded = mb_substr($bnHex, 0, 1); + $l = floor((mb_strlen($value) + 63) / 64); + $padding = (($l * 64 - mb_strlen($value) + 1) >= 0) ? $l * 64 - mb_strlen($value) : 0; + + return implode('', array_fill(0, 64-mb_strlen($bnHex), $padded)) . $bnHex . $value . implode('', array_fill(0, $padding, '0')); + } } \ No newline at end of file diff --git a/src/Contracts/Types/IType.php b/src/Contracts/Types/IType.php index 363e14d..6dc9cc3 100644 --- a/src/Contracts/Types/IType.php +++ b/src/Contracts/Types/IType.php @@ -27,4 +27,13 @@ interface IType * @return bool */ public function isDynamicType(); + + /** + * inputFormat + * + * @param mixed $value + * @param string $name + * @return string + */ + public function inputFormat($value, $name); } \ No newline at end of file diff --git a/src/Contracts/Types/Integer.php b/src/Contracts/Types/Integer.php index 385c9ca..ca0e557 100644 --- a/src/Contracts/Types/Integer.php +++ b/src/Contracts/Types/Integer.php @@ -11,8 +11,10 @@ namespace Web3\Contracts\Types; +use Web3\Utils; use Web3\Contracts\SolidityType; use Web3\Contracts\Types\IType; +use Web3\Formatters\Integer as IntegerFormatter; class Integer extends SolidityType implements IType { @@ -46,4 +48,16 @@ class Integer extends SolidityType implements IType { return false; } + + /** + * inputFormat + * + * @param mixed $value + * @param string $name + * @return string + */ + public function inputFormat($value, $name) + { + return IntegerFormatter::format($value); + } } \ No newline at end of file diff --git a/src/Contracts/Types/Str.php b/src/Contracts/Types/Str.php index dbd7661..5691c6d 100644 --- a/src/Contracts/Types/Str.php +++ b/src/Contracts/Types/Str.php @@ -11,8 +11,10 @@ namespace Web3\Contracts\Types; +use Web3\Utils; use Web3\Contracts\SolidityType; use Web3\Contracts\Types\IType; +use Web3\Formatters\Integer as IntegerFormatter; class Str extends SolidityType implements IType { @@ -46,4 +48,21 @@ class Str extends SolidityType implements IType { return true; } + + /** + * inputFormat + * + * @param mixed $value + * @param string $name + * @return string + */ + public function inputFormat($value, $name) + { + $value = Utils::toHex($value); + $prefix = IntegerFormatter::format(mb_strlen($value) / 2); + $l = floor((mb_strlen($value) + 63) / 64); + $padding = (($l * 64 - mb_strlen($value) + 1) >= 0) ? $l * 64 - mb_strlen($value) : 0; + + return $prefix . $value . implode('', array_fill(0, $padding, '0')); + } } \ No newline at end of file diff --git a/src/Contracts/Types/Uinteger.php b/src/Contracts/Types/Uinteger.php index 7a4a82e..b294666 100644 --- a/src/Contracts/Types/Uinteger.php +++ b/src/Contracts/Types/Uinteger.php @@ -11,8 +11,10 @@ namespace Web3\Contracts\Types; +use Web3\Utils; use Web3\Contracts\SolidityType; use Web3\Contracts\Types\IType; +use Web3\Formatters\Integer as IntegerFormatter; class Uinteger extends SolidityType implements IType { @@ -46,4 +48,16 @@ class Uinteger extends SolidityType implements IType { return false; } + + /** + * inputFormat + * + * @param mixed $value + * @param string $name + * @return string + */ + public function inputFormat($value, $name) + { + return IntegerFormatter::format($value); + } } \ No newline at end of file