simplito / elliptic-php

Fast, general Elliptic Curve Cryptography library. Supports curves used in Bitcoin, Ethereum and other cryptocurrencies (secp256k1, ed25519, ..)
MIT License
220 stars 52 forks source link

Error in verify signature: Cannot initialize #29

Open VingoYang opened 3 years ago

VingoYang commented 3 years ago

I am trying to verify the signature, but I got the error: Cannot initialize. Here is my code:

public static function verifySignature($hash, $signature, $address) { try { $sign = [ "r" => substr($signature, 0, 64), "s" => substr($signature, 64, 64), ]; $reCid = ord(hex2bin(substr($signature, 128, 2))); if ($reCid != ($reCid & 1)) { return false; } $ec = new EC('secp256k1'); $publicKey = $ec->recoverPubKey($hash, $sign, $reCid); return $address == self::pubKeyToAddress($publicKey); }catch (\Exception $exception) { throw new HashException($exception->getCode(),$exception->getMessage()); } } verifySignature("c230e229e1e7edd299b4c142ac105a84df7c114ef1d6c538ee9e8461f70b2ad3",""W6AdiMNBTRb/a9oa43CFnL+UjpdhJ4QuxfnVQuTY8EB0RQQ2pvWEeIF9c5oMrokVrCmtBiXe4vW3Fvu+mIZ2bwA=","0x38737be4bb9bdB44Fa4367935E087Dad925CE172");

I traced the error and found it threw out when executed to $ec->recoverPubKey($hash, $sign, $reCid);