1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| 依赖 php-ecrecover web3.pnp require './php-sdk/web3p/autoload.php'; require './php-sdk/php-ecrecover/autoload.php'; require_once './php-sdk/php-ecrecover/CryptoCurrencyPHP/Signature.class.php'; require_once './php-sdk/php-ecrecover/CryptoCurrencyPHP/SECp256k1.class.php'; require_once './php-sdk/php-ecrecover/CryptoCurrencyPHP/PointMathGMP.class.php';
use kornrunner\Keccak; use Signature; use Think\Controller; use Web3\Utils; $web3Utils = new Utils(); // $hexStrBody = $web3Utils::toHex($msg); $paramHash = $web3Utils::sha3($msg); // $arr['code'] = 0; // $arr['msg'] = $paramHash; // $this->ajaxReturn($arr, "json"); // 解析出区块链地址 $addUid = $this->VerifySigner('0x' . $sign, $paramHash); private function VerifySigner($signed, $hex) { $rHex = substr($signed, 2, 64); $sHex = substr($signed, 66, 64); $vValue = hexdec(substr($signed, 130, 2)); $messageHex = substr($hex, 2); $messageByteArray = unpack('C*', hex2bin($messageHex)); $messageGmp = gmp_init("0x" . $messageHex); $r = $rHex; //hex string without 0x $s = $sHex; //hex string without 0x $v = $vValue; //27 or 28
//with hex2bin it gives the same byte array as the javascript $rByteArray = unpack('C*', hex2bin($r)); $sByteArray = unpack('C*', hex2bin($s)); $rGmp = gmp_init("0x" . $r); $sGmp = gmp_init("0x" . $s);
if ($v != 27 && $v != 28) { $v += 27; }
$recovery = $v - 27; if ($recovery !== 0 && $recovery !== 1) { throw new Exception('Invalid signature v value'); }
$publicKey = Signature::recoverPublicKey($rGmp, $sGmp, $messageGmp, $recovery); $publicKeyString = $publicKey["x"] . $publicKey["y"];
return '0x' . substr($this->keccak256(hex2bin($publicKeyString)), -40);
}
|