inputFormat
This commit is contained in:
parent
c4d2c6edfd
commit
264ad69e9d
@ -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.');
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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'));
|
||||
}
|
||||
}
|
@ -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'));
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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'));
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user