diff --git a/src/Formatters/PrivateFormatter.php b/src/Formatters/PrivateFormatter.php new file mode 100644 index 0000000..4420f58 --- /dev/null +++ b/src/Formatters/PrivateFormatter.php @@ -0,0 +1,36 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Formatters; + +use InvalidArgumentException; +use Web3\Utils; +use Web3\Formatters\IFormatter; + +class PrivateFormatter implements IFormatter +{ + /** + * format + * + * @param mixed $value + * @return string + */ + public static function format($value) + { + $value = Utils::toString($value); + $value = mb_strtolower($value); + + if (Utils::isZeroPrefixed($value)) { + $value = Utils::stripZero($value); + } + return $value; + } +} diff --git a/src/Methods/Personal/ImportRawKey.php b/src/Methods/Personal/ImportRawKey.php index f2c6722..63fb71e 100644 --- a/src/Methods/Personal/ImportRawKey.php +++ b/src/Methods/Personal/ImportRawKey.php @@ -16,6 +16,7 @@ use Web3\Methods\EthMethod; use Web3\Validators\PrivateValidator; use Web3\Validators\StringValidator; use Web3\Formatters\StringFormatter; +use Web3\Formatters\PrivateFormatter; class ImportRawKey extends EthMethod { @@ -34,7 +35,7 @@ class ImportRawKey extends EthMethod * @var array */ protected $inputFormatters = [ - PrivateValidator::class, StringFormatter::class + PrivateFormatter::class, StringFormatter::class ]; /** diff --git a/src/Validators/PrivateValidator.php b/src/Validators/PrivateValidator.php index 57bfe8d..5656c22 100644 --- a/src/Validators/PrivateValidator.php +++ b/src/Validators/PrivateValidator.php @@ -26,6 +26,6 @@ class PrivateValidator if (!is_string($value)) { return false; } - return (preg_match('/^[a-fA-F0-9]{64}$/', $value) >= 1); + return (preg_match('/^(0x)?[a-fA-F0-9]{64}$/', $value) >= 1); } }