Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support a user defined prefix. #7

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 46 additions & 18 deletions src/Address.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
<?php declare(strict_types=1);

namespace kornrunner\Ethereum;
namespace Drupal\blockchain_credentials\Address;

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 = static::removePrefix($privateKey, $this->prefix);
$generator = EccFactory::getSecgCurves()->generator256k1();
if (empty ($privateKey)) {
$this->privateKey = $generator->createPrivateKey();
Expand All @@ -27,25 +38,42 @@ 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 static function removePrefix(string $any, string $prefix)
{
if (substr($any, 0, strlen($prefix)) === $prefix) {
return substr($any, strlen($prefix));
}
return $any;
}

public function get(): string {
$hash = Keccak::hash(hex2bin($this->getPublicKey()), 256);
return substr($hash, -40);
public static function addPrefix(string $any, string $prefix)
{
if (substr($any, 0, strlen($prefix)) !== $prefix) {
return $prefix . $any;
}
return $any;
}

/**
* @var PrivateKeyInterface
*/
private $privateKey;
public function getPrivateKey(): string
{
return static::addPrefix(str_pad(gmp_strval($this->privateKey->getSecret(), 16), self::SIZE, '0', STR_PAD_LEFT), $this->prefix);
}

private const SIZE = 64;
public function get(): string
{
$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 static::addPrefix(substr($publicKeySerializer->getUncompressedKey($publicKey), 2), $this->prefix);
}
}