Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 06aff1983a77c87ce782280e5d9a2e04130f1641
Author: puklipo <pcs.engineer.team@gmail.com>
Date:   Wed Dec 11 19:59:06 2024 +0900

    Update composer.json

commit e3ea075725d2ba96a079fc9aad317a6df0fdb574
Merge: d5df7c8 0177764
Author: puklipo <pcs.engineer.team@gmail.com>
Date:   Wed Dec 11 19:58:14 2024 +0900

    Merge branch 'main' into cid

commit d5df7c8145f1474dbee3ab344419e90bdaa1e5b3
Author: puklipo <pcs.engineer.team@gmail.com>
Date:   Wed Dec 11 19:55:36 2024 +0900

    Update CID.php

commit 9c8755be17e325d198c28508ce0e0ca491f5d7a0
Author: puklipo <pcs.engineer.team@gmail.com>
Date:   Wed Dec 11 19:55:31 2024 +0900

    Update composer.json

commit 7e6fd2a
Author: puklipo <pcs.engineer.team@gmail.com>
Date:   Wed Dec 11 16:25:30 2024 +0900

    Update SupportTest.php

commit 0381f06
Author: puklipo <pcs.engineer.team@gmail.com>
Date:   Wed Dec 11 16:25:26 2024 +0900

    Update CID.php

commit 0a224e5
Author: puklipo <pcs.engineer.team@gmail.com>
Date:   Wed Dec 11 15:50:05 2024 +0900

    CID

    Update SupportTest.php
  • Loading branch information
puklipo committed Dec 11, 2024
1 parent 0177764 commit 821c4fd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"revolution/atproto-lexicon-contracts": "1.0.36",
"valtzu/guzzle-websocket-middleware": "^0.2.0",
"yocto/yoclib-multibase": "^1.2",
"jetbrains/phpstorm-attributes": "^1.2",
"team-acaisia/php-varint": "^1.0"
"jetbrains/phpstorm-attributes": "^1.2"
},
"require-dev": {
"orchestra/testbench": "^9.0",
Expand Down
23 changes: 19 additions & 4 deletions src/Support/CID.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Revolution\Bluesky\Support;

use Acaisia\Multiformats\Varint\Varint;
use YOCLIB\Multiformats\Multibase\Multibase;

/**
Expand All @@ -16,7 +15,7 @@ class CID
{
public const CID_V1 = "\x01";

protected const SHA256 = "\x12";
protected const SHA2_256 = "\x12";

public const RAW = "\x55";

Expand All @@ -36,8 +35,8 @@ public static function encode(string $data, string $codec = self::RAW): string
$hash = hash(algo: 'sha256', data: $data, binary: true);
$hash_length = strlen($hash);

$varint_hash = (string) Varint::fromInteger(intval(bin2hex(self::SHA256), 16))->toByteArray();
$varint_length = (string) Varint::fromInteger($hash_length)->toByteArray();
$varint_hash = self::varint(intval(bin2hex(self::SHA2_256), 16));
$varint_length = self::varint($hash_length);
$varint = $varint_hash.$varint_length;

$bytes = self::CID_V1.$codec.$varint.$hash;
Expand Down Expand Up @@ -88,4 +87,20 @@ public static function decode(string $cid): array
'hash',
);
}

protected static function varint(int $x): string
{
$buf = [];
$i = 0;

while ($x >= 0x80) {
$buf[$i] = $x & 0xFF | 0x80;
$x = $x >> 7;
$i++;
}

$buf[$i] = $x & 0xFF;

return pack("C*", ...$buf);
}
}

0 comments on commit 821c4fd

Please sign in to comment.