From ce4fd0be9a3e553453eb8402a5592b121992a5b8 Mon Sep 17 00:00:00 2001 From: rgex <124898@supinfo.com> Date: Sun, 6 Dec 2015 19:11:20 -0500 Subject: [PATCH 1/2] code quality improvements --- src/BitcoinPHP/BitcoinECDSA/BitcoinECDSA.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/BitcoinPHP/BitcoinECDSA/BitcoinECDSA.php b/src/BitcoinPHP/BitcoinECDSA/BitcoinECDSA.php index 33d6711..4a4d16b 100755 --- a/src/BitcoinPHP/BitcoinECDSA/BitcoinECDSA.php +++ b/src/BitcoinPHP/BitcoinECDSA/BitcoinECDSA.php @@ -145,7 +145,7 @@ public function generateRandom256BitsHexaString($extra = 'FkejkzqesrfeifH3ioio9h $hex = bin2hex($bytes); $random = $hex . microtime(true) . rand(100000000000, 1000000000000) . $extra; - if (!$cStrong) { + if ($cStrong === false) { throw new \Exception('Your system is not able to generate strong enough random numbers'); } $res = $this->hash256($random); @@ -724,7 +724,7 @@ public function getUncompressedAddress($compressed = false, $derPubKey = null) { if($derPubKey !== null) { - if($compressed) { + if($compressed === true) { $address = $this->getPubKey($this->getPubKeyPointsWithDerPubKey($derPubKey)); } else { @@ -733,7 +733,7 @@ public function getUncompressedAddress($compressed = false, $derPubKey = null) } else { - if($compressed) { + if($compressed === true) { $address = $this->getPubKey(); } else { @@ -1096,7 +1096,7 @@ public function getPubKeyWithRS($flag, $R, $S, $hash) if($this->checkSignaturePoints($derPubKey, $R, $S, $hash)) return $derPubKey; else - return false; + return null; } From d2dea374ef382ead071dd50a4717eda2131cdc74 Mon Sep 17 00:00:00 2001 From: rgex <124898@supinfo.com> Date: Sun, 6 Dec 2015 19:14:59 -0500 Subject: [PATCH 2/2] code modernization --- src/BitcoinPHP/BitcoinECDSA/BitcoinECDSA.php | 42 +++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/src/BitcoinPHP/BitcoinECDSA/BitcoinECDSA.php b/src/BitcoinPHP/BitcoinECDSA/BitcoinECDSA.php index 4a4d16b..b7ee589 100755 --- a/src/BitcoinPHP/BitcoinECDSA/BitcoinECDSA.php +++ b/src/BitcoinPHP/BitcoinECDSA/BitcoinECDSA.php @@ -29,8 +29,10 @@ public function __construct() $this->p = gmp_init('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F', 16); $this->n = gmp_init('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141', 16); - $this->G = array('x' => gmp_init('55066263022277343669578718895168534326250603453777594175500187360389116729240'), - 'y' => gmp_init('32670510020758816978083085130507043184471273380659243275938904335757337482424')); + $this->G = [ + 'x' => gmp_init('55066263022277343669578718895168534326250603453777594175500187360389116729240'), + 'y' => gmp_init('32670510020758816978083085130507043184471273380659243275938904335757337482424') + ]; $this->networkPrefix = '00'; } @@ -84,15 +86,16 @@ public function getNetworkPrefix() */ public function base58_permutation($char, $reverse = false) { - $table = array('1','2','3','4','5','6','7','8','9','A','B','C','D', - 'E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W', - 'X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','m','n','o', - 'p','q','r','s','t','u','v','w','x','y','z' - ); + $table = [ + '1','2','3','4','5','6','7','8','9','A','B','C','D', + 'E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W', + 'X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','m','n','o', + 'p','q','r','s','t','u','v','w','x','y','z' + ]; if($reverse) { - $reversedTable = array(); + $reversedTable = []; foreach($table as $key => $element) { $reversedTable[$element] = $key; @@ -305,7 +308,7 @@ public function doublePoint(Array $pt) // nPtX = slope^2 - 2 * ptX // Equals slope^2 - ptX - ptX - $nPt = array(); + $nPt = []; $nPt['x'] = gmp_mod( gmp_sub( gmp_sub( @@ -377,7 +380,7 @@ public function addPoints(Array $pt1, Array $pt2) ); // nPtX = slope^2 - ptX1 - ptX2 - $nPt = array(); + $nPt = []; $nPt['x'] = gmp_mod( gmp_sub( gmp_sub( @@ -474,7 +477,7 @@ public function sqrt($a) // In an infinite number field you have -2^2 = 2^2 = 4 // In a finite number field you have a^2 = (p-a)^2 $sqrt2 = gmp_mod(gmp_sub($p, $sqrt1), $p); - return array($sqrt1, $sqrt2); + return [$sqrt1, $sqrt2]; } else { @@ -569,14 +572,14 @@ public function getPubKeyPointsWithDerPubKey($derPubKey) //uncompressed der encoded public key $x = substr($derPubKey, 2, 64); $y = substr($derPubKey, 66, 64); - return array('x' => $x, 'y' => $y); + return ['x' => $x, 'y' => $y]; } else if((substr($derPubKey, 0, 2) === '02' || substr($derPubKey, 0, 2) === '03') && strlen($derPubKey) === 66) { //compressed der encoded public key $x = substr($derPubKey, 2, 64); $y = $this->calculateYWithX($x, substr($derPubKey, 0, 2)); - return array('x' => $x, 'y' => $y); + return ['x' => $x, 'y' => $y]; } else { @@ -655,8 +658,9 @@ public function getPubKeyPoints() throw new \Exception('No Private Key was defined'); } - $pubKey = $this->mulPoint($k, - array('x' => $G['x'], 'y' => $G['y']) + $pubKey = $this->mulPoint( + $k, + ['x' => $G['x'], 'y' => $G['y']] ); $pubKey['x'] = gmp_strval($pubKey['x'], 16); @@ -682,7 +686,7 @@ public function getPubKeyPoints() * @return string (hexa) * @throws \Exception */ - public function getUncompressedPubKey(array $pubKeyPts = array()) + public function getUncompressedPubKey(array $pubKeyPts = []) { if(empty($pubKeyPts)) $pubKeyPts = $this->getPubKeyPoints(); @@ -698,7 +702,7 @@ public function getUncompressedPubKey(array $pubKeyPts = array()) * @return array|string * @throws \Exception */ - public function getPubKey(array $pubKeyPts = array()) + public function getPubKey(array $pubKeyPts = []) { if(empty($pubKeyPts)) $pubKeyPts = $this->getPubKeyPoints(); @@ -911,7 +915,7 @@ public function getSignatureHashPoints($hash, $nonce = null) $R = '0' . $R; } - return array('R' => $R, 'S' => $S); + return ['R' => $R, 'S' => $S]; } /*** @@ -1062,7 +1066,7 @@ public function getPubKeyWithRS($flag, $R, $S, $hash) if($y === null) return null; - $Rpt = array('x' => $x, 'y' => $y); + $Rpt = ['x' => $x, 'y' => $y]; //step 1.6.1 //calculate r^-1 (S*Rpt - eG)