diff --git a/examples/generate.php b/examples/generate.php new file mode 100644 index 0000000..45f3d39 --- /dev/null +++ b/examples/generate.php @@ -0,0 +1,12 @@ +generateKey(MetaHash::KEY_TYPE_SECP256K1)); + +\var_dump('secp256r1', $metaHash->generateKey(MetaHash::KEY_TYPE_SECP256R1)); diff --git a/src/MetaHash.php b/src/MetaHash.php index 3c6694a..ea17e46 100644 --- a/src/MetaHash.php +++ b/src/MetaHash.php @@ -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 */ @@ -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); } /** diff --git a/src/MetaHashCrypto.php b/src/MetaHashCrypto.php index 3af099f..7a867ae 100644 --- a/src/MetaHashCrypto.php +++ b/src/MetaHashCrypto.php @@ -35,7 +35,7 @@ class MetaHashCrypto public function __construct() { $this->adapter = EccFactory::getAdapter(); - $this->generator = EccFactory::getSecgCurves()->generator256r1(); + $this->generator = EccFactory::getSecgCurves()->generator256k1(); } /** @@ -43,9 +43,11 @@ public function __construct() * * @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, @@ -53,7 +55,13 @@ public function generateKey(): array '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);