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 | 119 | public function __construct($apiKey, $apiSecret, $network = 'BTC', $testnet = false, $apiVersion = 'v1', $apiEndpoint = null) { |
|
| 79 | |||
| 80 | 119 | list ($apiNetwork, $testnet) = Util::parseApiNetwork($network, $testnet); |
|
| 81 | |||
| 82 | 119 | if (is_null($apiEndpoint)) { |
|
| 83 | 119 | $apiEndpoint = getenv('BLOCKTRAIL_SDK_API_ENDPOINT') ?: "https://api.blocktrail.com"; |
|
| 84 | 119 | $apiEndpoint = "{$apiEndpoint}/{$apiVersion}/{$apiNetwork}/"; |
|
| 85 | } |
||
| 86 | |||
| 87 | // normalize network and set bitcoinlib to the right magic-bytes |
||
| 88 | 119 | list($this->network, $this->testnet, $regtest) = $this->normalizeNetwork($network, $testnet); |
|
| 89 | 119 | $this->setBitcoinLibMagicBytes($this->network, $this->testnet, $regtest); |
|
| 90 | |||
| 91 | 119 | $btccomEndpoint = getenv('BLOCKTRAIL_SDK_BTCCOM_API_ENDPOINT'); |
|
| 92 | 119 | if (!$btccomEndpoint) { |
|
| 93 | $btccomEndpoint = "https://" . ($this->network === "BCC" ? "bch-chain" : "chain") . ".api.btc.com"; |
||
| 94 | } |
||
| 95 | 119 | $btccomEndpoint = "{$btccomEndpoint}/v3/"; |
|
| 96 | |||
| 97 | 119 | if ($this->testnet && strpos($btccomEndpoint, "tchain") === false) { |
|
| 98 | 33 | $btccomEndpoint = \str_replace("chain", "tchain", $btccomEndpoint); |
|
| 99 | } |
||
| 100 | |||
| 101 | 119 | $this->blocktrailClient = new RestClient($apiEndpoint, $apiVersion, $apiKey, $apiSecret); |
|
| 102 | 119 | $this->dataClient = new RestClient($btccomEndpoint, $apiVersion, $apiKey, $apiSecret); |
|
| 103 | |||
| 104 | 119 | $this->converter = new BtccomConverter(); |
|
| 105 | 119 | } |
|
| 106 | |||
| 107 | /** |
||
| 108 | * normalize network string |
||
| 109 | * |
||
| 110 | * @param $network |
||
| 111 | * @param $testnet |
||
| 112 | * @return array |
||
| 113 | * @throws \Exception |
||
| 114 | */ |
||
| 115 | 119 | 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 | 119 | 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') { |
|
| 226 | $queryString = [ |
||
| 227 | 1 | 'page' => $page, |
|
| 228 | 1 | 'limit' => $limit, |
|
| 229 | 1 | 'sort_dir' => $sortDir |
|
| 230 | ]; |
||
| 231 | 1 | $response = $this->dataClient->get($this->converter->getUrlForAddressTransactions($address), $this->converter->paginationParams($queryString)); |
|
| 232 | 1 | return $this->converter->convertAddressTxs($response->body()); |
|
| 233 | } |
||
| 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') { |
|
| 262 | $queryString = [ |
||
| 263 | 1 | 'page' => $page, |
|
| 264 | 1 | 'limit' => $limit, |
|
| 265 | 1 | 'sort_dir' => $sortDir |
|
| 266 | ]; |
||
| 267 | 1 | $response = $this->dataClient->get($this->converter->getUrlForAddressUnspent($address), $this->converter->paginationParams($queryString)); |
|
| 268 | 1 | return $this->converter->convertAddressUnspentOutputs($response->body(), $address); |
|
| 269 | } |
||
| 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 the wallet API's latest block ['hash' => x, 'height' => y] |
||
| 348 | * @return array associative array containing the response |
||
| 349 | */ |
||
| 350 | 1 | public function getWalletBlockLatest() { |
|
| 354 | |||
| 355 | /** |
||
| 356 | * get an individual block |
||
| 357 | * @param string|integer $block a block hash or a block height |
||
| 358 | * @return array associative array containing the response |
||
| 359 | */ |
||
| 360 | 1 | public function block($block) { |
|
| 364 | |||
| 365 | /** |
||
| 366 | * get all transaction in a block (paginated) |
||
| 367 | * @param string|integer $block a block hash or a block height |
||
| 368 | * @param integer $page pagination: page number |
||
| 369 | * @param integer $limit pagination: records per page |
||
| 370 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 371 | * @return array associative array containing the response |
||
| 372 | */ |
||
| 373 | public function blockTransactions($block, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * get a single transaction |
||
| 385 | * @param string $txhash transaction hash |
||
| 386 | * @return array associative array containing the response |
||
| 387 | */ |
||
| 388 | 1 | public function transaction($txhash) { |
|
| 398 | |||
| 399 | /** |
||
| 400 | * get a single transaction |
||
| 401 | * @param string[] $txhashes list of transaction hashes (up to 20) |
||
| 402 | * @return array[] array containing the response |
||
| 403 | */ |
||
| 404 | 1 | public function transactions($txhashes) { |
|
| 408 | |||
| 409 | /** |
||
| 410 | * get a paginated list of all webhooks associated with the api user |
||
| 411 | * @param integer $page pagination: page number |
||
| 412 | * @param integer $limit pagination: records per page |
||
| 413 | * @return array associative array containing the response |
||
| 414 | */ |
||
| 415 | public function allWebhooks($page = 1, $limit = 20) { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * get an existing webhook by it's identifier |
||
| 426 | * @param string $identifier a unique identifier associated with the webhook |
||
| 427 | * @return array associative array containing the response |
||
| 428 | */ |
||
| 429 | public function getWebhook($identifier) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * create a new webhook |
||
| 436 | * @param string $url the url to receive the webhook events |
||
| 437 | * @param string $identifier a unique identifier to associate with this webhook |
||
| 438 | * @return array associative array containing the response |
||
| 439 | */ |
||
| 440 | 1 | public function setupWebhook($url, $identifier = null) { |
|
| 448 | |||
| 449 | /** |
||
| 450 | * update an existing webhook |
||
| 451 | * @param string $identifier the unique identifier of the webhook to update |
||
| 452 | * @param string $newUrl the new url to receive the webhook events |
||
| 453 | * @param string $newIdentifier a new unique identifier to associate with this webhook |
||
| 454 | * @return array associative array containing the response |
||
| 455 | */ |
||
| 456 | public function updateWebhook($identifier, $newUrl = null, $newIdentifier = null) { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * deletes an existing webhook and any event subscriptions associated with it |
||
| 467 | * @param string $identifier the unique identifier of the webhook to delete |
||
| 468 | * @return boolean true on success |
||
| 469 | */ |
||
| 470 | public function deleteWebhook($identifier) { |
||
| 474 | |||
| 475 | /** |
||
| 476 | * get a paginated list of all the events a webhook is subscribed to |
||
| 477 | * @param string $identifier the unique identifier of the webhook |
||
| 478 | * @param integer $page pagination: page number |
||
| 479 | * @param integer $limit pagination: records per page |
||
| 480 | * @return array associative array containing the response |
||
| 481 | */ |
||
| 482 | public function getWebhookEvents($identifier, $page = 1, $limit = 20) { |
||
| 490 | |||
| 491 | /** |
||
| 492 | * subscribes a webhook to transaction events of one particular transaction |
||
| 493 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 494 | * @param string $transaction the transaction hash |
||
| 495 | * @param integer $confirmations the amount of confirmations to send. |
||
| 496 | * @return array associative array containing the response |
||
| 497 | */ |
||
| 498 | public function subscribeTransaction($identifier, $transaction, $confirmations = 6) { |
||
| 507 | |||
| 508 | /** |
||
| 509 | * subscribes a webhook to transaction events on a particular address |
||
| 510 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 511 | * @param string $address the address hash |
||
| 512 | * @param integer $confirmations the amount of confirmations to send. |
||
| 513 | * @return array associative array containing the response |
||
| 514 | */ |
||
| 515 | public function subscribeAddressTransactions($identifier, $address, $confirmations = 6) { |
||
| 524 | |||
| 525 | /** |
||
| 526 | * batch subscribes a webhook to multiple transaction events |
||
| 527 | * |
||
| 528 | * @param string $identifier the unique identifier of the webhook |
||
| 529 | * @param array $batchData A 2D array of event data: |
||
| 530 | * [address => $address, confirmations => $confirmations] |
||
| 531 | * where $address is the address to subscibe to |
||
| 532 | * and optionally $confirmations is the amount of confirmations |
||
| 533 | * @return boolean true on success |
||
| 534 | */ |
||
| 535 | public function batchSubscribeAddressTransactions($identifier, $batchData) { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * subscribes a webhook to a new block event |
||
| 550 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 551 | * @return array associative array containing the response |
||
| 552 | */ |
||
| 553 | public function subscribeNewBlocks($identifier) { |
||
| 560 | |||
| 561 | /** |
||
| 562 | * removes an transaction event subscription from a webhook |
||
| 563 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 564 | * @param string $transaction the transaction hash of the event subscription |
||
| 565 | * @return boolean true on success |
||
| 566 | */ |
||
| 567 | public function unsubscribeTransaction($identifier, $transaction) { |
||
| 571 | |||
| 572 | /** |
||
| 573 | * removes an address transaction event subscription from a webhook |
||
| 574 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 575 | * @param string $address the address hash of the event subscription |
||
| 576 | * @return boolean true on success |
||
| 577 | */ |
||
| 578 | public function unsubscribeAddressTransactions($identifier, $address) { |
||
| 582 | |||
| 583 | /** |
||
| 584 | * removes a block event subscription from a webhook |
||
| 585 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 586 | * @return boolean true on success |
||
| 587 | */ |
||
| 588 | public function unsubscribeNewBlocks($identifier) { |
||
| 592 | |||
| 593 | /** |
||
| 594 | * create a new wallet |
||
| 595 | * - will generate a new primary seed (with password) and backup seed (without password) |
||
| 596 | * - send the primary seed (BIP39 'encrypted') and backup public key to the server |
||
| 597 | * - receive the blocktrail co-signing public key from the server |
||
| 598 | * |
||
| 599 | * Either takes one argument: |
||
| 600 | * @param array $options |
||
| 601 | * |
||
| 602 | * Or takes three arguments (old, deprecated syntax): |
||
| 603 | * (@nonPHP-doc) @param $identifier |
||
| 604 | * (@nonPHP-doc) @param $password |
||
| 605 | * (@nonPHP-doc) @param int $keyIndex override for the blocktrail cosigning key to use |
||
| 606 | * |
||
| 607 | * @return array[WalletInterface, array] list($wallet, $backupInfo) |
||
| 608 | * @throws \Exception |
||
| 609 | */ |
||
| 610 | public function createNewWallet($options) { |
||
| 654 | |||
| 655 | protected function createNewWalletV1($options) { |
||
| 776 | |||
| 777 | public static function randomBits($bits) { |
||
| 780 | |||
| 781 | public static function randomBytes($bytes) { |
||
| 784 | |||
| 785 | protected function createNewWalletV2($options) { |
||
| 909 | |||
| 910 | protected function createNewWalletV3($options) { |
||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * @param array $bip32Key |
||
| 1058 | * @throws BlocktrailSDKException |
||
| 1059 | */ |
||
| 1060 | 3 | private function verifyPublicBIP32Key(array $bip32Key) { |
|
| 1070 | |||
| 1071 | /** |
||
| 1072 | * @param array $walletData |
||
| 1073 | * @throws BlocktrailSDKException |
||
| 1074 | */ |
||
| 1075 | 3 | private function verifyPublicOnly(array $walletData) { |
|
| 1079 | |||
| 1080 | /** |
||
| 1081 | * create wallet using the API |
||
| 1082 | * |
||
| 1083 | * @param string $identifier the wallet identifier to create |
||
| 1084 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1085 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1086 | * @param string $primaryMnemonic mnemonic to store |
||
| 1087 | * @param string $checksum checksum to store |
||
| 1088 | * @param int $keyIndex account that we expect to use |
||
| 1089 | * @param bool $segwit opt in to segwit |
||
| 1090 | * @param bool $requireActivation require wallet to be activated by another endpoint |
||
| 1091 | * @return mixed |
||
| 1092 | * @throws BlocktrailSDKException |
||
| 1093 | * @throws \Exception |
||
| 1094 | */ |
||
| 1095 | public function storeNewWalletV1($identifier, $primaryPublicKey, $backupPublicKey, $primaryMnemonic, $checksum, $keyIndex, $segwit = false, $requireActivation = false) { |
||
| 1112 | |||
| 1113 | /** |
||
| 1114 | * create wallet using the API |
||
| 1115 | * |
||
| 1116 | * @param string $identifier the wallet identifier to create |
||
| 1117 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1118 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1119 | * @param $encryptedPrimarySeed |
||
| 1120 | * @param $encryptedSecret |
||
| 1121 | * @param $recoverySecret |
||
| 1122 | * @param string $checksum checksum to store |
||
| 1123 | * @param int $keyIndex account that we expect to use |
||
| 1124 | * @param bool $segwit opt in to segwit |
||
| 1125 | * @param bool $requireActivation require wallet to be activated by another endpoint |
||
| 1126 | * @return mixed |
||
| 1127 | * @throws BlocktrailSDKException |
||
| 1128 | * @throws \Exception |
||
| 1129 | */ |
||
| 1130 | 3 | public function storeNewWalletV2($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false, $requireActivation = false) { |
|
| 1150 | |||
| 1151 | /** |
||
| 1152 | * create wallet using the API |
||
| 1153 | * |
||
| 1154 | * @param string $identifier the wallet identifier to create |
||
| 1155 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1156 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1157 | * @param $encryptedPrimarySeed |
||
| 1158 | * @param $encryptedSecret |
||
| 1159 | * @param $recoverySecret |
||
| 1160 | * @param string $checksum checksum to store |
||
| 1161 | * @param int $keyIndex account that we expect to use |
||
| 1162 | * @param bool $segwit opt in to segwit |
||
| 1163 | * @param bool $requireActivation require wallet to be activated by another endpoint |
||
| 1164 | * @return mixed |
||
| 1165 | * @throws BlocktrailSDKException |
||
| 1166 | * @throws \Exception |
||
| 1167 | */ |
||
| 1168 | public function storeNewWalletV3($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false, $requireActivation = false) { |
||
| 1188 | |||
| 1189 | /** |
||
| 1190 | * upgrade wallet to use a new account number |
||
| 1191 | * the account number specifies which blocktrail cosigning key is used |
||
| 1192 | * |
||
| 1193 | * @param string $identifier the wallet identifier to be upgraded |
||
| 1194 | * @param int $keyIndex the new account to use |
||
| 1195 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1196 | * @return mixed |
||
| 1197 | */ |
||
| 1198 | public function upgradeKeyIndex($identifier, $keyIndex, $primaryPublicKey) { |
||
| 1207 | |||
| 1208 | /** |
||
| 1209 | * @param array $options |
||
| 1210 | * @return AddressReaderBase |
||
| 1211 | */ |
||
| 1212 | private function makeAddressReader(array $options) { |
||
| 1223 | |||
| 1224 | /** |
||
| 1225 | * initialize a previously created wallet |
||
| 1226 | * |
||
| 1227 | * Takes an options object, or accepts identifier/password for backwards compatiblity. |
||
| 1228 | * |
||
| 1229 | * Some of the options: |
||
| 1230 | * - "readonly/readOnly/read-only" can be to a boolean value, |
||
| 1231 | * so the wallet is loaded in read-only mode (no private key) |
||
| 1232 | * - "check_backup_key" can be set to your own backup key: |
||
| 1233 | * Format: ["M', "xpub..."] |
||
| 1234 | * Setting this will allow the SDK to check the server hasn't |
||
| 1235 | * a different key (one it happens to control) |
||
| 1236 | |||
| 1237 | * Either takes one argument: |
||
| 1238 | * @param array $options |
||
| 1239 | * |
||
| 1240 | * Or takes two arguments (old, deprecated syntax): |
||
| 1241 | * (@nonPHP-doc) @param string $identifier the wallet identifier to be initialized |
||
| 1242 | * (@nonPHP-doc) @param string $password the password to decrypt the mnemonic with |
||
| 1243 | * |
||
| 1244 | * @return WalletInterface |
||
| 1245 | * @throws \Exception |
||
| 1246 | */ |
||
| 1247 | 23 | public function initWallet($options) { |
|
| 1359 | |||
| 1360 | /** |
||
| 1361 | * get the wallet data from the server |
||
| 1362 | * |
||
| 1363 | * @param string $identifier the identifier of the wallet |
||
| 1364 | * @return mixed |
||
| 1365 | */ |
||
| 1366 | 23 | public function getWallet($identifier) { |
|
| 1370 | |||
| 1371 | /** |
||
| 1372 | * update the wallet data on the server |
||
| 1373 | * |
||
| 1374 | * @param string $identifier |
||
| 1375 | * @param $data |
||
| 1376 | * @return mixed |
||
| 1377 | */ |
||
| 1378 | public function updateWallet($identifier, $data) { |
||
| 1382 | |||
| 1383 | /** |
||
| 1384 | * delete a wallet from the server |
||
| 1385 | * the checksum address and a signature to verify you ownership of the key of that checksum address |
||
| 1386 | * is required to be able to delete a wallet |
||
| 1387 | * |
||
| 1388 | * @param string $identifier the identifier of the wallet |
||
| 1389 | * @param string $checksumAddress the address for your master private key (and the checksum used when creating the wallet) |
||
| 1390 | * @param string $signature a signature of the checksum address as message signed by the private key matching that address |
||
| 1391 | * @param bool $force ignore warnings (such as a non-zero balance) |
||
| 1392 | * @return mixed |
||
| 1393 | */ |
||
| 1394 | public function deleteWallet($identifier, $checksumAddress, $signature, $force = false) { |
||
| 1401 | |||
| 1402 | /** |
||
| 1403 | * create new backup key; |
||
| 1404 | * 1) a BIP39 mnemonic |
||
| 1405 | * 2) a seed from that mnemonic with a blank password |
||
| 1406 | * 3) a private key from that seed |
||
| 1407 | * |
||
| 1408 | * @return array [mnemonic, seed, key] |
||
| 1409 | */ |
||
| 1410 | protected function newBackupSeed() { |
||
| 1415 | |||
| 1416 | /** |
||
| 1417 | * create new primary key; |
||
| 1418 | * 1) a BIP39 mnemonic |
||
| 1419 | * 2) a seed from that mnemonic with the password |
||
| 1420 | * 3) a private key from that seed |
||
| 1421 | * |
||
| 1422 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1423 | * @return array [mnemonic, seed, key] |
||
| 1424 | * @TODO: require a strong password? |
||
| 1425 | */ |
||
| 1426 | protected function newPrimarySeed($passphrase) { |
||
| 1431 | |||
| 1432 | /** |
||
| 1433 | * create a new key; |
||
| 1434 | * 1) a BIP39 mnemonic |
||
| 1435 | * 2) a seed from that mnemonic with the password |
||
| 1436 | * 3) a private key from that seed |
||
| 1437 | * |
||
| 1438 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1439 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1440 | * @return array |
||
| 1441 | */ |
||
| 1442 | protected function generateNewSeed($passphrase = "", $forceEntropy = null) { |
||
| 1459 | |||
| 1460 | /** |
||
| 1461 | * generate a new mnemonic from some random entropy (512 bit) |
||
| 1462 | * |
||
| 1463 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1464 | * @return string |
||
| 1465 | * @throws \Exception |
||
| 1466 | */ |
||
| 1467 | protected function generateNewMnemonic($forceEntropy = null) { |
||
| 1477 | |||
| 1478 | /** |
||
| 1479 | * get the balance for the wallet |
||
| 1480 | * |
||
| 1481 | * @param string $identifier the identifier of the wallet |
||
| 1482 | * @return array |
||
| 1483 | */ |
||
| 1484 | public function getWalletBalance($identifier) { |
||
| 1488 | |||
| 1489 | /** |
||
| 1490 | * get a new derivation number for specified parent path |
||
| 1491 | * 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 |
||
| 1492 | * |
||
| 1493 | * returns the path |
||
| 1494 | * |
||
| 1495 | * @param string $identifier the identifier of the wallet |
||
| 1496 | * @param string $path the parent path for which to get a new derivation |
||
| 1497 | * @return string |
||
| 1498 | */ |
||
| 1499 | public function getNewDerivation($identifier, $path) { |
||
| 1503 | |||
| 1504 | /** |
||
| 1505 | * get a new derivation number for specified parent path |
||
| 1506 | * 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 |
||
| 1507 | * |
||
| 1508 | * @param string $identifier the identifier of the wallet |
||
| 1509 | * @param string $path the parent path for which to get a new derivation |
||
| 1510 | * @return mixed |
||
| 1511 | */ |
||
| 1512 | public function _getNewDerivation($identifier, $path) { |
||
| 1516 | |||
| 1517 | /** |
||
| 1518 | * get the path (and redeemScript) to specified address |
||
| 1519 | * |
||
| 1520 | * @param string $identifier |
||
| 1521 | * @param string $address |
||
| 1522 | * @return array |
||
| 1523 | * @throws \Exception |
||
| 1524 | */ |
||
| 1525 | public function getPathForAddress($identifier, $address) { |
||
| 1529 | |||
| 1530 | /** |
||
| 1531 | * send the transaction using the API |
||
| 1532 | * |
||
| 1533 | * @param string $identifier the identifier of the wallet |
||
| 1534 | * @param string|array $rawTransaction raw hex of the transaction (should be partially signed) |
||
| 1535 | * @param array $paths list of the paths that were used for the UTXO |
||
| 1536 | * @param bool $checkFee let the server verify the fee after signing |
||
| 1537 | * @param null $twoFactorToken |
||
| 1538 | * @return string the complete raw transaction |
||
| 1539 | * @throws \Exception |
||
| 1540 | */ |
||
| 1541 | public function sendTransaction($identifier, $rawTransaction, $paths, $checkFee = false, $twoFactorToken = null) { |
||
| 1572 | |||
| 1573 | /** |
||
| 1574 | * use the API to get the best inputs to use based on the outputs |
||
| 1575 | * |
||
| 1576 | * the return array has the following format: |
||
| 1577 | * [ |
||
| 1578 | * "utxos" => [ |
||
| 1579 | * [ |
||
| 1580 | * "hash" => "<txHash>", |
||
| 1581 | * "idx" => "<index of the output of that <txHash>", |
||
| 1582 | * "scriptpubkey_hex" => "<scriptPubKey-hex>", |
||
| 1583 | * "value" => 32746327, |
||
| 1584 | * "address" => "1address", |
||
| 1585 | * "path" => "m/44'/1'/0'/0/13", |
||
| 1586 | * "redeem_script" => "<redeemScript-hex>", |
||
| 1587 | * ], |
||
| 1588 | * ], |
||
| 1589 | * "fee" => 10000, |
||
| 1590 | * "change"=> 1010109201, |
||
| 1591 | * ] |
||
| 1592 | * |
||
| 1593 | * @param string $identifier the identifier of the wallet |
||
| 1594 | * @param array $outputs the outputs you want to create - array[address => satoshi-value] |
||
| 1595 | * @param bool $lockUTXO when TRUE the UTXOs selected will be locked for a few seconds |
||
| 1596 | * so you have some time to spend them without race-conditions |
||
| 1597 | * @param bool $allowZeroConf |
||
| 1598 | * @param string $feeStrategy |
||
| 1599 | * @param null|int $forceFee |
||
| 1600 | * @return array |
||
| 1601 | * @throws \Exception |
||
| 1602 | */ |
||
| 1603 | public function coinSelection($identifier, $outputs, $lockUTXO = false, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null) { |
||
| 1623 | |||
| 1624 | /** |
||
| 1625 | * |
||
| 1626 | * @param string $identifier the identifier of the wallet |
||
| 1627 | * @param bool $allowZeroConf |
||
| 1628 | * @param string $feeStrategy |
||
| 1629 | * @param null|int $forceFee |
||
| 1630 | * @param int $outputCnt |
||
| 1631 | * @return array |
||
| 1632 | * @throws \Exception |
||
| 1633 | */ |
||
| 1634 | public function walletMaxSpendable($identifier, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null, $outputCnt = 1) { |
||
| 1653 | |||
| 1654 | /** |
||
| 1655 | * @return array ['optimal_fee' => 10000, 'low_priority_fee' => 5000] |
||
| 1656 | */ |
||
| 1657 | public function feePerKB() { |
||
| 1661 | |||
| 1662 | /** |
||
| 1663 | * get the current price index |
||
| 1664 | * |
||
| 1665 | * @return array eg; ['USD' => 287.30] |
||
| 1666 | */ |
||
| 1667 | 1 | public function price() { |
|
| 1671 | |||
| 1672 | /** |
||
| 1673 | * setup webhook for wallet |
||
| 1674 | * |
||
| 1675 | * @param string $identifier the wallet identifier for which to create the webhook |
||
| 1676 | * @param string $webhookIdentifier the webhook identifier to use |
||
| 1677 | * @param string $url the url to receive the webhook events |
||
| 1678 | * @return array |
||
| 1679 | */ |
||
| 1680 | public function setupWalletWebhook($identifier, $webhookIdentifier, $url) { |
||
| 1684 | |||
| 1685 | /** |
||
| 1686 | * delete webhook for wallet |
||
| 1687 | * |
||
| 1688 | * @param string $identifier the wallet identifier for which to delete the webhook |
||
| 1689 | * @param string $webhookIdentifier the webhook identifier to delete |
||
| 1690 | * @return array |
||
| 1691 | */ |
||
| 1692 | public function deleteWalletWebhook($identifier, $webhookIdentifier) { |
||
| 1696 | |||
| 1697 | /** |
||
| 1698 | * lock a specific unspent output |
||
| 1699 | * |
||
| 1700 | * @param $identifier |
||
| 1701 | * @param $txHash |
||
| 1702 | * @param $txIdx |
||
| 1703 | * @param int $ttl |
||
| 1704 | * @return bool |
||
| 1705 | */ |
||
| 1706 | public function lockWalletUTXO($identifier, $txHash, $txIdx, $ttl = 3) { |
||
| 1710 | |||
| 1711 | /** |
||
| 1712 | * unlock a specific unspent output |
||
| 1713 | * |
||
| 1714 | * @param $identifier |
||
| 1715 | * @param $txHash |
||
| 1716 | * @param $txIdx |
||
| 1717 | * @return bool |
||
| 1718 | */ |
||
| 1719 | public function unlockWalletUTXO($identifier, $txHash, $txIdx) { |
||
| 1723 | |||
| 1724 | /** |
||
| 1725 | * get all transactions for wallet (paginated) |
||
| 1726 | * |
||
| 1727 | * @param string $identifier the wallet identifier for which to get transactions |
||
| 1728 | * @param integer $page pagination: page number |
||
| 1729 | * @param integer $limit pagination: records per page (max 500) |
||
| 1730 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1731 | * @return array associative array containing the response |
||
| 1732 | */ |
||
| 1733 | public function walletTransactions($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 1742 | |||
| 1743 | /** |
||
| 1744 | * get all addresses for wallet (paginated) |
||
| 1745 | * |
||
| 1746 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1747 | * @param integer $page pagination: page number |
||
| 1748 | * @param integer $limit pagination: records per page (max 500) |
||
| 1749 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1750 | * @return array associative array containing the response |
||
| 1751 | */ |
||
| 1752 | public function walletAddresses($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 1761 | |||
| 1762 | /** |
||
| 1763 | * get all UTXOs for wallet (paginated) |
||
| 1764 | * |
||
| 1765 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1766 | * @param integer $page pagination: page number |
||
| 1767 | * @param integer $limit pagination: records per page (max 500) |
||
| 1768 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1769 | * @param boolean $zeroconf include zero confirmation transactions |
||
| 1770 | * @return array associative array containing the response |
||
| 1771 | */ |
||
| 1772 | public function walletUTXOs($identifier, $page = 1, $limit = 20, $sortDir = 'asc', $zeroconf = true) { |
||
| 1782 | |||
| 1783 | /** |
||
| 1784 | * get a paginated list of all wallets associated with the api user |
||
| 1785 | * |
||
| 1786 | * @param integer $page pagination: page number |
||
| 1787 | * @param integer $limit pagination: records per page |
||
| 1788 | * @return array associative array containing the response |
||
| 1789 | */ |
||
| 1790 | public function allWallets($page = 1, $limit = 20) { |
||
| 1798 | |||
| 1799 | /** |
||
| 1800 | * send raw transaction |
||
| 1801 | * |
||
| 1802 | * @param $txHex |
||
| 1803 | * @return bool |
||
| 1804 | */ |
||
| 1805 | public function sendRawTransaction($txHex) { |
||
| 1809 | |||
| 1810 | /** |
||
| 1811 | * testnet only ;-) |
||
| 1812 | * |
||
| 1813 | * @param $address |
||
| 1814 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1815 | * @return mixed |
||
| 1816 | * @throws \Exception |
||
| 1817 | */ |
||
| 1818 | public function faucetWithdrawal($address, $amount = 10000) { |
||
| 1825 | |||
| 1826 | /** |
||
| 1827 | * Exists for BC. Remove at major bump. |
||
| 1828 | * |
||
| 1829 | * @see faucetWithdrawal |
||
| 1830 | * @deprecated |
||
| 1831 | * @param $address |
||
| 1832 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1833 | * @return mixed |
||
| 1834 | * @throws \Exception |
||
| 1835 | */ |
||
| 1836 | public function faucetWithdrawl($address, $amount = 10000) { |
||
| 1839 | |||
| 1840 | /** |
||
| 1841 | * verify a message signed bitcoin-core style |
||
| 1842 | * |
||
| 1843 | * @param string $message |
||
| 1844 | * @param string $address |
||
| 1845 | * @param string $signature |
||
| 1846 | * @return boolean |
||
| 1847 | */ |
||
| 1848 | 2 | public function verifyMessage($message, $address, $signature) { |
|
| 1862 | |||
| 1863 | /** |
||
| 1864 | * Take a base58 or cashaddress, and return only |
||
| 1865 | * the cash address. |
||
| 1866 | * This function only works on bitcoin cash. |
||
| 1867 | * @param string $input |
||
| 1868 | * @return string |
||
| 1869 | * @throws BlocktrailSDKException |
||
| 1870 | */ |
||
| 1871 | public function getLegacyBitcoinCashAddress($input) { |
||
| 1888 | |||
| 1889 | /** |
||
| 1890 | * convert a Satoshi value to a BTC value |
||
| 1891 | * |
||
| 1892 | * @param int $satoshi |
||
| 1893 | * @return float |
||
| 1894 | */ |
||
| 1895 | 1 | public static function toBTC($satoshi) { |
|
| 1898 | |||
| 1899 | /** |
||
| 1900 | * convert a Satoshi value to a BTC value and return it as a string |
||
| 1901 | |||
| 1902 | * @param int $satoshi |
||
| 1903 | * @return string |
||
| 1904 | */ |
||
| 1905 | public static function toBTCString($satoshi) { |
||
| 1908 | |||
| 1909 | /** |
||
| 1910 | * convert a BTC value to a Satoshi value |
||
| 1911 | * |
||
| 1912 | * @param float $btc |
||
| 1913 | * @return string |
||
| 1914 | */ |
||
| 1915 | 1 | public static function toSatoshiString($btc) { |
|
| 1918 | |||
| 1919 | /** |
||
| 1920 | * convert a BTC value to a Satoshi value |
||
| 1921 | * |
||
| 1922 | * @param float $btc |
||
| 1923 | * @return string |
||
| 1924 | */ |
||
| 1925 | 1 | public static function toSatoshi($btc) { |
|
| 1928 | |||
| 1929 | /** |
||
| 1930 | * json_decode helper that throws exceptions when it fails to decode |
||
| 1931 | * |
||
| 1932 | * @param $json |
||
| 1933 | * @param bool $assoc |
||
| 1934 | * @return mixed |
||
| 1935 | * @throws \Exception |
||
| 1936 | */ |
||
| 1937 | 3 | public static function jsonDecode($json, $assoc = false) { |
|
| 1950 | |||
| 1951 | /** |
||
| 1952 | * sort public keys for multisig script |
||
| 1953 | * |
||
| 1954 | * @param PublicKeyInterface[] $pubKeys |
||
| 1955 | * @return PublicKeyInterface[] |
||
| 1956 | */ |
||
| 1957 | public static function sortMultisigKeys(array $pubKeys) { |
||
| 1967 | |||
| 1968 | /** |
||
| 1969 | * read and decode the json payload from a webhook's POST request. |
||
| 1970 | * |
||
| 1971 | * @param bool $returnObject flag to indicate if an object or associative array should be returned |
||
| 1972 | * @return mixed|null |
||
| 1973 | * @throws \Exception |
||
| 1974 | */ |
||
| 1975 | public static function getWebhookPayload($returnObject = false) { |
||
| 1983 | |||
| 1984 | public static function normalizeBIP32KeyArray($keys) { |
||
| 1989 | |||
| 1990 | /** |
||
| 1991 | * @param array|BIP32Key $key |
||
| 1992 | * @return BIP32Key |
||
| 1993 | * @throws \Exception |
||
| 1994 | */ |
||
| 1995 | public static function normalizeBIP32Key($key) { |
||
| 2013 | } |
||
| 2014 |
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: