Complex classes like BlocktrailSDK 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 BlocktrailSDK, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class BlocktrailSDK implements BlocktrailSDKInterface { |
||
| 44 | /** |
||
| 45 | * @var Connection\RestClientInterface |
||
| 46 | */ |
||
| 47 | protected $blocktrailClient; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var Connection\RestClient |
||
| 51 | */ |
||
| 52 | protected $dataClient; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string currently only supporting; bitcoin |
||
| 56 | */ |
||
| 57 | protected $network; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var bool |
||
| 61 | */ |
||
| 62 | protected $testnet; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var ConverterInterface |
||
| 66 | */ |
||
| 67 | protected $converter; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param string $apiKey the API_KEY to use for authentication |
||
| 71 | * @param string $apiSecret the API_SECRET to use for authentication |
||
| 72 | * @param string $network the cryptocurrency 'network' to consume, eg BTC, LTC, etc |
||
| 73 | * @param bool $testnet testnet yes/no |
||
| 74 | * @param string $apiVersion the version of the API to consume |
||
| 75 | * @param null $apiEndpoint overwrite the endpoint used |
||
| 76 | * this will cause the $network, $testnet and $apiVersion to be ignored! |
||
| 77 | */ |
||
| 78 | 118 | public function __construct($apiKey, $apiSecret, $network = 'BTC', $testnet = false, $apiVersion = 'v1', $apiEndpoint = null) { |
|
| 106 | |||
| 107 | /** |
||
| 108 | * normalize network string |
||
| 109 | * |
||
| 110 | * @param $network |
||
| 111 | * @param $testnet |
||
| 112 | * @return array |
||
| 113 | * @throws \Exception |
||
| 114 | */ |
||
| 115 | 118 | protected function normalizeNetwork($network, $testnet) { |
|
| 119 | |||
| 120 | /** |
||
| 121 | * set BitcoinLib to the correct magic-byte defaults for the selected network |
||
| 122 | * |
||
| 123 | * @param $network |
||
| 124 | * @param bool $testnet |
||
| 125 | * @param bool $regtest |
||
| 126 | */ |
||
| 127 | 118 | protected function setBitcoinLibMagicBytes($network, $testnet, $regtest) { |
|
| 149 | |||
| 150 | /** |
||
| 151 | * enable CURL debugging output |
||
| 152 | * |
||
| 153 | * @param bool $debug |
||
| 154 | * |
||
| 155 | * @codeCoverageIgnore |
||
| 156 | */ |
||
| 157 | public function setCurlDebugging($debug = true) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * enable verbose errors |
||
| 164 | * |
||
| 165 | * @param bool $verboseErrors |
||
| 166 | * |
||
| 167 | * @codeCoverageIgnore |
||
| 168 | */ |
||
| 169 | public function setVerboseErrors($verboseErrors = true) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * set cURL default option on Guzzle client |
||
| 176 | * @param string $key |
||
| 177 | * @param bool $value |
||
| 178 | * |
||
| 179 | * @codeCoverageIgnore |
||
| 180 | */ |
||
| 181 | public function setCurlDefaultOption($key, $value) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return RestClientInterface |
||
| 188 | */ |
||
| 189 | 2 | public function getRestClient() { |
|
| 192 | |||
| 193 | /** |
||
| 194 | * @return RestClient |
||
| 195 | */ |
||
| 196 | public function getDataRestClient() { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @param RestClientInterface $restClient |
||
| 202 | */ |
||
| 203 | public function setRestClient(RestClientInterface $restClient) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * get a single address |
||
| 209 | * @param string $address address hash |
||
| 210 | * @return array associative array containing the response |
||
| 211 | */ |
||
| 212 | 1 | public function address($address) { |
|
| 216 | |||
| 217 | /** |
||
| 218 | * get all transactions for an address (paginated) |
||
| 219 | * @param string $address address hash |
||
| 220 | * @param integer $page pagination: page number |
||
| 221 | * @param integer $limit pagination: records per page (max 500) |
||
| 222 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 223 | * @return array associative array containing the response |
||
| 224 | */ |
||
| 225 | 1 | public function addressTransactions($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 234 | |||
| 235 | /** |
||
| 236 | * get all unconfirmed transactions for an address (paginated) |
||
| 237 | * @param string $address address hash |
||
| 238 | * @param integer $page pagination: page number |
||
| 239 | * @param integer $limit pagination: records per page (max 500) |
||
| 240 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 241 | * @return array associative array containing the response |
||
| 242 | */ |
||
| 243 | public function addressUnconfirmedTransactions($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * get all unspent outputs for an address (paginated) |
||
| 255 | * @param string $address address hash |
||
| 256 | * @param integer $page pagination: page number |
||
| 257 | * @param integer $limit pagination: records per page (max 500) |
||
| 258 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 259 | * @return array associative array containing the response |
||
| 260 | */ |
||
| 261 | 1 | public function addressUnspentOutputs($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 270 | |||
| 271 | /** |
||
| 272 | * get all unspent outputs for a batch of addresses (paginated) |
||
| 273 | * |
||
| 274 | * @param string[] $addresses |
||
| 275 | * @param integer $page pagination: page number |
||
| 276 | * @param integer $limit pagination: records per page (max 500) |
||
| 277 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 278 | * @return array associative array containing the response |
||
| 279 | * @throws \Exception |
||
| 280 | */ |
||
| 281 | public function batchAddressUnspentOutputs($addresses, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * verify ownership of an address |
||
| 308 | * @param string $address address hash |
||
| 309 | * @param string $signature a signed message (the address hash) using the private key of the address |
||
| 310 | * @return array associative array containing the response |
||
| 311 | */ |
||
| 312 | 1 | public function verifyAddress($address, $signature) { |
|
| 319 | |||
| 320 | /** |
||
| 321 | * get all blocks (paginated) |
||
| 322 | * @param integer $page pagination: page number |
||
| 323 | * @param integer $limit pagination: records per page |
||
| 324 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 325 | * @return array associative array containing the response |
||
| 326 | */ |
||
| 327 | 1 | public function allBlocks($page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 336 | |||
| 337 | /** |
||
| 338 | * get the latest block |
||
| 339 | * @return array associative array containing the response |
||
| 340 | */ |
||
| 341 | 1 | public function blockLatest() { |
|
| 345 | |||
| 346 | /** |
||
| 347 | * get an individual block |
||
| 348 | * @param string|integer $block a block hash or a block height |
||
| 349 | * @return array associative array containing the response |
||
| 350 | */ |
||
| 351 | 1 | public function block($block) { |
|
| 355 | |||
| 356 | /** |
||
| 357 | * get all transaction in a block (paginated) |
||
| 358 | * @param string|integer $block a block hash or a block height |
||
| 359 | * @param integer $page pagination: page number |
||
| 360 | * @param integer $limit pagination: records per page |
||
| 361 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 362 | * @return array associative array containing the response |
||
| 363 | */ |
||
| 364 | public function blockTransactions($block, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * get a single transaction |
||
| 376 | * @param string $txhash transaction hash |
||
| 377 | * @return array associative array containing the response |
||
| 378 | */ |
||
| 379 | 1 | public function transaction($txhash) { |
|
| 389 | |||
| 390 | /** |
||
| 391 | * get a single transaction |
||
| 392 | * @param string[] $txhashes list of transaction hashes (up to 20) |
||
| 393 | * @return array[] array containing the response |
||
| 394 | */ |
||
| 395 | 1 | public function transactions($txhashes) { |
|
| 399 | |||
| 400 | /** |
||
| 401 | * get a paginated list of all webhooks associated with the api user |
||
| 402 | * @param integer $page pagination: page number |
||
| 403 | * @param integer $limit pagination: records per page |
||
| 404 | * @return array associative array containing the response |
||
| 405 | */ |
||
| 406 | public function allWebhooks($page = 1, $limit = 20) { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * get an existing webhook by it's identifier |
||
| 417 | * @param string $identifier a unique identifier associated with the webhook |
||
| 418 | * @return array associative array containing the response |
||
| 419 | */ |
||
| 420 | public function getWebhook($identifier) { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * create a new webhook |
||
| 427 | * @param string $url the url to receive the webhook events |
||
| 428 | * @param string $identifier a unique identifier to associate with this webhook |
||
| 429 | * @return array associative array containing the response |
||
| 430 | */ |
||
| 431 | 1 | public function setupWebhook($url, $identifier = null) { |
|
| 439 | |||
| 440 | /** |
||
| 441 | * update an existing webhook |
||
| 442 | * @param string $identifier the unique identifier of the webhook to update |
||
| 443 | * @param string $newUrl the new url to receive the webhook events |
||
| 444 | * @param string $newIdentifier a new unique identifier to associate with this webhook |
||
| 445 | * @return array associative array containing the response |
||
| 446 | */ |
||
| 447 | public function updateWebhook($identifier, $newUrl = null, $newIdentifier = null) { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * deletes an existing webhook and any event subscriptions associated with it |
||
| 458 | * @param string $identifier the unique identifier of the webhook to delete |
||
| 459 | * @return boolean true on success |
||
| 460 | */ |
||
| 461 | public function deleteWebhook($identifier) { |
||
| 465 | |||
| 466 | /** |
||
| 467 | * get a paginated list of all the events a webhook is subscribed to |
||
| 468 | * @param string $identifier the unique identifier of the webhook |
||
| 469 | * @param integer $page pagination: page number |
||
| 470 | * @param integer $limit pagination: records per page |
||
| 471 | * @return array associative array containing the response |
||
| 472 | */ |
||
| 473 | public function getWebhookEvents($identifier, $page = 1, $limit = 20) { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * subscribes a webhook to transaction events of one particular transaction |
||
| 484 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 485 | * @param string $transaction the transaction hash |
||
| 486 | * @param integer $confirmations the amount of confirmations to send. |
||
| 487 | * @return array associative array containing the response |
||
| 488 | */ |
||
| 489 | public function subscribeTransaction($identifier, $transaction, $confirmations = 6) { |
||
| 498 | |||
| 499 | /** |
||
| 500 | * subscribes a webhook to transaction events on a particular address |
||
| 501 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 502 | * @param string $address the address hash |
||
| 503 | * @param integer $confirmations the amount of confirmations to send. |
||
| 504 | * @return array associative array containing the response |
||
| 505 | */ |
||
| 506 | public function subscribeAddressTransactions($identifier, $address, $confirmations = 6) { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * batch subscribes a webhook to multiple transaction events |
||
| 518 | * |
||
| 519 | * @param string $identifier the unique identifier of the webhook |
||
| 520 | * @param array $batchData A 2D array of event data: |
||
| 521 | * [address => $address, confirmations => $confirmations] |
||
| 522 | * where $address is the address to subscibe to |
||
| 523 | * and optionally $confirmations is the amount of confirmations |
||
| 524 | * @return boolean true on success |
||
| 525 | */ |
||
| 526 | public function batchSubscribeAddressTransactions($identifier, $batchData) { |
||
| 538 | |||
| 539 | /** |
||
| 540 | * subscribes a webhook to a new block event |
||
| 541 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 542 | * @return array associative array containing the response |
||
| 543 | */ |
||
| 544 | public function subscribeNewBlocks($identifier) { |
||
| 551 | |||
| 552 | /** |
||
| 553 | * removes an transaction event subscription from a webhook |
||
| 554 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 555 | * @param string $transaction the transaction hash of the event subscription |
||
| 556 | * @return boolean true on success |
||
| 557 | */ |
||
| 558 | public function unsubscribeTransaction($identifier, $transaction) { |
||
| 562 | |||
| 563 | /** |
||
| 564 | * removes an address transaction event subscription from a webhook |
||
| 565 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 566 | * @param string $address the address hash of the event subscription |
||
| 567 | * @return boolean true on success |
||
| 568 | */ |
||
| 569 | public function unsubscribeAddressTransactions($identifier, $address) { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * removes a block event subscription from a webhook |
||
| 576 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 577 | * @return boolean true on success |
||
| 578 | */ |
||
| 579 | public function unsubscribeNewBlocks($identifier) { |
||
| 583 | |||
| 584 | /** |
||
| 585 | * create a new wallet |
||
| 586 | * - will generate a new primary seed (with password) and backup seed (without password) |
||
| 587 | * - send the primary seed (BIP39 'encrypted') and backup public key to the server |
||
| 588 | * - receive the blocktrail co-signing public key from the server |
||
| 589 | * |
||
| 590 | * Either takes one argument: |
||
| 591 | * @param array $options |
||
| 592 | * |
||
| 593 | * Or takes three arguments (old, deprecated syntax): |
||
| 594 | * (@nonPHP-doc) @param $identifier |
||
| 595 | * (@nonPHP-doc) @param $password |
||
| 596 | * (@nonPHP-doc) @param int $keyIndex override for the blocktrail cosigning key to use |
||
| 597 | * |
||
| 598 | * @return array[WalletInterface, array] list($wallet, $backupInfo) |
||
| 599 | * @throws \Exception |
||
| 600 | */ |
||
| 601 | public function createNewWallet($options) { |
||
| 645 | |||
| 646 | protected function createNewWalletV1($options) { |
||
| 766 | |||
| 767 | public static function randomBits($bits) { |
||
| 770 | |||
| 771 | public static function randomBytes($bytes) { |
||
| 774 | |||
| 775 | protected function createNewWalletV2($options) { |
||
| 898 | |||
| 899 | protected function createNewWalletV3($options) { |
||
| 1043 | |||
| 1044 | /** |
||
| 1045 | * @param array $bip32Key |
||
| 1046 | * @throws BlocktrailSDKException |
||
| 1047 | */ |
||
| 1048 | 3 | private function verifyPublicBIP32Key(array $bip32Key) { |
|
| 1058 | |||
| 1059 | /** |
||
| 1060 | * @param array $walletData |
||
| 1061 | * @throws BlocktrailSDKException |
||
| 1062 | */ |
||
| 1063 | 3 | private function verifyPublicOnly(array $walletData) { |
|
| 1067 | |||
| 1068 | /** |
||
| 1069 | * create wallet using the API |
||
| 1070 | * |
||
| 1071 | * @param string $identifier the wallet identifier to create |
||
| 1072 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1073 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1074 | * @param string $primaryMnemonic mnemonic to store |
||
| 1075 | * @param string $checksum checksum to store |
||
| 1076 | * @param int $keyIndex account that we expect to use |
||
| 1077 | * @param bool $segwit opt in to segwit |
||
| 1078 | * @return mixed |
||
| 1079 | */ |
||
| 1080 | public function storeNewWalletV1($identifier, $primaryPublicKey, $backupPublicKey, $primaryMnemonic, $checksum, $keyIndex, $segwit = false) { |
||
| 1094 | |||
| 1095 | /** |
||
| 1096 | * create wallet using the API |
||
| 1097 | * |
||
| 1098 | * @param string $identifier the wallet identifier to create |
||
| 1099 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1100 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1101 | * @param $encryptedPrimarySeed |
||
| 1102 | * @param $encryptedSecret |
||
| 1103 | * @param $recoverySecret |
||
| 1104 | * @param string $checksum checksum to store |
||
| 1105 | * @param int $keyIndex account that we expect to use |
||
| 1106 | * @param bool $segwit opt in to segwit |
||
| 1107 | * @return mixed |
||
| 1108 | * @throws \Exception |
||
| 1109 | */ |
||
| 1110 | 3 | public function storeNewWalletV2($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false) { |
|
| 1127 | |||
| 1128 | /** |
||
| 1129 | * create wallet using the API |
||
| 1130 | * |
||
| 1131 | * @param string $identifier the wallet identifier to create |
||
| 1132 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1133 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1134 | * @param $encryptedPrimarySeed |
||
| 1135 | * @param $encryptedSecret |
||
| 1136 | * @param $recoverySecret |
||
| 1137 | * @param string $checksum checksum to store |
||
| 1138 | * @param int $keyIndex account that we expect to use |
||
| 1139 | * @param bool $segwit opt in to segwit |
||
| 1140 | * @return mixed |
||
| 1141 | * @throws \Exception |
||
| 1142 | */ |
||
| 1143 | public function storeNewWalletV3($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false) { |
||
| 1162 | |||
| 1163 | /** |
||
| 1164 | * upgrade wallet to use a new account number |
||
| 1165 | * the account number specifies which blocktrail cosigning key is used |
||
| 1166 | * |
||
| 1167 | * @param string $identifier the wallet identifier to be upgraded |
||
| 1168 | * @param int $keyIndex the new account to use |
||
| 1169 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1170 | * @return mixed |
||
| 1171 | */ |
||
| 1172 | public function upgradeKeyIndex($identifier, $keyIndex, $primaryPublicKey) { |
||
| 1181 | |||
| 1182 | /** |
||
| 1183 | * @param array $options |
||
| 1184 | * @return AddressReaderBase |
||
| 1185 | */ |
||
| 1186 | private function makeAddressReader(array $options) { |
||
| 1197 | |||
| 1198 | /** |
||
| 1199 | * initialize a previously created wallet |
||
| 1200 | * |
||
| 1201 | * Takes an options object, or accepts identifier/password for backwards compatiblity. |
||
| 1202 | * |
||
| 1203 | * Some of the options: |
||
| 1204 | * - "readonly/readOnly/read-only" can be to a boolean value, |
||
| 1205 | * so the wallet is loaded in read-only mode (no private key) |
||
| 1206 | * - "check_backup_key" can be set to your own backup key: |
||
| 1207 | * Format: ["M', "xpub..."] |
||
| 1208 | * Setting this will allow the SDK to check the server hasn't |
||
| 1209 | * a different key (one it happens to control) |
||
| 1210 | |||
| 1211 | * Either takes one argument: |
||
| 1212 | * @param array $options |
||
| 1213 | * |
||
| 1214 | * Or takes two arguments (old, deprecated syntax): |
||
| 1215 | * (@nonPHP-doc) @param string $identifier the wallet identifier to be initialized |
||
| 1216 | * (@nonPHP-doc) @param string $password the password to decrypt the mnemonic with |
||
| 1217 | * |
||
| 1218 | * @return WalletInterface |
||
| 1219 | * @throws \Exception |
||
| 1220 | */ |
||
| 1221 | 23 | public function initWallet($options) { |
|
| 1333 | |||
| 1334 | /** |
||
| 1335 | * get the wallet data from the server |
||
| 1336 | * |
||
| 1337 | * @param string $identifier the identifier of the wallet |
||
| 1338 | * @return mixed |
||
| 1339 | */ |
||
| 1340 | 23 | public function getWallet($identifier) { |
|
| 1344 | |||
| 1345 | /** |
||
| 1346 | * update the wallet data on the server |
||
| 1347 | * |
||
| 1348 | * @param string $identifier |
||
| 1349 | * @param $data |
||
| 1350 | * @return mixed |
||
| 1351 | */ |
||
| 1352 | public function updateWallet($identifier, $data) { |
||
| 1356 | |||
| 1357 | /** |
||
| 1358 | * delete a wallet from the server |
||
| 1359 | * the checksum address and a signature to verify you ownership of the key of that checksum address |
||
| 1360 | * is required to be able to delete a wallet |
||
| 1361 | * |
||
| 1362 | * @param string $identifier the identifier of the wallet |
||
| 1363 | * @param string $checksumAddress the address for your master private key (and the checksum used when creating the wallet) |
||
| 1364 | * @param string $signature a signature of the checksum address as message signed by the private key matching that address |
||
| 1365 | * @param bool $force ignore warnings (such as a non-zero balance) |
||
| 1366 | * @return mixed |
||
| 1367 | */ |
||
| 1368 | public function deleteWallet($identifier, $checksumAddress, $signature, $force = false) { |
||
| 1375 | |||
| 1376 | /** |
||
| 1377 | * create new backup key; |
||
| 1378 | * 1) a BIP39 mnemonic |
||
| 1379 | * 2) a seed from that mnemonic with a blank password |
||
| 1380 | * 3) a private key from that seed |
||
| 1381 | * |
||
| 1382 | * @return array [mnemonic, seed, key] |
||
| 1383 | */ |
||
| 1384 | protected function newBackupSeed() { |
||
| 1389 | |||
| 1390 | /** |
||
| 1391 | * create new primary key; |
||
| 1392 | * 1) a BIP39 mnemonic |
||
| 1393 | * 2) a seed from that mnemonic with the password |
||
| 1394 | * 3) a private key from that seed |
||
| 1395 | * |
||
| 1396 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1397 | * @return array [mnemonic, seed, key] |
||
| 1398 | * @TODO: require a strong password? |
||
| 1399 | */ |
||
| 1400 | protected function newPrimarySeed($passphrase) { |
||
| 1405 | |||
| 1406 | /** |
||
| 1407 | * create a new key; |
||
| 1408 | * 1) a BIP39 mnemonic |
||
| 1409 | * 2) a seed from that mnemonic with the password |
||
| 1410 | * 3) a private key from that seed |
||
| 1411 | * |
||
| 1412 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1413 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1414 | * @return array |
||
| 1415 | */ |
||
| 1416 | protected function generateNewSeed($passphrase = "", $forceEntropy = null) { |
||
| 1433 | |||
| 1434 | /** |
||
| 1435 | * generate a new mnemonic from some random entropy (512 bit) |
||
| 1436 | * |
||
| 1437 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1438 | * @return string |
||
| 1439 | * @throws \Exception |
||
| 1440 | */ |
||
| 1441 | protected function generateNewMnemonic($forceEntropy = null) { |
||
| 1451 | |||
| 1452 | /** |
||
| 1453 | * get the balance for the wallet |
||
| 1454 | * |
||
| 1455 | * @param string $identifier the identifier of the wallet |
||
| 1456 | * @return array |
||
| 1457 | */ |
||
| 1458 | public function getWalletBalance($identifier) { |
||
| 1462 | |||
| 1463 | /** |
||
| 1464 | * get a new derivation number for specified parent path |
||
| 1465 | * eg; m/44'/1'/0/0 results in m/44'/1'/0/0/0 and next time in m/44'/1'/0/0/1 and next time in m/44'/1'/0/0/2 |
||
| 1466 | * |
||
| 1467 | * returns the path |
||
| 1468 | * |
||
| 1469 | * @param string $identifier the identifier of the wallet |
||
| 1470 | * @param string $path the parent path for which to get a new derivation |
||
| 1471 | * @return string |
||
| 1472 | */ |
||
| 1473 | public function getNewDerivation($identifier, $path) { |
||
| 1477 | |||
| 1478 | /** |
||
| 1479 | * get a new derivation number for specified parent path |
||
| 1480 | * eg; m/44'/1'/0/0 results in m/44'/1'/0/0/0 and next time in m/44'/1'/0/0/1 and next time in m/44'/1'/0/0/2 |
||
| 1481 | * |
||
| 1482 | * @param string $identifier the identifier of the wallet |
||
| 1483 | * @param string $path the parent path for which to get a new derivation |
||
| 1484 | * @return mixed |
||
| 1485 | */ |
||
| 1486 | public function _getNewDerivation($identifier, $path) { |
||
| 1490 | |||
| 1491 | /** |
||
| 1492 | * get the path (and redeemScript) to specified address |
||
| 1493 | * |
||
| 1494 | * @param string $identifier |
||
| 1495 | * @param string $address |
||
| 1496 | * @return array |
||
| 1497 | * @throws \Exception |
||
| 1498 | */ |
||
| 1499 | public function getPathForAddress($identifier, $address) { |
||
| 1503 | |||
| 1504 | /** |
||
| 1505 | * send the transaction using the API |
||
| 1506 | * |
||
| 1507 | * @param string $identifier the identifier of the wallet |
||
| 1508 | * @param string|array $rawTransaction raw hex of the transaction (should be partially signed) |
||
| 1509 | * @param array $paths list of the paths that were used for the UTXO |
||
| 1510 | * @param bool $checkFee let the server verify the fee after signing |
||
| 1511 | * @param null $twoFactorToken |
||
| 1512 | * @return string the complete raw transaction |
||
| 1513 | * @throws \Exception |
||
| 1514 | */ |
||
| 1515 | public function sendTransaction($identifier, $rawTransaction, $paths, $checkFee = false, $twoFactorToken = null) { |
||
| 1546 | |||
| 1547 | /** |
||
| 1548 | * use the API to get the best inputs to use based on the outputs |
||
| 1549 | * |
||
| 1550 | * the return array has the following format: |
||
| 1551 | * [ |
||
| 1552 | * "utxos" => [ |
||
| 1553 | * [ |
||
| 1554 | * "hash" => "<txHash>", |
||
| 1555 | * "idx" => "<index of the output of that <txHash>", |
||
| 1556 | * "scriptpubkey_hex" => "<scriptPubKey-hex>", |
||
| 1557 | * "value" => 32746327, |
||
| 1558 | * "address" => "1address", |
||
| 1559 | * "path" => "m/44'/1'/0'/0/13", |
||
| 1560 | * "redeem_script" => "<redeemScript-hex>", |
||
| 1561 | * ], |
||
| 1562 | * ], |
||
| 1563 | * "fee" => 10000, |
||
| 1564 | * "change"=> 1010109201, |
||
| 1565 | * ] |
||
| 1566 | * |
||
| 1567 | * @param string $identifier the identifier of the wallet |
||
| 1568 | * @param array $outputs the outputs you want to create - array[address => satoshi-value] |
||
| 1569 | * @param bool $lockUTXO when TRUE the UTXOs selected will be locked for a few seconds |
||
| 1570 | * so you have some time to spend them without race-conditions |
||
| 1571 | * @param bool $allowZeroConf |
||
| 1572 | * @param string $feeStrategy |
||
| 1573 | * @param null|int $forceFee |
||
| 1574 | * @return array |
||
| 1575 | * @throws \Exception |
||
| 1576 | */ |
||
| 1577 | public function coinSelection($identifier, $outputs, $lockUTXO = false, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null) { |
||
| 1597 | |||
| 1598 | /** |
||
| 1599 | * |
||
| 1600 | * @param string $identifier the identifier of the wallet |
||
| 1601 | * @param bool $allowZeroConf |
||
| 1602 | * @param string $feeStrategy |
||
| 1603 | * @param null|int $forceFee |
||
| 1604 | * @param int $outputCnt |
||
| 1605 | * @return array |
||
| 1606 | * @throws \Exception |
||
| 1607 | */ |
||
| 1608 | public function walletMaxSpendable($identifier, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null, $outputCnt = 1) { |
||
| 1627 | |||
| 1628 | /** |
||
| 1629 | * @return array ['optimal_fee' => 10000, 'low_priority_fee' => 5000] |
||
| 1630 | */ |
||
| 1631 | public function feePerKB() { |
||
| 1635 | |||
| 1636 | /** |
||
| 1637 | * get the current price index |
||
| 1638 | * |
||
| 1639 | * @return array eg; ['USD' => 287.30] |
||
| 1640 | */ |
||
| 1641 | 1 | public function price() { |
|
| 1645 | |||
| 1646 | /** |
||
| 1647 | * setup webhook for wallet |
||
| 1648 | * |
||
| 1649 | * @param string $identifier the wallet identifier for which to create the webhook |
||
| 1650 | * @param string $webhookIdentifier the webhook identifier to use |
||
| 1651 | * @param string $url the url to receive the webhook events |
||
| 1652 | * @return array |
||
| 1653 | */ |
||
| 1654 | public function setupWalletWebhook($identifier, $webhookIdentifier, $url) { |
||
| 1658 | |||
| 1659 | /** |
||
| 1660 | * delete webhook for wallet |
||
| 1661 | * |
||
| 1662 | * @param string $identifier the wallet identifier for which to delete the webhook |
||
| 1663 | * @param string $webhookIdentifier the webhook identifier to delete |
||
| 1664 | * @return array |
||
| 1665 | */ |
||
| 1666 | public function deleteWalletWebhook($identifier, $webhookIdentifier) { |
||
| 1670 | |||
| 1671 | /** |
||
| 1672 | * lock a specific unspent output |
||
| 1673 | * |
||
| 1674 | * @param $identifier |
||
| 1675 | * @param $txHash |
||
| 1676 | * @param $txIdx |
||
| 1677 | * @param int $ttl |
||
| 1678 | * @return bool |
||
| 1679 | */ |
||
| 1680 | public function lockWalletUTXO($identifier, $txHash, $txIdx, $ttl = 3) { |
||
| 1684 | |||
| 1685 | /** |
||
| 1686 | * unlock a specific unspent output |
||
| 1687 | * |
||
| 1688 | * @param $identifier |
||
| 1689 | * @param $txHash |
||
| 1690 | * @param $txIdx |
||
| 1691 | * @return bool |
||
| 1692 | */ |
||
| 1693 | public function unlockWalletUTXO($identifier, $txHash, $txIdx) { |
||
| 1697 | |||
| 1698 | /** |
||
| 1699 | * get all transactions for wallet (paginated) |
||
| 1700 | * |
||
| 1701 | * @param string $identifier the wallet identifier for which to get transactions |
||
| 1702 | * @param integer $page pagination: page number |
||
| 1703 | * @param integer $limit pagination: records per page (max 500) |
||
| 1704 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1705 | * @return array associative array containing the response |
||
| 1706 | */ |
||
| 1707 | public function walletTransactions($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 1716 | |||
| 1717 | /** |
||
| 1718 | * get all addresses for wallet (paginated) |
||
| 1719 | * |
||
| 1720 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1721 | * @param integer $page pagination: page number |
||
| 1722 | * @param integer $limit pagination: records per page (max 500) |
||
| 1723 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1724 | * @return array associative array containing the response |
||
| 1725 | */ |
||
| 1726 | public function walletAddresses($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 1735 | |||
| 1736 | /** |
||
| 1737 | * get all UTXOs for wallet (paginated) |
||
| 1738 | * |
||
| 1739 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1740 | * @param integer $page pagination: page number |
||
| 1741 | * @param integer $limit pagination: records per page (max 500) |
||
| 1742 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1743 | * @param boolean $zeroconf include zero confirmation transactions |
||
| 1744 | * @return array associative array containing the response |
||
| 1745 | */ |
||
| 1746 | public function walletUTXOs($identifier, $page = 1, $limit = 20, $sortDir = 'asc', $zeroconf = true) { |
||
| 1756 | |||
| 1757 | /** |
||
| 1758 | * get a paginated list of all wallets associated with the api user |
||
| 1759 | * |
||
| 1760 | * @param integer $page pagination: page number |
||
| 1761 | * @param integer $limit pagination: records per page |
||
| 1762 | * @return array associative array containing the response |
||
| 1763 | */ |
||
| 1764 | public function allWallets($page = 1, $limit = 20) { |
||
| 1772 | |||
| 1773 | /** |
||
| 1774 | * send raw transaction |
||
| 1775 | * |
||
| 1776 | * @param $txHex |
||
| 1777 | * @return bool |
||
| 1778 | */ |
||
| 1779 | public function sendRawTransaction($txHex) { |
||
| 1783 | |||
| 1784 | /** |
||
| 1785 | * testnet only ;-) |
||
| 1786 | * |
||
| 1787 | * @param $address |
||
| 1788 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1789 | * @return mixed |
||
| 1790 | * @throws \Exception |
||
| 1791 | */ |
||
| 1792 | public function faucetWithdrawal($address, $amount = 10000) { |
||
| 1799 | |||
| 1800 | /** |
||
| 1801 | * Exists for BC. Remove at major bump. |
||
| 1802 | * |
||
| 1803 | * @see faucetWithdrawal |
||
| 1804 | * @deprecated |
||
| 1805 | * @param $address |
||
| 1806 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1807 | * @return mixed |
||
| 1808 | * @throws \Exception |
||
| 1809 | */ |
||
| 1810 | public function faucetWithdrawl($address, $amount = 10000) { |
||
| 1813 | |||
| 1814 | /** |
||
| 1815 | * verify a message signed bitcoin-core style |
||
| 1816 | * |
||
| 1817 | * @param string $message |
||
| 1818 | * @param string $address |
||
| 1819 | * @param string $signature |
||
| 1820 | * @return boolean |
||
| 1821 | */ |
||
| 1822 | 2 | public function verifyMessage($message, $address, $signature) { |
|
| 1836 | |||
| 1837 | /** |
||
| 1838 | * Take a base58 or cashaddress, and return only |
||
| 1839 | * the cash address. |
||
| 1840 | * This function only works on bitcoin cash. |
||
| 1841 | * @param string $input |
||
| 1842 | * @return string |
||
| 1843 | * @throws BlocktrailSDKException |
||
| 1844 | */ |
||
| 1845 | public function getLegacyBitcoinCashAddress($input) { |
||
| 1862 | |||
| 1863 | /** |
||
| 1864 | * convert a Satoshi value to a BTC value |
||
| 1865 | * |
||
| 1866 | * @param int $satoshi |
||
| 1867 | * @return float |
||
| 1868 | */ |
||
| 1869 | 1 | public static function toBTC($satoshi) { |
|
| 1872 | |||
| 1873 | /** |
||
| 1874 | * convert a Satoshi value to a BTC value and return it as a string |
||
| 1875 | |||
| 1876 | * @param int $satoshi |
||
| 1877 | * @return string |
||
| 1878 | */ |
||
| 1879 | public static function toBTCString($satoshi) { |
||
| 1882 | |||
| 1883 | /** |
||
| 1884 | * convert a BTC value to a Satoshi value |
||
| 1885 | * |
||
| 1886 | * @param float $btc |
||
| 1887 | * @return string |
||
| 1888 | */ |
||
| 1889 | 1 | public static function toSatoshiString($btc) { |
|
| 1892 | |||
| 1893 | /** |
||
| 1894 | * convert a BTC value to a Satoshi value |
||
| 1895 | * |
||
| 1896 | * @param float $btc |
||
| 1897 | * @return string |
||
| 1898 | */ |
||
| 1899 | 1 | public static function toSatoshi($btc) { |
|
| 1902 | |||
| 1903 | /** |
||
| 1904 | * json_decode helper that throws exceptions when it fails to decode |
||
| 1905 | * |
||
| 1906 | * @param $json |
||
| 1907 | * @param bool $assoc |
||
| 1908 | * @return mixed |
||
| 1909 | * @throws \Exception |
||
| 1910 | */ |
||
| 1911 | 3 | public static function jsonDecode($json, $assoc = false) { |
|
| 1924 | |||
| 1925 | /** |
||
| 1926 | * sort public keys for multisig script |
||
| 1927 | * |
||
| 1928 | * @param PublicKeyInterface[] $pubKeys |
||
| 1929 | * @return PublicKeyInterface[] |
||
| 1930 | */ |
||
| 1931 | public static function sortMultisigKeys(array $pubKeys) { |
||
| 1941 | |||
| 1942 | /** |
||
| 1943 | * read and decode the json payload from a webhook's POST request. |
||
| 1944 | * |
||
| 1945 | * @param bool $returnObject flag to indicate if an object or associative array should be returned |
||
| 1946 | * @return mixed|null |
||
| 1947 | * @throws \Exception |
||
| 1948 | */ |
||
| 1949 | public static function getWebhookPayload($returnObject = false) { |
||
| 1957 | |||
| 1958 | public static function normalizeBIP32KeyArray($keys) { |
||
| 1963 | |||
| 1964 | /** |
||
| 1965 | * @param array|BIP32Key $key |
||
| 1966 | * @return BIP32Key |
||
| 1967 | * @throws \Exception |
||
| 1968 | */ |
||
| 1969 | public static function normalizeBIP32Key($key) { |
||
| 1987 | } |
||
| 1988 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: