Browse Source

修改签名方式

solupro 5 years ago
parent
commit
9eac0a0bed
2 changed files with 16 additions and 1 deletions
  1. 15 0
      protected/extensions/Utils.php
  2. 1 1
      protected/models/Account.php

+ 15 - 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), self::HASH_SIZE);
+    }
+
     /**
      * toString
      * 

+ 1 - 1
protected/models/Account.php

@@ -280,7 +280,7 @@ class Account extends Model {
      * @throws Exception
      */
     public static function verifyEth($account, $data, $signed) {
-        $hash = Utils::sha3($data);
+        $hash = Utils::hashPersonalMessage($data);
 
         $messageGmp       = gmp_init($hash);
         $rHex   = substr($signed, 2, 64);