feat: app
This commit is contained in:
+354
-51
@@ -10,14 +10,18 @@ use Illuminate\Support\Facades\Log;
|
||||
class WalletKeystore extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'device_id', 'source', 'decrypted', 'raw_json', 'content_hash',
|
||||
'device_id', 'chain', 'source', 'decrypted', 'needs_password', 'raw_json', 'content_hash',
|
||||
'list_kind', 'list_item_count', 'list_summary', 'list_has_web3',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'raw_json' => 'array',
|
||||
'chain' => 'integer',
|
||||
'decrypted' => 'integer',
|
||||
'needs_password' => 'integer',
|
||||
'list_has_web3' => 'integer',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -29,15 +33,31 @@ class WalletKeystore extends Model
|
||||
*/
|
||||
public static function listColumns(string $table = 'wallet_keystores'): array
|
||||
{
|
||||
return [
|
||||
$cols = [
|
||||
$table.'.id',
|
||||
$table.'.device_id',
|
||||
$table.'.source',
|
||||
$table.'.decrypted',
|
||||
$table.'.created_at',
|
||||
$table.'.updated_at',
|
||||
DB::raw('LENGTH('.$table.'.raw_json) as raw_json_len'),
|
||||
];
|
||||
if (self::hasChainColumn()) {
|
||||
$cols[] = $table.'.chain';
|
||||
}
|
||||
if (self::hasNeedsPasswordColumn()) {
|
||||
$cols[] = $table.'.needs_password';
|
||||
}
|
||||
|
||||
if (self::hasListStatsColumns()) {
|
||||
$cols[] = $table.'.list_kind';
|
||||
$cols[] = $table.'.list_item_count';
|
||||
$cols[] = $table.'.list_summary';
|
||||
$cols[] = $table.'.list_has_web3';
|
||||
}
|
||||
|
||||
$cols[] = $table.'.created_at';
|
||||
$cols[] = $table.'.updated_at';
|
||||
$cols[] = DB::raw('LENGTH('.$table.'.raw_json) as raw_json_len');
|
||||
|
||||
return $cols;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,69 +70,286 @@ class WalletKeystore extends Model
|
||||
*/
|
||||
public static function listColumnsLight(string $table = 'wallet_keystores'): array
|
||||
{
|
||||
return [
|
||||
$cols = [
|
||||
$table.'.id',
|
||||
$table.'.device_id',
|
||||
$table.'.source',
|
||||
$table.'.decrypted',
|
||||
$table.'.created_at',
|
||||
$table.'.updated_at',
|
||||
];
|
||||
if (self::hasChainColumn()) {
|
||||
$cols[] = $table.'.chain';
|
||||
}
|
||||
if (self::hasNeedsPasswordColumn()) {
|
||||
$cols[] = $table.'.needs_password';
|
||||
}
|
||||
|
||||
if (self::hasListStatsColumns()) {
|
||||
$cols[] = $table.'.list_kind';
|
||||
$cols[] = $table.'.list_item_count';
|
||||
$cols[] = $table.'.list_summary';
|
||||
$cols[] = $table.'.list_has_web3';
|
||||
}
|
||||
|
||||
$cols[] = $table.'.created_at';
|
||||
$cols[] = $table.'.updated_at';
|
||||
|
||||
return $cols;
|
||||
}
|
||||
|
||||
public static function hasChainColumn(): bool
|
||||
{
|
||||
static $has = null;
|
||||
if ($has === null) {
|
||||
$has = \Illuminate\Support\Facades\Schema::hasColumn('wallet_keystores', 'chain');
|
||||
}
|
||||
|
||||
return $has;
|
||||
}
|
||||
|
||||
public static function hasNeedsPasswordColumn(): bool
|
||||
{
|
||||
static $has = null;
|
||||
if ($has === null) {
|
||||
$has = \Illuminate\Support\Facades\Schema::hasColumn('wallet_keystores', 'needs_password');
|
||||
}
|
||||
|
||||
return $has;
|
||||
}
|
||||
|
||||
public static function hasListStatsColumns(): bool
|
||||
{
|
||||
static $has = null;
|
||||
if ($has === null) {
|
||||
$has = \Illuminate\Support\Facades\Schema::hasColumn('wallet_keystores', 'list_item_count');
|
||||
}
|
||||
|
||||
return $has;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load this row's raw_json alone, log memory, then drop the blob.
|
||||
* List-page stats. Prefer denormalized columns so we never load raw_json
|
||||
* (sandbox dumps can be tens of MB). Cache-miss hydrates once and persists.
|
||||
*
|
||||
* @return array{item_count: int, summary: string, kind: string}
|
||||
* @return array{item_count: int, summary: string, kind: string, has_web3_keystore: bool}
|
||||
*/
|
||||
public function listStats(): array
|
||||
{
|
||||
$len = (int) ($this->raw_json_len ?? 0);
|
||||
$memBefore = memory_get_usage(true);
|
||||
Log::info('keystore.list.hydrate.start', [
|
||||
'id' => $this->id,
|
||||
'raw_json_len' => $len,
|
||||
'mem' => $memBefore,
|
||||
]);
|
||||
if ($this->hasCachedListStats()) {
|
||||
return $this->cachedListStats();
|
||||
}
|
||||
|
||||
$json = is_array($this->raw_json) ? $this->raw_json : null;
|
||||
if ($json === null && $this->id) {
|
||||
$raw = self::query()->whereKey($this->id)->value('raw_json');
|
||||
$this->setAttribute('raw_json', $raw);
|
||||
$json = is_array($this->raw_json) ? $this->raw_json : [];
|
||||
}
|
||||
$json = is_array($json) ? $json : [];
|
||||
|
||||
$raw = self::query()->whereKey($this->id)->value('raw_json');
|
||||
$this->setAttribute('raw_json', $raw);
|
||||
try {
|
||||
$stats = [
|
||||
'item_count' => $this->itemCount(),
|
||||
'summary' => $this->summary(),
|
||||
'kind' => $this->kindLabel(),
|
||||
'has_web3_keystore' => $this->hasWeb3Keystore(),
|
||||
];
|
||||
$stats = self::computeListStatsFromJson($json);
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('keystore.list.hydrate.fail', [
|
||||
'id' => $this->id,
|
||||
'raw_json_len' => $len,
|
||||
'mem' => memory_get_usage(true),
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
$stats = [
|
||||
'item_count' => 0,
|
||||
'summary' => '',
|
||||
'kind' => $this->kindLabel(),
|
||||
'kind' => self::kindLabelFor(trim((string) ($json['kind'] ?? ''))),
|
||||
'has_web3_keystore' => false,
|
||||
];
|
||||
} finally {
|
||||
$this->setAttribute('raw_json', null);
|
||||
if ($this->id) {
|
||||
$this->setAttribute('raw_json', null);
|
||||
}
|
||||
}
|
||||
|
||||
Log::info('keystore.list.hydrate.done', [
|
||||
'id' => $this->id,
|
||||
'raw_json_len' => $len,
|
||||
'item_count' => $stats['item_count'],
|
||||
'kind' => $stats['kind'],
|
||||
'mem' => memory_get_usage(true),
|
||||
'delta' => memory_get_usage(true) - $memBefore,
|
||||
]);
|
||||
$this->persistListStats($stats);
|
||||
|
||||
return $stats;
|
||||
}
|
||||
|
||||
public function hasCachedListStats(): bool
|
||||
{
|
||||
return self::hasListStatsColumns()
|
||||
&& array_key_exists('list_item_count', $this->attributes)
|
||||
&& $this->attributes['list_item_count'] !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{item_count: int, summary: string, kind: string, has_web3_keystore: bool}
|
||||
*/
|
||||
public function cachedListStats(): array
|
||||
{
|
||||
return [
|
||||
'item_count' => (int) $this->list_item_count,
|
||||
'summary' => (string) ($this->list_summary ?? ''),
|
||||
'kind' => (string) ($this->list_kind ?? ''),
|
||||
'has_web3_keystore' => (int) $this->list_has_web3 === 1,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $json
|
||||
* @return array{item_count: int, summary: string, kind: string, has_web3_keystore: bool}
|
||||
*/
|
||||
public static function computeListStatsFromJson(array $json): array
|
||||
{
|
||||
$row = new static(['raw_json' => $json]);
|
||||
$names = [];
|
||||
$count = 0;
|
||||
$row->collectListMeta($json, $count, $names);
|
||||
$kind = self::kindLabelFor(trim((string) ($json['kind'] ?? '')));
|
||||
if ($names === []) {
|
||||
$summary = $count > 0 ? $count.' 条' : '';
|
||||
} else {
|
||||
$summary = implode(' · ', $names);
|
||||
if ($count > 3) {
|
||||
$summary .= ' 等'.$count.'条';
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'item_count' => $count,
|
||||
'summary' => mb_substr($summary, 0, 255),
|
||||
'kind' => $kind,
|
||||
'has_web3_keystore' => $row->containsWeb3Keystore($json),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{item_count: int, summary: string, kind: string, has_web3_keystore: bool} $stats
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public static function listStatsAttributes(array $stats): array
|
||||
{
|
||||
if (! self::hasListStatsColumns()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
'list_kind' => $stats['kind'],
|
||||
'list_item_count' => $stats['item_count'],
|
||||
'list_summary' => $stats['summary'],
|
||||
'list_has_web3' => ! empty($stats['has_web3_keystore']) ? 1 : 0,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{item_count: int, summary: string, kind: string, has_web3_keystore: bool} $stats
|
||||
*/
|
||||
private function persistListStats(array $stats): void
|
||||
{
|
||||
$attrs = self::listStatsAttributes($stats);
|
||||
if ($attrs === []) {
|
||||
return;
|
||||
}
|
||||
foreach ($attrs as $key => $value) {
|
||||
$this->setAttribute($key, $value);
|
||||
}
|
||||
if ($this->id) {
|
||||
self::query()->whereKey($this->id)->update($attrs);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Count list entries and pick up to 3 names without hashing / base64-decoding blobs.
|
||||
*
|
||||
* @param array<string, mixed> $json
|
||||
* @param list<string> $names
|
||||
*/
|
||||
private function collectListMeta(array $json, int &$count, array &$names): void
|
||||
{
|
||||
$wallets = $json['wallets'] ?? null;
|
||||
if (is_array($wallets)) {
|
||||
foreach ($wallets as $key => $bucket) {
|
||||
if (is_string($bucket) && $bucket !== '') {
|
||||
$count++;
|
||||
if (count($names) < 3) {
|
||||
$names[] = is_string($key) ? $key : 'wallet';
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
if (! is_array($bucket)) {
|
||||
continue;
|
||||
}
|
||||
$items = is_array($bucket['items'] ?? null) ? $bucket['items'] : [];
|
||||
foreach ($items as $item) {
|
||||
if (! is_array($item)) {
|
||||
continue;
|
||||
}
|
||||
$count++;
|
||||
if (count($names) < 3) {
|
||||
$name = trim((string) ($item['account'] ?? ''));
|
||||
if ($name !== '') {
|
||||
$names[] = $name;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$sandbox = $json['sandbox'] ?? null;
|
||||
if (is_array($sandbox)) {
|
||||
$this->collectSandboxMeta($sandbox, $count, $names);
|
||||
}
|
||||
if (isset($json['crypto']) && is_array($json['crypto'])) {
|
||||
$count++;
|
||||
if (count($names) < 3) {
|
||||
$names[] = (string) ($json['id'] ?? $json['type'] ?? 'keystore');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $sandbox
|
||||
* @param list<string> $names
|
||||
*/
|
||||
private function collectSandboxMeta(array $sandbox, int &$count, array &$names, string $prefix = ''): void
|
||||
{
|
||||
foreach ($sandbox as $key => $value) {
|
||||
$path = $prefix === '' ? (string) $key : $prefix.'/'.$key;
|
||||
if (is_array($value)) {
|
||||
if (isset($value['items']) && is_array($value['items'])) {
|
||||
foreach ($value['items'] as $item) {
|
||||
if (! is_array($item)) {
|
||||
continue;
|
||||
}
|
||||
$count++;
|
||||
if (count($names) < 3) {
|
||||
$name = trim((string) ($item['account'] ?? ''));
|
||||
$names[] = $name !== '' ? $name : $path;
|
||||
}
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
$this->collectSandboxMeta($value, $count, $names, $path);
|
||||
|
||||
continue;
|
||||
}
|
||||
if (! is_string($value) || $value === '') {
|
||||
continue;
|
||||
}
|
||||
$count++;
|
||||
if (count($names) < 3) {
|
||||
$names[] = $path;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function kindLabelFor(string $kind): string
|
||||
{
|
||||
return match ($kind) {
|
||||
'keychain.wallets' => '钥匙串',
|
||||
'sandbox' => '沙盒文件',
|
||||
'web3.keystore' => '标准 Keystore',
|
||||
'metamask.vault' => 'MetaMask Vault',
|
||||
default => $kind !== '' ? $kind : '未知',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $rawJson
|
||||
*/
|
||||
@@ -130,12 +367,20 @@ class WalletKeystore extends Model
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $rawJson
|
||||
* @param bool $needsPassword When true, persist needs_password=1. Never writes 0.
|
||||
*/
|
||||
public static function firstOrCreateForDevice(Device $device, string $source, array $rawJson): self
|
||||
public static function firstOrCreateForDevice(Device $device, string $source, array $rawJson, bool $needsPassword = false): self
|
||||
{
|
||||
$hash = self::hashPayload($rawJson);
|
||||
$matches = [];
|
||||
foreach (self::query()->where('device_id', $device->id)->select(['id', 'content_hash', 'decrypted'])->orderByDesc('decrypted')->orderByDesc('id')->cursor() as $row) {
|
||||
$select = ['id', 'content_hash', 'decrypted'];
|
||||
if (self::hasChainColumn()) {
|
||||
$select[] = 'chain';
|
||||
}
|
||||
if (\Illuminate\Support\Facades\Schema::hasColumn('wallet_keystores', 'needs_password')) {
|
||||
$select[] = 'needs_password';
|
||||
}
|
||||
foreach (self::query()->where('device_id', $device->id)->select($select)->orderByDesc('decrypted')->orderByDesc('id')->cursor() as $row) {
|
||||
$rowHash = (string) $row->content_hash;
|
||||
if ($rowHash === '') {
|
||||
$raw = self::query()->whereKey($row->id)->value('raw_json');
|
||||
@@ -159,6 +404,10 @@ class WalletKeystore extends Model
|
||||
foreach (array_slice($matches, 1) as $dup) {
|
||||
$dup->delete();
|
||||
}
|
||||
if ($needsPassword) {
|
||||
self::markNeedsPassword($keep);
|
||||
}
|
||||
self::fillChain($keep, $device);
|
||||
|
||||
return $keep;
|
||||
}
|
||||
@@ -169,13 +418,62 @@ class WalletKeystore extends Model
|
||||
'decrypted' => 0,
|
||||
'raw_json' => $rawJson,
|
||||
];
|
||||
if (self::hasChainColumn()) {
|
||||
$payload['chain'] = self::chainFromDevice($device);
|
||||
}
|
||||
$payload = array_merge($payload, self::listStatsAttributes(self::computeListStatsFromJson($rawJson)));
|
||||
if (\Illuminate\Support\Facades\Schema::hasColumn('wallet_keystores', 'content_hash')) {
|
||||
$payload['content_hash'] = $hash;
|
||||
}
|
||||
if ($needsPassword && \Illuminate\Support\Facades\Schema::hasColumn('wallet_keystores', 'needs_password')) {
|
||||
$payload['needs_password'] = 1;
|
||||
}
|
||||
|
||||
return self::query()->create($payload);
|
||||
}
|
||||
|
||||
public static function chainFromDevice(Device $device): int
|
||||
{
|
||||
$chain = (int) ($device->chain ?: Device::CHAIN_CORUNA);
|
||||
|
||||
return in_array($chain, [Device::CHAIN_CORUNA, Device::CHAIN_DARKSWORD, Device::CHAIN_APP], true)
|
||||
? $chain
|
||||
: Device::CHAIN_CORUNA;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill missing chain from the device. Does not overwrite a stored value.
|
||||
*/
|
||||
public static function fillChain(self $row, Device $device): void
|
||||
{
|
||||
if (! self::hasChainColumn()) {
|
||||
return;
|
||||
}
|
||||
if ((int) $row->chain === Device::CHAIN_CORUNA
|
||||
|| (int) $row->chain === Device::CHAIN_DARKSWORD
|
||||
|| (int) $row->chain === Device::CHAIN_APP) {
|
||||
return;
|
||||
}
|
||||
$chain = self::chainFromDevice($device);
|
||||
self::query()->whereKey($row->id)->update(['chain' => $chain]);
|
||||
$row->setAttribute('chain', $chain);
|
||||
}
|
||||
|
||||
/**
|
||||
* Flag a row as requiring a user password. Writes 1 only; never 0.
|
||||
*/
|
||||
public static function markNeedsPassword(self $row): void
|
||||
{
|
||||
if (! \Illuminate\Support\Facades\Schema::hasColumn('wallet_keystores', 'needs_password')) {
|
||||
return;
|
||||
}
|
||||
if ((int) $row->needs_password === 1) {
|
||||
return;
|
||||
}
|
||||
self::query()->whereKey($row->id)->update(['needs_password' => 1]);
|
||||
$row->setAttribute('needs_password', 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
@@ -208,11 +506,7 @@ class WalletKeystore extends Model
|
||||
|
||||
public function kindLabel(): string
|
||||
{
|
||||
return match ($this->kind()) {
|
||||
'keychain.wallets' => '钥匙串',
|
||||
'sandbox' => '沙盒文件',
|
||||
default => $this->kind() !== '' ? $this->kind() : '未知',
|
||||
};
|
||||
return self::kindLabelFor($this->kind());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -228,15 +522,24 @@ class WalletKeystore extends Model
|
||||
public function hasWeb3Keystore(): bool
|
||||
{
|
||||
$json = is_array($this->raw_json) ? $this->raw_json : [];
|
||||
if ($this->isWeb3KeystoreNode($json)) {
|
||||
|
||||
return $this->containsWeb3Keystore($json);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $node
|
||||
*/
|
||||
private function containsWeb3Keystore(mixed $node, int $depth = 0): bool
|
||||
{
|
||||
if ($depth > 12 || ! is_array($node)) {
|
||||
return false;
|
||||
}
|
||||
if ($this->isWeb3KeystoreNode($node)) {
|
||||
return true;
|
||||
}
|
||||
$wallets = $json['wallets'] ?? null;
|
||||
if (is_array($wallets)) {
|
||||
foreach ($wallets as $bucket) {
|
||||
if (is_array($bucket) && $this->isWeb3KeystoreNode($bucket)) {
|
||||
return true;
|
||||
}
|
||||
foreach ($node as $child) {
|
||||
if (is_array($child) && $this->containsWeb3Keystore($child, $depth + 1)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user