Browse Source

Merge branch 'master' of http://svn.ouj.com:3000/eos_sicbo/chat

baichun 5 years ago
parent
commit
7899a00e2c
2 changed files with 22 additions and 2 deletions
  1. 20 0
      protected/extensions/Utils.php
  2. 2 2
      protected/models/Account.php

+ 20 - 0
protected/extensions/Utils.php

@@ -242,6 +242,21 @@ class Utils
         return '0x' . $hash;
     }
 
+    public static function hashPersonalMessage($message) {
+        if (stripos($message, '0x') === 0) {
+            $message = substr($message, 2);
+        }
+        if (!ctype_xdigit($message)) {
+            throw new InvalidArgumentException('Message should be a hexadecimal');
+        }
+        if (strlen($message) % 2) {
+            throw new InvalidArgumentException('Message size cannot be odd');
+        }
+        $buffer = unpack('C*', hex2bin($message));
+        $prefix = bin2hex("\u{0019}Ethereum Signed Message:\n" . sizeof($buffer));
+        return Keccak::hash(hex2bin($prefix . $message), 256);
+    }
+
     /**
      * toString
      * 
@@ -366,6 +381,11 @@ class Utils
         return $bn->divide($bnt);
     }
 
+    public static function strToHex($str) {
+        $hex = unpack('H*', $str);
+        return '0x' . array_shift($hex);
+    }
+
     /**
      * jsonMethodToString
      * 

+ 2 - 2
protected/models/Account.php

@@ -280,9 +280,9 @@ class Account extends Model {
      * @throws Exception
      */
     public static function verifyEth($account, $data, $signed) {
-        $hash = Utils::sha3($data);
+        $hash = Utils::hashPersonalMessage(Utils::strToHex($data));
 
-        $messageGmp       = gmp_init($hash);
+        $messageGmp       = gmp_init("0x" . $hash);
         $rHex   = substr($signed, 2, 64);
         $sHex   = substr($signed, 66, 64);
         $vValue = hexdec(substr($signed, 130, 2));