Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kawax committed Dec 3, 2024
1 parent f5a5307 commit 87ac816
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Crypto/AbstractKeypair.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

abstract class AbstractKeypair
{
protected const CURVE = '';
public const CURVE = '';

public const ALG = '';

Expand Down
29 changes: 7 additions & 22 deletions src/Crypto/DidKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
];

/**
Expand All @@ -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.'),
};

Expand Down Expand Up @@ -85,7 +80,7 @@ public static function parse(string $didkey): array

return [
'alg' => $alg,
'key' => $pem
'key' => $pem,
];
}

Expand All @@ -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));
Expand All @@ -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), '');
}
}
4 changes: 3 additions & 1 deletion src/Crypto/K256Keypair.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
4 changes: 3 additions & 1 deletion src/Crypto/P256Keypair.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

0 comments on commit 87ac816

Please sign in to comment.