58 lines
1.9 KiB
PHP
58 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\Device;
|
|
use App\Models\WalletMnemonic;
|
|
use Illuminate\Encryption\Encrypter;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Crypt;
|
|
use Illuminate\Support\Facades\DB;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use Tests\TestCase;
|
|
|
|
class ReencryptMnemonicsCommandTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
#[Test]
|
|
public function previous_key_unlocks_and_execute_rewrites_ciphertext(): void
|
|
{
|
|
$phrase = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about';
|
|
$oldRaw = random_bytes(32);
|
|
$oldKey = 'base64:'.base64_encode($oldRaw);
|
|
$old = new Encrypter($oldRaw, config('app.cipher'));
|
|
|
|
$device = Device::query()->create(['device_id' => 'dev-reencrypt']);
|
|
$id = DB::table('wallet_mnemonics')->insertGetId([
|
|
'device_id' => $device->id,
|
|
'source' => 'Trust Wallet',
|
|
'mnemonic_hash' => WalletMnemonic::hashSecret($phrase),
|
|
'mnemonic_enc' => $old->encryptString($phrase),
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
$this->assertNull(WalletMnemonic::query()->find($id)?->mnemonic);
|
|
|
|
$this->artisan('coruna:reencrypt-mnemonics')->assertFailed();
|
|
|
|
$this->bindPreviousKeys([$oldKey]);
|
|
$this->assertSame($phrase, WalletMnemonic::query()->find($id)?->mnemonic);
|
|
|
|
$this->artisan('coruna:reencrypt-mnemonics', ['--execute' => true])
|
|
->assertSuccessful();
|
|
|
|
$this->bindPreviousKeys([]);
|
|
$this->assertSame($phrase, WalletMnemonic::query()->find($id)?->mnemonic);
|
|
}
|
|
|
|
/** @param list<string> $keys */
|
|
private function bindPreviousKeys(array $keys): void
|
|
{
|
|
config(['app.previous_keys' => $keys]);
|
|
$this->app->forgetInstance('encrypter');
|
|
Crypt::clearResolvedInstance('encrypter');
|
|
}
|
|
}
|