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) { |
|
| 79 | |||
| 80 | 118 | list ($apiNetwork, $testnet) = Util::parseApiNetwork($network, $testnet); |
|
| 81 | |||
| 82 | 118 | if (is_null($apiEndpoint)) { |
|
| 83 | 118 | $apiEndpoint = getenv('BLOCKTRAIL_SDK_API_ENDPOINT') ?: "https://api.blocktrail.com"; |
|
| 84 | 118 | $apiEndpoint = "{$apiEndpoint}/{$apiVersion}/{$apiNetwork}/"; |
|
| 85 | } |
||
| 86 | |||
| 87 | 118 | $btccomEndpoint = getenv('BLOCKTRAIL_SDK_BTCCOM_API_ENDPOINT') ?: "https://chain.api.btc.com"; |
|
| 88 | 118 | $btccomEndpoint = "{$btccomEndpoint}/v3/"; |
|
| 89 | |||
| 90 | // normalize network and set bitcoinlib to the right magic-bytes |
||
| 91 | 118 | list($this->network, $this->testnet, $regtest) = $this->normalizeNetwork($network, $testnet); |
|
| 92 | 118 | $this->setBitcoinLibMagicBytes($this->network, $this->testnet, $regtest); |
|
| 93 | |||
| 94 | 118 | if ($this->testnet && strpos($btccomEndpoint, "tchain") === false) { |
|
| 95 | 33 | $btccomEndpoint = \str_replace("chain", "tchain", $btccomEndpoint); |
|
| 96 | } |
||
| 97 | |||
| 98 | 118 | $this->blocktrailClient = new RestClient($apiEndpoint, $apiVersion, $apiKey, $apiSecret); |
|
| 99 | 118 | $this->dataClient = new RestClient($btccomEndpoint, $apiVersion, $apiKey, $apiSecret); |
|
| 100 | |||
| 101 | 118 | $this->converter = new BtccomConverter(); |
|
| 102 | 118 | } |
|
| 103 | |||
| 104 | /** |
||
| 105 | * normalize network string |
||
| 106 | * |
||
| 107 | * @param $network |
||
| 108 | * @param $testnet |
||
| 109 | * @return array |
||
| 110 | * @throws \Exception |
||
| 111 | */ |
||
| 112 | 118 | protected function normalizeNetwork($network, $testnet) { |
|
| 113 | // [name, testnet, network] |
||
| 114 | 118 | return Util::normalizeNetwork($network, $testnet); |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * set BitcoinLib to the correct magic-byte defaults for the selected network |
||
| 119 | * |
||
| 120 | * @param $network |
||
| 121 | * @param bool $testnet |
||
| 122 | * @param bool $regtest |
||
| 123 | */ |
||
| 124 | 118 | protected function setBitcoinLibMagicBytes($network, $testnet, $regtest) { |
|
| 125 | |||
| 126 | 118 | if ($network === "bitcoin") { |
|
| 127 | 118 | if ($regtest) { |
|
| 128 | $useNetwork = NetworkFactory::bitcoinRegtest(); |
||
| 129 | 118 | } else if ($testnet) { |
|
| 130 | 29 | $useNetwork = NetworkFactory::bitcoinTestnet(); |
|
| 131 | } else { |
||
| 132 | 118 | $useNetwork = NetworkFactory::bitcoin(); |
|
| 133 | } |
||
| 134 | 4 | } else if ($network === "bitcoincash") { |
|
| 135 | 4 | if ($regtest) { |
|
| 136 | $useNetwork = new BitcoinCashRegtest(); |
||
| 137 | 4 | } else if ($testnet) { |
|
| 138 | 4 | $useNetwork = new BitcoinCashTestnet(); |
|
| 139 | } else { |
||
| 140 | $useNetwork = new BitcoinCash(); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | 118 | Bitcoin::setNetwork($useNetwork); |
|
|
|
|||
| 145 | 118 | } |
|
| 146 | |||
| 147 | /** |
||
| 148 | * enable CURL debugging output |
||
| 149 | * |
||
| 150 | * @param bool $debug |
||
| 151 | * |
||
| 152 | * @codeCoverageIgnore |
||
| 153 | */ |
||
| 154 | public function setCurlDebugging($debug = true) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * enable verbose errors |
||
| 161 | * |
||
| 162 | * @param bool $verboseErrors |
||
| 163 | * |
||
| 164 | * @codeCoverageIgnore |
||
| 165 | */ |
||
| 166 | public function setVerboseErrors($verboseErrors = true) { |
||
| 167 | $this->blocktrailClient->setVerboseErrors($verboseErrors); |
||
| 168 | $this->dataClient->setVerboseErrors($verboseErrors); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * set cURL default option on Guzzle client |
||
| 173 | * @param string $key |
||
| 174 | * @param bool $value |
||
| 175 | * |
||
| 176 | * @codeCoverageIgnore |
||
| 177 | */ |
||
| 178 | public function setCurlDefaultOption($key, $value) { |
||
| 179 | $this->blocktrailClient->setCurlDefaultOption($key, $value); |
||
| 180 | $this->dataClient->setCurlDefaultOption($key, $value); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return RestClientInterface |
||
| 185 | */ |
||
| 186 | 2 | public function getRestClient() { |
|
| 187 | 2 | return $this->blocktrailClient; |
|
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @return RestClient |
||
| 192 | */ |
||
| 193 | public function getDataRestClient() { |
||
| 194 | return $this->dataClient; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param RestClientInterface $restClient |
||
| 199 | */ |
||
| 200 | public function setRestClient(RestClientInterface $restClient) { |
||
| 201 | $this->client = $restClient; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * get a single address |
||
| 206 | * @param string $address address hash |
||
| 207 | * @return array associative array containing the response |
||
| 208 | */ |
||
| 209 | 1 | public function address($address) { |
|
| 213 | |||
| 214 | /** |
||
| 215 | * get all transactions for an address (paginated) |
||
| 216 | * @param string $address address hash |
||
| 217 | * @param integer $page pagination: page number |
||
| 218 | * @param integer $limit pagination: records per page (max 500) |
||
| 219 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 220 | * @return array associative array containing the response |
||
| 221 | */ |
||
| 222 | 1 | public function addressTransactions($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 231 | |||
| 232 | /** |
||
| 233 | * get all unconfirmed transactions for an address (paginated) |
||
| 234 | * @param string $address address hash |
||
| 235 | * @param integer $page pagination: page number |
||
| 236 | * @param integer $limit pagination: records per page (max 500) |
||
| 237 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 238 | * @return array associative array containing the response |
||
| 239 | */ |
||
| 240 | public function addressUnconfirmedTransactions($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * get all unspent outputs for an address (paginated) |
||
| 252 | * @param string $address address hash |
||
| 253 | * @param integer $page pagination: page number |
||
| 254 | * @param integer $limit pagination: records per page (max 500) |
||
| 255 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 256 | * @return array associative array containing the response |
||
| 257 | */ |
||
| 258 | 1 | public function addressUnspentOutputs($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 267 | |||
| 268 | /** |
||
| 269 | * get all unspent outputs for a batch of addresses (paginated) |
||
| 270 | * |
||
| 271 | * @param string[] $addresses |
||
| 272 | * @param integer $page pagination: page number |
||
| 273 | * @param integer $limit pagination: records per page (max 500) |
||
| 274 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 275 | * @return array associative array containing the response |
||
| 276 | * @throws \Exception |
||
| 277 | */ |
||
| 278 | public function batchAddressUnspentOutputs($addresses, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * verify ownership of an address |
||
| 305 | * @param string $address address hash |
||
| 306 | * @param string $signature a signed message (the address hash) using the private key of the address |
||
| 307 | * @return array associative array containing the response |
||
| 308 | */ |
||
| 309 | 1 | public function verifyAddress($address, $signature) { |
|
| 316 | |||
| 317 | /** |
||
| 318 | * get all blocks (paginated) |
||
| 319 | * @param integer $page pagination: page number |
||
| 320 | * @param integer $limit pagination: records per page |
||
| 321 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 322 | * @return array associative array containing the response |
||
| 323 | */ |
||
| 324 | 1 | public function allBlocks($page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 333 | |||
| 334 | /** |
||
| 335 | * get the latest block |
||
| 336 | * @return array associative array containing the response |
||
| 337 | */ |
||
| 338 | 1 | public function blockLatest() { |
|
| 342 | |||
| 343 | /** |
||
| 344 | * get an individual block |
||
| 345 | * @param string|integer $block a block hash or a block height |
||
| 346 | * @return array associative array containing the response |
||
| 347 | */ |
||
| 348 | 1 | public function block($block) { |
|
| 352 | |||
| 353 | /** |
||
| 354 | * get all transaction in a block (paginated) |
||
| 355 | * @param string|integer $block a block hash or a block height |
||
| 356 | * @param integer $page pagination: page number |
||
| 357 | * @param integer $limit pagination: records per page |
||
| 358 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 359 | * @return array associative array containing the response |
||
| 360 | */ |
||
| 361 | public function blockTransactions($block, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * get a single transaction |
||
| 373 | * @param string $txhash transaction hash |
||
| 374 | * @return array associative array containing the response |
||
| 375 | */ |
||
| 376 | 5 | public function transaction($txhash) { |
|
| 386 | |||
| 387 | /** |
||
| 388 | * get a single transaction |
||
| 389 | * @param string[] $txhashes list of transaction hashes (up to 20) |
||
| 390 | * @return array[] array containing the response |
||
| 391 | */ |
||
| 392 | 1 | public function transactions($txhashes) { |
|
| 396 | |||
| 397 | /** |
||
| 398 | * get a paginated list of all webhooks associated with the api user |
||
| 399 | * @param integer $page pagination: page number |
||
| 400 | * @param integer $limit pagination: records per page |
||
| 401 | * @return array associative array containing the response |
||
| 402 | */ |
||
| 403 | 1 | public function allWebhooks($page = 1, $limit = 20) { |
|
| 411 | |||
| 412 | /** |
||
| 413 | * get an existing webhook by it's identifier |
||
| 414 | * @param string $identifier a unique identifier associated with the webhook |
||
| 415 | * @return array associative array containing the response |
||
| 416 | */ |
||
| 417 | 1 | public function getWebhook($identifier) { |
|
| 421 | |||
| 422 | /** |
||
| 423 | * create a new webhook |
||
| 424 | * @param string $url the url to receive the webhook events |
||
| 425 | * @param string $identifier a unique identifier to associate with this webhook |
||
| 426 | * @return array associative array containing the response |
||
| 427 | */ |
||
| 428 | 1 | public function setupWebhook($url, $identifier = null) { |
|
| 436 | |||
| 437 | /** |
||
| 438 | * update an existing webhook |
||
| 439 | * @param string $identifier the unique identifier of the webhook to update |
||
| 440 | * @param string $newUrl the new url to receive the webhook events |
||
| 441 | * @param string $newIdentifier a new unique identifier to associate with this webhook |
||
| 442 | * @return array associative array containing the response |
||
| 443 | */ |
||
| 444 | 1 | public function updateWebhook($identifier, $newUrl = null, $newIdentifier = null) { |
|
| 452 | |||
| 453 | /** |
||
| 454 | * deletes an existing webhook and any event subscriptions associated with it |
||
| 455 | * @param string $identifier the unique identifier of the webhook to delete |
||
| 456 | * @return boolean true on success |
||
| 457 | */ |
||
| 458 | 1 | public function deleteWebhook($identifier) { |
|
| 462 | |||
| 463 | /** |
||
| 464 | * get a paginated list of all the events a webhook is subscribed to |
||
| 465 | * @param string $identifier the unique identifier of the webhook |
||
| 466 | * @param integer $page pagination: page number |
||
| 467 | * @param integer $limit pagination: records per page |
||
| 468 | * @return array associative array containing the response |
||
| 469 | */ |
||
| 470 | 1 | public function getWebhookEvents($identifier, $page = 1, $limit = 20) { |
|
| 478 | |||
| 479 | /** |
||
| 480 | * subscribes a webhook to transaction events of one particular transaction |
||
| 481 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 482 | * @param string $transaction the transaction hash |
||
| 483 | * @param integer $confirmations the amount of confirmations to send. |
||
| 484 | * @return array associative array containing the response |
||
| 485 | */ |
||
| 486 | public function subscribeTransaction($identifier, $transaction, $confirmations = 6) { |
||
| 487 | $postData = [ |
||
| 488 | 'event_type' => 'transaction', |
||
| 489 | 'transaction' => $transaction, |
||
| 490 | 'confirmations' => $confirmations, |
||
| 491 | ]; |
||
| 492 | $response = $this->blocktrailClient->post("webhook/{$identifier}/events", null, $postData, RestClient::AUTH_HTTP_SIG); |
||
| 493 | return self::jsonDecode($response->body(), true); |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * subscribes a webhook to transaction events on a particular address |
||
| 498 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 499 | * @param string $address the address hash |
||
| 500 | * @param integer $confirmations the amount of confirmations to send. |
||
| 501 | * @return array associative array containing the response |
||
| 502 | */ |
||
| 503 | public function subscribeAddressTransactions($identifier, $address, $confirmations = 6) { |
||
| 504 | $postData = [ |
||
| 505 | 'event_type' => 'address-transactions', |
||
| 506 | 'address' => $address, |
||
| 507 | 'confirmations' => $confirmations, |
||
| 508 | ]; |
||
| 509 | $response = $this->blocktrailClient->post("webhook/{$identifier}/events", null, $postData, RestClient::AUTH_HTTP_SIG); |
||
| 510 | return self::jsonDecode($response->body(), true); |
||
| 511 | } |
||
| 512 | |||
| 513 | /** |
||
| 514 | * batch subscribes a webhook to multiple transaction events |
||
| 515 | * |
||
| 516 | * @param string $identifier the unique identifier of the webhook |
||
| 517 | * @param array $batchData A 2D array of event data: |
||
| 518 | * [address => $address, confirmations => $confirmations] |
||
| 519 | * where $address is the address to subscibe to |
||
| 520 | * and optionally $confirmations is the amount of confirmations |
||
| 521 | * @return boolean true on success |
||
| 522 | */ |
||
| 523 | public function batchSubscribeAddressTransactions($identifier, $batchData) { |
||
| 524 | $postData = []; |
||
| 525 | foreach ($batchData as $record) { |
||
| 526 | $postData[] = [ |
||
| 527 | 'event_type' => 'address-transactions', |
||
| 528 | 'address' => $record['address'], |
||
| 529 | 'confirmations' => isset($record['confirmations']) ? $record['confirmations'] : 6, |
||
| 530 | ]; |
||
| 531 | } |
||
| 532 | $response = $this->blocktrailClient->post("webhook/{$identifier}/events/batch", null, $postData, RestClient::AUTH_HTTP_SIG); |
||
| 533 | return self::jsonDecode($response->body(), true); |
||
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * subscribes a webhook to a new block event |
||
| 538 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 539 | * @return array associative array containing the response |
||
| 540 | */ |
||
| 541 | public function subscribeNewBlocks($identifier) { |
||
| 542 | $postData = [ |
||
| 543 | 'event_type' => 'block', |
||
| 544 | ]; |
||
| 545 | $response = $this->blocktrailClient->post("webhook/{$identifier}/events", null, $postData, RestClient::AUTH_HTTP_SIG); |
||
| 546 | return self::jsonDecode($response->body(), true); |
||
| 547 | } |
||
| 548 | |||
| 549 | /** |
||
| 550 | * removes an transaction event subscription from a webhook |
||
| 551 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 552 | * @param string $transaction the transaction hash of the event subscription |
||
| 553 | * @return boolean true on success |
||
| 554 | */ |
||
| 555 | public function unsubscribeTransaction($identifier, $transaction) { |
||
| 556 | $response = $this->blocktrailClient->delete("webhook/{$identifier}/transaction/{$transaction}", null, null, RestClient::AUTH_HTTP_SIG); |
||
| 557 | return self::jsonDecode($response->body(), true); |
||
| 558 | } |
||
| 559 | |||
| 560 | /** |
||
| 561 | * removes an address transaction event subscription from a webhook |
||
| 562 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 563 | * @param string $address the address hash of the event subscription |
||
| 564 | * @return boolean true on success |
||
| 565 | */ |
||
| 566 | public function unsubscribeAddressTransactions($identifier, $address) { |
||
| 567 | $response = $this->blocktrailClient->delete("webhook/{$identifier}/address-transactions/{$address}", null, null, RestClient::AUTH_HTTP_SIG); |
||
| 568 | return self::jsonDecode($response->body(), true); |
||
| 569 | } |
||
| 570 | |||
| 571 | /** |
||
| 572 | * removes a block event subscription from a webhook |
||
| 573 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 574 | * @return boolean true on success |
||
| 575 | */ |
||
| 576 | public function unsubscribeNewBlocks($identifier) { |
||
| 577 | $response = $this->blocktrailClient->delete("webhook/{$identifier}/block", null, null, RestClient::AUTH_HTTP_SIG); |
||
| 578 | return self::jsonDecode($response->body(), true); |
||
| 579 | } |
||
| 580 | |||
| 581 | /** |
||
| 582 | * create a new wallet |
||
| 583 | * - will generate a new primary seed (with password) and backup seed (without password) |
||
| 584 | * - send the primary seed (BIP39 'encrypted') and backup public key to the server |
||
| 585 | * - receive the blocktrail co-signing public key from the server |
||
| 586 | * |
||
| 587 | * Either takes one argument: |
||
| 588 | * @param array $options |
||
| 589 | * |
||
| 590 | * Or takes three arguments (old, deprecated syntax): |
||
| 591 | * (@nonPHP-doc) @param $identifier |
||
| 592 | * (@nonPHP-doc) @param $password |
||
| 593 | * (@nonPHP-doc) @param int $keyIndex override for the blocktrail cosigning key to use |
||
| 594 | * |
||
| 595 | * @return array[WalletInterface, array] list($wallet, $backupInfo) |
||
| 596 | * @throws \Exception |
||
| 597 | */ |
||
| 598 | 7 | public function createNewWallet($options) { |
|
| 642 | |||
| 643 | 1 | protected function createNewWalletV1($options) { |
|
| 763 | |||
| 764 | 5 | public static function randomBits($bits) { |
|
| 767 | |||
| 768 | 5 | public static function randomBytes($bytes) { |
|
| 771 | |||
| 772 | 2 | protected function createNewWalletV2($options) { |
|
| 895 | |||
| 896 | 4 | protected function createNewWalletV3($options) { |
|
| 1040 | |||
| 1041 | /** |
||
| 1042 | * @param array $bip32Key |
||
| 1043 | * @throws BlocktrailSDKException |
||
| 1044 | */ |
||
| 1045 | 10 | private function verifyPublicBIP32Key(array $bip32Key) { |
|
| 1055 | |||
| 1056 | /** |
||
| 1057 | * @param array $walletData |
||
| 1058 | * @throws BlocktrailSDKException |
||
| 1059 | */ |
||
| 1060 | 10 | private function verifyPublicOnly(array $walletData) { |
|
| 1064 | |||
| 1065 | /** |
||
| 1066 | * create wallet using the API |
||
| 1067 | * |
||
| 1068 | * @param string $identifier the wallet identifier to create |
||
| 1069 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1070 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1071 | * @param string $primaryMnemonic mnemonic to store |
||
| 1072 | * @param string $checksum checksum to store |
||
| 1073 | * @param int $keyIndex account that we expect to use |
||
| 1074 | * @param bool $segwit opt in to segwit |
||
| 1075 | * @return mixed |
||
| 1076 | */ |
||
| 1077 | 1 | public function storeNewWalletV1($identifier, $primaryPublicKey, $backupPublicKey, $primaryMnemonic, $checksum, $keyIndex, $segwit = false) { |
|
| 1091 | |||
| 1092 | /** |
||
| 1093 | * create wallet using the API |
||
| 1094 | * |
||
| 1095 | * @param string $identifier the wallet identifier to create |
||
| 1096 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1097 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1098 | * @param $encryptedPrimarySeed |
||
| 1099 | * @param $encryptedSecret |
||
| 1100 | * @param $recoverySecret |
||
| 1101 | * @param string $checksum checksum to store |
||
| 1102 | * @param int $keyIndex account that we expect to use |
||
| 1103 | * @param bool $segwit opt in to segwit |
||
| 1104 | * @return mixed |
||
| 1105 | * @throws \Exception |
||
| 1106 | */ |
||
| 1107 | 5 | public function storeNewWalletV2($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false) { |
|
| 1124 | |||
| 1125 | /** |
||
| 1126 | * create wallet using the API |
||
| 1127 | * |
||
| 1128 | * @param string $identifier the wallet identifier to create |
||
| 1129 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1130 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1131 | * @param $encryptedPrimarySeed |
||
| 1132 | * @param $encryptedSecret |
||
| 1133 | * @param $recoverySecret |
||
| 1134 | * @param string $checksum checksum to store |
||
| 1135 | * @param int $keyIndex account that we expect to use |
||
| 1136 | * @param bool $segwit opt in to segwit |
||
| 1137 | * @return mixed |
||
| 1138 | * @throws \Exception |
||
| 1139 | */ |
||
| 1140 | 4 | public function storeNewWalletV3($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false) { |
|
| 1159 | |||
| 1160 | /** |
||
| 1161 | * upgrade wallet to use a new account number |
||
| 1162 | * the account number specifies which blocktrail cosigning key is used |
||
| 1163 | * |
||
| 1164 | * @param string $identifier the wallet identifier to be upgraded |
||
| 1165 | * @param int $keyIndex the new account to use |
||
| 1166 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1167 | * @return mixed |
||
| 1168 | */ |
||
| 1169 | 5 | public function upgradeKeyIndex($identifier, $keyIndex, $primaryPublicKey) { |
|
| 1178 | |||
| 1179 | /** |
||
| 1180 | * @param array $options |
||
| 1181 | * @return AddressReaderBase |
||
| 1182 | */ |
||
| 1183 | 23 | private function makeAddressReader(array $options) { |
|
| 1194 | |||
| 1195 | /** |
||
| 1196 | * initialize a previously created wallet |
||
| 1197 | * |
||
| 1198 | * Takes an options object, or accepts identifier/password for backwards compatiblity. |
||
| 1199 | * |
||
| 1200 | * Some of the options: |
||
| 1201 | * - "readonly/readOnly/read-only" can be to a boolean value, |
||
| 1202 | * so the wallet is loaded in read-only mode (no private key) |
||
| 1203 | * - "check_backup_key" can be set to your own backup key: |
||
| 1204 | * Format: ["M', "xpub..."] |
||
| 1205 | * Setting this will allow the SDK to check the server hasn't |
||
| 1206 | * a different key (one it happens to control) |
||
| 1207 | |||
| 1208 | * Either takes one argument: |
||
| 1209 | * @param array $options |
||
| 1210 | * |
||
| 1211 | * Or takes two arguments (old, deprecated syntax): |
||
| 1212 | * (@nonPHP-doc) @param string $identifier the wallet identifier to be initialized |
||
| 1213 | * (@nonPHP-doc) @param string $password the password to decrypt the mnemonic with |
||
| 1214 | * |
||
| 1215 | * @return WalletInterface |
||
| 1216 | * @throws \Exception |
||
| 1217 | */ |
||
| 1218 | 23 | public function initWallet($options) { |
|
| 1330 | |||
| 1331 | /** |
||
| 1332 | * get the wallet data from the server |
||
| 1333 | * |
||
| 1334 | * @param string $identifier the identifier of the wallet |
||
| 1335 | * @return mixed |
||
| 1336 | */ |
||
| 1337 | 23 | public function getWallet($identifier) { |
|
| 1341 | |||
| 1342 | /** |
||
| 1343 | * update the wallet data on the server |
||
| 1344 | * |
||
| 1345 | * @param string $identifier |
||
| 1346 | * @param $data |
||
| 1347 | * @return mixed |
||
| 1348 | */ |
||
| 1349 | 3 | public function updateWallet($identifier, $data) { |
|
| 1353 | |||
| 1354 | /** |
||
| 1355 | * delete a wallet from the server |
||
| 1356 | * the checksum address and a signature to verify you ownership of the key of that checksum address |
||
| 1357 | * is required to be able to delete a wallet |
||
| 1358 | * |
||
| 1359 | * @param string $identifier the identifier of the wallet |
||
| 1360 | * @param string $checksumAddress the address for your master private key (and the checksum used when creating the wallet) |
||
| 1361 | * @param string $signature a signature of the checksum address as message signed by the private key matching that address |
||
| 1362 | * @param bool $force ignore warnings (such as a non-zero balance) |
||
| 1363 | * @return mixed |
||
| 1364 | */ |
||
| 1365 | 10 | public function deleteWallet($identifier, $checksumAddress, $signature, $force = false) { |
|
| 1372 | |||
| 1373 | /** |
||
| 1374 | * create new backup key; |
||
| 1375 | * 1) a BIP39 mnemonic |
||
| 1376 | * 2) a seed from that mnemonic with a blank password |
||
| 1377 | * 3) a private key from that seed |
||
| 1378 | * |
||
| 1379 | * @return array [mnemonic, seed, key] |
||
| 1380 | */ |
||
| 1381 | 1 | protected function newBackupSeed() { |
|
| 1386 | |||
| 1387 | /** |
||
| 1388 | * create new primary key; |
||
| 1389 | * 1) a BIP39 mnemonic |
||
| 1390 | * 2) a seed from that mnemonic with the password |
||
| 1391 | * 3) a private key from that seed |
||
| 1392 | * |
||
| 1393 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1394 | * @return array [mnemonic, seed, key] |
||
| 1395 | * @TODO: require a strong password? |
||
| 1396 | */ |
||
| 1397 | 1 | protected function newPrimarySeed($passphrase) { |
|
| 1402 | |||
| 1403 | /** |
||
| 1404 | * create a new key; |
||
| 1405 | * 1) a BIP39 mnemonic |
||
| 1406 | * 2) a seed from that mnemonic with the password |
||
| 1407 | * 3) a private key from that seed |
||
| 1408 | * |
||
| 1409 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1410 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1411 | * @return array |
||
| 1412 | */ |
||
| 1413 | 1 | protected function generateNewSeed($passphrase = "", $forceEntropy = null) { |
|
| 1430 | |||
| 1431 | /** |
||
| 1432 | * generate a new mnemonic from some random entropy (512 bit) |
||
| 1433 | * |
||
| 1434 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1435 | * @return string |
||
| 1436 | * @throws \Exception |
||
| 1437 | */ |
||
| 1438 | 1 | protected function generateNewMnemonic($forceEntropy = null) { |
|
| 1448 | |||
| 1449 | /** |
||
| 1450 | * get the balance for the wallet |
||
| 1451 | * |
||
| 1452 | * @param string $identifier the identifier of the wallet |
||
| 1453 | * @return array |
||
| 1454 | */ |
||
| 1455 | 9 | public function getWalletBalance($identifier) { |
|
| 1459 | |||
| 1460 | /** |
||
| 1461 | * get a new derivation number for specified parent path |
||
| 1462 | * 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 |
||
| 1463 | * |
||
| 1464 | * returns the path |
||
| 1465 | * |
||
| 1466 | * @param string $identifier the identifier of the wallet |
||
| 1467 | * @param string $path the parent path for which to get a new derivation |
||
| 1468 | * @return string |
||
| 1469 | */ |
||
| 1470 | 1 | public function getNewDerivation($identifier, $path) { |
|
| 1474 | |||
| 1475 | /** |
||
| 1476 | * get a new derivation number for specified parent path |
||
| 1477 | * 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 |
||
| 1478 | * |
||
| 1479 | * @param string $identifier the identifier of the wallet |
||
| 1480 | * @param string $path the parent path for which to get a new derivation |
||
| 1481 | * @return mixed |
||
| 1482 | */ |
||
| 1483 | 15 | public function _getNewDerivation($identifier, $path) { |
|
| 1487 | |||
| 1488 | /** |
||
| 1489 | * get the path (and redeemScript) to specified address |
||
| 1490 | * |
||
| 1491 | * @param string $identifier |
||
| 1492 | * @param string $address |
||
| 1493 | * @return array |
||
| 1494 | * @throws \Exception |
||
| 1495 | */ |
||
| 1496 | 1 | public function getPathForAddress($identifier, $address) { |
|
| 1500 | |||
| 1501 | /** |
||
| 1502 | * send the transaction using the API |
||
| 1503 | * |
||
| 1504 | * @param string $identifier the identifier of the wallet |
||
| 1505 | * @param string|array $rawTransaction raw hex of the transaction (should be partially signed) |
||
| 1506 | * @param array $paths list of the paths that were used for the UTXO |
||
| 1507 | * @param bool $checkFee let the server verify the fee after signing |
||
| 1508 | * @param null $twoFactorToken |
||
| 1509 | * @return string the complete raw transaction |
||
| 1510 | * @throws \Exception |
||
| 1511 | */ |
||
| 1512 | 4 | public function sendTransaction($identifier, $rawTransaction, $paths, $checkFee = false, $twoFactorToken = null) { |
|
| 1543 | |||
| 1544 | /** |
||
| 1545 | * use the API to get the best inputs to use based on the outputs |
||
| 1546 | * |
||
| 1547 | * the return array has the following format: |
||
| 1548 | * [ |
||
| 1549 | * "utxos" => [ |
||
| 1550 | * [ |
||
| 1551 | * "hash" => "<txHash>", |
||
| 1552 | * "idx" => "<index of the output of that <txHash>", |
||
| 1553 | * "scriptpubkey_hex" => "<scriptPubKey-hex>", |
||
| 1554 | * "value" => 32746327, |
||
| 1555 | * "address" => "1address", |
||
| 1556 | * "path" => "m/44'/1'/0'/0/13", |
||
| 1557 | * "redeem_script" => "<redeemScript-hex>", |
||
| 1558 | * ], |
||
| 1559 | * ], |
||
| 1560 | * "fee" => 10000, |
||
| 1561 | * "change"=> 1010109201, |
||
| 1562 | * ] |
||
| 1563 | * |
||
| 1564 | * @param string $identifier the identifier of the wallet |
||
| 1565 | * @param array $outputs the outputs you want to create - array[address => satoshi-value] |
||
| 1566 | * @param bool $lockUTXO when TRUE the UTXOs selected will be locked for a few seconds |
||
| 1567 | * so you have some time to spend them without race-conditions |
||
| 1568 | * @param bool $allowZeroConf |
||
| 1569 | * @param string $feeStrategy |
||
| 1570 | * @param null|int $forceFee |
||
| 1571 | * @return array |
||
| 1572 | * @throws \Exception |
||
| 1573 | */ |
||
| 1574 | 12 | public function coinSelection($identifier, $outputs, $lockUTXO = false, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null) { |
|
| 1594 | |||
| 1595 | /** |
||
| 1596 | * |
||
| 1597 | * @param string $identifier the identifier of the wallet |
||
| 1598 | * @param bool $allowZeroConf |
||
| 1599 | * @param string $feeStrategy |
||
| 1600 | * @param null|int $forceFee |
||
| 1601 | * @param int $outputCnt |
||
| 1602 | * @return array |
||
| 1603 | * @throws \Exception |
||
| 1604 | */ |
||
| 1605 | public function walletMaxSpendable($identifier, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null, $outputCnt = 1) { |
||
| 1624 | |||
| 1625 | /** |
||
| 1626 | * @return array ['optimal_fee' => 10000, 'low_priority_fee' => 5000] |
||
| 1627 | */ |
||
| 1628 | 3 | public function feePerKB() { |
|
| 1632 | |||
| 1633 | /** |
||
| 1634 | * get the current price index |
||
| 1635 | * |
||
| 1636 | * @return array eg; ['USD' => 287.30] |
||
| 1637 | */ |
||
| 1638 | 1 | public function price() { |
|
| 1642 | |||
| 1643 | /** |
||
| 1644 | * setup webhook for wallet |
||
| 1645 | * |
||
| 1646 | * @param string $identifier the wallet identifier for which to create the webhook |
||
| 1647 | * @param string $webhookIdentifier the webhook identifier to use |
||
| 1648 | * @param string $url the url to receive the webhook events |
||
| 1649 | * @return array |
||
| 1650 | */ |
||
| 1651 | 1 | public function setupWalletWebhook($identifier, $webhookIdentifier, $url) { |
|
| 1655 | |||
| 1656 | /** |
||
| 1657 | * delete webhook for wallet |
||
| 1658 | * |
||
| 1659 | * @param string $identifier the wallet identifier for which to delete the webhook |
||
| 1660 | * @param string $webhookIdentifier the webhook identifier to delete |
||
| 1661 | * @return array |
||
| 1662 | */ |
||
| 1663 | 1 | public function deleteWalletWebhook($identifier, $webhookIdentifier) { |
|
| 1667 | |||
| 1668 | /** |
||
| 1669 | * lock a specific unspent output |
||
| 1670 | * |
||
| 1671 | * @param $identifier |
||
| 1672 | * @param $txHash |
||
| 1673 | * @param $txIdx |
||
| 1674 | * @param int $ttl |
||
| 1675 | * @return bool |
||
| 1676 | */ |
||
| 1677 | public function lockWalletUTXO($identifier, $txHash, $txIdx, $ttl = 3) { |
||
| 1681 | |||
| 1682 | /** |
||
| 1683 | * unlock a specific unspent output |
||
| 1684 | * |
||
| 1685 | * @param $identifier |
||
| 1686 | * @param $txHash |
||
| 1687 | * @param $txIdx |
||
| 1688 | * @return bool |
||
| 1689 | */ |
||
| 1690 | public function unlockWalletUTXO($identifier, $txHash, $txIdx) { |
||
| 1694 | |||
| 1695 | /** |
||
| 1696 | * get all transactions for wallet (paginated) |
||
| 1697 | * |
||
| 1698 | * @param string $identifier the wallet identifier for which to get transactions |
||
| 1699 | * @param integer $page pagination: page number |
||
| 1700 | * @param integer $limit pagination: records per page (max 500) |
||
| 1701 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1702 | * @return array associative array containing the response |
||
| 1703 | */ |
||
| 1704 | 1 | public function walletTransactions($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 1713 | |||
| 1714 | /** |
||
| 1715 | * get all addresses for wallet (paginated) |
||
| 1716 | * |
||
| 1717 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1718 | * @param integer $page pagination: page number |
||
| 1719 | * @param integer $limit pagination: records per page (max 500) |
||
| 1720 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1721 | * @return array associative array containing the response |
||
| 1722 | */ |
||
| 1723 | 1 | public function walletAddresses($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 1732 | |||
| 1733 | /** |
||
| 1734 | * get all UTXOs for wallet (paginated) |
||
| 1735 | * |
||
| 1736 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1737 | * @param integer $page pagination: page number |
||
| 1738 | * @param integer $limit pagination: records per page (max 500) |
||
| 1739 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1740 | * @param boolean $zeroconf include zero confirmation transactions |
||
| 1741 | * @return array associative array containing the response |
||
| 1742 | */ |
||
| 1743 | 1 | public function walletUTXOs($identifier, $page = 1, $limit = 20, $sortDir = 'asc', $zeroconf = true) { |
|
| 1753 | |||
| 1754 | /** |
||
| 1755 | * get a paginated list of all wallets associated with the api user |
||
| 1756 | * |
||
| 1757 | * @param integer $page pagination: page number |
||
| 1758 | * @param integer $limit pagination: records per page |
||
| 1759 | * @return array associative array containing the response |
||
| 1760 | */ |
||
| 1761 | 2 | public function allWallets($page = 1, $limit = 20) { |
|
| 1769 | |||
| 1770 | /** |
||
| 1771 | * send raw transaction |
||
| 1772 | * |
||
| 1773 | * @param $txHex |
||
| 1774 | * @return bool |
||
| 1775 | */ |
||
| 1776 | public function sendRawTransaction($txHex) { |
||
| 1780 | |||
| 1781 | /** |
||
| 1782 | * testnet only ;-) |
||
| 1783 | * |
||
| 1784 | * @param $address |
||
| 1785 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1786 | * @return mixed |
||
| 1787 | * @throws \Exception |
||
| 1788 | */ |
||
| 1789 | public function faucetWithdrawal($address, $amount = 10000) { |
||
| 1796 | |||
| 1797 | /** |
||
| 1798 | * Exists for BC. Remove at major bump. |
||
| 1799 | * |
||
| 1800 | * @see faucetWithdrawal |
||
| 1801 | * @deprecated |
||
| 1802 | * @param $address |
||
| 1803 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1804 | * @return mixed |
||
| 1805 | * @throws \Exception |
||
| 1806 | */ |
||
| 1807 | public function faucetWithdrawl($address, $amount = 10000) { |
||
| 1810 | |||
| 1811 | /** |
||
| 1812 | * verify a message signed bitcoin-core style |
||
| 1813 | * |
||
| 1814 | * @param string $message |
||
| 1815 | * @param string $address |
||
| 1816 | * @param string $signature |
||
| 1817 | * @return boolean |
||
| 1818 | */ |
||
| 1819 | 2 | public function verifyMessage($message, $address, $signature) { |
|
| 1833 | |||
| 1834 | /** |
||
| 1835 | * Take a base58 or cashaddress, and return only |
||
| 1836 | * the cash address. |
||
| 1837 | * This function only works on bitcoin cash. |
||
| 1838 | * @param string $input |
||
| 1839 | * @return string |
||
| 1840 | * @throws BlocktrailSDKException |
||
| 1841 | */ |
||
| 1842 | 1 | public function getLegacyBitcoinCashAddress($input) { |
|
| 1859 | |||
| 1860 | /** |
||
| 1861 | * convert a Satoshi value to a BTC value |
||
| 1862 | * |
||
| 1863 | * @param int $satoshi |
||
| 1864 | * @return float |
||
| 1865 | */ |
||
| 1866 | 1 | public static function toBTC($satoshi) { |
|
| 1869 | |||
| 1870 | /** |
||
| 1871 | * convert a Satoshi value to a BTC value and return it as a string |
||
| 1872 | |||
| 1873 | * @param int $satoshi |
||
| 1874 | * @return string |
||
| 1875 | */ |
||
| 1876 | public static function toBTCString($satoshi) { |
||
| 1879 | |||
| 1880 | /** |
||
| 1881 | * convert a BTC value to a Satoshi value |
||
| 1882 | * |
||
| 1883 | * @param float $btc |
||
| 1884 | * @return string |
||
| 1885 | */ |
||
| 1886 | 13 | public static function toSatoshiString($btc) { |
|
| 1889 | |||
| 1890 | /** |
||
| 1891 | * convert a BTC value to a Satoshi value |
||
| 1892 | * |
||
| 1893 | * @param float $btc |
||
| 1894 | * @return string |
||
| 1895 | */ |
||
| 1896 | 13 | public static function toSatoshi($btc) { |
|
| 1899 | |||
| 1900 | /** |
||
| 1901 | * json_decode helper that throws exceptions when it fails to decode |
||
| 1902 | * |
||
| 1903 | * @param $json |
||
| 1904 | * @param bool $assoc |
||
| 1905 | * @return mixed |
||
| 1906 | * @throws \Exception |
||
| 1907 | */ |
||
| 1908 | 31 | public static function jsonDecode($json, $assoc = false) { |
|
| 1921 | |||
| 1922 | /** |
||
| 1923 | * sort public keys for multisig script |
||
| 1924 | * |
||
| 1925 | * @param PublicKeyInterface[] $pubKeys |
||
| 1926 | * @return PublicKeyInterface[] |
||
| 1927 | */ |
||
| 1928 | 18 | public static function sortMultisigKeys(array $pubKeys) { |
|
| 1938 | |||
| 1939 | /** |
||
| 1940 | * read and decode the json payload from a webhook's POST request. |
||
| 1941 | * |
||
| 1942 | * @param bool $returnObject flag to indicate if an object or associative array should be returned |
||
| 1943 | * @return mixed|null |
||
| 1944 | * @throws \Exception |
||
| 1945 | */ |
||
| 1946 | public static function getWebhookPayload($returnObject = false) { |
||
| 1954 | |||
| 1955 | public static function normalizeBIP32KeyArray($keys) { |
||
| 1960 | |||
| 1961 | /** |
||
| 1962 | * @param array|BIP32Key $key |
||
| 1963 | * @return BIP32Key |
||
| 1964 | * @throws \Exception |
||
| 1965 | */ |
||
| 1966 | 26 | public static function normalizeBIP32Key($key) { |
|
| 1984 | } |
||
| 1985 |
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: