-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDownloadRepoCommand.php
109 lines (84 loc) · 2.78 KB
/
DownloadRepoCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
declare(strict_types=1);
namespace Revolution\Bluesky\Console;
use GuzzleHttp\Psr7\Utils;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Revolution\Bluesky\Core\CAR;
use Revolution\Bluesky\Crypto\DidKey;
use Revolution\Bluesky\Facades\Bluesky;
use Revolution\Bluesky\Support\DidDocument;
use Revolution\Bluesky\Support\Identity;
/**
* Sample command to download the actor's "CAR" file.
*
* ```
* php artisan bluesky:download-repo ***.bsky.social
* ```
*
* @link https://docs.bsky.app/blog/repo-export
*/
class DownloadRepoCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'bluesky:download-repo {actor : DID or handle}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Download actor\'s car file. Does not require auth.';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$actor = $this->argument('actor');
$this->warn('Actor: '.$actor);
if (Identity::isHandle($actor)) {
$did = Bluesky::resolveHandle($actor)->json('did');
} else {
$did = $actor;
}
if (! Identity::isDID($did)) {
$this->error('Invalid actor');
return 1;
}
/** @var string $did */
$this->warn('DID: '.$did);
$didDoc = DidDocument::make(Bluesky::identity()->resolveDID($did, cache: false)->json());
$pds = $didDoc->pdsUrl();
$this->warn('PDS: '.$pds);
$response = Bluesky::client(auth: false)
->sync()
->baseUrl($pds.'/xrpc/')
->getRepo(did: $did)
->throw();
$name = Str::slug($actor, dictionary: ['.' => '-', ':' => '-']);
$did_doc_file = collect(['bluesky', 'download', $name, $name.'-did.json'])
->implode(DIRECTORY_SEPARATOR);
Storage::put($did_doc_file, json_encode($didDoc->toArray(), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR));
$file = collect(['bluesky', 'download', $name, $name.'.car'])
->implode(DIRECTORY_SEPARATOR);
Storage::put($file, $response->body());
$this->line('Download: '.Storage::path($file));
$signed = CAR::signedCommit(Utils::streamFor(Storage::readStream($file)));
//dump($signed);
$pk = DidKey::parse($didDoc->publicKey());
if (CAR::verifySignedCommit($signed, $pk)) {
$this->info('Verified');
} else {
$this->error('Verify failed');
return 1;
}
$this->info('Download successful');
return 0;
}
}