43 lines
1.6 KiB
PHP
43 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace web\admin\demo\dyn;
|
|
|
|
class web3 {
|
|
public static function json_checkweb3() {
|
|
$post = new \ciy\post();
|
|
$addr = $post->get('addr');
|
|
if (empty($addr))
|
|
return errjson('请填写钱包地址');
|
|
$signature = $post->get('sign');
|
|
if (empty($signature))
|
|
return errjson('请填写签名');
|
|
$message = $post->get('hash');
|
|
if (empty($message))
|
|
return errjson('请填写hash');
|
|
//composer require simplito/elliptic-php kornrunner/keccak
|
|
require_once PATH_ROOT . '../libs/ethereum/autoload.php';
|
|
$prefix = "\x19Ethereum Signed Message:\n" . strlen($message);
|
|
$hash = \kornrunner\Keccak::hash($prefix . $message, 256);
|
|
$signature = substr($signature, 2);
|
|
if (strlen($signature) !== 130)
|
|
return '签名数据长度错误';
|
|
$r = substr($signature, 0, 64);
|
|
$s = substr($signature, 64, 64);
|
|
$v = substr($signature, 128, 2);
|
|
$v = hexdec($v);
|
|
if ($v == 27 || $v == 28)
|
|
$v -= 27;
|
|
$ec = new \Elliptic\EC('secp256k1');
|
|
$publicKey = $ec->recoverPubKey($hash, [
|
|
'r' => $r,
|
|
's' => $s
|
|
], $v);
|
|
$publicKeyHex = $publicKey->encode('hex');
|
|
$publicKeyBytes = substr(hex2bin($publicKeyHex), 1);
|
|
$addressFromKey = '0x' . substr(\kornrunner\Keccak::hash($publicKeyBytes, 256), 24);
|
|
$addressFromKey = strtolower($addressFromKey);
|
|
$ret['msg'] = strtolower($addr) === $addressFromKey ? "签名验证成功" : "签名验证失败";
|
|
return succjson($ret);
|
|
}
|
|
}
|