From 87dba49320f21a4ddcdc62278ec628c5edd5bc96 Mon Sep 17 00:00:00 2001 From: David Rosenbloom Date: Mon, 1 Jan 2024 16:10:11 -0500 Subject: [PATCH 1/4] support a user defined prefix. --- src/Address.php | 54 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/src/Address.php b/src/Address.php index 83eaa4b..4db18dc 100644 --- a/src/Address.php +++ b/src/Address.php @@ -4,13 +4,24 @@ use InvalidArgumentException; use kornrunner\Keccak; -use Mdanter\Ecc\EccFactory; use Mdanter\Ecc\Crypto\Key\PrivateKeyInterface; +use Mdanter\Ecc\EccFactory; use Mdanter\Ecc\Serializer\PublicKey\DerPublicKeySerializer; -class Address { +class Address +{ + + private const SIZE = 64; + protected $prefix = ''; + /** + * @var PrivateKeyInterface + */ + private $privateKey; - public function __construct(string $privateKey = '') { + public function __construct(string $privateKey = '', string $prefix = '') + { + $this->setPrefix($prefix); + $privateKey = $this->removePrefix($privateKey); $generator = EccFactory::getSecgCurves()->generator256k1(); if (empty ($privateKey)) { $this->privateKey = $generator->createPrivateKey(); @@ -27,25 +38,34 @@ public function __construct(string $privateKey = '') { } } - public function getPrivateKey(): string { - return str_pad(gmp_strval($this->privateKey->getSecret(), 16), self::SIZE, '0', STR_PAD_LEFT); + public function setPrefix(string $prefix = '') + { + $this->prefix = $prefix; } - public function getPublicKey(): string { - $publicKey = $this->privateKey->getPublicKey(); - $publicKeySerializer = new DerPublicKeySerializer(EccFactory::getAdapter()); - return substr($publicKeySerializer->getUncompressedKey($publicKey), 2); + public function removePrefix(string $any) + { + if (substr($any, 0, strlen($this->prefix)) === $this->prefix) { + return substr($any, strlen($this->prefix)); + } + return $any; } - public function get(): string { - $hash = Keccak::hash(hex2bin($this->getPublicKey()), 256); - return substr($hash, -40); + public function getPrivateKey(): string + { + return $this->prefix . str_pad(gmp_strval($this->privateKey->getSecret(), 16), self::SIZE, '0', STR_PAD_LEFT); } - /** - * @var PrivateKeyInterface - */ - private $privateKey; + public function get(): string + { + $hash = Keccak::hash(hex2bin($this->removePrefix($this->getPublicKey())), 256); + return $this->prefix . substr($hash, -40); + } - private const SIZE = 64; + public function getPublicKey(): string + { + $publicKey = $this->privateKey->getPublicKey(); + $publicKeySerializer = new DerPublicKeySerializer(EccFactory::getAdapter()); + return $this->prefix . substr($publicKeySerializer->getUncompressedKey($publicKey), 2); + } } From b06869c86c7f58791ca469467b7d2f191c130fe7 Mon Sep 17 00:00:00 2001 From: David Rosenbloom Date: Mon, 1 Jan 2024 16:31:18 -0500 Subject: [PATCH 2/4] use static functions for repeatable access without instantiation. --- src/Address.php | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/Address.php b/src/Address.php index 4db18dc..9eca400 100644 --- a/src/Address.php +++ b/src/Address.php @@ -21,7 +21,7 @@ class Address public function __construct(string $privateKey = '', string $prefix = '') { $this->setPrefix($prefix); - $privateKey = $this->removePrefix($privateKey); + $privateKey = static::removePrefix($privateKey, $this->prefix); $generator = EccFactory::getSecgCurves()->generator256k1(); if (empty ($privateKey)) { $this->privateKey = $generator->createPrivateKey(); @@ -43,29 +43,37 @@ public function setPrefix(string $prefix = '') $this->prefix = $prefix; } - public function removePrefix(string $any) + public static function removePrefix(string $any, string $prefix) { - if (substr($any, 0, strlen($this->prefix)) === $this->prefix) { - return substr($any, strlen($this->prefix)); + if (substr($any, 0, strlen($prefix)) === $prefix) { + return substr($any, strlen($prefix)); } return $any; } public function getPrivateKey(): string { - return $this->prefix . str_pad(gmp_strval($this->privateKey->getSecret(), 16), self::SIZE, '0', STR_PAD_LEFT); + return static::addPrefix(str_pad(gmp_strval($this->privateKey->getSecret(), 16), self::SIZE, '0', STR_PAD_LEFT), $this->prefix); + } + + public static function addPrefix(string $any, string $prefix) + { + if (substr($any, 0, strlen($prefix)) !== $prefix) { + return $prefix . $any; + } + return $any; } public function get(): string { - $hash = Keccak::hash(hex2bin($this->removePrefix($this->getPublicKey())), 256); - return $this->prefix . substr($hash, -40); + $hash = Keccak::hash(hex2bin(static::removePrefix($this->getPublicKey()), $this->prefix), 256); + return static::addPrefix(substr($hash, -40), $this->prefix); } public function getPublicKey(): string { $publicKey = $this->privateKey->getPublicKey(); $publicKeySerializer = new DerPublicKeySerializer(EccFactory::getAdapter()); - return $this->prefix . substr($publicKeySerializer->getUncompressedKey($publicKey), 2); + return static::addPrefix(substr($publicKeySerializer->getUncompressedKey($publicKey), 2), $this->prefix); } } From 2e10a2f6abb2ddf0258bea10cd4e6a91ebc30e0e Mon Sep 17 00:00:00 2001 From: David Rosenbloom Date: Mon, 1 Jan 2024 16:33:01 -0500 Subject: [PATCH 3/4] reorganize. --- src/Address.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Address.php b/src/Address.php index 9eca400..0a4c8fd 100644 --- a/src/Address.php +++ b/src/Address.php @@ -51,11 +51,6 @@ public static function removePrefix(string $any, string $prefix) return $any; } - public function getPrivateKey(): string - { - return static::addPrefix(str_pad(gmp_strval($this->privateKey->getSecret(), 16), self::SIZE, '0', STR_PAD_LEFT), $this->prefix); - } - public static function addPrefix(string $any, string $prefix) { if (substr($any, 0, strlen($prefix)) !== $prefix) { @@ -64,6 +59,11 @@ public static function addPrefix(string $any, string $prefix) return $any; } + public function getPrivateKey(): string + { + return static::addPrefix(str_pad(gmp_strval($this->privateKey->getSecret(), 16), self::SIZE, '0', STR_PAD_LEFT), $this->prefix); + } + public function get(): string { $hash = Keccak::hash(hex2bin(static::removePrefix($this->getPublicKey()), $this->prefix), 256); From 6c1d9bc43f4b3e53722412746b5580c6d4d74748 Mon Sep 17 00:00:00 2001 From: David Rosenbloom Date: Mon, 1 Jan 2024 16:47:34 -0500 Subject: [PATCH 4/4] correct error. --- src/Address.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Address.php b/src/Address.php index 0a4c8fd..ba3a13a 100644 --- a/src/Address.php +++ b/src/Address.php @@ -1,6 +1,6 @@ getPublicKey()), $this->prefix), 256); + $hash = Keccak::hash(hex2bin(static::removePrefix($this->getPublicKey(),$this->prefix)), 256); return static::addPrefix(substr($hash, -40), $this->prefix); }