Skip to content

Commit

Permalink
sig
Browse files Browse the repository at this point in the history
  • Loading branch information
puklipo committed Dec 26, 2024
1 parent 152e897 commit ca1011a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Crypto/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,20 @@ public static function fromCompact(string $sig): string

return ASN1::save($r, $s);
}

public static function toCompact(string $sig): string
{
if (strlen($sig) === 64) {
return $sig;
}

$arr = ASN1::load($sig);

/** @var BigInteger $r */
$r = $arr['r'];
/** @var BigInteger $s */
$s = $arr['s'];

return $r->toBytes().$s->toBytes();
}
}
2 changes: 2 additions & 0 deletions src/Labeler/Labeler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Revolution\Bluesky\Core\CBOR;
use Revolution\Bluesky\Core\CBOR\AtBytes;
use Revolution\Bluesky\Crypto\K256;
use Revolution\Bluesky\Crypto\Signature;
use Revolution\Bluesky\FeedGenerator\ValidateAuth;
use RuntimeException;

Expand Down Expand Up @@ -151,6 +152,7 @@ public static function signLabel(UnsignedLabel $unsigned): array
}

$sign = K256::load($key)->privateKey()->sign($bytes);
$sign = Signature::toCompact($sign);

$label = Arr::add($label, 'sig', new AtBytes($sign));
$label = SignedLabel::fromArray($label);
Expand Down
11 changes: 11 additions & 0 deletions tests/Feature/Crypto/CryptoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Revolution\Bluesky\Crypto\DidKey;
use Revolution\Bluesky\Crypto\Format\Base58btc;
use Revolution\Bluesky\Crypto\K256;
use Revolution\Bluesky\Crypto\Signature;
use Tests\TestCase;

class CryptoTest extends TestCase
Expand Down Expand Up @@ -49,4 +50,14 @@ public function test_did_key_encode(): void
$this->assertSame('ES256K', $parsed['alg']);
$this->assertSame($b58key, $b58key2);
}

public function test_sig_to_compact(): void
{
$sk = K256::create()->privateKey();

$sign = $sk->sign('test');

$compact = Signature::toCompact($sign);
$this->assertSame(64, strlen($compact));
}
}

0 comments on commit ca1011a

Please sign in to comment.