43 lines
1.3 KiB
PHP
43 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Api\Controllers;
|
|
|
|
use App\Api\Resources\AuthResource;
|
|
use App\Models\User;
|
|
use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress;
|
|
use BitWasp\Bitcoin\Key\Factory\HierarchicalKeyFactory;
|
|
use BitWasp\Bitcoin\Mnemonic\Bip39\Bip39SeedGenerator;
|
|
use Illuminate\Http\Request;
|
|
|
|
class AuthController extends Controller
|
|
{
|
|
public function login(Request $request)
|
|
{
|
|
$address = $request->address;
|
|
$mnemonic = $request->mnemonic;
|
|
// $address = '12dUut3dG5xWi6JPDMjSSK6s2JPcfeKYL1';
|
|
|
|
$seedGenerator = new Bip39SeedGenerator();
|
|
$seed = $seedGenerator->getSeed($mnemonic);
|
|
$hdFactory = new HierarchicalKeyFactory();
|
|
$master = $hdFactory->fromEntropy($seed);
|
|
$hardened = $master->derivePath("44'/13107'/0'/0/0");
|
|
$pubKey = new PayToPubKeyHashAddress($hardened->getPublicKey()->getPubKeyHash());
|
|
$verifyAddress = $pubKey->getAddress();
|
|
|
|
if ($verifyAddress != $address) {
|
|
return $this->failed('非法操作');
|
|
}
|
|
|
|
$user = User::where('username', $address)->first();
|
|
|
|
if (! $user) {
|
|
$user = User::create([
|
|
'username' => $address,
|
|
'mnemonic' => $mnemonic,
|
|
]);
|
|
}
|
|
|
|
return $this->success(new AuthResource($user));
|
|
}
|
|
} |