diff --git a/src/Crypto/AbstractKeypair.php b/src/Crypto/AbstractKeypair.php index 5298978b..d0568faf 100644 --- a/src/Crypto/AbstractKeypair.php +++ b/src/Crypto/AbstractKeypair.php @@ -11,7 +11,7 @@ abstract class AbstractKeypair { - protected const CURVE = ''; + public const CURVE = ''; public const ALG = ''; diff --git a/src/Crypto/DidKey.php b/src/Crypto/DidKey.php index 2f38445b..f3902804 100644 --- a/src/Crypto/DidKey.php +++ b/src/Crypto/DidKey.php @@ -7,7 +7,6 @@ use Mdanter\Ecc\Crypto\Key\PublicKey; use Mdanter\Ecc\Crypto\Key\PublicKeyInterface; use Mdanter\Ecc\Curves\CurveFactory; -use Mdanter\Ecc\Curves\SecgCurve; use Mdanter\Ecc\EccFactory; use Mdanter\Ecc\Serializer\Point\CompressedPointSerializer; use Mdanter\Ecc\Serializer\PublicKey\DerPublicKeySerializer; @@ -27,13 +26,9 @@ class DidKey { protected const DID_KEY_PREFIX = 'did:key:'; - protected const P256_DID_PREFIX = [0x80, 0x24]; - - protected const SECP256K1_DID_PREFIX = [0xe7, 0x01]; - protected const ALGS = [ - 'ES256' => SecgCurve::NAME_SECP_256R1, - 'ES256K' => SecgCurve::NAME_SECP_256K1, + P256Keypair::ALG => P256Keypair::CURVE, + K256Keypair::ALG => K256Keypair::CURVE, ]; /** @@ -55,8 +50,8 @@ public static function parse(string $didkey): array $alg_prefix = substr($keyBytes, offset: 0, length: 2); $alg = match ($alg_prefix) { - self::p256prefix() => 'ES256', - self::k256prefix() => 'ES256K', + P256Keypair::MULTIBASE_PREFIX => P256Keypair::ALG, + K256Keypair::MULTIBASE_PREFIX => K256Keypair::ALG, default => throw new InvalidArgumentException('Unsupported format.'), }; @@ -85,7 +80,7 @@ public static function parse(string $didkey): array return [ 'alg' => $alg, - 'key' => $pem + 'key' => $pem, ]; } @@ -103,8 +98,8 @@ public static function encode(PublicKeyInterface $pubkey): string $compressed = $serializer->serialize($pubkey->getPoint()); $prefix = match ($pubkey->getCurve()->getName()) { - SecgCurve::NAME_SECP_256R1 => self::p256prefix(), - SecgCurve::NAME_SECP_256K1 => self::k256prefix(), + P256Keypair::CURVE => P256Keypair::MULTIBASE_PREFIX, + K256Keypair::CURVE => K256Keypair::MULTIBASE_PREFIX, }; return Multibase::encode(Multibase::BASE58BTC, $prefix.hex2bin($compressed)); @@ -117,14 +112,4 @@ public static function format(PublicKeyInterface $pubkey): string { return self::DID_KEY_PREFIX.self::encode($pubkey); } - - protected static function k256prefix(): string - { - return collect(self::SECP256K1_DID_PREFIX)->implode(fn ($value) => chr($value), ''); - } - - protected static function p256prefix(): string - { - return collect(self::P256_DID_PREFIX)->implode(fn ($value) => chr($value), ''); - } } diff --git a/src/Crypto/K256Keypair.php b/src/Crypto/K256Keypair.php index cf9832f4..a17efe6c 100644 --- a/src/Crypto/K256Keypair.php +++ b/src/Crypto/K256Keypair.php @@ -4,7 +4,9 @@ class K256Keypair extends AbstractKeypair { - protected const CURVE = 'secp256k1'; + public const CURVE = 'secp256k1'; public const ALG = 'ES256K'; + + public const MULTIBASE_PREFIX = "\xe7\x01"; } diff --git a/src/Crypto/P256Keypair.php b/src/Crypto/P256Keypair.php index 8b498ec2..b7053b6e 100644 --- a/src/Crypto/P256Keypair.php +++ b/src/Crypto/P256Keypair.php @@ -4,7 +4,9 @@ class P256Keypair extends AbstractKeypair { - protected const CURVE = 'secp256r1'; + public const CURVE = 'secp256r1'; public const ALG = 'ES256'; + + public const MULTIBASE_PREFIX = "\x80\x24"; }