diff --git a/src/Bitpay/Bitauth.php b/src/Bitpay/Bitauth.php index 4a75706..fc47506 100644 --- a/src/Bitpay/Bitauth.php +++ b/src/Bitpay/Bitauth.php @@ -60,12 +60,11 @@ class Bitauth } /** - * @return Sin + * @return SinKey */ public function getSinFromPublicKey(\Bitpay\PublicKey $publicKey) { - // TODO - //return Sin::fromPubKey($publicKey); + return $publicKey->getSin(); } /** @@ -78,80 +77,6 @@ class Bitauth public function sign($data, \Bitpay\PrivateKey $privateKey) { return $privateKey->sign($data); - - //if (!ctype_xdigit($privateKey->getHex())) { - // throw new \Exception('The private key must be in hex format.'); - //} - - //if (empty($data)) { - // throw new \Exception('You did not provide any data to sign.'); - //} - - //$e = Util::decodeHex(hash('sha256', $data)); - - //do { - // if (substr(strtolower($privateKey->getHex()), 0, 2) != '0x') { - // $d = '0x'.$privateKey->getHex(); - // } else { - // $d = $privateKey->getHex(); - // } - - // $k = SecureRandom::generateRandom(32); - - // $k_hex = '0x'.strtolower(bin2hex($k)); - // $n_hex = '0x'.Secp256k1::N; - // $a_hex = '0x'.Secp256k1::A; - // $p_hex = '0x'.Secp256k1::P; - - // $Gx = '0x'.substr(Secp256k1::G, 0, 62); - // $Gy = '0x'.substr(Secp256k1::G, 64, 62); - - // $P = new Point($Gx, $Gy); - - // // Calculate a new curve point from Q=k*G (x1,y1) - // $R = Gmp::doubleAndAdd($k_hex, $P); - - // $Rx_hex = Util::encodeHex($R->getX()); - // $Ry_hex = Util::encodeHex($R->getY()); - - // while (strlen($Rx_hex) < 64) { - // $Rx_hex = '0'.$Rx_hex; - // } - - // while (strlen($Ry_hex) < 64) { - // $Ry_hex = '0'.$Ry_hex; - // } - - // // r = x1 mod n - // $r = gmp_strval(gmp_mod('0x'.$Rx_hex, $n_hex)); - - // // s = k^-1 * (e+d*r) mod n - // $edr = gmp_add($e, gmp_mul($d, $r)); - // $invk = gmp_invert($k_hex, $n_hex); - // $kedr = gmp_mul($invk, $edr); - // $s = gmp_strval(gmp_mod($kedr, $n_hex)); - - // // The signature is the pair (r,s) - // $signature = array( - // 'r' => Util::encodeHex($r), - // 's' => Util::encodeHex($s), - // ); - - // while (strlen($signature['r']) < 64) { - // $signature['r'] = '0'.$signature['r']; - // } - - // while (strlen($signature['s']) < 64) { - // $signature['s'] = '0'.$signature['s']; - // } - //} while (gmp_cmp($r, '0') <= 0 || gmp_cmp($s, '0') <= 0); - - //$sig = array( - // 'sig_rs' => $signature, - // 'sig_hex' => self::serializeSig($signature['r'], $signature['s']), - //); - - //return $sig['sig_hex']['seq']; } /** @@ -222,14 +147,12 @@ class Bitauth /** * Verifies a previously generated ECDSA signature * - * @param string - * @param string * @param array * @param array * @param string * @return boolean */ - public function verifySignature($contract, $publicKey, $signature, $Q, $data) + public function verifySignature($signature, $Q, $data) { $Gx = '0x'.substr(Secp256k1::G, 0, 62); $Gy = '0x'.substr(Secp256k1::G, 64, 62); @@ -262,20 +185,20 @@ class Bitauth // calculate u2 = r*w (mod n) $u2 = gmp_mod(gmp_mul($r, $w), $n_hex); - $P = array('x' => $Gx, 'y' => $Gy); + $P = new Point($Gx, $Gy); $Qx = '0x'.substr($Q, 2, 64); $Qy = '0x'.substr($Q, 66, 64); - $Q = array('x' => $Qx, 'y' => $Qy); + $Q = new Point($Qx, $Qy); // Get new point Z(x1,y1) = (u1 * G) + (u2 * Q) - $Za = Gmp::doubleAndAdd($u1, $P, $p_hex, $a_hex); - $Zb = Gmp::doubleAndAdd($u2, $Q, $p_hex, $a_hex); - $Z = Gmp::gmpPointAdd($Za, $Zb, $p_hex, $a_hex); + $Za = Gmp::doubleAndAdd($u1, $P); + $Zb = Gmp::doubleAndAdd($u2, $Q); + $Z = Gmp::gmpPointAdd($Za, $Zb); - $Zx_hex = Util::encodeHex($Z['x']); - $Zy_hex = Util::encodeHex($Z['y']); + $Zx_hex = Util::encodeHex($Z->getX()); + $Zy_hex = Util::encodeHex($Z->getY()); while (strlen($Zx_hex) < 64) { $Zx_hex = '0'.$Zx_hex; diff --git a/src/Bitpay/PublicKey.php b/src/Bitpay/PublicKey.php index fce40d3..e28800c 100644 --- a/src/Bitpay/PublicKey.php +++ b/src/Bitpay/PublicKey.php @@ -71,8 +71,12 @@ class PublicKey extends Key * * @return Bitpay\PublicKey */ - public function generate() + public function generate(PrivateKey $privateKey = null) { + if ($privateKey instanceof PrivateKey) { + $this->setPrivateKey($privateKey); + } + if (!empty($this->hex)) { return $this; }