Complex classes like Wallet often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Wallet, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | abstract class Wallet implements WalletInterface { |
||
| 29 | |||
| 30 | const WALLET_VERSION_V1 = 'v1'; |
||
| 31 | const WALLET_VERSION_V2 = 'v2'; |
||
| 32 | const WALLET_VERSION_V3 = 'v3'; |
||
| 33 | |||
| 34 | const BASE_FEE = 10000; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * development / debug setting |
||
| 38 | * when getting a new derivation from the API, |
||
| 39 | * will verify address / redeeemScript with the values the API provides |
||
| 40 | */ |
||
| 41 | const VERIFY_NEW_DERIVATION = true; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var BlocktrailSDKInterface |
||
| 45 | */ |
||
| 46 | protected $sdk; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $identifier; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * BIP32 master primary private key (m/) |
||
| 55 | * |
||
| 56 | * @var BIP32Key |
||
| 57 | */ |
||
| 58 | protected $primaryPrivateKey; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var BIP32Key[] |
||
| 62 | */ |
||
| 63 | protected $primaryPublicKeys; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * BIP32 master backup public key (M/) |
||
| 67 | |||
| 68 | * @var BIP32Key |
||
| 69 | */ |
||
| 70 | protected $backupPublicKey; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * map of blocktrail BIP32 public keys |
||
| 74 | * keyed by key index |
||
| 75 | * path should be `M / key_index'` |
||
| 76 | * |
||
| 77 | * @var BIP32Key[] |
||
| 78 | */ |
||
| 79 | protected $blocktrailPublicKeys; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * the 'Blocktrail Key Index' that is used for new addresses |
||
| 83 | * |
||
| 84 | * @var int |
||
| 85 | */ |
||
| 86 | protected $keyIndex; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * 'bitcoin' |
||
| 90 | * |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | protected $network; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * testnet yes / no |
||
| 97 | * |
||
| 98 | * @var bool |
||
| 99 | */ |
||
| 100 | protected $testnet; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * cache of public keys, by path |
||
| 104 | * |
||
| 105 | * @var BIP32Key[] |
||
| 106 | */ |
||
| 107 | protected $pubKeys = []; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * cache of address / redeemScript, by path |
||
| 111 | * |
||
| 112 | * @var string[][] [[address, redeemScript)], ] |
||
| 113 | */ |
||
| 114 | protected $derivations = []; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * reverse cache of paths by address |
||
| 118 | * |
||
| 119 | * @var string[] |
||
| 120 | */ |
||
| 121 | protected $derivationsByAddress = []; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var WalletPath |
||
| 125 | */ |
||
| 126 | protected $walletPath; |
||
| 127 | |||
| 128 | protected $checksum; |
||
| 129 | |||
| 130 | protected $locked = true; |
||
| 131 | |||
| 132 | protected $optimalFeePerKB; |
||
| 133 | protected $lowPriorityFeePerKB; |
||
| 134 | protected $feePerKBAge; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param BlocktrailSDKInterface $sdk SDK instance used to do requests |
||
| 138 | * @param string $identifier identifier of the wallet |
||
| 139 | * @param BIP32Key[] $primaryPublicKeys |
||
| 140 | * @param BIP32Key $backupPublicKey should be BIP32 master public key M/ |
||
| 141 | * @param BIP32Key[] $blocktrailPublicKeys |
||
| 142 | * @param int $keyIndex |
||
| 143 | * @param string $network |
||
| 144 | * @param bool $testnet |
||
| 145 | * @param string $checksum |
||
| 146 | */ |
||
| 147 | 15 | public function __construct(BlocktrailSDKInterface $sdk, $identifier, array $primaryPublicKeys, $backupPublicKey, array $blocktrailPublicKeys, $keyIndex, $network, $testnet, $checksum) { |
|
| 162 | |||
| 163 | /** |
||
| 164 | * return the wallet identifier |
||
| 165 | * |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | 10 | public function getIdentifier() { |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Returns the wallets backup public key |
||
| 174 | * |
||
| 175 | * @return [xpub, path] |
||
|
|
|||
| 176 | */ |
||
| 177 | 1 | public function getBackupKey() { |
|
| 180 | |||
| 181 | /** |
||
| 182 | * return list of Blocktrail co-sign extended public keys |
||
| 183 | * |
||
| 184 | * @return array[] [ [xpub, path] ] |
||
| 185 | */ |
||
| 186 | 5 | public function getBlocktrailPublicKeys() { |
|
| 191 | |||
| 192 | /** |
||
| 193 | * check if wallet is locked |
||
| 194 | * |
||
| 195 | * @return bool |
||
| 196 | */ |
||
| 197 | 10 | public function isLocked() { |
|
| 200 | |||
| 201 | /** |
||
| 202 | * upgrade wallet to different blocktrail cosign key |
||
| 203 | * |
||
| 204 | * @param $keyIndex |
||
| 205 | * @return bool |
||
| 206 | * @throws \Exception |
||
| 207 | */ |
||
| 208 | 5 | public function upgradeKeyIndex($keyIndex) { |
|
| 237 | |||
| 238 | /** |
||
| 239 | * get a new BIP32 derivation for the next (unused) address |
||
| 240 | * by requesting it from the API |
||
| 241 | * |
||
| 242 | * @return string |
||
| 243 | * @throws \Exception |
||
| 244 | */ |
||
| 245 | 10 | protected function getNewDerivation() { |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @param string|BIP32Path $path |
||
| 274 | * @return BIP32Key|false |
||
| 275 | * @throws \Exception |
||
| 276 | * |
||
| 277 | * @TODO: hmm? |
||
| 278 | */ |
||
| 279 | 10 | protected function getParentPublicKey($path) { |
|
| 296 | |||
| 297 | /** |
||
| 298 | * get address for the specified path |
||
| 299 | * |
||
| 300 | * @param string|BIP32Path $path |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | 10 | public function getAddressByPath($path) { |
|
| 314 | |||
| 315 | /** |
||
| 316 | * get address and redeemScript for specified path |
||
| 317 | * |
||
| 318 | * @param string $path |
||
| 319 | * @return array[string, ScriptInterface] [address, redeemScript] |
||
| 320 | */ |
||
| 321 | 10 | public function getRedeemScriptByPath($path) { |
|
| 333 | |||
| 334 | /** |
||
| 335 | * @param BIP32Key $key |
||
| 336 | * @param string|BIP32Path $path |
||
| 337 | * @return string |
||
| 338 | */ |
||
| 339 | protected function getAddressFromKey(BIP32Key $key, $path) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param BIP32Key $key |
||
| 345 | * @param string|BIP32Path $path |
||
| 346 | * @return array[string, ScriptInterface] [address, redeemScript] |
||
| 347 | * @throws \Exception |
||
| 348 | */ |
||
| 349 | 10 | protected function getRedeemScriptFromKey(BIP32Key $key, $path) { |
|
| 362 | |||
| 363 | /** |
||
| 364 | * get the path (and redeemScript) to specified address |
||
| 365 | * |
||
| 366 | * @param string $address |
||
| 367 | * @return array |
||
| 368 | */ |
||
| 369 | public function getPathForAddress($address) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param string|BIP32Path $path |
||
| 375 | * @return BIP32Key |
||
| 376 | * @throws \Exception |
||
| 377 | */ |
||
| 378 | 10 | public function getBlocktrailPublicKey($path) { |
|
| 389 | |||
| 390 | /** |
||
| 391 | * generate a new derived key and return the new path and address for it |
||
| 392 | * |
||
| 393 | * @return string[] [path, address] |
||
| 394 | */ |
||
| 395 | 10 | public function getNewAddressPair() { |
|
| 401 | |||
| 402 | /** |
||
| 403 | * generate a new derived private key and return the new address for it |
||
| 404 | * |
||
| 405 | * @return string |
||
| 406 | */ |
||
| 407 | 7 | public function getNewAddress() { |
|
| 410 | |||
| 411 | /** |
||
| 412 | * get the balance for the wallet |
||
| 413 | * |
||
| 414 | * @return int[] [confirmed, unconfirmed] |
||
| 415 | */ |
||
| 416 | 9 | public function getBalance() { |
|
| 421 | |||
| 422 | /** |
||
| 423 | * do wallet discovery (slow) |
||
| 424 | * |
||
| 425 | * @param int $gap the gap setting to use for discovery |
||
| 426 | * @return int[] [confirmed, unconfirmed] |
||
| 427 | */ |
||
| 428 | 2 | public function doDiscovery($gap = 200) { |
|
| 433 | |||
| 434 | /** |
||
| 435 | * create, sign and send a transaction |
||
| 436 | * |
||
| 437 | * @param array $outputs [address => value, ] or [[address, value], ] or [['address' => address, 'value' => value], ] coins to send |
||
| 438 | * value should be INT |
||
| 439 | * @param string $changeAddress change address to use (autogenerated if NULL) |
||
| 440 | * @param bool $allowZeroConf |
||
| 441 | * @param bool $randomizeChangeIdx randomize the location of the change (for increased privacy / anonimity) |
||
| 442 | * @param string $feeStrategy |
||
| 443 | * @param null|int $forceFee set a fixed fee instead of automatically calculating the correct fee, not recommended! |
||
| 444 | * @return string the txid / transaction hash |
||
| 445 | * @throws \Exception |
||
| 446 | */ |
||
| 447 | 8 | public function pay(array $outputs, $changeAddress = null, $allowZeroConf = false, $randomizeChangeIdx = true, $feeStrategy = self::FEE_STRATEGY_OPTIMAL, $forceFee = null) { |
|
| 469 | |||
| 470 | /** |
||
| 471 | * determine max spendable from wallet after fees |
||
| 472 | * |
||
| 473 | * @param bool $allowZeroConf |
||
| 474 | * @param string $feeStrategy |
||
| 475 | * @param null|int $forceFee set a fixed fee instead of automatically calculating the correct fee, not recommended! |
||
| 476 | * @param int $outputCnt |
||
| 477 | * @return string |
||
| 478 | * @throws BlocktrailSDKException |
||
| 479 | */ |
||
| 480 | public function getMaxSpendable($allowZeroConf = false, $feeStrategy = self::FEE_STRATEGY_OPTIMAL, $forceFee = null, $outputCnt = 1) { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * parse outputs into normalized struct |
||
| 486 | * |
||
| 487 | * @param array $outputs [address => value, ] or [[address, value], ] or [['address' => address, 'value' => value], ] |
||
| 488 | * @return array [['address' => address, 'value' => value], ] |
||
| 489 | */ |
||
| 490 | 9 | public static function normalizeOutputsStruct(array $outputs) { |
|
| 518 | |||
| 519 | /** |
||
| 520 | * 'fund' the txBuilder with UTXOs (modified in place) |
||
| 521 | * |
||
| 522 | * @param TransactionBuilder $txBuilder |
||
| 523 | * @param bool|true $lockUTXOs |
||
| 524 | * @param bool|false $allowZeroConf |
||
| 525 | * @param null|int $forceFee |
||
| 526 | * @return TransactionBuilder |
||
| 527 | */ |
||
| 528 | 8 | public function coinSelectionForTxBuilder(TransactionBuilder $txBuilder, $lockUTXOs = true, $allowZeroConf = false, $forceFee = null) { |
|
| 550 | |||
| 551 | /** |
||
| 552 | * build inputs and outputs lists for TransactionBuilder |
||
| 553 | * |
||
| 554 | * @param TransactionBuilder $txBuilder |
||
| 555 | * @return [TransactionInterface, SignInfo[]] |
||
| 556 | * @throws \Exception |
||
| 557 | */ |
||
| 558 | 3 | public function buildTx(TransactionBuilder $txBuilder) { |
|
| 643 | |||
| 644 | 3 | public function determineFeeAndChange(TransactionBuilder $txBuilder, $optimalFeePerKB, $lowPriorityFeePerKB) { |
|
| 700 | |||
| 701 | /** |
||
| 702 | * create, sign and send transction based on TransactionBuilder |
||
| 703 | * |
||
| 704 | * @param TransactionBuilder $txBuilder |
||
| 705 | * @param bool $apiCheckFee let the API check if the fee is correct |
||
| 706 | * @return string |
||
| 707 | * @throws \Exception |
||
| 708 | */ |
||
| 709 | 2 | public function sendTx(TransactionBuilder $txBuilder, $apiCheckFee = true) { |
|
| 714 | |||
| 715 | /** |
||
| 716 | * !! INTERNAL METHOD, public for testing purposes !! |
||
| 717 | * create, sign and send transction based on inputs and outputs |
||
| 718 | * |
||
| 719 | * @param Transaction $tx |
||
| 720 | * @param SignInfo[] $signInfo |
||
| 721 | * @param bool $apiCheckFee let the API check if the fee is correct |
||
| 722 | * @return string |
||
| 723 | * @throws \Exception |
||
| 724 | * @internal |
||
| 725 | */ |
||
| 726 | 2 | public function _sendTx(Transaction $tx, array $signInfo, $apiCheckFee = true) { |
|
| 745 | |||
| 746 | /** |
||
| 747 | * only supports estimating fee for 2of3 multsig UTXOs and P2PKH/P2SH outputs |
||
| 748 | * |
||
| 749 | * @param int $utxoCnt number of unspent inputs in transaction |
||
| 750 | * @param int $outputCnt number of outputs in transaction |
||
| 751 | * @return float |
||
| 752 | * @access public reminder that people might use this! |
||
| 753 | */ |
||
| 754 | 1 | public static function estimateFee($utxoCnt, $outputCnt) { |
|
| 759 | |||
| 760 | /** |
||
| 761 | * @param int $size size in bytes |
||
| 762 | * @return int fee in satoshi |
||
| 763 | */ |
||
| 764 | 3 | public static function baseFeeForSize($size) { |
|
| 769 | |||
| 770 | /** |
||
| 771 | * @param int $txinSize |
||
| 772 | * @param int $txoutSize |
||
| 773 | * @return float |
||
| 774 | */ |
||
| 775 | 5 | public static function estimateSize($txinSize, $txoutSize) { |
|
| 778 | |||
| 779 | /** |
||
| 780 | * only supports estimating size for P2PKH/P2SH outputs |
||
| 781 | * |
||
| 782 | * @param int $outputCnt number of outputs in transaction |
||
| 783 | * @return float |
||
| 784 | */ |
||
| 785 | 2 | public static function estimateSizeOutputs($outputCnt) { |
|
| 788 | |||
| 789 | /** |
||
| 790 | * only supports estimating size for 2of3 multsig UTXOs |
||
| 791 | * |
||
| 792 | * @param int $utxoCnt number of unspent inputs in transaction |
||
| 793 | * @return float |
||
| 794 | */ |
||
| 795 | 5 | public static function estimateSizeUTXOs($utxoCnt) { |
|
| 831 | |||
| 832 | /** |
||
| 833 | * determine how much fee is required based on the inputs and outputs |
||
| 834 | * this is an estimation, not a proper 100% correct calculation |
||
| 835 | * |
||
| 836 | * @param UTXO[] $utxos |
||
| 837 | * @param array[] $outputs |
||
| 838 | * @param $feeStrategy |
||
| 839 | * @param $optimalFeePerKB |
||
| 840 | * @param $lowPriorityFeePerKB |
||
| 841 | * @return int |
||
| 842 | * @throws BlocktrailSDKException |
||
| 843 | */ |
||
| 844 | 3 | protected function determineFee($utxos, $outputs, $feeStrategy, $optimalFeePerKB, $lowPriorityFeePerKB) { |
|
| 874 | |||
| 875 | /** |
||
| 876 | * determine how much change is left over based on the inputs and outputs and the fee |
||
| 877 | * |
||
| 878 | * @param UTXO[] $utxos |
||
| 879 | * @param array[] $outputs |
||
| 880 | * @param int $fee |
||
| 881 | * @return int |
||
| 882 | */ |
||
| 883 | 3 | protected function determineChange($utxos, $outputs, $fee) { |
|
| 891 | |||
| 892 | /** |
||
| 893 | * sign a raw transaction with the private keys that we have |
||
| 894 | * |
||
| 895 | * @param Transaction $tx |
||
| 896 | * @param SignInfo[] $signInfo |
||
| 897 | * @return TransactionInterface |
||
| 898 | * @throws \Exception |
||
| 899 | */ |
||
| 900 | 2 | protected function signTransaction(Transaction $tx, array $signInfo) { |
|
| 926 | |||
| 927 | /** |
||
| 928 | * send the transaction using the API |
||
| 929 | * |
||
| 930 | * @param string $signed |
||
| 931 | * @param string[] $paths |
||
| 932 | * @param bool $checkFee |
||
| 933 | * @return string the complete raw transaction |
||
| 934 | * @throws \Exception |
||
| 935 | */ |
||
| 936 | 2 | protected function sendTransaction($signed, $paths, $checkFee = false) { |
|
| 939 | |||
| 940 | /** |
||
| 941 | * @param \array[] $outputs |
||
| 942 | * @param bool $lockUTXO |
||
| 943 | * @param bool $allowZeroConf |
||
| 944 | * @param int|null|string $feeStrategy |
||
| 945 | * @param null $forceFee |
||
| 946 | * @return array |
||
| 947 | */ |
||
| 948 | 8 | public function coinSelection($outputs, $lockUTXO = true, $allowZeroConf = false, $feeStrategy = self::FEE_STRATEGY_OPTIMAL, $forceFee = null) { |
|
| 957 | |||
| 958 | 3 | public function getOptimalFeePerKB() { |
|
| 965 | |||
| 966 | 3 | public function getLowPriorityFeePerKB() { |
|
| 973 | |||
| 974 | 1 | public function updateFeePerKB() { |
|
| 982 | |||
| 983 | /** |
||
| 984 | * delete the wallet |
||
| 985 | * |
||
| 986 | * @param bool $force ignore warnings (such as non-zero balance) |
||
| 987 | * @return mixed |
||
| 988 | * @throws \Exception |
||
| 989 | */ |
||
| 990 | 10 | public function deleteWallet($force = false) { |
|
| 998 | |||
| 999 | /** |
||
| 1000 | * create checksum to verify ownership of the master primary key |
||
| 1001 | * |
||
| 1002 | * @return string[] [address, signature] |
||
| 1003 | */ |
||
| 1004 | 10 | protected function createChecksumVerificationSignature() { |
|
| 1015 | |||
| 1016 | /** |
||
| 1017 | * setup a webhook for our wallet |
||
| 1018 | * |
||
| 1019 | * @param string $url URL to receive webhook events |
||
| 1020 | * @param string $identifier identifier for the webhook, defaults to WALLET-{$this->identifier} |
||
| 1021 | * @return array |
||
| 1022 | */ |
||
| 1023 | 1 | public function setupWebhook($url, $identifier = null) { |
|
| 1027 | |||
| 1028 | /** |
||
| 1029 | * @param string $identifier identifier for the webhook, defaults to WALLET-{$this->identifier} |
||
| 1030 | * @return mixed |
||
| 1031 | */ |
||
| 1032 | 1 | public function deleteWebhook($identifier = null) { |
|
| 1036 | |||
| 1037 | /** |
||
| 1038 | * lock a specific unspent output |
||
| 1039 | * |
||
| 1040 | * @param $txHash |
||
| 1041 | * @param $txIdx |
||
| 1042 | * @param int $ttl |
||
| 1043 | * @return bool |
||
| 1044 | */ |
||
| 1045 | public function lockUTXO($txHash, $txIdx, $ttl = 3) { |
||
| 1048 | |||
| 1049 | /** |
||
| 1050 | * unlock a specific unspent output |
||
| 1051 | * |
||
| 1052 | * @param $txHash |
||
| 1053 | * @param $txIdx |
||
| 1054 | * @return bool |
||
| 1055 | */ |
||
| 1056 | public function unlockUTXO($txHash, $txIdx) { |
||
| 1059 | |||
| 1060 | /** |
||
| 1061 | * get all transactions for the wallet (paginated) |
||
| 1062 | * |
||
| 1063 | * @param integer $page pagination: page number |
||
| 1064 | * @param integer $limit pagination: records per page (max 500) |
||
| 1065 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1066 | * @return array associative array containing the response |
||
| 1067 | */ |
||
| 1068 | 1 | public function transactions($page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 1071 | |||
| 1072 | /** |
||
| 1073 | * get all addresses for the wallet (paginated) |
||
| 1074 | * |
||
| 1075 | * @param integer $page pagination: page number |
||
| 1076 | * @param integer $limit pagination: records per page (max 500) |
||
| 1077 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1078 | * @return array associative array containing the response |
||
| 1079 | */ |
||
| 1080 | 1 | public function addresses($page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 1083 | |||
| 1084 | /** |
||
| 1085 | * get all UTXOs for the wallet (paginated) |
||
| 1086 | * |
||
| 1087 | * @param integer $page pagination: page number |
||
| 1088 | * @param integer $limit pagination: records per page (max 500) |
||
| 1089 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1090 | * @param boolean $zeroconf include zero confirmation transactions |
||
| 1091 | * @return array associative array containing the response |
||
| 1092 | */ |
||
| 1093 | 1 | public function utxos($page = 1, $limit = 20, $sortDir = 'asc', $zeroconf = true) { |
|
| 1096 | } |
||
| 1097 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.