Skip to content
This repository has been archived by the owner on Nov 14, 2022. It is now read-only.

Commit

Permalink
add secp256k1 & secp256r1 key types generate
Browse files Browse the repository at this point in the history
  • Loading branch information
xboston committed Jul 19, 2019
1 parent d732baa commit 79b6378
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
12 changes: 12 additions & 0 deletions examples/generate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);

require __DIR__.'/../vendor/autoload.php';

use Metahash\MetaHash;

$metaHash = new MetaHash();


\var_dump('secp256k1', $metaHash->generateKey(MetaHash::KEY_TYPE_SECP256K1));

\var_dump('secp256r1', $metaHash->generateKey(MetaHash::KEY_TYPE_SECP256R1));
9 changes: 7 additions & 2 deletions src/MetaHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class MetaHash
public const NETWORK_TEST = 'test';
public const NETWORK_DEV = 'dev';

public const KEY_TYPE_SECP256R1 = 0;
public const KEY_TYPE_SECP256K1 = 1;

/**
* @var int
*/
Expand Down Expand Up @@ -129,11 +132,13 @@ public function setDebug(bool $debug): void
*
* @see https://developers.metahash.org/hc/en-us/articles/360002712193-Getting-started-with-Metahash-network
*
* @param int $keyType
*
* @return array
*/
public function generateKey(): array
public function generateKey($keyType = MetaHash::KEY_TYPE_SECP256K1): array
{
return $this->getMetahashCrypto()->generateKey();
return $this->getMetahashCrypto()->generateKey($keyType);
}

/**
Expand Down
14 changes: 11 additions & 3 deletions src/MetaHashCrypto.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,33 @@ class MetaHashCrypto
public function __construct()
{
$this->adapter = EccFactory::getAdapter();
$this->generator = EccFactory::getSecgCurves()->generator256r1();
$this->generator = EccFactory::getSecgCurves()->generator256k1();
}

/**
* Metahash key generate
*
* @see https://developers.metahash.org/hc/en-us/articles/360002712193-Getting-started-with-Metahash-network
*
* @param int $keyType
*
* @return array
*/
public function generateKey(): array
public function generateKey($keyType = MetaHash::KEY_TYPE_SECP256K1): array
{
$result = [
'private' => null,
'public' => null,
'address' => null,
];

$private = $this->generator->createPrivateKey();
$generator = $this->generator;

if ($keyType === MetaHash::KEY_TYPE_SECP256R1) {
$generator = EccFactory::getSecgCurves()->generator256r1();
}

$private = $generator->createPrivateKey();
$serializerPrivate = new DerPrivateKeySerializer($this->adapter);
$dataPrivate = $serializerPrivate->serialize($private);
$result['private'] = '0x'.\bin2hex($dataPrivate);
Expand Down

0 comments on commit 79b6378

Please sign in to comment.