From 75288407c06318d109c34e61a903b704aabda9f7 Mon Sep 17 00:00:00 2001 From: alexdnepro Date: Wed, 11 Jan 2023 16:13:22 +0300 Subject: [PATCH 01/32] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 3fa5d3b..7aaa20a 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ } ], "require": { - "php": "^7.0", + "php": ">=7.0", "stephenhill/base58": "^1.1", "bitwasp/bech32": "^0.0.1", "btccom/cashaddress": "^0.0.3" From 246bd7fdaebec7a66db770c9107e45010b8f2029 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 16 May 2023 19:41:41 -0700 Subject: [PATCH 02/32] Add PHPCS --- composer.json | 23 ++++++++++++++++++++--- phpcs.xml | 11 +++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 phpcs.xml diff --git a/composer.json b/composer.json index 7aaa20a..112f82b 100644 --- a/composer.json +++ b/composer.json @@ -14,10 +14,11 @@ } ], "require": { - "php": ">=7.0", - "stephenhill/base58": "^1.1", "bitwasp/bech32": "^0.0.1", - "btccom/cashaddress": "^0.0.3" + "btccom/cashaddress": "^0.0.3", + "dealerdirect/phpcodesniffer-composer-installer": "*", + "squizlabs/php_codesniffer": "*", + "stephenhill/base58": "^1.1" }, "require-dev": { "phpunit/phpunit": "^5.7" @@ -31,5 +32,21 @@ "psr-4": { "AndKom\\Bitcoin\\Address\\Tests\\": "tests/" } + }, + "config": { + "sort-packages": true, + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } + }, + "scripts": { + "test": [ + "phpunit" + ], + "lint": [ + "phpcbf || true", + "phpcs || true", + "phpstan" + ] } } diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..c40d9c0 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,11 @@ + + + + PHPCS configuration file. + + src + tests + + + + \ No newline at end of file From 84718d078b0152da9f06e01fbf0bb9bad8757757 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 16 May 2023 19:43:04 -0700 Subject: [PATCH 03/32] Autofix `phpcbf` --- src/Exception.php | 2 +- src/Network/NetworkFactory.php | 12 ++++++------ src/Network/NetworkInterface.php | 2 +- src/Network/Networks/Bitcoin.php | 3 ++- src/Network/Networks/BitcoinCash.php | 8 +++++--- src/Network/Networks/BitcoinGold.php | 2 +- src/Network/Networks/BitcoinTestnet.php | 2 +- src/Network/Networks/Dash.php | 2 +- src/Network/Networks/DashTestnet.php | 2 +- src/Network/Networks/Dogecoin.php | 2 +- src/Network/Networks/DogecoinTestnet.php | 2 +- src/Network/Networks/Litecoin.php | 2 +- src/Network/Networks/LitecoinTestnet.php | 2 +- src/Network/Networks/Viacoin.php | 2 +- src/Network/Networks/ViacoinTestnet.php | 2 +- src/Network/Networks/Zcash.php | 2 +- src/Output/AbstractOutput.php | 4 ++-- src/Output/Op.php | 2 +- src/Output/OutputFactory.php | 18 +++++++++--------- src/Output/OutputInterface.php | 8 ++++---- src/Output/Outputs/P2ms.php | 12 +++++++----- src/Output/Outputs/P2pk.php | 12 +++++++----- src/Output/Outputs/P2pkh.php | 9 +++++---- src/Output/Outputs/P2sh.php | 12 +++++++----- src/Output/Outputs/P2wpkh.php | 12 +++++++----- src/Output/Outputs/P2wsh.php | 12 +++++++----- src/Utils.php | 14 +++++++------- src/Validate.php | 10 +++++----- tests/BitcoinCashTest.php | 2 +- tests/BitcoinTest.php | 2 +- tests/OutputFactoryTest.php | 2 +- tests/P2msTest.php | 2 +- tests/P2pkTest.php | 2 +- tests/P2pkhTest.php | 2 +- tests/P2shTest.php | 2 +- tests/P2wpkhTest.php | 2 +- tests/P2wshTest.php | 2 +- 37 files changed, 103 insertions(+), 89 deletions(-) diff --git a/src/Exception.php b/src/Exception.php index 34b30d5..2f5be19 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -10,4 +10,4 @@ */ class Exception extends \Exception { -} \ No newline at end of file +} diff --git a/src/Network/NetworkFactory.php b/src/Network/NetworkFactory.php index e35582b..0180889 100644 --- a/src/Network/NetworkFactory.php +++ b/src/Network/NetworkFactory.php @@ -29,7 +29,7 @@ class NetworkFactory /** * @var array */ - static protected $networks = [ + protected static $networks = [ 'bitcoin', 'bitcoinTestnet', 'bitcoinCash', @@ -49,12 +49,12 @@ class NetworkFactory /** * @var NetworkInterface */ - static protected $defaultNetwork; + protected static $defaultNetwork; /** * @param NetworkInterface $network */ - static public function setDefaultNetwork(NetworkInterface $network) + public static function setDefaultNetwork(NetworkInterface $network) { static::$defaultNetwork = $network; } @@ -62,7 +62,7 @@ static public function setDefaultNetwork(NetworkInterface $network) /** * @return NetworkInterface */ - static public function getDefaultNetwork(): NetworkInterface + public static function getDefaultNetwork(): NetworkInterface { return static::$defaultNetwork ?: static::bitcoin(); } @@ -102,6 +102,6 @@ public function __call($name, $arguments) */ public static function __callStatic($name, $arguments) { - return (new static)->createNetwork($name, $arguments); + return (new static())->createNetwork($name, $arguments); } -} \ No newline at end of file +} diff --git a/src/Network/NetworkInterface.php b/src/Network/NetworkInterface.php index d4c06f3..935682c 100644 --- a/src/Network/NetworkInterface.php +++ b/src/Network/NetworkInterface.php @@ -41,4 +41,4 @@ public function getAddressP2wsh(string $witnessHash): string; * @return OutputInterface */ public function decodeAddress(string $address): OutputInterface; -} \ No newline at end of file +} diff --git a/src/Network/Networks/Bitcoin.php b/src/Network/Networks/Bitcoin.php index 39f6790..8fd025d 100644 --- a/src/Network/Networks/Bitcoin.php +++ b/src/Network/Networks/Bitcoin.php @@ -13,6 +13,7 @@ use AndKom\Bitcoin\Address\Output\Outputs\P2wsh; use AndKom\Bitcoin\Address\Utils; use AndKom\Bitcoin\Address\Validate; + use function BitWasp\Bech32\decodeSegwit; use function BitWasp\Bech32\encodeSegwit; @@ -121,4 +122,4 @@ public function decodeAddress(string $address): OutputInterface throw new Exception('Cannot decode address.'); } -} \ No newline at end of file +} diff --git a/src/Network/Networks/BitcoinCash.php b/src/Network/Networks/BitcoinCash.php index ceeb462..c9f5a44 100644 --- a/src/Network/Networks/BitcoinCash.php +++ b/src/Network/Networks/BitcoinCash.php @@ -62,8 +62,10 @@ public function getAddressP2sh(string $scriptHash): string */ public function decodeAddress(string $address): OutputInterface { - if (strpos($address, $this->prefixP2pkh) !== 0 || - strpos($address, $this->prefixP2sh) !== 0) { + if ( + strpos($address, $this->prefixP2pkh) !== 0 || + strpos($address, $this->prefixP2sh) !== 0 + ) { throw new Exception('Cannot decode address.'); } @@ -75,4 +77,4 @@ public function decodeAddress(string $address): OutputInterface return new P2sh($hash); } } -} \ No newline at end of file +} diff --git a/src/Network/Networks/BitcoinGold.php b/src/Network/Networks/BitcoinGold.php index 092eb79..8930b56 100644 --- a/src/Network/Networks/BitcoinGold.php +++ b/src/Network/Networks/BitcoinGold.php @@ -24,4 +24,4 @@ class BitcoinGold extends Bitcoin * @var null */ protected $prefixBech32 = 'btg'; -} \ No newline at end of file +} diff --git a/src/Network/Networks/BitcoinTestnet.php b/src/Network/Networks/BitcoinTestnet.php index 206eb18..aee67fd 100644 --- a/src/Network/Networks/BitcoinTestnet.php +++ b/src/Network/Networks/BitcoinTestnet.php @@ -24,4 +24,4 @@ class BitcoinTestnet extends Bitcoin * @var string */ protected $prefixBech32 = 'tb'; -} \ No newline at end of file +} diff --git a/src/Network/Networks/Dash.php b/src/Network/Networks/Dash.php index 13071f5..c93b045 100644 --- a/src/Network/Networks/Dash.php +++ b/src/Network/Networks/Dash.php @@ -24,4 +24,4 @@ class Dash extends Bitcoin * @var null */ protected $prefixBech32 = null; -} \ No newline at end of file +} diff --git a/src/Network/Networks/DashTestnet.php b/src/Network/Networks/DashTestnet.php index 2d1a88f..7388931 100644 --- a/src/Network/Networks/DashTestnet.php +++ b/src/Network/Networks/DashTestnet.php @@ -19,4 +19,4 @@ class DashTestnet extends Dash * @var string */ protected $prefixP2sh = "\x13"; -} \ No newline at end of file +} diff --git a/src/Network/Networks/Dogecoin.php b/src/Network/Networks/Dogecoin.php index c126c10..7704e66 100644 --- a/src/Network/Networks/Dogecoin.php +++ b/src/Network/Networks/Dogecoin.php @@ -24,4 +24,4 @@ class Dogecoin extends Bitcoin * @var null */ protected $prefixBech32 = null; -} \ No newline at end of file +} diff --git a/src/Network/Networks/DogecoinTestnet.php b/src/Network/Networks/DogecoinTestnet.php index e7cd0d8..da2ebbd 100644 --- a/src/Network/Networks/DogecoinTestnet.php +++ b/src/Network/Networks/DogecoinTestnet.php @@ -24,4 +24,4 @@ class DogecoinTestnet extends Dogecoin * @var null */ protected $prefixBech32 = null; -} \ No newline at end of file +} diff --git a/src/Network/Networks/Litecoin.php b/src/Network/Networks/Litecoin.php index 4c11212..a7e01d7 100644 --- a/src/Network/Networks/Litecoin.php +++ b/src/Network/Networks/Litecoin.php @@ -24,4 +24,4 @@ class Litecoin extends Bitcoin * @var string */ protected $prefixBech32 = 'ltc'; -} \ No newline at end of file +} diff --git a/src/Network/Networks/LitecoinTestnet.php b/src/Network/Networks/LitecoinTestnet.php index 080516b..bc9d6f8 100644 --- a/src/Network/Networks/LitecoinTestnet.php +++ b/src/Network/Networks/LitecoinTestnet.php @@ -24,4 +24,4 @@ class LitecoinTestnet extends Litecoin * @var string */ protected $prefixBech32 = 'tltc'; -} \ No newline at end of file +} diff --git a/src/Network/Networks/Viacoin.php b/src/Network/Networks/Viacoin.php index 8861fa5..15e699e 100644 --- a/src/Network/Networks/Viacoin.php +++ b/src/Network/Networks/Viacoin.php @@ -24,4 +24,4 @@ class Viacoin extends Bitcoin * @var string */ protected $prefixBech32 = 'via'; -} \ No newline at end of file +} diff --git a/src/Network/Networks/ViacoinTestnet.php b/src/Network/Networks/ViacoinTestnet.php index 5a83d02..9aa687d 100644 --- a/src/Network/Networks/ViacoinTestnet.php +++ b/src/Network/Networks/ViacoinTestnet.php @@ -24,4 +24,4 @@ class ViacoinTestnet extends Viacoin * @var string */ protected $prefixBech32 = 'tvia'; -} \ No newline at end of file +} diff --git a/src/Network/Networks/Zcash.php b/src/Network/Networks/Zcash.php index 4ccefb2..bfc6464 100644 --- a/src/Network/Networks/Zcash.php +++ b/src/Network/Networks/Zcash.php @@ -24,4 +24,4 @@ class Zcash extends Bitcoin * @var null */ protected $prefixBech32 = null; -} \ No newline at end of file +} diff --git a/src/Output/AbstractOutput.php b/src/Output/AbstractOutput.php index 8a3f01c..972a459 100644 --- a/src/Output/AbstractOutput.php +++ b/src/Output/AbstractOutput.php @@ -53,7 +53,7 @@ protected function network(NetworkInterface $network = null): NetworkInterface * @return OutputInterface * @throws Exception */ - static public function fromHex(string $hex): OutputInterface + public static function fromHex(string $hex): OutputInterface { $script = hex2bin($hex); @@ -63,4 +63,4 @@ static public function fromHex(string $hex): OutputInterface return static::fromScript($script); } -} \ No newline at end of file +} diff --git a/src/Output/Op.php b/src/Output/Op.php index f5cdf1f..28a1f16 100644 --- a/src/Output/Op.php +++ b/src/Output/Op.php @@ -16,4 +16,4 @@ interface Op const HASH160 = "\xa9"; const CHECKSIG = "\xac"; const CHECKMULTISIG = "\xae"; -} \ No newline at end of file +} diff --git a/src/Output/OutputFactory.php b/src/Output/OutputFactory.php index 1c058db..f0af81b 100644 --- a/src/Output/OutputFactory.php +++ b/src/Output/OutputFactory.php @@ -24,7 +24,7 @@ class OutputFactory * @return OutputInterface * @throws \AndKom\Bitcoin\Address\Exception */ - static public function p2pk(string $pubKey): OutputInterface + public static function p2pk(string $pubKey): OutputInterface { return new P2pk($pubKey); } @@ -34,7 +34,7 @@ static public function p2pk(string $pubKey): OutputInterface * @return OutputInterface * @throws \AndKom\Bitcoin\Address\Exception */ - static public function p2pkh(string $pubKeyHash): OutputInterface + public static function p2pkh(string $pubKeyHash): OutputInterface { return new P2pkh($pubKeyHash); } @@ -45,7 +45,7 @@ static public function p2pkh(string $pubKeyHash): OutputInterface * @return OutputInterface * @throws \AndKom\Bitcoin\Address\Exception */ - static public function p2ms(int $m, array $pubKeys): OutputInterface + public static function p2ms(int $m, array $pubKeys): OutputInterface { return new P2ms($m, $pubKeys); } @@ -55,7 +55,7 @@ static public function p2ms(int $m, array $pubKeys): OutputInterface * @return OutputInterface * @throws \AndKom\Bitcoin\Address\Exception */ - static public function p2sh(OutputInterface $output): OutputInterface + public static function p2sh(OutputInterface $output): OutputInterface { return new P2sh($output); } @@ -65,7 +65,7 @@ static public function p2sh(OutputInterface $output): OutputInterface * @return OutputInterface * @throws \AndKom\Bitcoin\Address\Exception */ - static public function p2wpkh(string $pubKeyHash): OutputInterface + public static function p2wpkh(string $pubKeyHash): OutputInterface { return new P2wpkh($pubKeyHash); } @@ -75,7 +75,7 @@ static public function p2wpkh(string $pubKeyHash): OutputInterface * @return OutputInterface * @throws \AndKom\Bitcoin\Address\Exception */ - static public function p2wsh(OutputInterface $output): OutputInterface + public static function p2wsh(OutputInterface $output): OutputInterface { return new P2wsh($output); } @@ -85,7 +85,7 @@ static public function p2wsh(OutputInterface $output): OutputInterface * @return OutputInterface * @throws Exception */ - static public function fromScript(string $script): OutputInterface + public static function fromScript(string $script): OutputInterface { $map = [ P2pk::COMPRESSED_SCRIPT_LEN => P2pk::class, @@ -114,8 +114,8 @@ static public function fromScript(string $script): OutputInterface * @return OutputInterface * @throws Exception */ - static public function fromHex(string $hex): OutputInterface + public static function fromHex(string $hex): OutputInterface { return static::fromScript(Utils::hex2bin($hex)); } -} \ No newline at end of file +} diff --git a/src/Output/OutputInterface.php b/src/Output/OutputInterface.php index da95b52..59a62aa 100644 --- a/src/Output/OutputInterface.php +++ b/src/Output/OutputInterface.php @@ -49,17 +49,17 @@ public function address(NetworkInterface $network = null): string; * @throws Exception * @return void */ - static public function validateScript(string $script); + public static function validateScript(string $script); /** * @param string $script * @return OutputInterface */ - static public function fromScript(string $script): OutputInterface; + public static function fromScript(string $script): OutputInterface; /** * @param string $hex * @return OutputInterface */ - static public function fromHex(string $hex): OutputInterface; -} \ No newline at end of file + public static function fromHex(string $hex): OutputInterface; +} diff --git a/src/Output/Outputs/P2ms.php b/src/Output/Outputs/P2ms.php index 91d8625..0ed8374 100644 --- a/src/Output/Outputs/P2ms.php +++ b/src/Output/Outputs/P2ms.php @@ -103,7 +103,7 @@ public function address(NetworkInterface $network = null): string * @param string $script * @throws Exception */ - static public function validateScript(string $script) + public static function validateScript(string $script) { $scriptLen = strlen($script); @@ -121,8 +121,10 @@ static public function validateScript(string $script) for ($i = 1, $c = 0; $i < $scriptLen - 2; $c++) { $pubKeyLen = ord($script[$i]); - if (Validate::COMPRESSED_PUBKEY_LEN != $pubKeyLen && - Validate::UNCOMPRESSED_PUBKEY_LEN != $pubKeyLen) { + if ( + Validate::COMPRESSED_PUBKEY_LEN != $pubKeyLen && + Validate::UNCOMPRESSED_PUBKEY_LEN != $pubKeyLen + ) { throw new Exception('Invalid pubkey length.'); } @@ -139,7 +141,7 @@ static public function validateScript(string $script) * @return OutputInterface * @throws Exception */ - static public function fromScript(string $script): OutputInterface + public static function fromScript(string $script): OutputInterface { static::validateScript($script); @@ -155,4 +157,4 @@ static public function fromScript(string $script): OutputInterface return new static($m, $pubKeys); } -} \ No newline at end of file +} diff --git a/src/Output/Outputs/P2pk.php b/src/Output/Outputs/P2pk.php index fe890f0..8f04de0 100644 --- a/src/Output/Outputs/P2pk.php +++ b/src/Output/Outputs/P2pk.php @@ -66,12 +66,14 @@ public function address(NetworkInterface $network = null): string * @param string $script * @throws Exception */ - static public function validateScript(string $script) + public static function validateScript(string $script) { $scriptLen = strlen($script); - if (static::COMPRESSED_SCRIPT_LEN != $scriptLen && - static::UNCOMPRESSED_SCRIPT_LEN != $scriptLen) { + if ( + static::COMPRESSED_SCRIPT_LEN != $scriptLen && + static::UNCOMPRESSED_SCRIPT_LEN != $scriptLen + ) { throw new Exception('Invalid P2PK script length.'); } @@ -85,7 +87,7 @@ static public function validateScript(string $script) * @return OutputInterface * @throws Exception */ - static public function fromScript(string $script): OutputInterface + public static function fromScript(string $script): OutputInterface { static::validateScript($script); @@ -93,4 +95,4 @@ static public function fromScript(string $script): OutputInterface return new static($pubKey); } -} \ No newline at end of file +} diff --git a/src/Output/Outputs/P2pkh.php b/src/Output/Outputs/P2pkh.php index ee6e4c9..81f0e25 100644 --- a/src/Output/Outputs/P2pkh.php +++ b/src/Output/Outputs/P2pkh.php @@ -64,13 +64,14 @@ public function address(NetworkInterface $network = null): string * @param string $script * @throws Exception */ - static public function validateScript(string $script) + public static function validateScript(string $script) { if (static::SCRIPT_LEN != strlen($script)) { throw new Exception('Invalid P2PKH script length.'); } - if (Op::DUP != $script[0] || + if ( + Op::DUP != $script[0] || Op::HASH160 != $script[1] || "\x14" != $script[2] || Op::EQUALVERIFY != $script[-2] || @@ -85,7 +86,7 @@ static public function validateScript(string $script) * @return OutputInterface * @throws Exception */ - static public function fromScript(string $script): OutputInterface + public static function fromScript(string $script): OutputInterface { static::validateScript($script); @@ -93,4 +94,4 @@ static public function fromScript(string $script): OutputInterface return new static($pubKeyHash); } -} \ No newline at end of file +} diff --git a/src/Output/Outputs/P2sh.php b/src/Output/Outputs/P2sh.php index 2fe8dd2..d7ee767 100644 --- a/src/Output/Outputs/P2sh.php +++ b/src/Output/Outputs/P2sh.php @@ -69,15 +69,17 @@ public function address(NetworkInterface $network = null): string * @param string $script * @throws Exception */ - static public function validateScript(string $script) + public static function validateScript(string $script) { if (static::SCRIPT_LEN != strlen($script)) { throw new Exception('Invalid P2SH script length.'); } - if (Op::HASH160 != $script[0] || + if ( + Op::HASH160 != $script[0] || "\x14" != $script[1] || - Op::EQUAL != $script[-1]) { + Op::EQUAL != $script[-1] + ) { throw new Exception('Invalid P2SH script format.'); } } @@ -87,7 +89,7 @@ static public function validateScript(string $script) * @return OutputInterface * @throws Exception */ - static public function fromScript(string $script): OutputInterface + public static function fromScript(string $script): OutputInterface { static::validateScript($script); @@ -95,4 +97,4 @@ static public function fromScript(string $script): OutputInterface return new static($scriptHash); } -} \ No newline at end of file +} diff --git a/src/Output/Outputs/P2wpkh.php b/src/Output/Outputs/P2wpkh.php index 622d771..c709963 100644 --- a/src/Output/Outputs/P2wpkh.php +++ b/src/Output/Outputs/P2wpkh.php @@ -63,14 +63,16 @@ public function address(NetworkInterface $network = null): string * @param string $script * @throws Exception */ - static public function validateScript(string $script) + public static function validateScript(string $script) { if (static::SCRIPT_LEN != strlen($script)) { throw new Exception('Invalid P2WPKH script length.'); } - if ("\x00" != $script[0] || - "\x14" != $script[1]) { + if ( + "\x00" != $script[0] || + "\x14" != $script[1] + ) { throw new Exception('Invalid P2WPKH script format.'); } } @@ -80,7 +82,7 @@ static public function validateScript(string $script) * @return OutputInterface * @throws Exception */ - static public function fromScript(string $script): OutputInterface + public static function fromScript(string $script): OutputInterface { static::validateScript($script); @@ -88,4 +90,4 @@ static public function fromScript(string $script): OutputInterface return new static($pubKeyHash); } -} \ No newline at end of file +} diff --git a/src/Output/Outputs/P2wsh.php b/src/Output/Outputs/P2wsh.php index 3df21ba..dbe0931 100644 --- a/src/Output/Outputs/P2wsh.php +++ b/src/Output/Outputs/P2wsh.php @@ -67,14 +67,16 @@ public function address(NetworkInterface $network = null): string * @param string $script * @throws Exception */ - static public function validateScript(string $script) + public static function validateScript(string $script) { if (static::SCRIPT_LEN != strlen($script)) { throw new Exception('Invalid P2WSH script length.'); } - if ("\x00" != $script[0] || - "\x20" != $script[1]) { + if ( + "\x00" != $script[0] || + "\x20" != $script[1] + ) { throw new Exception('Invalid P2WSH script format.'); } } @@ -84,7 +86,7 @@ static public function validateScript(string $script) * @return OutputInterface * @throws Exception */ - static public function fromScript(string $script): OutputInterface + public static function fromScript(string $script): OutputInterface { static::validateScript($script); @@ -92,4 +94,4 @@ static public function fromScript(string $script): OutputInterface return new static($witnessHash); } -} \ No newline at end of file +} diff --git a/src/Utils.php b/src/Utils.php index 0232166..2f37351 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -16,7 +16,7 @@ class Utils * @param string $data * @return string */ - static public function sha256(string $data): string + public static function sha256(string $data): string { return hash('sha256', $data, true); } @@ -25,7 +25,7 @@ static public function sha256(string $data): string * @param string $data * @return string */ - static public function hash256(string $data): string + public static function hash256(string $data): string { return static::sha256(static::sha256($data)); } @@ -34,7 +34,7 @@ static public function hash256(string $data): string * @param string $data * @return string */ - static public function hash160(string $data): string + public static function hash160(string $data): string { return hash('ripemd160', static::sha256($data), true); } @@ -44,7 +44,7 @@ static public function hash160(string $data): string * @return string * @throws Exception */ - static public function hex2bin(string $hex): string + public static function hex2bin(string $hex): string { $bin = @hex2bin($hex); @@ -61,7 +61,7 @@ static public function hex2bin(string $hex): string * @return string * @throws \Exception */ - static public function base58encode(string $hash, string $prefix = "\x00"): string + public static function base58encode(string $hash, string $prefix = "\x00"): string { $payload = $prefix . Validate::pubKeyHash($hash); $checksum = substr(static::hash256($payload), 0, 4); @@ -75,7 +75,7 @@ static public function base58encode(string $hash, string $prefix = "\x00"): stri * @return array * @throws \Exception */ - static public function base58decode(string $base58): array + public static function base58decode(string $base58): array { $address = (new Base58())->decode($base58); $addressLen = strlen($address); @@ -97,4 +97,4 @@ static public function base58decode(string $base58): array return [$hash, $prefix]; } -} \ No newline at end of file +} diff --git a/src/Validate.php b/src/Validate.php index 3574d35..1bf9ae6 100644 --- a/src/Validate.php +++ b/src/Validate.php @@ -23,7 +23,7 @@ class Validate * @return string * @throws Exception */ - static public function pubKey(string $pubKey): string + public static function pubKey(string $pubKey): string { $len = strlen($pubKey); @@ -48,7 +48,7 @@ static public function pubKey(string $pubKey): string * @return string * @throws Exception */ - static public function pubKeyHash(string $pubKeyHash): string + public static function pubKeyHash(string $pubKeyHash): string { if (static::PUBKEY_HASH_LEN != strlen($pubKeyHash)) { throw new Exception(sprintf('Invalid pubkey hash: %s.', bin2hex($pubKeyHash))); @@ -62,7 +62,7 @@ static public function pubKeyHash(string $pubKeyHash): string * @return string * @throws Exception */ - static public function scriptHash(string $scriptHash): string + public static function scriptHash(string $scriptHash): string { if (static::SCRIPT_HASH_LEN != strlen($scriptHash)) { throw new Exception(sprintf('Invalid script hash: %s.', bin2hex($scriptHash))); @@ -76,7 +76,7 @@ static public function scriptHash(string $scriptHash): string * @return string * @throws Exception */ - static public function witnessHash(string $witnessHash): string + public static function witnessHash(string $witnessHash): string { if (static::WITNESS_HASH_LEN != strlen($witnessHash)) { throw new Exception(sprintf('Invalid witness hash: %s.', bin2hex($witnessHash))); @@ -84,4 +84,4 @@ static public function witnessHash(string $witnessHash): string return $witnessHash; } -} \ No newline at end of file +} diff --git a/tests/BitcoinCashTest.php b/tests/BitcoinCashTest.php index 2ececb6..f02ef7a 100644 --- a/tests/BitcoinCashTest.php +++ b/tests/BitcoinCashTest.php @@ -24,4 +24,4 @@ public function testDecodeP2SH() { $this->assertInstanceOf(P2sh::class, NetworkFactory::bitcoinCash()->decodeAddress('bitcoincash:pzp7awma0x4p6wyw8v9vvkuc43vqcndqrg9umkmd8g')); } -} \ No newline at end of file +} diff --git a/tests/BitcoinTest.php b/tests/BitcoinTest.php index 7d3b1bf..50c7ff1 100644 --- a/tests/BitcoinTest.php +++ b/tests/BitcoinTest.php @@ -36,4 +36,4 @@ public function testDecodeP2WSH() { $this->assertInstanceOf(P2wsh::class, NetworkFactory::bitcoin()->decodeAddress('bc1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8s4plngs')); } -} \ No newline at end of file +} diff --git a/tests/OutputFactoryTest.php b/tests/OutputFactoryTest.php index ee9c206..8618e78 100644 --- a/tests/OutputFactoryTest.php +++ b/tests/OutputFactoryTest.php @@ -72,4 +72,4 @@ public function testFromHexP2WSH() $output = OutputFactory::fromHex('002028205333db922f66e8a941b4a32d66de5cea03d9cda46e3e6658935272b9b24f'); $this->assertInstanceOf(P2wsh::class, $output); } -} \ No newline at end of file +} diff --git a/tests/P2msTest.php b/tests/P2msTest.php index 9c9c5cb..f45f3ea 100644 --- a/tests/P2msTest.php +++ b/tests/P2msTest.php @@ -48,4 +48,4 @@ public function testFromScript() $output = $this->getOutput(); $this->assertEquals($output->script(), P2ms::fromScript($output->script())->script()); } -} \ No newline at end of file +} diff --git a/tests/P2pkTest.php b/tests/P2pkTest.php index 4a8a2e7..a89fe42 100644 --- a/tests/P2pkTest.php +++ b/tests/P2pkTest.php @@ -47,4 +47,4 @@ public function testFromScript() $output = $this->getOutput(); $this->assertEquals($output->script(), P2pk::fromScript($output->script())->script()); } -} \ No newline at end of file +} diff --git a/tests/P2pkhTest.php b/tests/P2pkhTest.php index 8df2144..2943ce3 100644 --- a/tests/P2pkhTest.php +++ b/tests/P2pkhTest.php @@ -153,4 +153,4 @@ public function testFromScript() $output = $this->getOutput(); $this->assertEquals($output->script(), P2pkh::fromScript($output->script())->script()); } -} \ No newline at end of file +} diff --git a/tests/P2shTest.php b/tests/P2shTest.php index 0ae623b..804c181 100644 --- a/tests/P2shTest.php +++ b/tests/P2shTest.php @@ -155,4 +155,4 @@ public function testFromScript() $output = $this->getOutput(); $this->assertEquals($output->script(), P2sh::fromScript($output->script())->script()); } -} \ No newline at end of file +} diff --git a/tests/P2wpkhTest.php b/tests/P2wpkhTest.php index 35230df..2b291a8 100644 --- a/tests/P2wpkhTest.php +++ b/tests/P2wpkhTest.php @@ -97,4 +97,4 @@ public function testFromScript() $output = $this->getOutput(); $this->assertEquals($output->script(), P2wpkh::fromScript($output->script())->script()); } -} \ No newline at end of file +} diff --git a/tests/P2wshTest.php b/tests/P2wshTest.php index 1eacb6f..cc8d646 100644 --- a/tests/P2wshTest.php +++ b/tests/P2wshTest.php @@ -99,4 +99,4 @@ public function testFromScript() $output = $this->getOutput(); $this->assertEquals($output->script(), P2wsh::fromScript($output->script())->script()); } -} \ No newline at end of file +} From 67cf3bc0cee727166f22c1073c1a846e4bfdc2a5 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 16 May 2023 19:45:02 -0700 Subject: [PATCH 04/32] `const` -> `public const` --- src/Output/Op.php | 12 ++++++------ src/Output/Outputs/P2ms.php | 2 +- src/Output/Outputs/P2pk.php | 4 ++-- src/Output/Outputs/P2pkh.php | 2 +- src/Output/Outputs/P2sh.php | 2 +- src/Output/Outputs/P2wpkh.php | 2 +- src/Output/Outputs/P2wsh.php | 2 +- src/Validate.php | 14 +++++++------- 8 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Output/Op.php b/src/Output/Op.php index 28a1f16..f67540d 100644 --- a/src/Output/Op.php +++ b/src/Output/Op.php @@ -10,10 +10,10 @@ */ interface Op { - const DUP = "\x76"; - const EQUAL = "\x87"; - const EQUALVERIFY = "\x88"; - const HASH160 = "\xa9"; - const CHECKSIG = "\xac"; - const CHECKMULTISIG = "\xae"; + public const DUP = "\x76"; + public const EQUAL = "\x87"; + public const EQUALVERIFY = "\x88"; + public const HASH160 = "\xa9"; + public const CHECKSIG = "\xac"; + public const CHECKMULTISIG = "\xae"; } diff --git a/src/Output/Outputs/P2ms.php b/src/Output/Outputs/P2ms.php index 0ed8374..f2f779d 100644 --- a/src/Output/Outputs/P2ms.php +++ b/src/Output/Outputs/P2ms.php @@ -18,7 +18,7 @@ */ class P2ms extends AbstractOutput { - const MIN_SCRIPT_LEN = 37; + public const MIN_SCRIPT_LEN = 37; /** * Number of signatures diff --git a/src/Output/Outputs/P2pk.php b/src/Output/Outputs/P2pk.php index 8f04de0..0da2a21 100644 --- a/src/Output/Outputs/P2pk.php +++ b/src/Output/Outputs/P2pk.php @@ -19,8 +19,8 @@ */ class P2pk extends AbstractOutput { - const COMPRESSED_SCRIPT_LEN = 35; - const UNCOMPRESSED_SCRIPT_LEN = 67; + public const COMPRESSED_SCRIPT_LEN = 35; + public const UNCOMPRESSED_SCRIPT_LEN = 67; /** * @var string diff --git a/src/Output/Outputs/P2pkh.php b/src/Output/Outputs/P2pkh.php index 81f0e25..1d8c392 100644 --- a/src/Output/Outputs/P2pkh.php +++ b/src/Output/Outputs/P2pkh.php @@ -18,7 +18,7 @@ */ class P2pkh extends AbstractOutput { - const SCRIPT_LEN = 25; + public const SCRIPT_LEN = 25; /** * @var string diff --git a/src/Output/Outputs/P2sh.php b/src/Output/Outputs/P2sh.php index d7ee767..638cc22 100644 --- a/src/Output/Outputs/P2sh.php +++ b/src/Output/Outputs/P2sh.php @@ -18,7 +18,7 @@ */ class P2sh extends AbstractOutput { - const SCRIPT_LEN = 23; + public const SCRIPT_LEN = 23; /** * @var string diff --git a/src/Output/Outputs/P2wpkh.php b/src/Output/Outputs/P2wpkh.php index c709963..e8a2eb0 100644 --- a/src/Output/Outputs/P2wpkh.php +++ b/src/Output/Outputs/P2wpkh.php @@ -17,7 +17,7 @@ */ class P2wpkh extends AbstractOutput { - const SCRIPT_LEN = 22; + public const SCRIPT_LEN = 22; /** * @var string diff --git a/src/Output/Outputs/P2wsh.php b/src/Output/Outputs/P2wsh.php index dbe0931..56a3517 100644 --- a/src/Output/Outputs/P2wsh.php +++ b/src/Output/Outputs/P2wsh.php @@ -17,7 +17,7 @@ */ class P2wsh extends AbstractOutput { - const SCRIPT_LEN = 34; + public const SCRIPT_LEN = 34; /** * @var string diff --git a/src/Validate.php b/src/Validate.php index 1bf9ae6..4cbdb5d 100644 --- a/src/Validate.php +++ b/src/Validate.php @@ -10,13 +10,13 @@ */ class Validate { - const COMPRESSED_PUBKEY_LEN = 33; - const COMPRESSED_PUBKEY_PREFIXES = ["\x02", "\x03"]; - const UNCOMPRESSED_PUBKEY_LEN = 65; - const UNCOMRPESSED_PUBKEY_PREFIX = "\04"; - const PUBKEY_HASH_LEN = 20; - const SCRIPT_HASH_LEN = 20; - const WITNESS_HASH_LEN = 32; + public const COMPRESSED_PUBKEY_LEN = 33; + public const COMPRESSED_PUBKEY_PREFIXES = ["\x02", "\x03"]; + public const UNCOMPRESSED_PUBKEY_LEN = 65; + public const UNCOMRPESSED_PUBKEY_PREFIX = "\04"; + public const PUBKEY_HASH_LEN = 20; + public const SCRIPT_HASH_LEN = 20; + public const WITNESS_HASH_LEN = 32; /** * @param string $pubKey From f8a40a4ec0cbfc68ad7112160b5581198ec6a2f4 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 16 May 2023 19:52:05 -0700 Subject: [PATCH 05/32] phpcs line-length --- tests/BitcoinCashTest.php | 10 ++++-- tests/BitcoinTest.php | 20 ++++++++--- tests/P2msTest.php | 10 ++++-- tests/P2pkTest.php | 10 ++++-- tests/P2pkhTest.php | 75 +++++++++++++++++++++++++++++++-------- tests/P2shTest.php | 70 ++++++++++++++++++++++++++++-------- tests/P2wpkhTest.php | 40 ++++++++++++++++----- tests/P2wshTest.php | 40 ++++++++++++++++----- 8 files changed, 220 insertions(+), 55 deletions(-) diff --git a/tests/BitcoinCashTest.php b/tests/BitcoinCashTest.php index f02ef7a..be7f3c6 100644 --- a/tests/BitcoinCashTest.php +++ b/tests/BitcoinCashTest.php @@ -17,11 +17,17 @@ class BitcoinCashTest extends TestCase { public function testDecodeP2PKH() { - $this->assertInstanceOf(P2pkh::class, NetworkFactory::bitcoinCash()->decodeAddress('bitcoincash:qp63uahgrxged4z5jswyt5dn5v3lzsem6cy4spdc2h')); + $this->assertInstanceOf( + P2pkh::class, + NetworkFactory::bitcoinCash()->decodeAddress('bitcoincash:qp63uahgrxged4z5jswyt5dn5v3lzsem6cy4spdc2h') + ); } public function testDecodeP2SH() { - $this->assertInstanceOf(P2sh::class, NetworkFactory::bitcoinCash()->decodeAddress('bitcoincash:pzp7awma0x4p6wyw8v9vvkuc43vqcndqrg9umkmd8g')); + $this->assertInstanceOf( + P2sh::class, + NetworkFactory::bitcoinCash()->decodeAddress('bitcoincash:pzp7awma0x4p6wyw8v9vvkuc43vqcndqrg9umkmd8g') + ); } } diff --git a/tests/BitcoinTest.php b/tests/BitcoinTest.php index 50c7ff1..21fa5f8 100644 --- a/tests/BitcoinTest.php +++ b/tests/BitcoinTest.php @@ -19,21 +19,33 @@ class BitcoinTest extends TestCase { public function testDecodeP2PKH() { - $this->assertInstanceOf(P2pkh::class, NetworkFactory::bitcoin()->decodeAddress('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH')); + $this->assertInstanceOf( + P2pkh::class, + NetworkFactory::bitcoin()->decodeAddress('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH') + ); } public function testDecodeP2SH() { - $this->assertInstanceOf(P2sh::class, NetworkFactory::bitcoin()->decodeAddress('3DicS6C8JZm59RsrgXr56iVHzYdQngiehV')); + $this->assertInstanceOf( + P2sh::class, + NetworkFactory::bitcoin()->decodeAddress('3DicS6C8JZm59RsrgXr56iVHzYdQngiehV') + ); } public function testDecodeP2WPKH() { - $this->assertInstanceOf(P2wpkh::class, NetworkFactory::bitcoin()->decodeAddress('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4')); + $this->assertInstanceOf( + P2wpkh::class, + NetworkFactory::bitcoin()->decodeAddress('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4') + ); } public function testDecodeP2WSH() { - $this->assertInstanceOf(P2wsh::class, NetworkFactory::bitcoin()->decodeAddress('bc1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8s4plngs')); + $this->assertInstanceOf( + P2wsh::class, + NetworkFactory::bitcoin()->decodeAddress('bc1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8s4plngs') + ); } } diff --git a/tests/P2msTest.php b/tests/P2msTest.php index f45f3ea..8f261a2 100644 --- a/tests/P2msTest.php +++ b/tests/P2msTest.php @@ -29,7 +29,10 @@ protected function getOutput(): OutputInterface */ public function testHex() { - $this->assertEquals('51210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f8179851ae', $this->getOutput()->hex()); + $this->assertEquals( + '51210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f8179851ae', + $this->getOutput()->hex() + ); } /** @@ -37,7 +40,10 @@ public function testHex() */ public function testAsm() { - $this->assertEquals('1 PUSHDATA(33)[0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798] 1 CHECKMULTISIG', $this->getOutput()->asm()); + $this->assertEquals( + '1 PUSHDATA(33)[0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798] 1 CHECKMULTISIG', + $this->getOutput()->asm() + ); } /** diff --git a/tests/P2pkTest.php b/tests/P2pkTest.php index a89fe42..27dfa5a 100644 --- a/tests/P2pkTest.php +++ b/tests/P2pkTest.php @@ -28,7 +28,10 @@ protected function getOutput(): OutputInterface */ public function testHex() { - $this->assertEquals('210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ac', $this->getOutput()->hex()); + $this->assertEquals( + '210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ac', + $this->getOutput()->hex() + ); } /** @@ -36,7 +39,10 @@ public function testHex() */ public function testAsm() { - $this->assertEquals('PUSHDATA(33)[0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798] CHECKSIG', $this->getOutput()->asm()); + $this->assertEquals( + 'PUSHDATA(33)[0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798] CHECKSIG', + $this->getOutput()->asm() + ); } /** diff --git a/tests/P2pkhTest.php b/tests/P2pkhTest.php index 2943ce3..3d65429 100644 --- a/tests/P2pkhTest.php +++ b/tests/P2pkhTest.php @@ -30,7 +30,10 @@ protected function getOutput(): OutputInterface */ public function testHex() { - $this->assertEquals('76a914751e76e8199196d454941c45d1b3a323f1433bd688ac', $this->getOutput()->hex()); + $this->assertEquals( + '76a914751e76e8199196d454941c45d1b3a323f1433bd688ac', + $this->getOutput()->hex() + ); } /** @@ -38,7 +41,10 @@ public function testHex() */ public function testAsm() { - $this->assertEquals('DUP HASH160 PUSHDATA(20)[751e76e8199196d454941c45d1b3a323f1433bd6] EQUALVERIFY CHECKSIG', $this->getOutput()->asm()); + $this->assertEquals( + 'DUP HASH160 PUSHDATA(20)[751e76e8199196d454941c45d1b3a323f1433bd6] EQUALVERIFY CHECKSIG', + $this->getOutput()->asm() + ); } /** @@ -46,7 +52,10 @@ public function testAsm() */ public function testAddressBitcoin() { - $this->assertEquals('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH', $this->getOutput()->address()); + $this->assertEquals( + '1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH', + $this->getOutput()->address() + ); } /** @@ -54,7 +63,10 @@ public function testAddressBitcoin() */ public function testAddressBitcoinTestnet() { - $this->assertEquals('mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r', $this->getOutput()->address(NetworkFactory::bitcoinTestnet())); + $this->assertEquals( + 'mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r', + $this->getOutput()->address(NetworkFactory::bitcoinTestnet()) + ); } /** @@ -62,7 +74,10 @@ public function testAddressBitcoinTestnet() */ public function testAddressBitcoinCash() { - $this->assertEquals('bitcoincash:qp63uahgrxged4z5jswyt5dn5v3lzsem6cy4spdc2h', $this->getOutput()->address(NetworkFactory::bitcoinCash())); + $this->assertEquals( + 'bitcoincash:qp63uahgrxged4z5jswyt5dn5v3lzsem6cy4spdc2h', + $this->getOutput()->address(NetworkFactory::bitcoinCash()) + ); } /** @@ -70,7 +85,10 @@ public function testAddressBitcoinCash() */ public function testAddressBitcoinGold() { - $this->assertEquals('GUXByHDZLvU4DnVH9imSFckt3HEQ5cFgE5', $this->getOutput()->address(NetworkFactory::bitcoinGold())); + $this->assertEquals( + 'GUXByHDZLvU4DnVH9imSFckt3HEQ5cFgE5', + $this->getOutput()->address(NetworkFactory::bitcoinGold()) + ); } /** @@ -78,7 +96,10 @@ public function testAddressBitcoinGold() */ public function testAddressLitecoin() { - $this->assertEquals('LVuDpNCSSj6pQ7t9Pv6d6sUkLKoqDEVUnJ', $this->getOutput()->address(NetworkFactory::litecoin())); + $this->assertEquals( + 'LVuDpNCSSj6pQ7t9Pv6d6sUkLKoqDEVUnJ', + $this->getOutput()->address(NetworkFactory::litecoin()) + ); } /** @@ -86,7 +107,10 @@ public function testAddressLitecoin() */ public function testAddressLitecoinTestnet() { - $this->assertEquals('mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r', $this->getOutput()->address(NetworkFactory::litecoinTestnet())); + $this->assertEquals( + 'mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r', + $this->getOutput()->address(NetworkFactory::litecoinTestnet()) + ); } /** @@ -94,7 +118,10 @@ public function testAddressLitecoinTestnet() */ public function testAddressDogecoin() { - $this->assertEquals('DFpN6QqFfUm3gKNaxN6tNcab1FArL9cZLE', $this->getOutput()->address(NetworkFactory::dogecoin())); + $this->assertEquals( + 'DFpN6QqFfUm3gKNaxN6tNcab1FArL9cZLE', + $this->getOutput()->address(NetworkFactory::dogecoin()) + ); } /** @@ -102,7 +129,10 @@ public function testAddressDogecoin() */ public function testAddressDogecoinTestnet() { - $this->assertEquals('nesRpRaAbTDmZHwmzBkLd2AtF7Z9L9z5S2', $this->getOutput()->address(NetworkFactory::dogecoinTestnet())); + $this->assertEquals( + 'nesRpRaAbTDmZHwmzBkLd2AtF7Z9L9z5S2', + $this->getOutput()->address(NetworkFactory::dogecoinTestnet()) + ); } /** @@ -110,7 +140,10 @@ public function testAddressDogecoinTestnet() */ public function testAddressViacoin() { - $this->assertEquals('Vkg6Ts44mskyD668xZkxFkjqovjXX9yUzZ', $this->getOutput()->address(NetworkFactory::viacoin())); + $this->assertEquals( + 'Vkg6Ts44mskyD668xZkxFkjqovjXX9yUzZ', + $this->getOutput()->address(NetworkFactory::viacoin()) + ); } /** @@ -118,7 +151,10 @@ public function testAddressViacoin() */ public function testAddressViacoinTestnet() { - $this->assertEquals('tHbsbwkCXyi31MtzL4QoQmyu4BAMJz8hS6', $this->getOutput()->address(NetworkFactory::viacoinTestnet())); + $this->assertEquals( + 'tHbsbwkCXyi31MtzL4QoQmyu4BAMJz8hS6', + $this->getOutput()->address(NetworkFactory::viacoinTestnet()) + ); } /** @@ -126,7 +162,10 @@ public function testAddressViacoinTestnet() */ public function testAddressDash() { - $this->assertEquals('XmN7PQYWKn5MJFna5fRYgP6mxT2F7xpekE', $this->getOutput()->address(NetworkFactory::dash())); + $this->assertEquals( + 'XmN7PQYWKn5MJFna5fRYgP6mxT2F7xpekE', + $this->getOutput()->address(NetworkFactory::dash()) + ); } /** @@ -134,7 +173,10 @@ public function testAddressDash() */ public function testAddressDashTestnet() { - $this->assertEquals('y7f7RFKf49GYpZa2d6QdEHFLcEFfuoNcer', $this->getOutput()->address(NetworkFactory::dashTestnet())); + $this->assertEquals( + 'y7f7RFKf49GYpZa2d6QdEHFLcEFfuoNcer', + $this->getOutput()->address(NetworkFactory::dashTestnet()) + ); } /** @@ -142,7 +184,10 @@ public function testAddressDashTestnet() */ public function testAddressZcash() { - $this->assertEquals('t1UYsZVJkLPeMjxEtACvSxfWuNmddpWfxzs', $this->getOutput()->address(NetworkFactory::zcash())); + $this->assertEquals( + 't1UYsZVJkLPeMjxEtACvSxfWuNmddpWfxzs', + $this->getOutput()->address(NetworkFactory::zcash()) + ); } /** diff --git a/tests/P2shTest.php b/tests/P2shTest.php index 804c181..adbe49a 100644 --- a/tests/P2shTest.php +++ b/tests/P2shTest.php @@ -32,7 +32,10 @@ protected function getOutput(): OutputInterface */ public function testHex() { - $this->assertEquals('a91483eebb7d79aa1d388e3b0ac65b98ac580c4da01a87', $this->getOutput()->hex()); + $this->assertEquals( + 'a91483eebb7d79aa1d388e3b0ac65b98ac580c4da01a87', + $this->getOutput()->hex() + ); } /** @@ -40,7 +43,10 @@ public function testHex() */ public function testAsm() { - $this->assertEquals('HASH160 PUSHDATA(20)[83eebb7d79aa1d388e3b0ac65b98ac580c4da01a] EQUAL', $this->getOutput()->asm()); + $this->assertEquals( + 'HASH160 PUSHDATA(20)[83eebb7d79aa1d388e3b0ac65b98ac580c4da01a] EQUAL', + $this->getOutput()->asm() + ); } /** @@ -56,7 +62,10 @@ public function testAddressBitcoin() */ public function testAddressBitcoinTestnet() { - $this->assertEquals('2N5GpVq89v2GRMDWQMfTwifUZCtqaczC6Y7', $this->getOutput()->address(NetworkFactory::bitcoinTestnet())); + $this->assertEquals( + '2N5GpVq89v2GRMDWQMfTwifUZCtqaczC6Y7', + $this->getOutput()->address(NetworkFactory::bitcoinTestnet()) + ); } /** @@ -64,7 +73,10 @@ public function testAddressBitcoinTestnet() */ public function testAddressBitcoinCash() { - $this->assertEquals('bitcoincash:pzp7awma0x4p6wyw8v9vvkuc43vqcndqrg9umkmd8g', $this->getOutput()->address(NetworkFactory::bitcoinCash())); + $this->assertEquals( + 'bitcoincash:pzp7awma0x4p6wyw8v9vvkuc43vqcndqrg9umkmd8g', + $this->getOutput()->address(NetworkFactory::bitcoinCash()) + ); } /** @@ -72,7 +84,10 @@ public function testAddressBitcoinCash() */ public function testAddressBitcoinGold() { - $this->assertEquals('dRSt8KXceu9YfGjDmCAAKNdAVRXbFQWRxF', $this->getOutput()->address(NetworkFactory::bitcoinGold())); + $this->assertEquals( + 'dRSt8KXceu9YfGjDmCAAKNdAVRXbFQWRxF', + $this->getOutput()->address(NetworkFactory::bitcoinGold()) + ); } /** @@ -80,7 +95,10 @@ public function testAddressBitcoinGold() */ public function testAddressLitecoin() { - $this->assertEquals('MKvkjyc6FgcVww9knQqQvMjhKFDrpERUsa', $this->getOutput()->address(NetworkFactory::litecoin())); + $this->assertEquals( + 'MKvkjyc6FgcVww9knQqQvMjhKFDrpERUsa', + $this->getOutput()->address(NetworkFactory::litecoin()) + ); } /** @@ -88,7 +106,10 @@ public function testAddressLitecoin() */ public function testAddressLitecoinTestnet() { - $this->assertEquals('QYdacqzPw8KWVQGSymVxoMuzMHHQYBayi6', $this->getOutput()->address(NetworkFactory::litecoinTestnet())); + $this->assertEquals( + 'QYdacqzPw8KWVQGSymVxoMuzMHHQYBayi6', + $this->getOutput()->address(NetworkFactory::litecoinTestnet()) + ); } /** @@ -96,7 +117,10 @@ public function testAddressLitecoinTestnet() */ public function testAddressDogecoin() { - $this->assertEquals('A4TsAwG2Nddy3oFL6fWVLr7fh81SuuSoLQ', $this->getOutput()->address(NetworkFactory::dogecoin())); + $this->assertEquals( + 'A4TsAwG2Nddy3oFL6fWVLr7fh81SuuSoLQ', + $this->getOutput()->address(NetworkFactory::dogecoin()) + ); } /** @@ -104,7 +128,10 @@ public function testAddressDogecoin() */ public function testAddressDogecoinTestnet() { - $this->assertEquals('2N5GpVq89v2GRMDWQMfTwifUZCtqaczC6Y7', $this->getOutput()->address(NetworkFactory::dogecoinTestnet())); + $this->assertEquals( + '2N5GpVq89v2GRMDWQMfTwifUZCtqaczC6Y7', + $this->getOutput()->address(NetworkFactory::dogecoinTestnet()) + ); } /** @@ -112,7 +139,10 @@ public function testAddressDogecoinTestnet() */ public function testAddressViacoin() { - $this->assertEquals('EVBW18YCBcjc3ZnHNHAzgE7KcfqpgPjScU', $this->getOutput()->address(NetworkFactory::viacoin())); + $this->assertEquals( + 'EVBW18YCBcjc3ZnHNHAzgE7KcfqpgPjScU', + $this->getOutput()->address(NetworkFactory::viacoin()) + ); } /** @@ -120,7 +150,10 @@ public function testAddressViacoin() */ public function testAddressViacoinTestnet() { - $this->assertEquals('2N5GpVq89v2GRMDWQMfTwifUZCtqaczC6Y7', $this->getOutput()->address(NetworkFactory::viacoinTestnet())); + $this->assertEquals( + '2N5GpVq89v2GRMDWQMfTwifUZCtqaczC6Y7', + $this->getOutput()->address(NetworkFactory::viacoinTestnet()) + ); } /** @@ -128,7 +161,10 @@ public function testAddressViacoinTestnet() */ public function testAddressDash() { - $this->assertEquals('7eSFGHUJ7Yri9CQox9WaS6Uwv6TngFDeEa', $this->getOutput()->address(NetworkFactory::dash())); + $this->assertEquals( + '7eSFGHUJ7Yri9CQox9WaS6Uwv6TngFDeEa', + $this->getOutput()->address(NetworkFactory::dash()) + ); } /** @@ -136,7 +172,10 @@ public function testAddressDash() */ public function testAddressDashTestnet() { - $this->assertEquals('8rT4DcNAF6FLbVq52QWXtUJJocEcmYMNRG', $this->getOutput()->address(NetworkFactory::dashTestnet())); + $this->assertEquals( + '8rT4DcNAF6FLbVq52QWXtUJJocEcmYMNRG', + $this->getOutput()->address(NetworkFactory::dashTestnet()) + ); } /** @@ -144,7 +183,10 @@ public function testAddressDashTestnet() */ public function testAddressZcash() { - $this->assertEquals('t3WbDSRcGGtYfk4vkcxfCEXbDFCpVZxhxKh', $this->getOutput()->address(NetworkFactory::zcash())); + $this->assertEquals( + 't3WbDSRcGGtYfk4vkcxfCEXbDFCpVZxhxKh', + $this->getOutput()->address(NetworkFactory::zcash()) + ); } /** diff --git a/tests/P2wpkhTest.php b/tests/P2wpkhTest.php index 2b291a8..ec7102f 100644 --- a/tests/P2wpkhTest.php +++ b/tests/P2wpkhTest.php @@ -30,7 +30,10 @@ protected function getOutput(): OutputInterface */ public function testHex() { - $this->assertEquals('0014751e76e8199196d454941c45d1b3a323f1433bd6', $this->getOutput()->hex()); + $this->assertEquals( + '0014751e76e8199196d454941c45d1b3a323f1433bd6', + $this->getOutput()->hex() + ); } /** @@ -38,7 +41,10 @@ public function testHex() */ public function testAsm() { - $this->assertEquals('0 PUSHDATA(20)[751e76e8199196d454941c45d1b3a323f1433bd6]', $this->getOutput()->asm()); + $this->assertEquals( + '0 PUSHDATA(20)[751e76e8199196d454941c45d1b3a323f1433bd6]', + $this->getOutput()->asm() + ); } /** @@ -46,7 +52,10 @@ public function testAsm() */ public function testAddressBitcoin() { - $this->assertEquals('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4', $this->getOutput()->address()); + $this->assertEquals( + 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4', + $this->getOutput()->address() + ); } /** @@ -54,7 +63,10 @@ public function testAddressBitcoin() */ public function testAddressBitcoinTestnet() { - $this->assertEquals('tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx', $this->getOutput()->address(NetworkFactory::bitcoinTestnet())); + $this->assertEquals( + 'tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx', + $this->getOutput()->address(NetworkFactory::bitcoinTestnet()) + ); } /** @@ -62,7 +74,10 @@ public function testAddressBitcoinTestnet() */ public function testAddressLitecoin() { - $this->assertEquals('ltc1qw508d6qejxtdg4y5r3zarvary0c5xw7kgmn4n9', $this->getOutput()->address(NetworkFactory::litecoin())); + $this->assertEquals( + 'ltc1qw508d6qejxtdg4y5r3zarvary0c5xw7kgmn4n9', + $this->getOutput()->address(NetworkFactory::litecoin()) + ); } /** @@ -70,7 +85,10 @@ public function testAddressLitecoin() */ public function testAddressLitecoinTestnet() { - $this->assertEquals('tltc1qw508d6qejxtdg4y5r3zarvary0c5xw7klfsuq0', $this->getOutput()->address(NetworkFactory::litecoinTestnet())); + $this->assertEquals( + 'tltc1qw508d6qejxtdg4y5r3zarvary0c5xw7klfsuq0', + $this->getOutput()->address(NetworkFactory::litecoinTestnet()) + ); } /** @@ -78,7 +96,10 @@ public function testAddressLitecoinTestnet() */ public function testAddressViacoin() { - $this->assertEquals('via1qw508d6qejxtdg4y5r3zarvary0c5xw7kxzdzsn', $this->getOutput()->address(NetworkFactory::viacoin())); + $this->assertEquals( + 'via1qw508d6qejxtdg4y5r3zarvary0c5xw7kxzdzsn', + $this->getOutput()->address(NetworkFactory::viacoin()) + ); } /** @@ -86,7 +107,10 @@ public function testAddressViacoin() */ public function testAddressViacoinTestnet() { - $this->assertEquals('tvia1qw508d6qejxtdg4y5r3zarvary0c5xw7k3swtre', $this->getOutput()->address(NetworkFactory::viacoinTestnet())); + $this->assertEquals( + 'tvia1qw508d6qejxtdg4y5r3zarvary0c5xw7k3swtre', + $this->getOutput()->address(NetworkFactory::viacoinTestnet()) + ); } /** diff --git a/tests/P2wshTest.php b/tests/P2wshTest.php index cc8d646..4097ac0 100644 --- a/tests/P2wshTest.php +++ b/tests/P2wshTest.php @@ -32,7 +32,10 @@ protected function getOutput(): OutputInterface */ public function testHex() { - $this->assertEquals('002028205333db922f66e8a941b4a32d66de5cea03d9cda46e3e6658935272b9b24f', $this->getOutput()->hex()); + $this->assertEquals( + '002028205333db922f66e8a941b4a32d66de5cea03d9cda46e3e6658935272b9b24f', + $this->getOutput()->hex() + ); } /** @@ -40,7 +43,10 @@ public function testHex() */ public function testAsm() { - $this->assertEquals('0 PUSHDATA(32)[28205333db922f66e8a941b4a32d66de5cea03d9cda46e3e6658935272b9b24f]', $this->getOutput()->asm()); + $this->assertEquals( + '0 PUSHDATA(32)[28205333db922f66e8a941b4a32d66de5cea03d9cda46e3e6658935272b9b24f]', + $this->getOutput()->asm() + ); } /** @@ -48,7 +54,10 @@ public function testAsm() */ public function testAddressBitcoin() { - $this->assertEquals('bc1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8s4plngs', $this->getOutput()->address()); + $this->assertEquals( + 'bc1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8s4plngs', + $this->getOutput()->address() + ); } /** @@ -56,7 +65,10 @@ public function testAddressBitcoin() */ public function testAddressBitcoinTestnet() { - $this->assertEquals('tb1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8szffujl', $this->getOutput()->address(NetworkFactory::bitcoinTestnet())); + $this->assertEquals( + 'tb1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8szffujl', + $this->getOutput()->address(NetworkFactory::bitcoinTestnet()) + ); } /** @@ -64,7 +76,10 @@ public function testAddressBitcoinTestnet() */ public function testAddressLitecoin() { - $this->assertEquals('ltc1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8sk93rj4', $this->getOutput()->address(NetworkFactory::litecoin())); + $this->assertEquals( + 'ltc1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8sk93rj4', + $this->getOutput()->address(NetworkFactory::litecoin()) + ); } /** @@ -72,7 +87,10 @@ public function testAddressLitecoin() */ public function testAddressLitecoinTestnet() { - $this->assertEquals('tltc1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8sa24adq', $this->getOutput()->address(NetworkFactory::litecoinTestnet())); + $this->assertEquals( + 'tltc1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8sa24adq', + $this->getOutput()->address(NetworkFactory::litecoinTestnet()) + ); } /** @@ -80,7 +98,10 @@ public function testAddressLitecoinTestnet() */ public function testAddressViacoin() { - $this->assertEquals('via1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8s7ulzpx', $this->getOutput()->address(NetworkFactory::viacoin())); + $this->assertEquals( + 'via1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8s7ulzpx', + $this->getOutput()->address(NetworkFactory::viacoin()) + ); } /** @@ -88,7 +109,10 @@ public function testAddressViacoin() */ public function testAddressViacoinTestnet() { - $this->assertEquals('tvia1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8s4nmu7n', $this->getOutput()->address(NetworkFactory::viacoinTestnet())); + $this->assertEquals( + 'tvia1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8s4nmu7n', + $this->getOutput()->address(NetworkFactory::viacoinTestnet()) + ); } /** From d011f32a185390e516c3ce5d7217ef1701beaeb5 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 16 May 2023 19:57:25 -0700 Subject: [PATCH 06/32] Add PhpStan --- composer.json | 1 + phpstan.neon | 8 ++++++++ 2 files changed, 9 insertions(+) create mode 100644 phpstan.neon diff --git a/composer.json b/composer.json index 112f82b..053ae2a 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ "stephenhill/base58": "^1.1" }, "require-dev": { + "phpstan/phpstan": "^1.10", "phpunit/phpunit": "^5.7" }, "autoload": { diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..fc5530e --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,8 @@ +parameters: + level: 8 + paths: + - src + - tests + excludePaths: + - tests/reports + treatPhpDocTypesAsCertain: false \ No newline at end of file From 3d5b1d5ffb73988e965829fbd3f0db2e659f5f24 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 16 May 2023 19:57:37 -0700 Subject: [PATCH 07/32] phsptan: return type --- tests/BitcoinCashTest.php | 4 ++-- tests/BitcoinTest.php | 8 ++++---- tests/OutputFactoryTest.php | 12 ++++++------ tests/P2msTest.php | 6 +++--- tests/P2pkTest.php | 6 +++--- tests/P2pkhTest.php | 32 ++++++++++++++++---------------- tests/P2shTest.php | 32 ++++++++++++++++---------------- tests/P2wpkhTest.php | 18 +++++++++--------- tests/P2wshTest.php | 18 +++++++++--------- 9 files changed, 68 insertions(+), 68 deletions(-) diff --git a/tests/BitcoinCashTest.php b/tests/BitcoinCashTest.php index be7f3c6..becb2a8 100644 --- a/tests/BitcoinCashTest.php +++ b/tests/BitcoinCashTest.php @@ -15,7 +15,7 @@ */ class BitcoinCashTest extends TestCase { - public function testDecodeP2PKH() + public function testDecodeP2PKH(): void { $this->assertInstanceOf( P2pkh::class, @@ -23,7 +23,7 @@ public function testDecodeP2PKH() ); } - public function testDecodeP2SH() + public function testDecodeP2SH(): void { $this->assertInstanceOf( P2sh::class, diff --git a/tests/BitcoinTest.php b/tests/BitcoinTest.php index 21fa5f8..80d4d3a 100644 --- a/tests/BitcoinTest.php +++ b/tests/BitcoinTest.php @@ -17,7 +17,7 @@ */ class BitcoinTest extends TestCase { - public function testDecodeP2PKH() + public function testDecodeP2PKH(): void { $this->assertInstanceOf( P2pkh::class, @@ -25,7 +25,7 @@ public function testDecodeP2PKH() ); } - public function testDecodeP2SH() + public function testDecodeP2SH(): void { $this->assertInstanceOf( P2sh::class, @@ -33,7 +33,7 @@ public function testDecodeP2SH() ); } - public function testDecodeP2WPKH() + public function testDecodeP2WPKH(): void { $this->assertInstanceOf( P2wpkh::class, @@ -41,7 +41,7 @@ public function testDecodeP2WPKH() ); } - public function testDecodeP2WSH() + public function testDecodeP2WSH(): void { $this->assertInstanceOf( P2wsh::class, diff --git a/tests/OutputFactoryTest.php b/tests/OutputFactoryTest.php index 8618e78..5e6de21 100644 --- a/tests/OutputFactoryTest.php +++ b/tests/OutputFactoryTest.php @@ -22,7 +22,7 @@ class OutputFactoryTest extends TestCase /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testFromHexP2PK() + public function testFromHexP2PK(): void { $output = OutputFactory::fromHex('210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ac'); $this->assertInstanceOf(P2pk::class, $output); @@ -31,7 +31,7 @@ public function testFromHexP2PK() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testFromHexP2PKH() + public function testFromHexP2PKH(): void { $output = OutputFactory::fromHex('76a914751e76e8199196d454941c45d1b3a323f1433bd688ac'); $this->assertInstanceOf(P2pkh::class, $output); @@ -40,7 +40,7 @@ public function testFromHexP2PKH() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testFromHexP2MS() + public function testFromHexP2MS(): void { $output = OutputFactory::fromHex('51210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f8179851ae'); $this->assertInstanceOf(P2ms::class, $output); @@ -49,7 +49,7 @@ public function testFromHexP2MS() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testFromHexP2SH() + public function testFromHexP2SH(): void { $output = OutputFactory::fromHex('a91483eebb7d79aa1d388e3b0ac65b98ac580c4da01a87'); $this->assertInstanceOf(P2sh::class, $output); @@ -58,7 +58,7 @@ public function testFromHexP2SH() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testFromHexP2WPKH() + public function testFromHexP2WPKH(): void { $output = OutputFactory::fromHex('0014751e76e8199196d454941c45d1b3a323f1433bd6'); $this->assertInstanceOf(P2wpkh::class, $output); @@ -67,7 +67,7 @@ public function testFromHexP2WPKH() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testFromHexP2WSH() + public function testFromHexP2WSH(): void { $output = OutputFactory::fromHex('002028205333db922f66e8a941b4a32d66de5cea03d9cda46e3e6658935272b9b24f'); $this->assertInstanceOf(P2wsh::class, $output); diff --git a/tests/P2msTest.php b/tests/P2msTest.php index 8f261a2..b6f33d0 100644 --- a/tests/P2msTest.php +++ b/tests/P2msTest.php @@ -27,7 +27,7 @@ protected function getOutput(): OutputInterface /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testHex() + public function testHex(): void { $this->assertEquals( '51210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f8179851ae', @@ -38,7 +38,7 @@ public function testHex() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAsm() + public function testAsm(): void { $this->assertEquals( '1 PUSHDATA(33)[0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798] 1 CHECKMULTISIG', @@ -49,7 +49,7 @@ public function testAsm() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testFromScript() + public function testFromScript(): void { $output = $this->getOutput(); $this->assertEquals($output->script(), P2ms::fromScript($output->script())->script()); diff --git a/tests/P2pkTest.php b/tests/P2pkTest.php index 27dfa5a..4d4b4e0 100644 --- a/tests/P2pkTest.php +++ b/tests/P2pkTest.php @@ -26,7 +26,7 @@ protected function getOutput(): OutputInterface /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testHex() + public function testHex(): void { $this->assertEquals( '210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ac', @@ -37,7 +37,7 @@ public function testHex() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAsm() + public function testAsm(): void { $this->assertEquals( 'PUSHDATA(33)[0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798] CHECKSIG', @@ -48,7 +48,7 @@ public function testAsm() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testFromScript() + public function testFromScript(): void { $output = $this->getOutput(); $this->assertEquals($output->script(), P2pk::fromScript($output->script())->script()); diff --git a/tests/P2pkhTest.php b/tests/P2pkhTest.php index 3d65429..bdea9b1 100644 --- a/tests/P2pkhTest.php +++ b/tests/P2pkhTest.php @@ -28,7 +28,7 @@ protected function getOutput(): OutputInterface /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testHex() + public function testHex(): void { $this->assertEquals( '76a914751e76e8199196d454941c45d1b3a323f1433bd688ac', @@ -39,7 +39,7 @@ public function testHex() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAsm() + public function testAsm(): void { $this->assertEquals( 'DUP HASH160 PUSHDATA(20)[751e76e8199196d454941c45d1b3a323f1433bd6] EQUALVERIFY CHECKSIG', @@ -50,7 +50,7 @@ public function testAsm() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressBitcoin() + public function testAddressBitcoin(): void { $this->assertEquals( '1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH', @@ -61,7 +61,7 @@ public function testAddressBitcoin() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressBitcoinTestnet() + public function testAddressBitcoinTestnet(): void { $this->assertEquals( 'mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r', @@ -72,7 +72,7 @@ public function testAddressBitcoinTestnet() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressBitcoinCash() + public function testAddressBitcoinCash(): void { $this->assertEquals( 'bitcoincash:qp63uahgrxged4z5jswyt5dn5v3lzsem6cy4spdc2h', @@ -83,7 +83,7 @@ public function testAddressBitcoinCash() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressBitcoinGold() + public function testAddressBitcoinGold(): void { $this->assertEquals( 'GUXByHDZLvU4DnVH9imSFckt3HEQ5cFgE5', @@ -94,7 +94,7 @@ public function testAddressBitcoinGold() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressLitecoin() + public function testAddressLitecoin(): void { $this->assertEquals( 'LVuDpNCSSj6pQ7t9Pv6d6sUkLKoqDEVUnJ', @@ -105,7 +105,7 @@ public function testAddressLitecoin() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressLitecoinTestnet() + public function testAddressLitecoinTestnet(): void { $this->assertEquals( 'mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r', @@ -116,7 +116,7 @@ public function testAddressLitecoinTestnet() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressDogecoin() + public function testAddressDogecoin(): void { $this->assertEquals( 'DFpN6QqFfUm3gKNaxN6tNcab1FArL9cZLE', @@ -127,7 +127,7 @@ public function testAddressDogecoin() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressDogecoinTestnet() + public function testAddressDogecoinTestnet(): void { $this->assertEquals( 'nesRpRaAbTDmZHwmzBkLd2AtF7Z9L9z5S2', @@ -138,7 +138,7 @@ public function testAddressDogecoinTestnet() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressViacoin() + public function testAddressViacoin(): void { $this->assertEquals( 'Vkg6Ts44mskyD668xZkxFkjqovjXX9yUzZ', @@ -149,7 +149,7 @@ public function testAddressViacoin() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressViacoinTestnet() + public function testAddressViacoinTestnet(): void { $this->assertEquals( 'tHbsbwkCXyi31MtzL4QoQmyu4BAMJz8hS6', @@ -160,7 +160,7 @@ public function testAddressViacoinTestnet() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressDash() + public function testAddressDash(): void { $this->assertEquals( 'XmN7PQYWKn5MJFna5fRYgP6mxT2F7xpekE', @@ -171,7 +171,7 @@ public function testAddressDash() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressDashTestnet() + public function testAddressDashTestnet(): void { $this->assertEquals( 'y7f7RFKf49GYpZa2d6QdEHFLcEFfuoNcer', @@ -182,7 +182,7 @@ public function testAddressDashTestnet() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressZcash() + public function testAddressZcash(): void { $this->assertEquals( 't1UYsZVJkLPeMjxEtACvSxfWuNmddpWfxzs', @@ -193,7 +193,7 @@ public function testAddressZcash() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testFromScript() + public function testFromScript(): void { $output = $this->getOutput(); $this->assertEquals($output->script(), P2pkh::fromScript($output->script())->script()); diff --git a/tests/P2shTest.php b/tests/P2shTest.php index adbe49a..f9b62d1 100644 --- a/tests/P2shTest.php +++ b/tests/P2shTest.php @@ -30,7 +30,7 @@ protected function getOutput(): OutputInterface /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testHex() + public function testHex(): void { $this->assertEquals( 'a91483eebb7d79aa1d388e3b0ac65b98ac580c4da01a87', @@ -41,7 +41,7 @@ public function testHex() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAsm() + public function testAsm(): void { $this->assertEquals( 'HASH160 PUSHDATA(20)[83eebb7d79aa1d388e3b0ac65b98ac580c4da01a] EQUAL', @@ -52,7 +52,7 @@ public function testAsm() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressBitcoin() + public function testAddressBitcoin(): void { $this->assertEquals('3DicS6C8JZm59RsrgXr56iVHzYdQngiehV', $this->getOutput()->address()); } @@ -60,7 +60,7 @@ public function testAddressBitcoin() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressBitcoinTestnet() + public function testAddressBitcoinTestnet(): void { $this->assertEquals( '2N5GpVq89v2GRMDWQMfTwifUZCtqaczC6Y7', @@ -71,7 +71,7 @@ public function testAddressBitcoinTestnet() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressBitcoinCash() + public function testAddressBitcoinCash(): void { $this->assertEquals( 'bitcoincash:pzp7awma0x4p6wyw8v9vvkuc43vqcndqrg9umkmd8g', @@ -82,7 +82,7 @@ public function testAddressBitcoinCash() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressBitcoinGold() + public function testAddressBitcoinGold(): void { $this->assertEquals( 'dRSt8KXceu9YfGjDmCAAKNdAVRXbFQWRxF', @@ -93,7 +93,7 @@ public function testAddressBitcoinGold() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressLitecoin() + public function testAddressLitecoin(): void { $this->assertEquals( 'MKvkjyc6FgcVww9knQqQvMjhKFDrpERUsa', @@ -104,7 +104,7 @@ public function testAddressLitecoin() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressLitecoinTestnet() + public function testAddressLitecoinTestnet(): void { $this->assertEquals( 'QYdacqzPw8KWVQGSymVxoMuzMHHQYBayi6', @@ -115,7 +115,7 @@ public function testAddressLitecoinTestnet() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressDogecoin() + public function testAddressDogecoin(): void { $this->assertEquals( 'A4TsAwG2Nddy3oFL6fWVLr7fh81SuuSoLQ', @@ -126,7 +126,7 @@ public function testAddressDogecoin() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressDogecoinTestnet() + public function testAddressDogecoinTestnet(): void { $this->assertEquals( '2N5GpVq89v2GRMDWQMfTwifUZCtqaczC6Y7', @@ -137,7 +137,7 @@ public function testAddressDogecoinTestnet() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressViacoin() + public function testAddressViacoin(): void { $this->assertEquals( 'EVBW18YCBcjc3ZnHNHAzgE7KcfqpgPjScU', @@ -148,7 +148,7 @@ public function testAddressViacoin() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressViacoinTestnet() + public function testAddressViacoinTestnet(): void { $this->assertEquals( '2N5GpVq89v2GRMDWQMfTwifUZCtqaczC6Y7', @@ -159,7 +159,7 @@ public function testAddressViacoinTestnet() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressDash() + public function testAddressDash(): void { $this->assertEquals( '7eSFGHUJ7Yri9CQox9WaS6Uwv6TngFDeEa', @@ -170,7 +170,7 @@ public function testAddressDash() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressDashTestnet() + public function testAddressDashTestnet(): void { $this->assertEquals( '8rT4DcNAF6FLbVq52QWXtUJJocEcmYMNRG', @@ -181,7 +181,7 @@ public function testAddressDashTestnet() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressZcash() + public function testAddressZcash(): void { $this->assertEquals( 't3WbDSRcGGtYfk4vkcxfCEXbDFCpVZxhxKh', @@ -192,7 +192,7 @@ public function testAddressZcash() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testFromScript() + public function testFromScript(): void { $output = $this->getOutput(); $this->assertEquals($output->script(), P2sh::fromScript($output->script())->script()); diff --git a/tests/P2wpkhTest.php b/tests/P2wpkhTest.php index ec7102f..b2e08c8 100644 --- a/tests/P2wpkhTest.php +++ b/tests/P2wpkhTest.php @@ -28,7 +28,7 @@ protected function getOutput(): OutputInterface /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testHex() + public function testHex(): void { $this->assertEquals( '0014751e76e8199196d454941c45d1b3a323f1433bd6', @@ -39,7 +39,7 @@ public function testHex() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAsm() + public function testAsm(): void { $this->assertEquals( '0 PUSHDATA(20)[751e76e8199196d454941c45d1b3a323f1433bd6]', @@ -50,7 +50,7 @@ public function testAsm() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressBitcoin() + public function testAddressBitcoin(): void { $this->assertEquals( 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4', @@ -61,7 +61,7 @@ public function testAddressBitcoin() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressBitcoinTestnet() + public function testAddressBitcoinTestnet(): void { $this->assertEquals( 'tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx', @@ -72,7 +72,7 @@ public function testAddressBitcoinTestnet() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressLitecoin() + public function testAddressLitecoin(): void { $this->assertEquals( 'ltc1qw508d6qejxtdg4y5r3zarvary0c5xw7kgmn4n9', @@ -83,7 +83,7 @@ public function testAddressLitecoin() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressLitecoinTestnet() + public function testAddressLitecoinTestnet(): void { $this->assertEquals( 'tltc1qw508d6qejxtdg4y5r3zarvary0c5xw7klfsuq0', @@ -94,7 +94,7 @@ public function testAddressLitecoinTestnet() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressViacoin() + public function testAddressViacoin(): void { $this->assertEquals( 'via1qw508d6qejxtdg4y5r3zarvary0c5xw7kxzdzsn', @@ -105,7 +105,7 @@ public function testAddressViacoin() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressViacoinTestnet() + public function testAddressViacoinTestnet(): void { $this->assertEquals( 'tvia1qw508d6qejxtdg4y5r3zarvary0c5xw7k3swtre', @@ -116,7 +116,7 @@ public function testAddressViacoinTestnet() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testFromScript() + public function testFromScript(): void { $output = $this->getOutput(); $this->assertEquals($output->script(), P2wpkh::fromScript($output->script())->script()); diff --git a/tests/P2wshTest.php b/tests/P2wshTest.php index 4097ac0..289c433 100644 --- a/tests/P2wshTest.php +++ b/tests/P2wshTest.php @@ -30,7 +30,7 @@ protected function getOutput(): OutputInterface /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testHex() + public function testHex(): void { $this->assertEquals( '002028205333db922f66e8a941b4a32d66de5cea03d9cda46e3e6658935272b9b24f', @@ -41,7 +41,7 @@ public function testHex() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAsm() + public function testAsm(): void { $this->assertEquals( '0 PUSHDATA(32)[28205333db922f66e8a941b4a32d66de5cea03d9cda46e3e6658935272b9b24f]', @@ -52,7 +52,7 @@ public function testAsm() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressBitcoin() + public function testAddressBitcoin(): void { $this->assertEquals( 'bc1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8s4plngs', @@ -63,7 +63,7 @@ public function testAddressBitcoin() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressBitcoinTestnet() + public function testAddressBitcoinTestnet(): void { $this->assertEquals( 'tb1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8szffujl', @@ -74,7 +74,7 @@ public function testAddressBitcoinTestnet() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressLitecoin() + public function testAddressLitecoin(): void { $this->assertEquals( 'ltc1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8sk93rj4', @@ -85,7 +85,7 @@ public function testAddressLitecoin() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressLitecoinTestnet() + public function testAddressLitecoinTestnet(): void { $this->assertEquals( 'tltc1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8sa24adq', @@ -96,7 +96,7 @@ public function testAddressLitecoinTestnet() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressViacoin() + public function testAddressViacoin(): void { $this->assertEquals( 'via1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8s7ulzpx', @@ -107,7 +107,7 @@ public function testAddressViacoin() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testAddressViacoinTestnet() + public function testAddressViacoinTestnet(): void { $this->assertEquals( 'tvia1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8s4nmu7n', @@ -118,7 +118,7 @@ public function testAddressViacoinTestnet() /** * @throws \AndKom\Bitcoin\Address\Exception */ - public function testFromScript() + public function testFromScript(): void { $output = $this->getOutput(); $this->assertEquals($output->script(), P2wsh::fromScript($output->script())->script()); From f699624226f7934eda639b4e0c27db24d75bdd7a Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 16 May 2023 20:52:31 -0700 Subject: [PATCH 08/32] Fix `Unsafe usage of new static()` --- src/Network/NetworkFactory.php | 2 +- src/Output/Outputs/P2ms.php | 2 +- src/Output/Outputs/P2pk.php | 2 +- src/Output/Outputs/P2pkh.php | 2 +- src/Output/Outputs/P2sh.php | 2 +- src/Output/Outputs/P2wpkh.php | 2 +- src/Output/Outputs/P2wsh.php | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Network/NetworkFactory.php b/src/Network/NetworkFactory.php index 0180889..467bc2b 100644 --- a/src/Network/NetworkFactory.php +++ b/src/Network/NetworkFactory.php @@ -102,6 +102,6 @@ public function __call($name, $arguments) */ public static function __callStatic($name, $arguments) { - return (new static())->createNetwork($name, $arguments); + return (new NetworkFactory())->createNetwork($name, $arguments); } } diff --git a/src/Output/Outputs/P2ms.php b/src/Output/Outputs/P2ms.php index f2f779d..02ae213 100644 --- a/src/Output/Outputs/P2ms.php +++ b/src/Output/Outputs/P2ms.php @@ -155,6 +155,6 @@ public static function fromScript(string $script): OutputInterface $i += $pubKeyLen + 1; } - return new static($m, $pubKeys); + return new P2ms($m, $pubKeys); } } diff --git a/src/Output/Outputs/P2pk.php b/src/Output/Outputs/P2pk.php index 0da2a21..4696afe 100644 --- a/src/Output/Outputs/P2pk.php +++ b/src/Output/Outputs/P2pk.php @@ -93,6 +93,6 @@ public static function fromScript(string $script): OutputInterface $pubKey = substr($script, 1, -1); - return new static($pubKey); + return new P2pk($pubKey); } } diff --git a/src/Output/Outputs/P2pkh.php b/src/Output/Outputs/P2pkh.php index 1d8c392..35d1b86 100644 --- a/src/Output/Outputs/P2pkh.php +++ b/src/Output/Outputs/P2pkh.php @@ -92,6 +92,6 @@ public static function fromScript(string $script): OutputInterface $pubKeyHash = substr($script, 3, 20); - return new static($pubKeyHash); + return new P2pkh($pubKeyHash); } } diff --git a/src/Output/Outputs/P2sh.php b/src/Output/Outputs/P2sh.php index 638cc22..5959e12 100644 --- a/src/Output/Outputs/P2sh.php +++ b/src/Output/Outputs/P2sh.php @@ -95,6 +95,6 @@ public static function fromScript(string $script): OutputInterface $scriptHash = substr($script, 2, -1); - return new static($scriptHash); + return new P2sh($scriptHash); } } diff --git a/src/Output/Outputs/P2wpkh.php b/src/Output/Outputs/P2wpkh.php index e8a2eb0..352b4ef 100644 --- a/src/Output/Outputs/P2wpkh.php +++ b/src/Output/Outputs/P2wpkh.php @@ -88,6 +88,6 @@ public static function fromScript(string $script): OutputInterface $pubKeyHash = substr($script, 2, 20); - return new static($pubKeyHash); + return new P2wpkh($pubKeyHash); } } diff --git a/src/Output/Outputs/P2wsh.php b/src/Output/Outputs/P2wsh.php index 56a3517..8566c06 100644 --- a/src/Output/Outputs/P2wsh.php +++ b/src/Output/Outputs/P2wsh.php @@ -92,6 +92,6 @@ public static function fromScript(string $script): OutputInterface $witnessHash = substr($script, 2, 32); - return new static($witnessHash); + return new P2wsh($witnessHash); } } From 6a84bbc3bde2be9c58f593001ade208b17f0936d Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 16 May 2023 20:54:46 -0700 Subject: [PATCH 09/32] property is not covariant with PHPDoc --- src/Network/Networks/Bitcoin.php | 2 +- src/Network/Networks/BitcoinGold.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Network/Networks/Bitcoin.php b/src/Network/Networks/Bitcoin.php index 8fd025d..c63f525 100644 --- a/src/Network/Networks/Bitcoin.php +++ b/src/Network/Networks/Bitcoin.php @@ -34,7 +34,7 @@ class Bitcoin implements NetworkInterface protected $prefixP2sh = "\x05"; /** - * @var string + * @var ?string */ protected $prefixBech32 = 'bc'; diff --git a/src/Network/Networks/BitcoinGold.php b/src/Network/Networks/BitcoinGold.php index 8930b56..0f0903c 100644 --- a/src/Network/Networks/BitcoinGold.php +++ b/src/Network/Networks/BitcoinGold.php @@ -21,7 +21,7 @@ class BitcoinGold extends Bitcoin protected $prefixP2sh = "\x17"; /** - * @var null + * @var ?string */ protected $prefixBech32 = 'btg'; } From 63194e30459d02418dbd9c90dc302a69dd4b4d6f Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 16 May 2023 21:11:05 -0700 Subject: [PATCH 10/32] PhpStan level 6 fixes --- src/Network/NetworkFactory.php | 18 +++++++++--------- src/Output/OutputFactory.php | 2 +- src/Output/Outputs/P2ms.php | 2 +- src/Utils.php | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Network/NetworkFactory.php b/src/Network/NetworkFactory.php index 467bc2b..5c12efe 100644 --- a/src/Network/NetworkFactory.php +++ b/src/Network/NetworkFactory.php @@ -27,7 +27,7 @@ class NetworkFactory { /** - * @var array + * @var string[] */ protected static $networks = [ 'bitcoin', @@ -54,7 +54,7 @@ class NetworkFactory /** * @param NetworkInterface $network */ - public static function setDefaultNetwork(NetworkInterface $network) + public static function setDefaultNetwork(NetworkInterface $network): void { static::$defaultNetwork = $network; } @@ -69,7 +69,7 @@ public static function getDefaultNetwork(): NetworkInterface /** * @param string $name - * @param array $arguments + * @param array $arguments * @return NetworkInterface * @throws Exception */ @@ -85,22 +85,22 @@ public function createNetwork(string $name, array $arguments): NetworkInterface } /** - * @param $name - * @param $arguments + * @param string $name + * @param array $arguments * @return NetworkInterface * @throws Exception */ - public function __call($name, $arguments) + public function __call(string $name, array $arguments) { return $this->createNetwork($name, $arguments); } /** - * @param $name - * @param $arguments + * @param string $name + * @param array $arguments * @return NetworkInterface */ - public static function __callStatic($name, $arguments) + public static function __callStatic(string $name, array $arguments) { return (new NetworkFactory())->createNetwork($name, $arguments); } diff --git a/src/Output/OutputFactory.php b/src/Output/OutputFactory.php index f0af81b..f717d94 100644 --- a/src/Output/OutputFactory.php +++ b/src/Output/OutputFactory.php @@ -41,7 +41,7 @@ public static function p2pkh(string $pubKeyHash): OutputInterface /** * @param int $m - * @param array $pubKeys + * @param string[] $pubKeys * @return OutputInterface * @throws \AndKom\Bitcoin\Address\Exception */ diff --git a/src/Output/Outputs/P2ms.php b/src/Output/Outputs/P2ms.php index 02ae213..7527a63 100644 --- a/src/Output/Outputs/P2ms.php +++ b/src/Output/Outputs/P2ms.php @@ -35,7 +35,7 @@ class P2ms extends AbstractOutput /** * P2ms constructor. * @param int $m Number of signatures - * @param array $pubKeys + * @param string[] $pubKeys * @throws Exception */ public function __construct(int $m, array $pubKeys) diff --git a/src/Utils.php b/src/Utils.php index 2f37351..8143c3a 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -72,7 +72,7 @@ public static function base58encode(string $hash, string $prefix = "\x00"): stri /** * @param string $base58 - * @return array + * @return string[] * @throws \Exception */ public static function base58decode(string $base58): array From be5e4f0adaac865f8583ce9d20ed7d7ab561c882 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 16 May 2023 21:28:13 -0700 Subject: [PATCH 11/32] PhpStan level 7 fixes --- src/Network/NetworkFactory.php | 8 +++++++- tests/P2msTest.php | 2 +- tests/P2pkTest.php | 2 +- tests/P2pkhTest.php | 2 +- tests/P2shTest.php | 2 +- tests/P2wpkhTest.php | 2 +- tests/P2wshTest.php | 2 +- 7 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Network/NetworkFactory.php b/src/Network/NetworkFactory.php index 5c12efe..aa5535a 100644 --- a/src/Network/NetworkFactory.php +++ b/src/Network/NetworkFactory.php @@ -81,7 +81,13 @@ public function createNetwork(string $name, array $arguments): NetworkInterface $class = 'AndKom\\Bitcoin\\Address\\Network\\Networks\\' . ucfirst($name); - return new $class(...$arguments); + $network = new $class(...$arguments); + + if (! $network instanceof NetworkInterface) { + throw new Exception("{$class} was not a NetworkInterface"); + } + + return $network; } /** diff --git a/tests/P2msTest.php b/tests/P2msTest.php index b6f33d0..c26a301 100644 --- a/tests/P2msTest.php +++ b/tests/P2msTest.php @@ -21,7 +21,7 @@ class P2msTest extends TestCase */ protected function getOutput(): OutputInterface { - return OutputFactory::p2ms(1, [hex2bin('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798')]); + return OutputFactory::p2ms(1, [hex2bin('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798') ?: '']); } /** diff --git a/tests/P2pkTest.php b/tests/P2pkTest.php index 4d4b4e0..973e004 100644 --- a/tests/P2pkTest.php +++ b/tests/P2pkTest.php @@ -20,7 +20,7 @@ class P2pkTest extends P2pkhTest */ protected function getOutput(): OutputInterface { - return OutputFactory::p2pk(hex2bin('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798')); + return OutputFactory::p2pk(hex2bin('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798') ?: ''); } /** diff --git a/tests/P2pkhTest.php b/tests/P2pkhTest.php index bdea9b1..ddeaa4a 100644 --- a/tests/P2pkhTest.php +++ b/tests/P2pkhTest.php @@ -22,7 +22,7 @@ class P2pkhTest extends TestCase */ protected function getOutput(): OutputInterface { - return OutputFactory::p2pkh(hex2bin('751e76e8199196d454941c45d1b3a323f1433bd6')); + return OutputFactory::p2pkh(hex2bin('751e76e8199196d454941c45d1b3a323f1433bd6') ?: ''); } /** diff --git a/tests/P2shTest.php b/tests/P2shTest.php index f9b62d1..54f6491 100644 --- a/tests/P2shTest.php +++ b/tests/P2shTest.php @@ -23,7 +23,7 @@ class P2shTest extends TestCase protected function getOutput(): OutputInterface { $factory = new OutputFactory(); - $p2ms = $factory->p2ms(1, [hex2bin('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798')]); + $p2ms = $factory->p2ms(1, [hex2bin('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798') ?: '']); return $factory->p2sh($p2ms); } diff --git a/tests/P2wpkhTest.php b/tests/P2wpkhTest.php index b2e08c8..72d17b6 100644 --- a/tests/P2wpkhTest.php +++ b/tests/P2wpkhTest.php @@ -22,7 +22,7 @@ class P2wpkhTest extends TestCase */ protected function getOutput(): OutputInterface { - return OutputFactory::p2wpkh(hex2bin('751e76e8199196d454941c45d1b3a323f1433bd6')); + return OutputFactory::p2wpkh(hex2bin('751e76e8199196d454941c45d1b3a323f1433bd6') ?: ''); } /** diff --git a/tests/P2wshTest.php b/tests/P2wshTest.php index 289c433..09ef992 100644 --- a/tests/P2wshTest.php +++ b/tests/P2wshTest.php @@ -23,7 +23,7 @@ class P2wshTest extends TestCase protected function getOutput(): OutputInterface { $factory = new OutputFactory(); - $p2ms = $factory->p2ms(1, [hex2bin('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798')]); + $p2ms = $factory->p2ms(1, [hex2bin('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798') ?: '']); return $factory->p2wsh($p2ms); } From 1b1e9fb4a0abd29d813856af6419fe3bcf13b9ee Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 16 May 2023 21:29:33 -0700 Subject: [PATCH 12/32] fix: phpcs line length --- tests/P2msTest.php | 5 ++++- tests/P2shTest.php | 5 ++++- tests/P2wshTest.php | 5 ++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/tests/P2msTest.php b/tests/P2msTest.php index c26a301..65eb51b 100644 --- a/tests/P2msTest.php +++ b/tests/P2msTest.php @@ -21,7 +21,10 @@ class P2msTest extends TestCase */ protected function getOutput(): OutputInterface { - return OutputFactory::p2ms(1, [hex2bin('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798') ?: '']); + return OutputFactory::p2ms( + 1, + [hex2bin('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798') ?: ''] + ); } /** diff --git a/tests/P2shTest.php b/tests/P2shTest.php index 54f6491..48a92ee 100644 --- a/tests/P2shTest.php +++ b/tests/P2shTest.php @@ -23,7 +23,10 @@ class P2shTest extends TestCase protected function getOutput(): OutputInterface { $factory = new OutputFactory(); - $p2ms = $factory->p2ms(1, [hex2bin('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798') ?: '']); + $p2ms = $factory->p2ms( + 1, + [hex2bin('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798') ?: ''] + ); return $factory->p2sh($p2ms); } diff --git a/tests/P2wshTest.php b/tests/P2wshTest.php index 09ef992..0f822b1 100644 --- a/tests/P2wshTest.php +++ b/tests/P2wshTest.php @@ -23,7 +23,10 @@ class P2wshTest extends TestCase protected function getOutput(): OutputInterface { $factory = new OutputFactory(); - $p2ms = $factory->p2ms(1, [hex2bin('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798') ?: '']); + $p2ms = $factory->p2ms( + 1, + [hex2bin('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798') ?: ''] + ); return $factory->p2wsh($p2ms); } From 679d473f1dd9b51b387c562c895e29bc8e784c2b Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 16 May 2023 21:41:16 -0700 Subject: [PATCH 13/32] Add PHP code coverage exclusions to linting --- .gitignore | 3 ++- phpcs.xml | 3 +++ phpstan.neon | 2 +- phpunit.xml.dist | 5 +++++ 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index d23f2c0..8d7a421 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea /vendor -composer.lock \ No newline at end of file +composer.lock +tests/_reports/ \ No newline at end of file diff --git a/phpcs.xml b/phpcs.xml index c40d9c0..f797aba 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -8,4 +8,7 @@ + tests/_reports + tests/_data + \ No newline at end of file diff --git a/phpstan.neon b/phpstan.neon index fc5530e..93b2e48 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,5 +4,5 @@ parameters: - src - tests excludePaths: - - tests/reports + - tests/_reports treatPhpDocTypesAsCertain: false \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 08e0253..d9f76e1 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -2,6 +2,11 @@ + + + src + + ./tests/ From b64cd0dac4dbeba739725c2363db431749752923 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 16 May 2023 21:45:16 -0700 Subject: [PATCH 14/32] Add README header, coverage badge, coverage workflow --- .github/coverage.svg | 21 ++++++++ .github/workflows/codecoverage.yml | 82 ++++++++++++++++++++++++++++++ README.md | 2 + 3 files changed, 105 insertions(+) create mode 100644 .github/coverage.svg create mode 100644 .github/workflows/codecoverage.yml diff --git a/.github/coverage.svg b/.github/coverage.svg new file mode 100644 index 0000000..00a08aa --- /dev/null +++ b/.github/coverage.svg @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + PHPUnit + PHPUnit + 83% + 83% + + \ No newline at end of file diff --git a/.github/workflows/codecoverage.yml b/.github/workflows/codecoverage.yml new file mode 100644 index 0000000..0b8676f --- /dev/null +++ b/.github/workflows/codecoverage.yml @@ -0,0 +1,82 @@ +name: Code Coverage + +# Runs PHPUnit with code coverage enabled, commits the html report to +# GitHub Pages, generates a README badge with the coverage percentage. +# +# Requires a gh-pages branch already created. +# +# git checkout --orphan gh-pages +# touch index.html +# git add index.html +# git commit -m 'Set up gh-pages branch' index.html +# git push origin gh-pages +# git checkout master +# +# @author BrianHenryIE + +on: + push: + branches: + - master + +jobs: + tests: + runs-on: ubuntu-latest + + steps: + + - name: Git checkout + uses: actions/checkout@v2 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '7.4' + coverage: xdebug + +# - name: Checkout GitHub Pages branch for code coverage report +# uses: actions/checkout@v2 +# with: +# ref: gh-pages +# path: tests/_reports/html + + - name: Install dependencies + run: composer install --prefer-dist --no-suggest --no-progress + +# - name: Clear previous code coverage +# run: | +# cd tests/_reports/html +# rm -rf * +# cd ../../.. + + - name: Run tests with code coverage + run: XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text --coverage-clover tests/_reports/clover.xml --coverage-html tests/_reports/html + +# - name: Edit phpcov html output to work with gh-pages +# run: | +# cd tests/_reports/html +# mv _css css; find . -depth -name '*.html' -exec sed -i "s/_css\//css\//" {} + +# mv _icons icons; find . -depth -name '*.html' -exec sed -i "s/_icons\//icons\//" {} + +# mv _js js; find . -depth -name '*.html' -exec sed -i "s/_js\//js\//" {} + +# git add * +# cd ../../.. + +# - name: Commit code coverage to gh-pages +# uses: stefanzweifel/git-auto-commit-action@v4.1.1 +# with: +# repository: tests/_reports/html +# branch: gh-pages +# commit_message: "🤖 Commit code coverage to gh-pages" +# commit_options: +# env: +# GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" + + - name: Update README badge + run: vendor/bin/php-coverage-badger tests/_reports/clover.xml .github/coverage.svg + + - name: Commit code coverage badge + uses: stefanzweifel/git-auto-commit-action@v4.1.1 + with: + commit_message: "🤖 Commit code coverage badge" + + diff --git a/README.md b/README.md index 205d672..33af980 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![PHP 7.4](https://img.shields.io/badge/PHP-7.4-8892BF.svg)]() [![PHPCS PSR-12](https://img.shields.io/badge/PHPCS-PSR–12-226146.svg)](https://www.php-fig.org/psr/psr-12/) [![PHPUnit ](.github/coverage.svg)](https://brianhenryie.github.io/bh-php-monero-explorer/) [![PHPStan ](https://img.shields.io/badge/PHPStan-Level%208-2a5ea7.svg)](https://phpstan.org/) + ## PHP Bitcoin Address A simple P2PK, P2PKH, P2SH, P2WPKH, P2WSH output script/address parser/generator. From 5550724f60507ff90772cc6b5d86554c04e4ad41 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Wed, 15 Nov 2023 10:31:51 -0800 Subject: [PATCH 15/32] Move packages to `require-dev` --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 053ae2a..86bbbad 100644 --- a/composer.json +++ b/composer.json @@ -16,13 +16,13 @@ "require": { "bitwasp/bech32": "^0.0.1", "btccom/cashaddress": "^0.0.3", - "dealerdirect/phpcodesniffer-composer-installer": "*", - "squizlabs/php_codesniffer": "*", "stephenhill/base58": "^1.1" }, "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "*", "phpstan/phpstan": "^1.10", - "phpunit/phpunit": "^5.7" + "phpunit/phpunit": "^5.7", + "squizlabs/php_codesniffer": "*" }, "autoload": { "psr-4": { From c1945ead2dff99c828fe0d7cba7403cd03f45e8f Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 16 May 2023 19:41:41 -0700 Subject: [PATCH 16/32] Add PHPCS --- composer.json | 16 ++++++++++++++++ phpcs.xml | 11 +++++++++++ 2 files changed, 27 insertions(+) create mode 100644 phpcs.xml diff --git a/composer.json b/composer.json index 6d2a827..514f4b2 100644 --- a/composer.json +++ b/composer.json @@ -33,5 +33,21 @@ "psr-4": { "AndKom\\Bitcoin\\Address\\Tests\\": "tests/" } + }, + "config": { + "sort-packages": true, + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } + }, + "scripts": { + "test": [ + "phpunit" + ], + "lint": [ + "phpcbf || true", + "phpcs || true", + "phpstan" + ] } } diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..c40d9c0 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,11 @@ + + + + PHPCS configuration file. + + src + tests + + + + \ No newline at end of file From 232ddb8a36673e24c93d7d7386a5a96fe5917514 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 16 May 2023 19:57:25 -0700 Subject: [PATCH 17/32] Add PhpStan --- composer.json | 1 + phpstan.neon | 8 ++++++++ 2 files changed, 9 insertions(+) create mode 100644 phpstan.neon diff --git a/composer.json b/composer.json index 514f4b2..a1a9cc6 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ "shanecurran/phpecc": "^0.0.1" }, "require-dev": { + "phpstan/phpstan": "^1.10", "phpunit/phpunit": ">=5.7" }, "autoload": { diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..fc5530e --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,8 @@ +parameters: + level: 8 + paths: + - src + - tests + excludePaths: + - tests/reports + treatPhpDocTypesAsCertain: false \ No newline at end of file From c15714df5832dc18e10198ee92df24c9dbc7fb20 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 16 May 2023 21:41:16 -0700 Subject: [PATCH 18/32] Add PHP code coverage exclusions to linting --- .gitignore | 3 ++- phpcs.xml | 3 +++ phpstan.neon | 2 +- phpunit.xml.dist | 5 +++++ 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 2c750c4..34e851f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea .phpunit.* /vendor -composer.lock \ No newline at end of file +composer.lock +tests/_reports/ \ No newline at end of file diff --git a/phpcs.xml b/phpcs.xml index c40d9c0..f797aba 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -8,4 +8,7 @@ + tests/_reports + tests/_data + \ No newline at end of file diff --git a/phpstan.neon b/phpstan.neon index fc5530e..93b2e48 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,5 +4,5 @@ parameters: - src - tests excludePaths: - - tests/reports + - tests/_reports treatPhpDocTypesAsCertain: false \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ff0ea7c..a3cd1ab 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -11,4 +11,9 @@ ./src + + + ./src + + From bea7c6e11e9d4a31dbb2de6e47d35375c2560910 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 16 May 2023 21:45:16 -0700 Subject: [PATCH 19/32] Add README header, coverage badge, coverage workflow --- .github/coverage.svg | 21 ++++++++ .github/workflows/codecoverage.yml | 82 ++++++++++++++++++++++++++++++ README.md | 2 + 3 files changed, 105 insertions(+) create mode 100644 .github/coverage.svg create mode 100644 .github/workflows/codecoverage.yml diff --git a/.github/coverage.svg b/.github/coverage.svg new file mode 100644 index 0000000..00a08aa --- /dev/null +++ b/.github/coverage.svg @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + PHPUnit + PHPUnit + 83% + 83% + + \ No newline at end of file diff --git a/.github/workflows/codecoverage.yml b/.github/workflows/codecoverage.yml new file mode 100644 index 0000000..0b8676f --- /dev/null +++ b/.github/workflows/codecoverage.yml @@ -0,0 +1,82 @@ +name: Code Coverage + +# Runs PHPUnit with code coverage enabled, commits the html report to +# GitHub Pages, generates a README badge with the coverage percentage. +# +# Requires a gh-pages branch already created. +# +# git checkout --orphan gh-pages +# touch index.html +# git add index.html +# git commit -m 'Set up gh-pages branch' index.html +# git push origin gh-pages +# git checkout master +# +# @author BrianHenryIE + +on: + push: + branches: + - master + +jobs: + tests: + runs-on: ubuntu-latest + + steps: + + - name: Git checkout + uses: actions/checkout@v2 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '7.4' + coverage: xdebug + +# - name: Checkout GitHub Pages branch for code coverage report +# uses: actions/checkout@v2 +# with: +# ref: gh-pages +# path: tests/_reports/html + + - name: Install dependencies + run: composer install --prefer-dist --no-suggest --no-progress + +# - name: Clear previous code coverage +# run: | +# cd tests/_reports/html +# rm -rf * +# cd ../../.. + + - name: Run tests with code coverage + run: XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text --coverage-clover tests/_reports/clover.xml --coverage-html tests/_reports/html + +# - name: Edit phpcov html output to work with gh-pages +# run: | +# cd tests/_reports/html +# mv _css css; find . -depth -name '*.html' -exec sed -i "s/_css\//css\//" {} + +# mv _icons icons; find . -depth -name '*.html' -exec sed -i "s/_icons\//icons\//" {} + +# mv _js js; find . -depth -name '*.html' -exec sed -i "s/_js\//js\//" {} + +# git add * +# cd ../../.. + +# - name: Commit code coverage to gh-pages +# uses: stefanzweifel/git-auto-commit-action@v4.1.1 +# with: +# repository: tests/_reports/html +# branch: gh-pages +# commit_message: "🤖 Commit code coverage to gh-pages" +# commit_options: +# env: +# GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" + + - name: Update README badge + run: vendor/bin/php-coverage-badger tests/_reports/clover.xml .github/coverage.svg + + - name: Commit code coverage badge + uses: stefanzweifel/git-auto-commit-action@v4.1.1 + with: + commit_message: "🤖 Commit code coverage badge" + + diff --git a/README.md b/README.md index 907e245..4179355 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![PHP 7.4](https://img.shields.io/badge/PHP-7.4-8892BF.svg)]() [![PHPCS PSR-12](https://img.shields.io/badge/PHPCS-PSR–12-226146.svg)](https://www.php-fig.org/psr/psr-12/) [![PHPUnit ](.github/coverage.svg)](https://brianhenryie.github.io/bh-php-monero-explorer/) [![PHPStan ](https://img.shields.io/badge/PHPStan-Level%208-2a5ea7.svg)](https://phpstan.org/) + ## PHP Bitcoin Address A simple P2PK, P2PKH, P2SH, P2WPKH, P2WSH, P2TR output script/address parser/generator/validator. From 31b610d43a76cd4a0daa380d7a035d43935d0c76 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Wed, 15 Nov 2023 10:31:51 -0800 Subject: [PATCH 20/32] Move packages to `require-dev` --- composer.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a1a9cc6..2862cdf 100644 --- a/composer.json +++ b/composer.json @@ -22,8 +22,10 @@ "shanecurran/phpecc": "^0.0.1" }, "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "*", "phpstan/phpstan": "^1.10", - "phpunit/phpunit": ">=5.7" + "phpunit/phpunit": ">=5.7", + "squizlabs/php_codesniffer": "*" }, "autoload": { "psr-4": { From 4ad0b268054122ecf66fda1f007e88c6f9a449cc Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Wed, 15 Nov 2023 10:41:33 -0800 Subject: [PATCH 21/32] `phpcbf` autofix --- src/Ecc/Point.php | 12 +++++++---- src/Exception.php | 2 +- src/Network/NetworkFactory.php | 12 +++++------ src/Network/NetworkInterface.php | 2 +- src/Network/Networks/Bitcoin.php | 6 +++--- src/Network/Networks/BitcoinAbstract.php | 14 ++++++------ src/Network/Networks/BitcoinCash.php | 8 ++++--- src/Network/Networks/BitcoinGold.php | 4 ++-- src/Network/Networks/BitcoinTestnet.php | 2 +- src/Network/Networks/Dash.php | 2 +- src/Network/Networks/DashTestnet.php | 2 +- src/Network/Networks/Dogecoin.php | 2 +- src/Network/Networks/DogecoinTestnet.php | 2 +- src/Network/Networks/Litecoin.php | 2 +- src/Network/Networks/LitecoinTestnet.php | 2 +- src/Network/Networks/Viacoin.php | 4 ++-- src/Network/Networks/ViacoinTestnet.php | 2 +- src/Network/Networks/Zcash.php | 2 +- src/Output/AbstractOutput.php | 4 ++-- src/Output/Op.php | 2 +- src/Output/OutputFactory.php | 22 +++++++++---------- src/Output/OutputInterface.php | 8 +++---- src/Output/Outputs/P2ms.php | 12 ++++++----- src/Output/Outputs/P2pk.php | 12 ++++++----- src/Output/Outputs/P2pkh.php | 9 ++++---- src/Output/Outputs/P2sh.php | 12 ++++++----- src/Output/Outputs/P2tr.php | 12 ++++++----- src/Output/Outputs/P2wpkh.php | 12 ++++++----- src/Output/Outputs/P2wsh.php | 12 ++++++----- src/Taproot.php | 2 +- src/Utils.php | 16 +++++++------- src/Validate.php | 10 ++++----- tests/BitcoinCashTest.php | 2 +- tests/BitcoinTest.php | 27 ++++++++++++++---------- tests/OutputFactoryTest.php | 2 +- tests/P2msTest.php | 2 +- tests/P2pkTest.php | 2 +- tests/P2pkhTest.php | 2 +- tests/P2shTest.php | 2 +- tests/P2trTest.php | 2 +- tests/P2wpkhTest.php | 2 +- tests/P2wshTest.php | 2 +- tests/TaprootTest.php | 2 +- 43 files changed, 150 insertions(+), 124 deletions(-) diff --git a/src/Ecc/Point.php b/src/Ecc/Point.php index eb11431..26a9d93 100644 --- a/src/Ecc/Point.php +++ b/src/Ecc/Point.php @@ -50,24 +50,28 @@ public function liftX(): self $curve->getPrime() ); - if ($adapter->cmp( + if ( + $adapter->cmp( $c, $adapter->powmod( $y, gmp_init(2, 10), $curve->getPrime() ) - ) !== 0) { + ) !== 0 + ) { throw new Exception('C is not equal to point Y^2.'); } - if ($adapter->cmp( + if ( + $adapter->cmp( $adapter->mod( $y, gmp_init(2, 10) ), gmp_init(0, 10) - ) === 0) { + ) === 0 + ) { $point = new Point($adapter, $curve, $x, $y); } else { $point = new Point($adapter, $curve, $x, $adapter->sub($curve->getPrime(), $y)); diff --git a/src/Exception.php b/src/Exception.php index 34b30d5..2f5be19 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -10,4 +10,4 @@ */ class Exception extends \Exception { -} \ No newline at end of file +} diff --git a/src/Network/NetworkFactory.php b/src/Network/NetworkFactory.php index e35582b..0180889 100644 --- a/src/Network/NetworkFactory.php +++ b/src/Network/NetworkFactory.php @@ -29,7 +29,7 @@ class NetworkFactory /** * @var array */ - static protected $networks = [ + protected static $networks = [ 'bitcoin', 'bitcoinTestnet', 'bitcoinCash', @@ -49,12 +49,12 @@ class NetworkFactory /** * @var NetworkInterface */ - static protected $defaultNetwork; + protected static $defaultNetwork; /** * @param NetworkInterface $network */ - static public function setDefaultNetwork(NetworkInterface $network) + public static function setDefaultNetwork(NetworkInterface $network) { static::$defaultNetwork = $network; } @@ -62,7 +62,7 @@ static public function setDefaultNetwork(NetworkInterface $network) /** * @return NetworkInterface */ - static public function getDefaultNetwork(): NetworkInterface + public static function getDefaultNetwork(): NetworkInterface { return static::$defaultNetwork ?: static::bitcoin(); } @@ -102,6 +102,6 @@ public function __call($name, $arguments) */ public static function __callStatic($name, $arguments) { - return (new static)->createNetwork($name, $arguments); + return (new static())->createNetwork($name, $arguments); } -} \ No newline at end of file +} diff --git a/src/Network/NetworkInterface.php b/src/Network/NetworkInterface.php index 8f6707f..975e7f6 100644 --- a/src/Network/NetworkInterface.php +++ b/src/Network/NetworkInterface.php @@ -53,4 +53,4 @@ public function decodeAddress(string $address): OutputInterface; * @return bool */ public function validateAddress(string $address): bool; -} \ No newline at end of file +} diff --git a/src/Network/Networks/Bitcoin.php b/src/Network/Networks/Bitcoin.php index 8a556a2..b22a106 100644 --- a/src/Network/Networks/Bitcoin.php +++ b/src/Network/Networks/Bitcoin.php @@ -11,12 +11,12 @@ class Bitcoin extends BitcoinAbstract { /** - * @var bool + * @var bool */ protected $hasSegwit = true; /** - * @var bool + * @var bool */ protected $hasTaproot = true; -} \ No newline at end of file +} diff --git a/src/Network/Networks/BitcoinAbstract.php b/src/Network/Networks/BitcoinAbstract.php index 755ad60..7e522e0 100644 --- a/src/Network/Networks/BitcoinAbstract.php +++ b/src/Network/Networks/BitcoinAbstract.php @@ -15,8 +15,10 @@ use AndKom\Bitcoin\Address\Utils; use AndKom\Bitcoin\Address\Validate; use BrooksYang\Bech32m\Exception\Bech32mException; + use function BrooksYang\Bech32m\decodeSegwit; use function BrooksYang\Bech32m\encodeSegwit; + use const BrooksYang\Bech32m\BECH32; use const BrooksYang\Bech32m\BECH32M; @@ -28,7 +30,7 @@ abstract class BitcoinAbstract implements NetworkInterface { const VERSION_SEGWIT = 0; const VERSION_TAPROOT = 1; - + /** * @var string */ @@ -45,12 +47,12 @@ abstract class BitcoinAbstract implements NetworkInterface protected $prefixBech32 = 'bc'; /** - * @var bool + * @var bool */ protected $hasSegwit = false; /** - * @var bool + * @var bool */ protected $hasTaproot = false; @@ -85,7 +87,7 @@ public function getAddressP2wpkh(string $pubKeyHash): string if (!$this->hasSegwit) { throw new Exception('Segwit is not supported.'); } - + return encodeSegwit($this->prefixBech32, 0, $pubKeyHash, BECH32); } @@ -149,7 +151,7 @@ public function decodeAddress(string $address): OutputInterface if ($this->hasTaproot && 0 === strpos($address, $this->prefixBech32)) { try { list($version, $hash) = decodeSegwit($this->prefixBech32, $address, BECH32M); - + if ($version === static::VERSION_TAPROOT) { return new P2tr($hash); } @@ -185,4 +187,4 @@ public function validateAddress(string $address): bool return false; } } -} \ No newline at end of file +} diff --git a/src/Network/Networks/BitcoinCash.php b/src/Network/Networks/BitcoinCash.php index faf68ca..639c515 100644 --- a/src/Network/Networks/BitcoinCash.php +++ b/src/Network/Networks/BitcoinCash.php @@ -59,8 +59,10 @@ public function getAddressP2sh(string $scriptHash): string */ public function decodeAddress(string $address): OutputInterface { - if (strpos($address, $this->prefixP2pkh) !== 0 || - strpos($address, $this->prefixP2sh) !== 0) { + if ( + strpos($address, $this->prefixP2pkh) !== 0 || + strpos($address, $this->prefixP2sh) !== 0 + ) { throw new Exception('Cannot decode address.'); } @@ -72,4 +74,4 @@ public function decodeAddress(string $address): OutputInterface return new P2sh($hash); } } -} \ No newline at end of file +} diff --git a/src/Network/Networks/BitcoinGold.php b/src/Network/Networks/BitcoinGold.php index a282e09..c224e42 100644 --- a/src/Network/Networks/BitcoinGold.php +++ b/src/Network/Networks/BitcoinGold.php @@ -26,7 +26,7 @@ class BitcoinGold extends Bitcoin protected $prefixBech32 = 'btg'; /** - * @var bool + * @var bool */ protected $hasTaproot = false; -} \ No newline at end of file +} diff --git a/src/Network/Networks/BitcoinTestnet.php b/src/Network/Networks/BitcoinTestnet.php index 206eb18..aee67fd 100644 --- a/src/Network/Networks/BitcoinTestnet.php +++ b/src/Network/Networks/BitcoinTestnet.php @@ -24,4 +24,4 @@ class BitcoinTestnet extends Bitcoin * @var string */ protected $prefixBech32 = 'tb'; -} \ No newline at end of file +} diff --git a/src/Network/Networks/Dash.php b/src/Network/Networks/Dash.php index c88fe38..c4379bb 100644 --- a/src/Network/Networks/Dash.php +++ b/src/Network/Networks/Dash.php @@ -19,4 +19,4 @@ class Dash extends BitcoinAbstract * @var string */ protected $prefixP2sh = "\x10"; -} \ No newline at end of file +} diff --git a/src/Network/Networks/DashTestnet.php b/src/Network/Networks/DashTestnet.php index 2d1a88f..7388931 100644 --- a/src/Network/Networks/DashTestnet.php +++ b/src/Network/Networks/DashTestnet.php @@ -19,4 +19,4 @@ class DashTestnet extends Dash * @var string */ protected $prefixP2sh = "\x13"; -} \ No newline at end of file +} diff --git a/src/Network/Networks/Dogecoin.php b/src/Network/Networks/Dogecoin.php index 3479996..23b8289 100644 --- a/src/Network/Networks/Dogecoin.php +++ b/src/Network/Networks/Dogecoin.php @@ -19,4 +19,4 @@ class Dogecoin extends BitcoinAbstract * @var string */ protected $prefixP2sh = "\x16"; -} \ No newline at end of file +} diff --git a/src/Network/Networks/DogecoinTestnet.php b/src/Network/Networks/DogecoinTestnet.php index a428edb..26f9caf 100644 --- a/src/Network/Networks/DogecoinTestnet.php +++ b/src/Network/Networks/DogecoinTestnet.php @@ -19,4 +19,4 @@ class DogecoinTestnet extends Dogecoin * @var string */ protected $prefixP2sh = "\xc4"; -} \ No newline at end of file +} diff --git a/src/Network/Networks/Litecoin.php b/src/Network/Networks/Litecoin.php index 4c11212..a7e01d7 100644 --- a/src/Network/Networks/Litecoin.php +++ b/src/Network/Networks/Litecoin.php @@ -24,4 +24,4 @@ class Litecoin extends Bitcoin * @var string */ protected $prefixBech32 = 'ltc'; -} \ No newline at end of file +} diff --git a/src/Network/Networks/LitecoinTestnet.php b/src/Network/Networks/LitecoinTestnet.php index 080516b..bc9d6f8 100644 --- a/src/Network/Networks/LitecoinTestnet.php +++ b/src/Network/Networks/LitecoinTestnet.php @@ -24,4 +24,4 @@ class LitecoinTestnet extends Litecoin * @var string */ protected $prefixBech32 = 'tltc'; -} \ No newline at end of file +} diff --git a/src/Network/Networks/Viacoin.php b/src/Network/Networks/Viacoin.php index 368599e..d519c13 100644 --- a/src/Network/Networks/Viacoin.php +++ b/src/Network/Networks/Viacoin.php @@ -26,7 +26,7 @@ class Viacoin extends Bitcoin protected $prefixBech32 = 'via'; /** - * @var bool + * @var bool */ protected $hasTaproot = false; -} \ No newline at end of file +} diff --git a/src/Network/Networks/ViacoinTestnet.php b/src/Network/Networks/ViacoinTestnet.php index 5a83d02..9aa687d 100644 --- a/src/Network/Networks/ViacoinTestnet.php +++ b/src/Network/Networks/ViacoinTestnet.php @@ -24,4 +24,4 @@ class ViacoinTestnet extends Viacoin * @var string */ protected $prefixBech32 = 'tvia'; -} \ No newline at end of file +} diff --git a/src/Network/Networks/Zcash.php b/src/Network/Networks/Zcash.php index 20bdb4d..2723ae8 100644 --- a/src/Network/Networks/Zcash.php +++ b/src/Network/Networks/Zcash.php @@ -19,4 +19,4 @@ class Zcash extends BitcoinAbstract * @var string */ protected $prefixP2sh = "\x1c\xbd"; -} \ No newline at end of file +} diff --git a/src/Output/AbstractOutput.php b/src/Output/AbstractOutput.php index 8a3f01c..972a459 100644 --- a/src/Output/AbstractOutput.php +++ b/src/Output/AbstractOutput.php @@ -53,7 +53,7 @@ protected function network(NetworkInterface $network = null): NetworkInterface * @return OutputInterface * @throws Exception */ - static public function fromHex(string $hex): OutputInterface + public static function fromHex(string $hex): OutputInterface { $script = hex2bin($hex); @@ -63,4 +63,4 @@ static public function fromHex(string $hex): OutputInterface return static::fromScript($script); } -} \ No newline at end of file +} diff --git a/src/Output/Op.php b/src/Output/Op.php index f5cdf1f..28a1f16 100644 --- a/src/Output/Op.php +++ b/src/Output/Op.php @@ -16,4 +16,4 @@ interface Op const HASH160 = "\xa9"; const CHECKSIG = "\xac"; const CHECKMULTISIG = "\xae"; -} \ No newline at end of file +} diff --git a/src/Output/OutputFactory.php b/src/Output/OutputFactory.php index fdc9589..2414453 100644 --- a/src/Output/OutputFactory.php +++ b/src/Output/OutputFactory.php @@ -25,7 +25,7 @@ class OutputFactory * @return OutputInterface * @throws Exception */ - static public function p2pk(string $pubKey): OutputInterface + public static function p2pk(string $pubKey): OutputInterface { return new P2pk($pubKey); } @@ -35,7 +35,7 @@ static public function p2pk(string $pubKey): OutputInterface * @return OutputInterface * @throws Exception */ - static public function p2pkh(string $pubKeyHash): OutputInterface + public static function p2pkh(string $pubKeyHash): OutputInterface { return new P2pkh($pubKeyHash); } @@ -46,7 +46,7 @@ static public function p2pkh(string $pubKeyHash): OutputInterface * @return OutputInterface * @throws Exception */ - static public function p2ms(int $m, array $pubKeys): OutputInterface + public static function p2ms(int $m, array $pubKeys): OutputInterface { return new P2ms($m, $pubKeys); } @@ -56,7 +56,7 @@ static public function p2ms(int $m, array $pubKeys): OutputInterface * @return OutputInterface * @throws Exception */ - static public function p2sh(OutputInterface $output): OutputInterface + public static function p2sh(OutputInterface $output): OutputInterface { return new P2sh($output); } @@ -66,7 +66,7 @@ static public function p2sh(OutputInterface $output): OutputInterface * @return OutputInterface * @throws Exception */ - static public function p2wpkh(string $pubKeyHash): OutputInterface + public static function p2wpkh(string $pubKeyHash): OutputInterface { return new P2wpkh($pubKeyHash); } @@ -76,7 +76,7 @@ static public function p2wpkh(string $pubKeyHash): OutputInterface * @return OutputInterface * @throws Exception */ - static public function p2wsh(OutputInterface $output): OutputInterface + public static function p2wsh(OutputInterface $output): OutputInterface { return new P2wsh($output); } @@ -85,7 +85,7 @@ static public function p2wsh(OutputInterface $output): OutputInterface * @param string $taprootPubKey * @return OutputInterface */ - static public function p2tr(string $taprootPubKey): OutputInterface + public static function p2tr(string $taprootPubKey): OutputInterface { return new P2tr($taprootPubKey); } @@ -95,7 +95,7 @@ static public function p2tr(string $taprootPubKey): OutputInterface * @return OutputInterface * @throws Exception */ - static public function fromScript(string $script): OutputInterface + public static function fromScript(string $script): OutputInterface { $map = [ P2pk::COMPRESSED_SCRIPT_LEN => P2pk::class, @@ -114,7 +114,7 @@ static public function fromScript(string $script): OutputInterface $class = P2wpkh::class; } elseif (P2wsh::SCRIPT_LEN === $scriptLen && P2wsh::WITNESS_VERSION === $script[0]) { $class = P2wsh::class; - } else if (P2wsh::SCRIPT_LEN === $scriptLen && P2tr::WITNESS_VERSION === $script[0]) { + } elseif (P2wsh::SCRIPT_LEN === $scriptLen && P2tr::WITNESS_VERSION === $script[0]) { $class = P2tr::class; } else { throw new Exception('Unknown script type.'); @@ -128,8 +128,8 @@ static public function fromScript(string $script): OutputInterface * @return OutputInterface * @throws Exception */ - static public function fromHex(string $hex): OutputInterface + public static function fromHex(string $hex): OutputInterface { return static::fromScript(Utils::hex2bin($hex)); } -} \ No newline at end of file +} diff --git a/src/Output/OutputInterface.php b/src/Output/OutputInterface.php index 22e1291..3ee5e99 100644 --- a/src/Output/OutputInterface.php +++ b/src/Output/OutputInterface.php @@ -54,17 +54,17 @@ public function address(NetworkInterface $network = null): string; * @throws Exception * @return void */ - static public function validateScript(string $script); + public static function validateScript(string $script); /** * @param string $script * @return OutputInterface */ - static public function fromScript(string $script): OutputInterface; + public static function fromScript(string $script): OutputInterface; /** * @param string $hex * @return OutputInterface */ - static public function fromHex(string $hex): OutputInterface; -} \ No newline at end of file + public static function fromHex(string $hex): OutputInterface; +} diff --git a/src/Output/Outputs/P2ms.php b/src/Output/Outputs/P2ms.php index 6146f91..358edd0 100644 --- a/src/Output/Outputs/P2ms.php +++ b/src/Output/Outputs/P2ms.php @@ -127,7 +127,7 @@ public function address(NetworkInterface $network = null): string * @param string $script * @throws Exception */ - static public function validateScript(string $script) + public static function validateScript(string $script) { $scriptLen = strlen($script); @@ -145,8 +145,10 @@ static public function validateScript(string $script) for ($i = 1, $c = 0; $i < $scriptLen - 2; $c++) { $pubKeyLen = ord($script[$i]); - if (Validate::COMPRESSED_PUBKEY_LEN != $pubKeyLen && - Validate::UNCOMPRESSED_PUBKEY_LEN != $pubKeyLen) { + if ( + Validate::COMPRESSED_PUBKEY_LEN != $pubKeyLen && + Validate::UNCOMPRESSED_PUBKEY_LEN != $pubKeyLen + ) { throw new Exception('Invalid pubkey length.'); } @@ -163,7 +165,7 @@ static public function validateScript(string $script) * @return OutputInterface * @throws Exception */ - static public function fromScript(string $script): OutputInterface + public static function fromScript(string $script): OutputInterface { static::validateScript($script); @@ -179,4 +181,4 @@ static public function fromScript(string $script): OutputInterface return new static($m, $pubKeys); } -} \ No newline at end of file +} diff --git a/src/Output/Outputs/P2pk.php b/src/Output/Outputs/P2pk.php index 1cddc8f..7dff9fd 100644 --- a/src/Output/Outputs/P2pk.php +++ b/src/Output/Outputs/P2pk.php @@ -82,12 +82,14 @@ public function address(NetworkInterface $network = null): string * @param string $script * @throws Exception */ - static public function validateScript(string $script) + public static function validateScript(string $script) { $scriptLen = strlen($script); - if (static::COMPRESSED_SCRIPT_LEN != $scriptLen && - static::UNCOMPRESSED_SCRIPT_LEN != $scriptLen) { + if ( + static::COMPRESSED_SCRIPT_LEN != $scriptLen && + static::UNCOMPRESSED_SCRIPT_LEN != $scriptLen + ) { throw new Exception('Invalid P2PK script length.'); } @@ -101,7 +103,7 @@ static public function validateScript(string $script) * @return OutputInterface * @throws Exception */ - static public function fromScript(string $script): OutputInterface + public static function fromScript(string $script): OutputInterface { static::validateScript($script); @@ -109,4 +111,4 @@ static public function fromScript(string $script): OutputInterface return new static($pubKey); } -} \ No newline at end of file +} diff --git a/src/Output/Outputs/P2pkh.php b/src/Output/Outputs/P2pkh.php index 8edeb31..58ea93e 100644 --- a/src/Output/Outputs/P2pkh.php +++ b/src/Output/Outputs/P2pkh.php @@ -80,13 +80,14 @@ public function address(NetworkInterface $network = null): string * @param string $script * @throws Exception */ - static public function validateScript(string $script) + public static function validateScript(string $script) { if (static::SCRIPT_LEN != strlen($script)) { throw new Exception('Invalid P2PKH script length.'); } - if (Op::DUP != $script[0] || + if ( + Op::DUP != $script[0] || Op::HASH160 != $script[1] || "\x14" != $script[2] || Op::EQUALVERIFY != $script[-2] || @@ -101,7 +102,7 @@ static public function validateScript(string $script) * @return OutputInterface * @throws Exception */ - static public function fromScript(string $script): OutputInterface + public static function fromScript(string $script): OutputInterface { static::validateScript($script); @@ -109,4 +110,4 @@ static public function fromScript(string $script): OutputInterface return new static($pubKeyHash); } -} \ No newline at end of file +} diff --git a/src/Output/Outputs/P2sh.php b/src/Output/Outputs/P2sh.php index f2ccec6..02c7cc0 100644 --- a/src/Output/Outputs/P2sh.php +++ b/src/Output/Outputs/P2sh.php @@ -85,15 +85,17 @@ public function address(NetworkInterface $network = null): string * @param string $script * @throws Exception */ - static public function validateScript(string $script) + public static function validateScript(string $script) { if (static::SCRIPT_LEN != strlen($script)) { throw new Exception('Invalid P2SH script length.'); } - if (Op::HASH160 != $script[0] || + if ( + Op::HASH160 != $script[0] || "\x14" != $script[1] || - Op::EQUAL != $script[-1]) { + Op::EQUAL != $script[-1] + ) { throw new Exception('Invalid P2SH script format.'); } } @@ -103,7 +105,7 @@ static public function validateScript(string $script) * @return OutputInterface * @throws Exception */ - static public function fromScript(string $script): OutputInterface + public static function fromScript(string $script): OutputInterface { static::validateScript($script); @@ -111,4 +113,4 @@ static public function fromScript(string $script): OutputInterface return new static($scriptHash); } -} \ No newline at end of file +} diff --git a/src/Output/Outputs/P2tr.php b/src/Output/Outputs/P2tr.php index 8350567..a32ae4e 100644 --- a/src/Output/Outputs/P2tr.php +++ b/src/Output/Outputs/P2tr.php @@ -78,14 +78,16 @@ public function address(NetworkInterface $network = null): string * @param string $script * @throws Exception */ - static public function validateScript(string $script) + public static function validateScript(string $script) { if (static::SCRIPT_LEN != strlen($script)) { throw new Exception('Invalid P2TR script length.'); } - if (static::WITNESS_VERSION != $script[0] || - "\x20" != $script[1]) { + if ( + static::WITNESS_VERSION != $script[0] || + "\x20" != $script[1] + ) { throw new Exception('Invalid P2TR script format.'); } } @@ -95,7 +97,7 @@ static public function validateScript(string $script) * @return OutputInterface * @throws Exception */ - static public function fromScript(string $script): OutputInterface + public static function fromScript(string $script): OutputInterface { static::validateScript($script); @@ -103,4 +105,4 @@ static public function fromScript(string $script): OutputInterface return new static($taprootPubKey); } -} \ No newline at end of file +} diff --git a/src/Output/Outputs/P2wpkh.php b/src/Output/Outputs/P2wpkh.php index 95a9ac4..ca10eb7 100644 --- a/src/Output/Outputs/P2wpkh.php +++ b/src/Output/Outputs/P2wpkh.php @@ -80,14 +80,16 @@ public function address(NetworkInterface $network = null): string * @param string $script * @throws Exception */ - static public function validateScript(string $script) + public static function validateScript(string $script) { if (static::SCRIPT_LEN != strlen($script)) { throw new Exception('Invalid P2WPKH script length.'); } - if (static::WITNESS_VERSION != $script[0] || - "\x14" != $script[1]) { + if ( + static::WITNESS_VERSION != $script[0] || + "\x14" != $script[1] + ) { throw new Exception('Invalid P2WPKH script format.'); } } @@ -97,7 +99,7 @@ static public function validateScript(string $script) * @return OutputInterface * @throws Exception */ - static public function fromScript(string $script): OutputInterface + public static function fromScript(string $script): OutputInterface { static::validateScript($script); @@ -105,4 +107,4 @@ static public function fromScript(string $script): OutputInterface return new static($pubKeyHash); } -} \ No newline at end of file +} diff --git a/src/Output/Outputs/P2wsh.php b/src/Output/Outputs/P2wsh.php index 1b0323d..84e2c6b 100644 --- a/src/Output/Outputs/P2wsh.php +++ b/src/Output/Outputs/P2wsh.php @@ -84,14 +84,16 @@ public function address(NetworkInterface $network = null): string * @param string $script * @throws Exception */ - static public function validateScript(string $script) + public static function validateScript(string $script) { if (static::SCRIPT_LEN != strlen($script)) { throw new Exception('Invalid P2WSH script length.'); } - if (static::WITNESS_VERSION != $script[0] || - "\x20" != $script[1]) { + if ( + static::WITNESS_VERSION != $script[0] || + "\x20" != $script[1] + ) { throw new Exception('Invalid P2WSH script format.'); } } @@ -101,7 +103,7 @@ static public function validateScript(string $script) * @return OutputInterface * @throws Exception */ - static public function fromScript(string $script): OutputInterface + public static function fromScript(string $script): OutputInterface { static::validateScript($script); @@ -109,4 +111,4 @@ static public function fromScript(string $script): OutputInterface return new static($witnessHash); } -} \ No newline at end of file +} diff --git a/src/Taproot.php b/src/Taproot.php index 506349b..5b9549f 100644 --- a/src/Taproot.php +++ b/src/Taproot.php @@ -20,7 +20,7 @@ class Taproot * @return PointInterface * @throws Exception */ - static public function construct(string $pubKey, string $merkleRoot = ''): string + public static function construct(string $pubKey, string $merkleRoot = ''): string { $generator = EccFactory::getSecgCurves()->generator256k1(); $adapter = $generator->getAdapter(); diff --git a/src/Utils.php b/src/Utils.php index a6112f0..6d024f7 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -16,7 +16,7 @@ class Utils * @param string $data * @return string */ - static public function sha256(string $data): string + public static function sha256(string $data): string { return hash('sha256', $data, true); } @@ -25,7 +25,7 @@ static public function sha256(string $data): string * @param string $data * @return string */ - static public function hash256(string $data): string + public static function hash256(string $data): string { return static::sha256(static::sha256($data)); } @@ -34,7 +34,7 @@ static public function hash256(string $data): string * @param string $data * @return string */ - static public function hash160(string $data): string + public static function hash160(string $data): string { return hash('ripemd160', static::sha256($data), true); } @@ -44,7 +44,7 @@ static public function hash160(string $data): string * @return string * @throws Exception */ - static public function hex2bin(string $hex): string + public static function hex2bin(string $hex): string { $bin = @hex2bin($hex); @@ -61,7 +61,7 @@ static public function hex2bin(string $hex): string * @return string * @throws \Exception */ - static public function base58encode(string $hash, string $prefix = "\x00"): string + public static function base58encode(string $hash, string $prefix = "\x00"): string { $payload = $prefix . Validate::pubKeyHash($hash); $checksum = substr(static::hash256($payload), 0, 4); @@ -75,7 +75,7 @@ static public function base58encode(string $hash, string $prefix = "\x00"): stri * @return array * @throws \Exception */ - static public function base58decode(string $base58): array + public static function base58decode(string $base58): array { $address = (new Base58())->decode($base58); $addressLen = strlen($address); @@ -103,9 +103,9 @@ static public function base58decode(string $base58): array * @param string $data * @return string */ - static public function taggedHash(string $tag, string $data): string + public static function taggedHash(string $tag, string $data): string { $tagHash = static::sha256($tag); return static::sha256($tagHash . $tagHash . $data); } -} \ No newline at end of file +} diff --git a/src/Validate.php b/src/Validate.php index 3574d35..1bf9ae6 100644 --- a/src/Validate.php +++ b/src/Validate.php @@ -23,7 +23,7 @@ class Validate * @return string * @throws Exception */ - static public function pubKey(string $pubKey): string + public static function pubKey(string $pubKey): string { $len = strlen($pubKey); @@ -48,7 +48,7 @@ static public function pubKey(string $pubKey): string * @return string * @throws Exception */ - static public function pubKeyHash(string $pubKeyHash): string + public static function pubKeyHash(string $pubKeyHash): string { if (static::PUBKEY_HASH_LEN != strlen($pubKeyHash)) { throw new Exception(sprintf('Invalid pubkey hash: %s.', bin2hex($pubKeyHash))); @@ -62,7 +62,7 @@ static public function pubKeyHash(string $pubKeyHash): string * @return string * @throws Exception */ - static public function scriptHash(string $scriptHash): string + public static function scriptHash(string $scriptHash): string { if (static::SCRIPT_HASH_LEN != strlen($scriptHash)) { throw new Exception(sprintf('Invalid script hash: %s.', bin2hex($scriptHash))); @@ -76,7 +76,7 @@ static public function scriptHash(string $scriptHash): string * @return string * @throws Exception */ - static public function witnessHash(string $witnessHash): string + public static function witnessHash(string $witnessHash): string { if (static::WITNESS_HASH_LEN != strlen($witnessHash)) { throw new Exception(sprintf('Invalid witness hash: %s.', bin2hex($witnessHash))); @@ -84,4 +84,4 @@ static public function witnessHash(string $witnessHash): string return $witnessHash; } -} \ No newline at end of file +} diff --git a/tests/BitcoinCashTest.php b/tests/BitcoinCashTest.php index 2ececb6..f02ef7a 100644 --- a/tests/BitcoinCashTest.php +++ b/tests/BitcoinCashTest.php @@ -24,4 +24,4 @@ public function testDecodeP2SH() { $this->assertInstanceOf(P2sh::class, NetworkFactory::bitcoinCash()->decodeAddress('bitcoincash:pzp7awma0x4p6wyw8v9vvkuc43vqcndqrg9umkmd8g')); } -} \ No newline at end of file +} diff --git a/tests/BitcoinTest.php b/tests/BitcoinTest.php index 70b8976..2b4007f 100644 --- a/tests/BitcoinTest.php +++ b/tests/BitcoinTest.php @@ -42,24 +42,29 @@ public function testDecodeP2TR() { $this->assertInstanceOf(P2tr::class, NetworkFactory::bitcoin()->decodeAddress('bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr')); } - - public function testValidateAddress() { + + public function testValidateAddress() + { $this->assertFalse(NetworkFactory::bitcoin()->validateAddress('some')); } - - public function testValidateAddressP2PKH() { + + public function testValidateAddressP2PKH() + { $this->assertTrue(NetworkFactory::bitcoin()->validateAddress('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH')); } - - public function testValidateAddressP2SH() { + + public function testValidateAddressP2SH() + { $this->assertTrue(NetworkFactory::bitcoin()->validateAddress('3DicS6C8JZm59RsrgXr56iVHzYdQngiehV')); } - - public function testValidateAddressP2WSH() { + + public function testValidateAddressP2WSH() + { $this->assertTrue(NetworkFactory::bitcoin()->validateAddress('bc1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8s4plngs')); } - - public function testValidateAddressP2TR() { + + public function testValidateAddressP2TR() + { $this->assertTrue(NetworkFactory::bitcoin()->validateAddress('bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr')); } -} \ No newline at end of file +} diff --git a/tests/OutputFactoryTest.php b/tests/OutputFactoryTest.php index aa9fbb6..d33ed1a 100644 --- a/tests/OutputFactoryTest.php +++ b/tests/OutputFactoryTest.php @@ -98,4 +98,4 @@ public function testFromHexP2TR() $this->assertEquals($output->getTaprootPubKey(), hex2bin('a60869f0dbcf1dc659c9cecbaf8050135ea9e8cdc487053f1dc6880949dc684c')); $this->assertEquals($output->type(), 'p2tr'); } -} \ No newline at end of file +} diff --git a/tests/P2msTest.php b/tests/P2msTest.php index f3222e0..3d33950 100644 --- a/tests/P2msTest.php +++ b/tests/P2msTest.php @@ -49,4 +49,4 @@ public function testFromScript() $output = $this->getOutput(); $this->assertEquals($output->script(), P2ms::fromScript($output->script())->script()); } -} \ No newline at end of file +} diff --git a/tests/P2pkTest.php b/tests/P2pkTest.php index 89e0526..896190b 100644 --- a/tests/P2pkTest.php +++ b/tests/P2pkTest.php @@ -48,4 +48,4 @@ public function testFromScript() $output = $this->getOutput(); $this->assertEquals($output->script(), P2pk::fromScript($output->script())->script()); } -} \ No newline at end of file +} diff --git a/tests/P2pkhTest.php b/tests/P2pkhTest.php index 1987118..0aff6d7 100644 --- a/tests/P2pkhTest.php +++ b/tests/P2pkhTest.php @@ -162,4 +162,4 @@ public function testFromScript() $output = $this->getOutput(); $this->assertEquals($output->script(), P2pkh::fromScript($output->script())->script()); } -} \ No newline at end of file +} diff --git a/tests/P2shTest.php b/tests/P2shTest.php index 243590d..fc6318e 100644 --- a/tests/P2shTest.php +++ b/tests/P2shTest.php @@ -164,4 +164,4 @@ public function testFromScript() $output = $this->getOutput(); $this->assertEquals($output->script(), P2sh::fromScript($output->script())->script()); } -} \ No newline at end of file +} diff --git a/tests/P2trTest.php b/tests/P2trTest.php index 83a79ba..bd62ba2 100644 --- a/tests/P2trTest.php +++ b/tests/P2trTest.php @@ -84,4 +84,4 @@ public function testFromScript() $output = $this->getOutput(); $this->assertEquals($output->script(), P2tr::fromScript($output->script())->script()); } -} \ No newline at end of file +} diff --git a/tests/P2wpkhTest.php b/tests/P2wpkhTest.php index 639b3ea..2b5d32d 100644 --- a/tests/P2wpkhTest.php +++ b/tests/P2wpkhTest.php @@ -98,4 +98,4 @@ public function testFromScript() $output = $this->getOutput(); $this->assertEquals($output->script(), P2wpkh::fromScript($output->script())->script()); } -} \ No newline at end of file +} diff --git a/tests/P2wshTest.php b/tests/P2wshTest.php index 2caf16d..9c7f1d4 100644 --- a/tests/P2wshTest.php +++ b/tests/P2wshTest.php @@ -100,4 +100,4 @@ public function testFromScript() $output = $this->getOutput(); $this->assertEquals($output->script(), P2wsh::fromScript($output->script())->script()); } -} \ No newline at end of file +} diff --git a/tests/TaprootTest.php b/tests/TaprootTest.php index a11460e..25d26aa 100644 --- a/tests/TaprootTest.php +++ b/tests/TaprootTest.php @@ -33,4 +33,4 @@ function testAddress() ); } } -} \ No newline at end of file +} From daa998ae4976d36d1b878c10dcdccc08aab425d1 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Wed, 15 Nov 2023 10:53:51 -0800 Subject: [PATCH 22/32] Fix phpcs "Visibility must be declared on all constants" --- src/Network/Networks/BitcoinAbstract.php | 4 ++-- src/Output/Outputs/P2tr.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Network/Networks/BitcoinAbstract.php b/src/Network/Networks/BitcoinAbstract.php index 7e522e0..8af2b8b 100644 --- a/src/Network/Networks/BitcoinAbstract.php +++ b/src/Network/Networks/BitcoinAbstract.php @@ -28,8 +28,8 @@ */ abstract class BitcoinAbstract implements NetworkInterface { - const VERSION_SEGWIT = 0; - const VERSION_TAPROOT = 1; + public const VERSION_SEGWIT = 0; + public const VERSION_TAPROOT = 1; /** * @var string diff --git a/src/Output/Outputs/P2tr.php b/src/Output/Outputs/P2tr.php index a32ae4e..6f3b0d3 100644 --- a/src/Output/Outputs/P2tr.php +++ b/src/Output/Outputs/P2tr.php @@ -16,8 +16,8 @@ */ class P2tr extends AbstractOutput { - const SCRIPT_LEN = 34; - const WITNESS_VERSION = "\x51"; + public const SCRIPT_LEN = 34; + public const WITNESS_VERSION = "\x51"; /** * @var string From bed661abbfd36dfd5da75258ccd11935d6705bf1 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Wed, 15 Nov 2023 10:57:15 -0800 Subject: [PATCH 23/32] Fix phpcs "Line exceeds 120 characters" --- tests/BitcoinTest.php | 19 ++++++++++++++++--- tests/OutputFactoryTest.php | 20 ++++++++++++++++---- tests/P2shTest.php | 5 ++++- tests/P2trTest.php | 34 +++++++++++++++++++++++++++------- 4 files changed, 63 insertions(+), 15 deletions(-) diff --git a/tests/BitcoinTest.php b/tests/BitcoinTest.php index e9ef56a..cbfe69f 100644 --- a/tests/BitcoinTest.php +++ b/tests/BitcoinTest.php @@ -52,7 +52,12 @@ public function testDecodeP2WSH(): void public function testDecodeP2TR() { - $this->assertInstanceOf(P2tr::class, NetworkFactory::bitcoin()->decodeAddress('bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr')); + $this->assertInstanceOf( + P2tr::class, + NetworkFactory::bitcoin()->decodeAddress( + 'bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr' + ) + ); } public function testValidateAddress() @@ -72,11 +77,19 @@ public function testValidateAddressP2SH() public function testValidateAddressP2WSH() { - $this->assertTrue(NetworkFactory::bitcoin()->validateAddress('bc1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8s4plngs')); + $this->assertTrue( + NetworkFactory::bitcoin()->validateAddress( + 'bc1q9qs9xv7mjghkd69fgx62xttxmeww5q7eekjxu0nxtzf4yu4ekf8s4plngs' + ) + ); } public function testValidateAddressP2TR() { - $this->assertTrue(NetworkFactory::bitcoin()->validateAddress('bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr')); + $this->assertTrue( + NetworkFactory::bitcoin()->validateAddress( + 'bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr' + ) + ); } } diff --git a/tests/OutputFactoryTest.php b/tests/OutputFactoryTest.php index 181eba0..837cc7e 100644 --- a/tests/OutputFactoryTest.php +++ b/tests/OutputFactoryTest.php @@ -28,7 +28,10 @@ public function testFromHexP2PK(): void { $output = OutputFactory::fromHex('210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ac'); $this->assertInstanceOf(P2pk::class, $output); - $this->assertEquals($output->getPubKey(), hex2bin('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798')); + $this->assertEquals( + $output->getPubKey(), + hex2bin('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798') + ); $this->assertEquals($output->type(), 'p2pk'); } @@ -50,7 +53,10 @@ public function testFromHexP2MS(): void { $output = OutputFactory::fromHex('51210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f8179851ae'); $this->assertInstanceOf(P2ms::class, $output); - $this->assertEquals($output->getPubKeys(), [hex2bin('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798')]); + $this->assertEquals( + $output->getPubKeys(), + [hex2bin('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798')] + ); $this->assertEquals($output->getSigCount(), 1); $this->assertEquals($output->type(), 'p2ms'); } @@ -84,7 +90,10 @@ public function testFromHexP2WSH(): void { $output = OutputFactory::fromHex('002028205333db922f66e8a941b4a32d66de5cea03d9cda46e3e6658935272b9b24f'); $this->assertInstanceOf(P2wsh::class, $output); - $this->assertEquals($output->getWitnessHash(), hex2bin('28205333db922f66e8a941b4a32d66de5cea03d9cda46e3e6658935272b9b24f')); + $this->assertEquals( + $output->getWitnessHash(), + hex2bin('28205333db922f66e8a941b4a32d66de5cea03d9cda46e3e6658935272b9b24f') + ); $this->assertEquals($output->type(), 'p2wsh'); } @@ -95,7 +104,10 @@ public function testFromHexP2TR() { $output = OutputFactory::fromHex('5120a60869f0dbcf1dc659c9cecbaf8050135ea9e8cdc487053f1dc6880949dc684c'); $this->assertInstanceOf(P2tr::class, $output); - $this->assertEquals($output->getTaprootPubKey(), hex2bin('a60869f0dbcf1dc659c9cecbaf8050135ea9e8cdc487053f1dc6880949dc684c')); + $this->assertEquals( + $output->getTaprootPubKey(), + hex2bin('a60869f0dbcf1dc659c9cecbaf8050135ea9e8cdc487053f1dc6880949dc684c') + ); $this->assertEquals($output->type(), 'p2tr'); } } diff --git a/tests/P2shTest.php b/tests/P2shTest.php index 2789789..10c542a 100644 --- a/tests/P2shTest.php +++ b/tests/P2shTest.php @@ -88,7 +88,10 @@ public function testAddressBitcoinCash(): void */ public function testAddressBitcoinGold(): void { - $this->assertEquals('AToUA3ZK5p6qsEPR85qopyPTKdGPaUNd9V', $this->getOutput()->address(NetworkFactory::bitcoinGold())); + $this->assertEquals( + 'AToUA3ZK5p6qsEPR85qopyPTKdGPaUNd9V', + $this->getOutput()->address(NetworkFactory::bitcoinGold()) + ); } /** diff --git a/tests/P2trTest.php b/tests/P2trTest.php index bd62ba2..c2b5254 100644 --- a/tests/P2trTest.php +++ b/tests/P2trTest.php @@ -24,7 +24,9 @@ class P2trTest extends TestCase */ protected function getOutput(): OutputInterface { - $taprootPubKey = Taproot::construct(hex2bin('03cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115')); + $taprootPubKey = Taproot::construct( + hex2bin('03cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115') + ); return OutputFactory::p2tr($taprootPubKey); } @@ -33,7 +35,10 @@ protected function getOutput(): OutputInterface */ public function testHex() { - $this->assertEquals('5120a60869f0dbcf1dc659c9cecbaf8050135ea9e8cdc487053f1dc6880949dc684c', $this->getOutput()->hex()); + $this->assertEquals( + '5120a60869f0dbcf1dc659c9cecbaf8050135ea9e8cdc487053f1dc6880949dc684c', + $this->getOutput()->hex() + ); } /** @@ -41,7 +46,10 @@ public function testHex() */ public function testAsm() { - $this->assertEquals('1 PUSHDATA(32)[a60869f0dbcf1dc659c9cecbaf8050135ea9e8cdc487053f1dc6880949dc684c]', $this->getOutput()->asm()); + $this->assertEquals( + '1 PUSHDATA(32)[a60869f0dbcf1dc659c9cecbaf8050135ea9e8cdc487053f1dc6880949dc684c]', + $this->getOutput()->asm() + ); } /** @@ -49,7 +57,10 @@ public function testAsm() */ public function testAddressBitcoin() { - $this->assertEquals('bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr', $this->getOutput()->address()); + $this->assertEquals( + 'bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr', + $this->getOutput()->address() + ); } /** @@ -57,7 +68,10 @@ public function testAddressBitcoin() */ public function testAddressBitcoinTestnet() { - $this->assertEquals('tb1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqp3mvzv', $this->getOutput()->address(NetworkFactory::bitcoinTestnet())); + $this->assertEquals( + 'tb1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqp3mvzv', + $this->getOutput()->address(NetworkFactory::bitcoinTestnet()) + ); } /** @@ -65,7 +79,10 @@ public function testAddressBitcoinTestnet() */ public function testAddressLitecoin() { - $this->assertEquals('ltc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxq4arnzx', $this->getOutput()->address(NetworkFactory::litecoin())); + $this->assertEquals( + 'ltc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxq4arnzx', + $this->getOutput()->address(NetworkFactory::litecoin()) + ); } /** @@ -73,7 +90,10 @@ public function testAddressLitecoin() */ public function testAddressLitecoinTestnet() { - $this->assertEquals('tltc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxq7j8dan', $this->getOutput()->address(NetworkFactory::litecoinTestnet())); + $this->assertEquals( + 'tltc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxq7j8dan', + $this->getOutput()->address(NetworkFactory::litecoinTestnet()) + ); } /** From 0726ecaaae28e35b06c839b41f2fd4fa6f5072f2 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Wed, 15 Nov 2023 10:58:15 -0800 Subject: [PATCH 24/32] fix phpcs --- tests/TaprootTest.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/TaprootTest.php b/tests/TaprootTest.php index 25d26aa..a05ed74 100644 --- a/tests/TaprootTest.php +++ b/tests/TaprootTest.php @@ -15,13 +15,17 @@ */ class TaprootTest extends TestCase { - function testAddress() + public function testAddress() { $pubKeys = [ - '026f7b861432127845aac3f0685f03359d73c5009929cfa88920aaba8419e0d3fc' => 'tb1pw9d6svmf4zsl27qnz76fcmz9yxpeqsz0xqsed59pdh68lr4ud5vsjk506u', - '02be3a48320efd83fb3c80c961a06b25c3049f63a0b0d9009c27ecbbca80fb2d85' => 'tb1p8969s4chlgj6jncd0qyqv77cha00uk7v6nnm4pzpr67u7p4jkgsqemy6yq', - '03ee397d520b5a9e59b0813eed5f84274af42ba2fa074cd5c68ad01b1b1521ecb1' => 'tb1pk9v0nuu0cr78c9lmrl22crdjwtxhsr0ekjuv9p8hkjquwtnknxdqprjpjh', - '0367ca30afae9d9b5374c9d34b0dc684044bf04a19c9a299b1d68bd6da173c9ccb' => 'tb1pyxakgvcv6cq2zakm599zuxs7mljfww0fj9k00w3z9e5g8uf4gujqxwws6a', + '026f7b861432127845aac3f0685f03359d73c5009929cfa88920aaba8419e0d3fc' + => 'tb1pw9d6svmf4zsl27qnz76fcmz9yxpeqsz0xqsed59pdh68lr4ud5vsjk506u', + '02be3a48320efd83fb3c80c961a06b25c3049f63a0b0d9009c27ecbbca80fb2d85' + => 'tb1p8969s4chlgj6jncd0qyqv77cha00uk7v6nnm4pzpr67u7p4jkgsqemy6yq', + '03ee397d520b5a9e59b0813eed5f84274af42ba2fa074cd5c68ad01b1b1521ecb1' + => 'tb1pk9v0nuu0cr78c9lmrl22crdjwtxhsr0ekjuv9p8hkjquwtnknxdqprjpjh', + '0367ca30afae9d9b5374c9d34b0dc684044bf04a19c9a299b1d68bd6da173c9ccb' + => 'tb1pyxakgvcv6cq2zakm599zuxs7mljfww0fj9k00w3z9e5g8uf4gujqxwws6a', ]; $network = NetworkFactory::bitcoinTestnet(); From f8c2d8874faebe4eb5d74188bc2ef576d8eb30a2 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Wed, 15 Nov 2023 11:00:41 -0800 Subject: [PATCH 25/32] Fix phpstan level 2 --- src/Ecc/Point.php | 2 +- src/Output/Outputs/P2tr.php | 2 +- src/Taproot.php | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Ecc/Point.php b/src/Ecc/Point.php index 26a9d93..836dbb9 100644 --- a/src/Ecc/Point.php +++ b/src/Ecc/Point.php @@ -95,6 +95,6 @@ static function fromPubKey(GmpMathInterface $adapter, CurveFpInterface $curve, s $y = $curve->recoverYfromX($prefix === '03', $x); } - return new static($adapter, $curve, $x, $y); + return new Point($adapter, $curve, $x, $y); } } diff --git a/src/Output/Outputs/P2tr.php b/src/Output/Outputs/P2tr.php index 6f3b0d3..f2e94d9 100644 --- a/src/Output/Outputs/P2tr.php +++ b/src/Output/Outputs/P2tr.php @@ -103,6 +103,6 @@ public static function fromScript(string $script): OutputInterface $taprootPubKey = substr($script, 2, 32); - return new static($taprootPubKey); + return new P2tr($taprootPubKey); } } diff --git a/src/Taproot.php b/src/Taproot.php index 5b9549f..aad656c 100644 --- a/src/Taproot.php +++ b/src/Taproot.php @@ -17,7 +17,6 @@ class Taproot /** * @param string $pubKey * @param string $merkleRoot - * @return PointInterface * @throws Exception */ public static function construct(string $pubKey, string $merkleRoot = ''): string From aa244238ce6d41d294002e27900bcbf940fceb9a Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Wed, 15 Nov 2023 11:02:14 -0800 Subject: [PATCH 26/32] Fix phpstan level 3 --- src/Ecc/Point.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Ecc/Point.php b/src/Ecc/Point.php index 836dbb9..22f2c95 100644 --- a/src/Ecc/Point.php +++ b/src/Ecc/Point.php @@ -16,7 +16,7 @@ class Point extends \Mdanter\Ecc\Primitives\Point { /** - * @return $this + * @return Point * @throws Exception */ public function liftX(): self From 2b5c50ee15aed2642c0fa3804dec2c4a63f5e287 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Wed, 15 Nov 2023 11:04:56 -0800 Subject: [PATCH 27/32] Fix phpstan level 6 --- tests/BitcoinTest.php | 12 ++++++------ tests/OutputFactoryTest.php | 2 +- tests/P2pkhTest.php | 2 +- tests/P2shTest.php | 2 +- tests/P2trTest.php | 14 +++++++------- tests/TaprootTest.php | 2 +- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/BitcoinTest.php b/tests/BitcoinTest.php index cbfe69f..47947c2 100644 --- a/tests/BitcoinTest.php +++ b/tests/BitcoinTest.php @@ -50,7 +50,7 @@ public function testDecodeP2WSH(): void ); } - public function testDecodeP2TR() + public function testDecodeP2TR(): void { $this->assertInstanceOf( P2tr::class, @@ -60,22 +60,22 @@ public function testDecodeP2TR() ); } - public function testValidateAddress() + public function testValidateAddress(): void { $this->assertFalse(NetworkFactory::bitcoin()->validateAddress('some')); } - public function testValidateAddressP2PKH() + public function testValidateAddressP2PKH(): void { $this->assertTrue(NetworkFactory::bitcoin()->validateAddress('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH')); } - public function testValidateAddressP2SH() + public function testValidateAddressP2SH(): void { $this->assertTrue(NetworkFactory::bitcoin()->validateAddress('3DicS6C8JZm59RsrgXr56iVHzYdQngiehV')); } - public function testValidateAddressP2WSH() + public function testValidateAddressP2WSH(): void { $this->assertTrue( NetworkFactory::bitcoin()->validateAddress( @@ -84,7 +84,7 @@ public function testValidateAddressP2WSH() ); } - public function testValidateAddressP2TR() + public function testValidateAddressP2TR(): void { $this->assertTrue( NetworkFactory::bitcoin()->validateAddress( diff --git a/tests/OutputFactoryTest.php b/tests/OutputFactoryTest.php index 837cc7e..9d3811a 100644 --- a/tests/OutputFactoryTest.php +++ b/tests/OutputFactoryTest.php @@ -100,7 +100,7 @@ public function testFromHexP2WSH(): void /** * @throws Exception */ - public function testFromHexP2TR() + public function testFromHexP2TR(): void { $output = OutputFactory::fromHex('5120a60869f0dbcf1dc659c9cecbaf8050135ea9e8cdc487053f1dc6880949dc684c'); $this->assertInstanceOf(P2tr::class, $output); diff --git a/tests/P2pkhTest.php b/tests/P2pkhTest.php index 2479876..dd054dc 100644 --- a/tests/P2pkhTest.php +++ b/tests/P2pkhTest.php @@ -194,7 +194,7 @@ public function testAddressZcash(): void /** * @throws Exception */ - public function testAddressClams() + public function testAddressClams(): void { $this->assertEquals('xJyuT2j5dnLoBhHraFjzG2hmMDjnS3fefx', $this->getOutput()->address(NetworkFactory::clams())); } diff --git a/tests/P2shTest.php b/tests/P2shTest.php index 10c542a..00b16cd 100644 --- a/tests/P2shTest.php +++ b/tests/P2shTest.php @@ -196,7 +196,7 @@ public function testAddressZcash(): void /** * @throws Exception */ - public function testAddressClams() + public function testAddressClams(): void { $this->assertEquals('6SRSJxaRz1U5gtzYstWcyifb2agxWxnFiW', $this->getOutput()->address(NetworkFactory::clams())); } diff --git a/tests/P2trTest.php b/tests/P2trTest.php index c2b5254..cbe354e 100644 --- a/tests/P2trTest.php +++ b/tests/P2trTest.php @@ -33,7 +33,7 @@ protected function getOutput(): OutputInterface /** * @throws Exception */ - public function testHex() + public function testHex(): void { $this->assertEquals( '5120a60869f0dbcf1dc659c9cecbaf8050135ea9e8cdc487053f1dc6880949dc684c', @@ -44,7 +44,7 @@ public function testHex() /** * @throws Exception */ - public function testAsm() + public function testAsm(): void { $this->assertEquals( '1 PUSHDATA(32)[a60869f0dbcf1dc659c9cecbaf8050135ea9e8cdc487053f1dc6880949dc684c]', @@ -55,7 +55,7 @@ public function testAsm() /** * @throws Exception */ - public function testAddressBitcoin() + public function testAddressBitcoin(): void { $this->assertEquals( 'bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr', @@ -66,7 +66,7 @@ public function testAddressBitcoin() /** * @throws Exception */ - public function testAddressBitcoinTestnet() + public function testAddressBitcoinTestnet(): void { $this->assertEquals( 'tb1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqp3mvzv', @@ -77,7 +77,7 @@ public function testAddressBitcoinTestnet() /** * @throws Exception */ - public function testAddressLitecoin() + public function testAddressLitecoin(): void { $this->assertEquals( 'ltc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxq4arnzx', @@ -88,7 +88,7 @@ public function testAddressLitecoin() /** * @throws Exception */ - public function testAddressLitecoinTestnet() + public function testAddressLitecoinTestnet(): void { $this->assertEquals( 'tltc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxq7j8dan', @@ -99,7 +99,7 @@ public function testAddressLitecoinTestnet() /** * @throws Exception */ - public function testFromScript() + public function testFromScript(): void { $output = $this->getOutput(); $this->assertEquals($output->script(), P2tr::fromScript($output->script())->script()); diff --git a/tests/TaprootTest.php b/tests/TaprootTest.php index a05ed74..5b49f4a 100644 --- a/tests/TaprootTest.php +++ b/tests/TaprootTest.php @@ -15,7 +15,7 @@ */ class TaprootTest extends TestCase { - public function testAddress() + public function testAddress(): void { $pubKeys = [ '026f7b861432127845aac3f0685f03359d73c5009929cfa88920aaba8419e0d3fc' From fc88c5e87ea69e4cda71cee76f12eeb35d8ce12b Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Wed, 15 Nov 2023 11:08:58 -0800 Subject: [PATCH 28/32] Fix phpstan level 7 --- tests/P2trTest.php | 2 +- tests/TaprootTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/P2trTest.php b/tests/P2trTest.php index cbe354e..c636774 100644 --- a/tests/P2trTest.php +++ b/tests/P2trTest.php @@ -25,7 +25,7 @@ class P2trTest extends TestCase protected function getOutput(): OutputInterface { $taprootPubKey = Taproot::construct( - hex2bin('03cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115') + hex2bin('03cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115') ?: '' ); return OutputFactory::p2tr($taprootPubKey); } diff --git a/tests/TaprootTest.php b/tests/TaprootTest.php index 5b49f4a..a76d53b 100644 --- a/tests/TaprootTest.php +++ b/tests/TaprootTest.php @@ -32,7 +32,7 @@ public function testAddress(): void foreach ($pubKeys as $pubKey => $address) { $this->assertEquals( - OutputFactory::p2tr(Taproot::construct(hex2bin($pubKey)))->address($network), + OutputFactory::p2tr(Taproot::construct(hex2bin($pubKey) ?: ''))->address($network), $address ); } From ce73f3ca5062d05a6173c5b2f9f322b656741d7f Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Wed, 15 Nov 2023 11:09:20 -0800 Subject: [PATCH 29/32] Update phpstan.neon --- phpstan.neon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpstan.neon b/phpstan.neon index 93b2e48..0ac5e16 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ parameters: - level: 8 + level: 9 paths: - src - tests From 4df90b34793e8bb8dd8be89e803ed468e9c85c30 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Wed, 15 Nov 2023 11:11:16 -0800 Subject: [PATCH 30/32] Add test-coverage scripts --- composer.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/composer.json b/composer.json index 2862cdf..e885d70 100644 --- a/composer.json +++ b/composer.json @@ -47,6 +47,9 @@ "test": [ "phpunit" ], + "test-coverage":[ + "phpunit --coverage-text --coverage-clover tests/_reports/clover.xml --coverage-html tests/_reports/html" + ], "lint": [ "phpcbf || true", "phpcs || true", From 15287edf4ce700f498d8df4314c65aeffce911e1 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Wed, 15 Nov 2023 11:11:18 -0800 Subject: [PATCH 31/32] Update coverage.svg --- .github/coverage.svg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/coverage.svg b/.github/coverage.svg index 00a08aa..95327c6 100644 --- a/.github/coverage.svg +++ b/.github/coverage.svg @@ -15,7 +15,7 @@ PHPUnit PHPUnit - 83% - 83% + 86% + 86% \ No newline at end of file From fc6f9276b4242da715bd345ac8400b146e59c9e9 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Wed, 15 Nov 2023 11:12:15 -0800 Subject: [PATCH 32/32] Fix "PHP 7.1" (was .4) in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4179355..9d82836 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![PHP 7.4](https://img.shields.io/badge/PHP-7.4-8892BF.svg)]() [![PHPCS PSR-12](https://img.shields.io/badge/PHPCS-PSR–12-226146.svg)](https://www.php-fig.org/psr/psr-12/) [![PHPUnit ](.github/coverage.svg)](https://brianhenryie.github.io/bh-php-monero-explorer/) [![PHPStan ](https://img.shields.io/badge/PHPStan-Level%208-2a5ea7.svg)](https://phpstan.org/) +[![PHP 7.1](https://img.shields.io/badge/PHP-7.1-8892BF.svg)]() [![PHPCS PSR-12](https://img.shields.io/badge/PHPCS-PSR–12-226146.svg)](https://www.php-fig.org/psr/psr-12/) [![PHPUnit ](.github/coverage.svg)](https://brianhenryie.github.io/bh-php-monero-explorer/) [![PHPStan ](https://img.shields.io/badge/PHPStan-Level%208-2a5ea7.svg)](https://phpstan.org/) ## PHP Bitcoin Address