44
55use InvalidArgumentException ;
66use kornrunner \Serializer \HexPrivateKeySerializer ;
7+ use kornrunner \Serializer \HexSignatureSerializer ;
78use kornrunner \Signature \Signer ;
89use Mdanter \Ecc \Crypto \Signature \SignatureInterface ;
910use Mdanter \Ecc \Curves \CurveFactory ;
@@ -24,11 +25,14 @@ class Secp256k1
2425
2526 protected $ algorithm ;
2627
28+ protected $ signatureSerializer ;
29+
2730 public function __construct (string $ hashAlgorithm ='sha256 ' ) {
2831 $ this ->adapter = new ConstantTimeMath ();
2932 $ this ->generator = CurveFactory::getGeneratorByName (SecgCurve::NAME_SECP_256K1 );
3033 $ this ->curve = $ this ->generator ->getCurve ();
3134 $ this ->deserializer = new HexPrivateKeySerializer ($ this ->generator );
35+ $ this ->signatureSerializer = new HexSignatureSerializer ();
3236 $ this ->algorithm = $ hashAlgorithm ;
3337 }
3438
@@ -49,8 +53,12 @@ public function sign(string $hash, string $privateKey, array $options=[]): Signa
4953 return $ signer ->sign ($ key , $ hex_hash , $ randomK );
5054 }
5155
52- public function verify (string $ hash , SignatureInterface $ signature , string $ publicKey ): bool
56+ public function verify (string $ hash , SignatureInterface | string $ signature , string $ publicKey ): bool
5357 {
58+ if (is_string ($ signature )) {
59+ $ signature = $ this ->signatureSerializer ->parse ($ signature );
60+ }
61+
5462 $ gmpKey = $ this ->decodePoint ($ publicKey );
5563 $ key = $ this ->generator ->getPublickeyFrom ($ gmpKey ->getX (), $ gmpKey ->getY ());
5664 $ hex_hash = gmp_init ($ hash , 16 );
0 commit comments