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 | 80 | public function __construct($apiKey, $apiSecret, $network = 'BTC', $testnet = false, $apiVersion = 'v1', $apiEndpoint = null) { |
|
| 105 | |||
| 106 | /** |
||
| 107 | * normalize network string |
||
| 108 | * |
||
| 109 | * @param $network |
||
| 110 | * @param $testnet |
||
| 111 | * @return array |
||
| 112 | * @throws \Exception |
||
| 113 | */ |
||
| 114 | 80 | protected function normalizeNetwork($network, $testnet) { |
|
| 118 | |||
| 119 | /** |
||
| 120 | * set BitcoinLib to the correct magic-byte defaults for the selected network |
||
| 121 | * |
||
| 122 | * @param $network |
||
| 123 | * @param bool $testnet |
||
| 124 | * @param bool $regtest |
||
| 125 | */ |
||
| 126 | 80 | protected function setBitcoinLibMagicBytes($network, $testnet, $regtest) { |
|
| 148 | |||
| 149 | /** |
||
| 150 | * enable CURL debugging output |
||
| 151 | * |
||
| 152 | * @param bool $debug |
||
| 153 | * |
||
| 154 | * @codeCoverageIgnore |
||
| 155 | */ |
||
| 156 | public function setCurlDebugging($debug = true) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * enable verbose errors |
||
| 163 | * |
||
| 164 | * @param bool $verboseErrors |
||
| 165 | * |
||
| 166 | * @codeCoverageIgnore |
||
| 167 | */ |
||
| 168 | public function setVerboseErrors($verboseErrors = true) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * set cURL default option on Guzzle client |
||
| 175 | * @param string $key |
||
| 176 | * @param bool $value |
||
| 177 | * |
||
| 178 | * @codeCoverageIgnore |
||
| 179 | */ |
||
| 180 | public function setCurlDefaultOption($key, $value) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return RestClientInterface |
||
| 187 | */ |
||
| 188 | 1 | public function getRestClient() { |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @return RestClient |
||
| 194 | */ |
||
| 195 | public function getDataRestClient() { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param RestClientInterface $restClient |
||
| 201 | */ |
||
| 202 | public function setRestClient(RestClientInterface $restClient) { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * get a single address |
||
| 208 | * @param string $address address hash |
||
| 209 | * @return array associative array containing the response |
||
| 210 | */ |
||
| 211 | 2 | public function address($address) { |
|
| 215 | |||
| 216 | /** |
||
| 217 | * get all transactions for an address (paginated) |
||
| 218 | * @param string $address address hash |
||
| 219 | * @param integer $page pagination: page number |
||
| 220 | * @param integer $limit pagination: records per page (max 500) |
||
| 221 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 222 | * @return array associative array containing the response |
||
| 223 | */ |
||
| 224 | 2 | public function addressTransactions($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 233 | |||
| 234 | /** |
||
| 235 | * get all unconfirmed transactions for an address (paginated) |
||
| 236 | * @param string $address address hash |
||
| 237 | * @param integer $page pagination: page number |
||
| 238 | * @param integer $limit pagination: records per page (max 500) |
||
| 239 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 240 | * @return array associative array containing the response |
||
| 241 | */ |
||
| 242 | public function addressUnconfirmedTransactions($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * get all unspent outputs for an address (paginated) |
||
| 254 | * @param string $address address hash |
||
| 255 | * @param integer $page pagination: page number |
||
| 256 | * @param integer $limit pagination: records per page (max 500) |
||
| 257 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 258 | * @return array associative array containing the response |
||
| 259 | */ |
||
| 260 | 2 | public function addressUnspentOutputs($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 269 | |||
| 270 | /** |
||
| 271 | * get all unspent outputs for a batch of addresses (paginated) |
||
| 272 | * |
||
| 273 | * @param string[] $addresses |
||
| 274 | * @param integer $page pagination: page number |
||
| 275 | * @param integer $limit pagination: records per page (max 500) |
||
| 276 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 277 | * @return array associative array containing the response |
||
| 278 | * @throws \Exception |
||
| 279 | */ |
||
| 280 | public function batchAddressUnspentOutputs($addresses, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * verify ownership of an address |
||
| 307 | * @param string $address address hash |
||
| 308 | * @param string $signature a signed message (the address hash) using the private key of the address |
||
| 309 | * @return array associative array containing the response |
||
| 310 | */ |
||
| 311 | 2 | public function verifyAddress($address, $signature) { |
|
| 318 | |||
| 319 | /** |
||
| 320 | * get all blocks (paginated) |
||
| 321 | * @param integer $page pagination: page number |
||
| 322 | * @param integer $limit pagination: records per page |
||
| 323 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 324 | * @return array associative array containing the response |
||
| 325 | */ |
||
| 326 | 2 | public function allBlocks($page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 335 | |||
| 336 | /** |
||
| 337 | * get the latest block |
||
| 338 | * @return array associative array containing the response |
||
| 339 | */ |
||
| 340 | 1 | public function blockLatest() { |
|
| 344 | |||
| 345 | /** |
||
| 346 | * get the wallet API's latest block ['hash' => x, 'height' => y] |
||
| 347 | * @return array associative array containing the response |
||
| 348 | */ |
||
| 349 | 1 | public function getWalletBlockLatest() { |
|
| 350 | 1 | $response = $this->blocktrailClient->get("block/latest"); |
|
| 351 | 1 | return BlocktrailSDK::jsonDecode($response->body(), true); |
|
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * get an individual block |
||
| 356 | * @param string|integer $block a block hash or a block height |
||
| 357 | * @return array associative array containing the response |
||
| 358 | */ |
||
| 359 | 4 | public function block($block) { |
|
| 363 | |||
| 364 | /** |
||
| 365 | * get all transaction in a block (paginated) |
||
| 366 | * @param string|integer $block a block hash or a block height |
||
| 367 | * @param integer $page pagination: page number |
||
| 368 | * @param integer $limit pagination: records per page |
||
| 369 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 370 | * @return array associative array containing the response |
||
| 371 | */ |
||
| 372 | 1 | public function blockTransactions($block, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 381 | |||
| 382 | /** |
||
| 383 | * get a single transaction |
||
| 384 | * @param string $txhash transaction hash |
||
| 385 | * @return array associative array containing the response |
||
| 386 | */ |
||
| 387 | 2 | public function transaction($txhash) { |
|
| 397 | |||
| 398 | /** |
||
| 399 | * get a single transaction |
||
| 400 | * @param string[] $txhashes list of transaction hashes (up to 20) |
||
| 401 | * @return array[] array containing the response |
||
| 402 | */ |
||
| 403 | 2 | public function transactions($txhashes) { |
|
| 407 | |||
| 408 | /** |
||
| 409 | * get a paginated list of all webhooks associated with the api user |
||
| 410 | * @param integer $page pagination: page number |
||
| 411 | * @param integer $limit pagination: records per page |
||
| 412 | * @return array associative array containing the response |
||
| 413 | */ |
||
| 414 | 1 | public function allWebhooks($page = 1, $limit = 20) { |
|
| 422 | |||
| 423 | /** |
||
| 424 | * get an existing webhook by it's identifier |
||
| 425 | * @param string $identifier a unique identifier associated with the webhook |
||
| 426 | * @return array associative array containing the response |
||
| 427 | */ |
||
| 428 | 1 | public function getWebhook($identifier) { |
|
| 432 | |||
| 433 | /** |
||
| 434 | * create a new webhook |
||
| 435 | * @param string $url the url to receive the webhook events |
||
| 436 | * @param string $identifier a unique identifier to associate with this webhook |
||
| 437 | * @return array associative array containing the response |
||
| 438 | */ |
||
| 439 | 1 | public function setupWebhook($url, $identifier = null) { |
|
| 447 | |||
| 448 | /** |
||
| 449 | * update an existing webhook |
||
| 450 | * @param string $identifier the unique identifier of the webhook to update |
||
| 451 | * @param string $newUrl the new url to receive the webhook events |
||
| 452 | * @param string $newIdentifier a new unique identifier to associate with this webhook |
||
| 453 | * @return array associative array containing the response |
||
| 454 | */ |
||
| 455 | 1 | public function updateWebhook($identifier, $newUrl = null, $newIdentifier = null) { |
|
| 463 | |||
| 464 | /** |
||
| 465 | * deletes an existing webhook and any event subscriptions associated with it |
||
| 466 | * @param string $identifier the unique identifier of the webhook to delete |
||
| 467 | * @return boolean true on success |
||
| 468 | */ |
||
| 469 | 1 | public function deleteWebhook($identifier) { |
|
| 473 | |||
| 474 | /** |
||
| 475 | * get a paginated list of all the events a webhook is subscribed to |
||
| 476 | * @param string $identifier the unique identifier of the webhook |
||
| 477 | * @param integer $page pagination: page number |
||
| 478 | * @param integer $limit pagination: records per page |
||
| 479 | * @return array associative array containing the response |
||
| 480 | */ |
||
| 481 | 1 | public function getWebhookEvents($identifier, $page = 1, $limit = 20) { |
|
| 489 | |||
| 490 | /** |
||
| 491 | * subscribes a webhook to transaction events of one particular transaction |
||
| 492 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 493 | * @param string $transaction the transaction hash |
||
| 494 | * @param integer $confirmations the amount of confirmations to send. |
||
| 495 | * @return array associative array containing the response |
||
| 496 | */ |
||
| 497 | 1 | public function subscribeTransaction($identifier, $transaction, $confirmations = 6) { |
|
| 506 | |||
| 507 | /** |
||
| 508 | * subscribes a webhook to transaction events on a particular address |
||
| 509 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 510 | * @param string $address the address hash |
||
| 511 | * @param integer $confirmations the amount of confirmations to send. |
||
| 512 | * @return array associative array containing the response |
||
| 513 | */ |
||
| 514 | 1 | public function subscribeAddressTransactions($identifier, $address, $confirmations = 6) { |
|
| 523 | |||
| 524 | /** |
||
| 525 | * batch subscribes a webhook to multiple transaction events |
||
| 526 | * |
||
| 527 | * @param string $identifier the unique identifier of the webhook |
||
| 528 | * @param array $batchData A 2D array of event data: |
||
| 529 | * [address => $address, confirmations => $confirmations] |
||
| 530 | * where $address is the address to subscibe to |
||
| 531 | * and optionally $confirmations is the amount of confirmations |
||
| 532 | * @return boolean true on success |
||
| 533 | */ |
||
| 534 | 1 | public function batchSubscribeAddressTransactions($identifier, $batchData) { |
|
| 546 | |||
| 547 | /** |
||
| 548 | * subscribes a webhook to a new block event |
||
| 549 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 550 | * @return array associative array containing the response |
||
| 551 | */ |
||
| 552 | 1 | public function subscribeNewBlocks($identifier) { |
|
| 559 | |||
| 560 | /** |
||
| 561 | * removes an transaction event subscription from a webhook |
||
| 562 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 563 | * @param string $transaction the transaction hash of the event subscription |
||
| 564 | * @return boolean true on success |
||
| 565 | */ |
||
| 566 | 1 | public function unsubscribeTransaction($identifier, $transaction) { |
|
| 570 | |||
| 571 | /** |
||
| 572 | * removes an address transaction event subscription from a webhook |
||
| 573 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 574 | * @param string $address the address hash of the event subscription |
||
| 575 | * @return boolean true on success |
||
| 576 | */ |
||
| 577 | 1 | public function unsubscribeAddressTransactions($identifier, $address) { |
|
| 581 | |||
| 582 | /** |
||
| 583 | * removes a block event subscription from a webhook |
||
| 584 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 585 | * @return boolean true on success |
||
| 586 | */ |
||
| 587 | 1 | public function unsubscribeNewBlocks($identifier) { |
|
| 591 | |||
| 592 | /** |
||
| 593 | * create a new wallet |
||
| 594 | * - will generate a new primary seed (with password) and backup seed (without password) |
||
| 595 | * - send the primary seed (BIP39 'encrypted') and backup public key to the server |
||
| 596 | * - receive the blocktrail co-signing public key from the server |
||
| 597 | * |
||
| 598 | * Either takes one argument: |
||
| 599 | * @param array $options |
||
| 600 | * |
||
| 601 | * Or takes three arguments (old, deprecated syntax): |
||
| 602 | * (@nonPHP-doc) @param $identifier |
||
| 603 | * (@nonPHP-doc) @param $password |
||
| 604 | * (@nonPHP-doc) @param int $keyIndex override for the blocktrail cosigning key to use |
||
| 605 | * |
||
| 606 | * @return array[WalletInterface, array] list($wallet, $backupInfo) |
||
| 607 | * @throws \Exception |
||
| 608 | */ |
||
| 609 | 9 | public function createNewWallet($options) { |
|
| 610 | 9 | if (!is_array($options)) { |
|
| 611 | $args = func_get_args(); |
||
| 612 | $options = [ |
||
| 613 | "identifier" => $args[0], |
||
| 614 | "password" => $args[1], |
||
| 615 | "key_index" => isset($args[2]) ? $args[2] : null, |
||
| 616 | ]; |
||
| 617 | } |
||
| 618 | |||
| 619 | 9 | if (isset($options['password'])) { |
|
| 620 | 9 | if (isset($options['passphrase'])) { |
|
| 621 | 1 | throw new \InvalidArgumentException("Can only provide either passphrase or password"); |
|
| 622 | } else { |
||
| 623 | 8 | $options['passphrase'] = $options['password']; |
|
| 624 | } |
||
| 625 | } |
||
| 626 | |||
| 627 | 8 | if (!isset($options['passphrase'])) { |
|
| 628 | $options['passphrase'] = null; |
||
| 629 | } |
||
| 630 | |||
| 631 | 8 | if (!isset($options['key_index'])) { |
|
| 632 | 1 | $options['key_index'] = 0; |
|
| 633 | } |
||
| 634 | |||
| 635 | 8 | if (!isset($options['wallet_version'])) { |
|
| 636 | 3 | $options['wallet_version'] = Wallet::WALLET_VERSION_V3; |
|
| 637 | } |
||
| 638 | |||
| 639 | 8 | switch ($options['wallet_version']) { |
|
| 640 | 8 | case Wallet::WALLET_VERSION_V1: |
|
| 641 | 2 | return $this->createNewWalletV1($options); |
|
| 642 | |||
| 643 | 6 | case Wallet::WALLET_VERSION_V2: |
|
| 644 | 2 | return $this->createNewWalletV2($options); |
|
| 645 | |||
| 646 | 4 | case Wallet::WALLET_VERSION_V3: |
|
| 647 | 3 | return $this->createNewWalletV3($options); |
|
| 648 | |||
| 649 | default: |
||
| 650 | 1 | throw new \InvalidArgumentException("Invalid wallet version"); |
|
| 651 | } |
||
| 652 | } |
||
| 653 | |||
| 654 | 2 | protected function createNewWalletV1($options) { |
|
| 774 | |||
| 775 | public function randomBits($bits) { |
||
| 778 | |||
| 779 | public function randomBytes($bytes) { |
||
| 782 | |||
| 783 | 2 | protected function createNewWalletV2($options) { |
|
| 903 | |||
| 904 | 3 | protected function createNewWalletV3($options) { |
|
| 1042 | |||
| 1043 | public function newV2PrimarySeed() { |
||
| 1046 | |||
| 1047 | public function newV2BackupSeed() { |
||
| 1050 | |||
| 1051 | public function newV2Secret($passphrase) { |
||
| 1057 | |||
| 1058 | public function newV2EncryptedPrimarySeed($primarySeed, $secret) { |
||
| 1061 | |||
| 1062 | public function newV2RecoverySecret($secret) { |
||
| 1068 | |||
| 1069 | public function newV3PrimarySeed() { |
||
| 1072 | |||
| 1073 | public function newV3BackupSeed() { |
||
| 1076 | |||
| 1077 | public function newV3Secret($passphrase) { |
||
| 1084 | |||
| 1085 | public function newV3EncryptedPrimarySeed(Buffer $primarySeed, Buffer $secret) { |
||
| 1089 | |||
| 1090 | public function newV3RecoverySecret(Buffer $secret) { |
||
| 1097 | |||
| 1098 | /** |
||
| 1099 | * @param array $bip32Key |
||
| 1100 | * @throws BlocktrailSDKException |
||
| 1101 | */ |
||
| 1102 | private function verifyPublicBIP32Key(array $bip32Key) { |
||
| 1112 | |||
| 1113 | /** |
||
| 1114 | * @param array $walletData |
||
| 1115 | * @throws BlocktrailSDKException |
||
| 1116 | */ |
||
| 1117 | private function verifyPublicOnly(array $walletData) { |
||
| 1121 | |||
| 1122 | /** |
||
| 1123 | * create wallet using the API |
||
| 1124 | * |
||
| 1125 | * @param string $identifier the wallet identifier to create |
||
| 1126 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1127 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1128 | * @param string $primaryMnemonic mnemonic to store |
||
| 1129 | * @param string $checksum checksum to store |
||
| 1130 | * @param int $keyIndex account that we expect to use |
||
| 1131 | * @param bool $segwit opt in to segwit |
||
| 1132 | * @return mixed |
||
| 1133 | */ |
||
| 1134 | public function storeNewWalletV1($identifier, $primaryPublicKey, $backupPublicKey, $primaryMnemonic, $checksum, $keyIndex, $segwit = false) { |
||
| 1148 | |||
| 1149 | /** |
||
| 1150 | * create wallet using the API |
||
| 1151 | * |
||
| 1152 | * @param string $identifier the wallet identifier to create |
||
| 1153 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1154 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1155 | * @param $encryptedPrimarySeed |
||
| 1156 | * @param $encryptedSecret |
||
| 1157 | * @param $recoverySecret |
||
| 1158 | * @param string $checksum checksum to store |
||
| 1159 | * @param int $keyIndex account that we expect to use |
||
| 1160 | * @param bool $segwit opt in to segwit |
||
| 1161 | * @return mixed |
||
| 1162 | * @throws \Exception |
||
| 1163 | */ |
||
| 1164 | public function storeNewWalletV2($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false) { |
||
| 1181 | |||
| 1182 | /** |
||
| 1183 | * create wallet using the API |
||
| 1184 | * |
||
| 1185 | * @param string $identifier the wallet identifier to create |
||
| 1186 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1187 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1188 | * @param $encryptedPrimarySeed |
||
| 1189 | * @param $encryptedSecret |
||
| 1190 | * @param $recoverySecret |
||
| 1191 | * @param string $checksum checksum to store |
||
| 1192 | * @param int $keyIndex account that we expect to use |
||
| 1193 | * @param bool $segwit opt in to segwit |
||
| 1194 | * @return mixed |
||
| 1195 | * @throws \Exception |
||
| 1196 | */ |
||
| 1197 | public function storeNewWalletV3($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false) { |
||
| 1216 | |||
| 1217 | /** |
||
| 1218 | * upgrade wallet to use a new account number |
||
| 1219 | * the account number specifies which blocktrail cosigning key is used |
||
| 1220 | * |
||
| 1221 | * @param string $identifier the wallet identifier to be upgraded |
||
| 1222 | * @param int $keyIndex the new account to use |
||
| 1223 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1224 | * @return mixed |
||
| 1225 | */ |
||
| 1226 | public function upgradeKeyIndex($identifier, $keyIndex, $primaryPublicKey) { |
||
| 1235 | |||
| 1236 | /** |
||
| 1237 | * @param array $options |
||
| 1238 | * @return AddressReaderBase |
||
| 1239 | */ |
||
| 1240 | 54 | private function makeAddressReader(array $options) { |
|
| 1251 | |||
| 1252 | /** |
||
| 1253 | * initialize a previously created wallet |
||
| 1254 | * |
||
| 1255 | * Takes an options object, or accepts identifier/password for backwards compatiblity. |
||
| 1256 | * |
||
| 1257 | * Some of the options: |
||
| 1258 | * - "readonly/readOnly/read-only" can be to a boolean value, |
||
| 1259 | * so the wallet is loaded in read-only mode (no private key) |
||
| 1260 | * - "check_backup_key" can be set to your own backup key: |
||
| 1261 | * Format: ["M', "xpub..."] |
||
| 1262 | * Setting this will allow the SDK to check the server hasn't |
||
| 1263 | * a different key (one it happens to control) |
||
| 1264 | |||
| 1265 | * Either takes one argument: |
||
| 1266 | * @param array $options |
||
| 1267 | * |
||
| 1268 | * Or takes two arguments (old, deprecated syntax): |
||
| 1269 | * (@nonPHP-doc) @param string $identifier the wallet identifier to be initialized |
||
| 1270 | * (@nonPHP-doc) @param string $password the password to decrypt the mnemonic with |
||
| 1271 | * |
||
| 1272 | * @return WalletInterface |
||
| 1273 | * @throws \Exception |
||
| 1274 | */ |
||
| 1275 | 49 | public function initWallet($options) { |
|
| 1387 | |||
| 1388 | /** |
||
| 1389 | * get the wallet data from the server |
||
| 1390 | * |
||
| 1391 | * @param string $identifier the identifier of the wallet |
||
| 1392 | * @return mixed |
||
| 1393 | */ |
||
| 1394 | public function getWallet($identifier) { |
||
| 1398 | |||
| 1399 | /** |
||
| 1400 | * update the wallet data on the server |
||
| 1401 | * |
||
| 1402 | * @param string $identifier |
||
| 1403 | * @param $data |
||
| 1404 | * @return mixed |
||
| 1405 | */ |
||
| 1406 | public function updateWallet($identifier, $data) { |
||
| 1410 | |||
| 1411 | /** |
||
| 1412 | * delete a wallet from the server |
||
| 1413 | * the checksum address and a signature to verify you ownership of the key of that checksum address |
||
| 1414 | * is required to be able to delete a wallet |
||
| 1415 | * |
||
| 1416 | * @param string $identifier the identifier of the wallet |
||
| 1417 | * @param string $checksumAddress the address for your master private key (and the checksum used when creating the wallet) |
||
| 1418 | * @param string $signature a signature of the checksum address as message signed by the private key matching that address |
||
| 1419 | * @param bool $force ignore warnings (such as a non-zero balance) |
||
| 1420 | * @return mixed |
||
| 1421 | */ |
||
| 1422 | public function deleteWallet($identifier, $checksumAddress, $signature, $force = false) { |
||
| 1429 | |||
| 1430 | /** |
||
| 1431 | * create new backup key; |
||
| 1432 | * 1) a BIP39 mnemonic |
||
| 1433 | * 2) a seed from that mnemonic with a blank password |
||
| 1434 | * 3) a private key from that seed |
||
| 1435 | * |
||
| 1436 | * @return array [mnemonic, seed, key] |
||
| 1437 | */ |
||
| 1438 | 2 | protected function newV1BackupSeed() { |
|
| 1443 | |||
| 1444 | /** |
||
| 1445 | * create new primary key; |
||
| 1446 | * 1) a BIP39 mnemonic |
||
| 1447 | * 2) a seed from that mnemonic with the password |
||
| 1448 | * 3) a private key from that seed |
||
| 1449 | * |
||
| 1450 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1451 | * @return array [mnemonic, seed, key] |
||
| 1452 | * @TODO: require a strong password? |
||
| 1453 | */ |
||
| 1454 | 2 | protected function newV1PrimarySeed($passphrase) { |
|
| 1459 | |||
| 1460 | /** |
||
| 1461 | * create a new key; |
||
| 1462 | * 1) a BIP39 mnemonic |
||
| 1463 | * 2) a seed from that mnemonic with the password |
||
| 1464 | * 3) a private key from that seed |
||
| 1465 | * |
||
| 1466 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1467 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1468 | * @return array |
||
| 1469 | */ |
||
| 1470 | 2 | protected function generateNewSeed($passphrase = "", $forceEntropy = null) { |
|
| 1487 | |||
| 1488 | /** |
||
| 1489 | * generate a new mnemonic from some random entropy (512 bit) |
||
| 1490 | * |
||
| 1491 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1492 | * @return string |
||
| 1493 | * @throws \Exception |
||
| 1494 | */ |
||
| 1495 | public function generateNewMnemonic($forceEntropy = null) { |
||
| 1505 | |||
| 1506 | /** |
||
| 1507 | * get the balance for the wallet |
||
| 1508 | * |
||
| 1509 | * @param string $identifier the identifier of the wallet |
||
| 1510 | * @return array |
||
| 1511 | */ |
||
| 1512 | public function getWalletBalance($identifier) { |
||
| 1516 | |||
| 1517 | /** |
||
| 1518 | * get a new derivation number for specified parent path |
||
| 1519 | * 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 |
||
| 1520 | * |
||
| 1521 | * returns the path |
||
| 1522 | * |
||
| 1523 | * @param string $identifier the identifier of the wallet |
||
| 1524 | * @param string $path the parent path for which to get a new derivation |
||
| 1525 | * @return string |
||
| 1526 | */ |
||
| 1527 | public function getNewDerivation($identifier, $path) { |
||
| 1531 | |||
| 1532 | /** |
||
| 1533 | * get a new derivation number for specified parent path |
||
| 1534 | * 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 |
||
| 1535 | * |
||
| 1536 | * @param string $identifier the identifier of the wallet |
||
| 1537 | * @param string $path the parent path for which to get a new derivation |
||
| 1538 | * @return mixed |
||
| 1539 | */ |
||
| 1540 | public function _getNewDerivation($identifier, $path) { |
||
| 1544 | |||
| 1545 | /** |
||
| 1546 | * get the path (and redeemScript) to specified address |
||
| 1547 | * |
||
| 1548 | * @param string $identifier |
||
| 1549 | * @param string $address |
||
| 1550 | * @return array |
||
| 1551 | * @throws \Exception |
||
| 1552 | */ |
||
| 1553 | public function getPathForAddress($identifier, $address) { |
||
| 1557 | |||
| 1558 | /** |
||
| 1559 | * send the transaction using the API |
||
| 1560 | * |
||
| 1561 | * @param string $identifier the identifier of the wallet |
||
| 1562 | * @param string|array $rawTransaction raw hex of the transaction (should be partially signed) |
||
| 1563 | * @param array $paths list of the paths that were used for the UTXO |
||
| 1564 | * @param bool $checkFee let the server verify the fee after signing |
||
| 1565 | * @param null $twoFactorToken |
||
| 1566 | * @return string the complete raw transaction |
||
| 1567 | * @throws \Exception |
||
| 1568 | */ |
||
| 1569 | public function sendTransaction($identifier, $rawTransaction, $paths, $checkFee = false, $twoFactorToken = null) { |
||
| 1600 | |||
| 1601 | /** |
||
| 1602 | * use the API to get the best inputs to use based on the outputs |
||
| 1603 | * |
||
| 1604 | * the return array has the following format: |
||
| 1605 | * [ |
||
| 1606 | * "utxos" => [ |
||
| 1607 | * [ |
||
| 1608 | * "hash" => "<txHash>", |
||
| 1609 | * "idx" => "<index of the output of that <txHash>", |
||
| 1610 | * "scriptpubkey_hex" => "<scriptPubKey-hex>", |
||
| 1611 | * "value" => 32746327, |
||
| 1612 | * "address" => "1address", |
||
| 1613 | * "path" => "m/44'/1'/0'/0/13", |
||
| 1614 | * "redeem_script" => "<redeemScript-hex>", |
||
| 1615 | * ], |
||
| 1616 | * ], |
||
| 1617 | * "fee" => 10000, |
||
| 1618 | * "change"=> 1010109201, |
||
| 1619 | * ] |
||
| 1620 | * |
||
| 1621 | * @param string $identifier the identifier of the wallet |
||
| 1622 | * @param array $outputs the outputs you want to create - array[address => satoshi-value] |
||
| 1623 | * @param bool $lockUTXO when TRUE the UTXOs selected will be locked for a few seconds |
||
| 1624 | * so you have some time to spend them without race-conditions |
||
| 1625 | * @param bool $allowZeroConf |
||
| 1626 | * @param string $feeStrategy |
||
| 1627 | * @param null|int $forceFee |
||
| 1628 | * @return array |
||
| 1629 | * @throws \Exception |
||
| 1630 | */ |
||
| 1631 | public function coinSelection($identifier, $outputs, $lockUTXO = false, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null) { |
||
| 1653 | |||
| 1654 | /** |
||
| 1655 | * |
||
| 1656 | * @param string $identifier the identifier of the wallet |
||
| 1657 | * @param bool $allowZeroConf |
||
| 1658 | * @param string $feeStrategy |
||
| 1659 | * @param null|int $forceFee |
||
| 1660 | * @param int $outputCnt |
||
| 1661 | * @return array |
||
| 1662 | * @throws \Exception |
||
| 1663 | */ |
||
| 1664 | public function walletMaxSpendable($identifier, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null, $outputCnt = 1) { |
||
| 1683 | |||
| 1684 | /** |
||
| 1685 | * @return array ['optimal_fee' => 10000, 'low_priority_fee' => 5000] |
||
| 1686 | */ |
||
| 1687 | public function feePerKB() { |
||
| 1691 | |||
| 1692 | /** |
||
| 1693 | * get the current price index |
||
| 1694 | * |
||
| 1695 | * @return array eg; ['USD' => 287.30] |
||
| 1696 | */ |
||
| 1697 | 2 | public function price() { |
|
| 1701 | |||
| 1702 | /** |
||
| 1703 | * setup webhook for wallet |
||
| 1704 | * |
||
| 1705 | * @param string $identifier the wallet identifier for which to create the webhook |
||
| 1706 | * @param string $webhookIdentifier the webhook identifier to use |
||
| 1707 | * @param string $url the url to receive the webhook events |
||
| 1708 | * @return array |
||
| 1709 | */ |
||
| 1710 | public function setupWalletWebhook($identifier, $webhookIdentifier, $url) { |
||
| 1714 | |||
| 1715 | /** |
||
| 1716 | * delete webhook for wallet |
||
| 1717 | * |
||
| 1718 | * @param string $identifier the wallet identifier for which to delete the webhook |
||
| 1719 | * @param string $webhookIdentifier the webhook identifier to delete |
||
| 1720 | * @return array |
||
| 1721 | */ |
||
| 1722 | public function deleteWalletWebhook($identifier, $webhookIdentifier) { |
||
| 1726 | |||
| 1727 | /** |
||
| 1728 | * lock a specific unspent output |
||
| 1729 | * |
||
| 1730 | * @param $identifier |
||
| 1731 | * @param $txHash |
||
| 1732 | * @param $txIdx |
||
| 1733 | * @param int $ttl |
||
| 1734 | * @return bool |
||
| 1735 | */ |
||
| 1736 | public function lockWalletUTXO($identifier, $txHash, $txIdx, $ttl = 3) { |
||
| 1740 | |||
| 1741 | /** |
||
| 1742 | * unlock a specific unspent output |
||
| 1743 | * |
||
| 1744 | * @param $identifier |
||
| 1745 | * @param $txHash |
||
| 1746 | * @param $txIdx |
||
| 1747 | * @return bool |
||
| 1748 | */ |
||
| 1749 | public function unlockWalletUTXO($identifier, $txHash, $txIdx) { |
||
| 1753 | |||
| 1754 | /** |
||
| 1755 | * get all transactions for wallet (paginated) |
||
| 1756 | * |
||
| 1757 | * @param string $identifier the wallet identifier for which to get transactions |
||
| 1758 | * @param integer $page pagination: page number |
||
| 1759 | * @param integer $limit pagination: records per page (max 500) |
||
| 1760 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1761 | * @return array associative array containing the response |
||
| 1762 | */ |
||
| 1763 | public function walletTransactions($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 1772 | |||
| 1773 | /** |
||
| 1774 | * get all addresses for wallet (paginated) |
||
| 1775 | * |
||
| 1776 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1777 | * @param integer $page pagination: page number |
||
| 1778 | * @param integer $limit pagination: records per page (max 500) |
||
| 1779 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1780 | * @return array associative array containing the response |
||
| 1781 | */ |
||
| 1782 | public function walletAddresses($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 1791 | |||
| 1792 | /** |
||
| 1793 | * get all UTXOs for wallet (paginated) |
||
| 1794 | * |
||
| 1795 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1796 | * @param integer $page pagination: page number |
||
| 1797 | * @param integer $limit pagination: records per page (max 500) |
||
| 1798 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1799 | * @param boolean $zeroconf include zero confirmation transactions |
||
| 1800 | * @return array associative array containing the response |
||
| 1801 | */ |
||
| 1802 | public function walletUTXOs($identifier, $page = 1, $limit = 20, $sortDir = 'asc', $zeroconf = true) { |
||
| 1812 | |||
| 1813 | /** |
||
| 1814 | * get a paginated list of all wallets associated with the api user |
||
| 1815 | * |
||
| 1816 | * @param integer $page pagination: page number |
||
| 1817 | * @param integer $limit pagination: records per page |
||
| 1818 | * @return array associative array containing the response |
||
| 1819 | */ |
||
| 1820 | public function allWallets($page = 1, $limit = 20) { |
||
| 1828 | |||
| 1829 | /** |
||
| 1830 | * send raw transaction |
||
| 1831 | * |
||
| 1832 | * @param $txHex |
||
| 1833 | * @return bool |
||
| 1834 | */ |
||
| 1835 | public function sendRawTransaction($txHex) { |
||
| 1839 | |||
| 1840 | /** |
||
| 1841 | * testnet only ;-) |
||
| 1842 | * |
||
| 1843 | * @param $address |
||
| 1844 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1845 | * @return mixed |
||
| 1846 | * @throws \Exception |
||
| 1847 | */ |
||
| 1848 | public function faucetWithdrawal($address, $amount = 10000) { |
||
| 1855 | |||
| 1856 | /** |
||
| 1857 | * Exists for BC. Remove at major bump. |
||
| 1858 | * |
||
| 1859 | * @see faucetWithdrawal |
||
| 1860 | * @deprecated |
||
| 1861 | * @param $address |
||
| 1862 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1863 | * @return mixed |
||
| 1864 | * @throws \Exception |
||
| 1865 | */ |
||
| 1866 | public function faucetWithdrawl($address, $amount = 10000) { |
||
| 1869 | |||
| 1870 | /** |
||
| 1871 | * verify a message signed bitcoin-core style |
||
| 1872 | * |
||
| 1873 | * @param string $message |
||
| 1874 | * @param string $address |
||
| 1875 | * @param string $signature |
||
| 1876 | * @return boolean |
||
| 1877 | */ |
||
| 1878 | 2 | public function verifyMessage($message, $address, $signature) { |
|
| 1892 | |||
| 1893 | /** |
||
| 1894 | * Take a base58 or cashaddress, and return only |
||
| 1895 | * the cash address. |
||
| 1896 | * This function only works on bitcoin cash. |
||
| 1897 | * @param string $input |
||
| 1898 | * @return string |
||
| 1899 | * @throws BlocktrailSDKException |
||
| 1900 | */ |
||
| 1901 | 1 | public function getLegacyBitcoinCashAddress($input) { |
|
| 1918 | |||
| 1919 | /** |
||
| 1920 | * convert a Satoshi value to a BTC value |
||
| 1921 | * |
||
| 1922 | * @param int $satoshi |
||
| 1923 | * @return float |
||
| 1924 | */ |
||
| 1925 | 1 | public static function toBTC($satoshi) { |
|
| 1928 | |||
| 1929 | /** |
||
| 1930 | * convert a Satoshi value to a BTC value and return it as a string |
||
| 1931 | |||
| 1932 | * @param int $satoshi |
||
| 1933 | * @return string |
||
| 1934 | */ |
||
| 1935 | public static function toBTCString($satoshi) { |
||
| 1938 | |||
| 1939 | /** |
||
| 1940 | * convert a BTC value to a Satoshi value |
||
| 1941 | * |
||
| 1942 | * @param float $btc |
||
| 1943 | * @return string |
||
| 1944 | */ |
||
| 1945 | 20 | public static function toSatoshiString($btc) { |
|
| 1948 | |||
| 1949 | /** |
||
| 1950 | * convert a BTC value to a Satoshi value |
||
| 1951 | * |
||
| 1952 | * @param float $btc |
||
| 1953 | * @return string |
||
| 1954 | */ |
||
| 1955 | 20 | public static function toSatoshi($btc) { |
|
| 1958 | |||
| 1959 | /** |
||
| 1960 | * json_decode helper that throws exceptions when it fails to decode |
||
| 1961 | * |
||
| 1962 | * @param $json |
||
| 1963 | * @param bool $assoc |
||
| 1964 | * @return mixed |
||
| 1965 | * @throws \Exception |
||
| 1966 | */ |
||
| 1967 | 7 | public static function jsonDecode($json, $assoc = false) { |
|
| 1980 | |||
| 1981 | /** |
||
| 1982 | * sort public keys for multisig script |
||
| 1983 | * |
||
| 1984 | * @param PublicKeyInterface[] $pubKeys |
||
| 1985 | * @return PublicKeyInterface[] |
||
| 1986 | */ |
||
| 1987 | 31 | public static function sortMultisigKeys(array $pubKeys) { |
|
| 1997 | |||
| 1998 | /** |
||
| 1999 | * read and decode the json payload from a webhook's POST request. |
||
| 2000 | * |
||
| 2001 | * @param bool $returnObject flag to indicate if an object or associative array should be returned |
||
| 2002 | * @return mixed|null |
||
| 2003 | * @throws \Exception |
||
| 2004 | */ |
||
| 2005 | public static function getWebhookPayload($returnObject = false) { |
||
| 2013 | |||
| 2014 | public static function normalizeBIP32KeyArray($keys) { |
||
| 2019 | |||
| 2020 | /** |
||
| 2021 | * @param array|BIP32Key $key |
||
| 2022 | * @return BIP32Key |
||
| 2023 | * @throws \Exception |
||
| 2024 | */ |
||
| 2025 | 54 | public static function normalizeBIP32Key($key) { |
|
| 2043 | |||
| 2044 | 3 | public function shuffle($arr) { |
|
| 2047 | } |
||
| 2048 |
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: