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) { |
||
| 775 | |||
| 776 | public static function randomBits($bits) { |
||
| 779 | |||
| 780 | public static function randomBytes($bytes) { |
||
| 783 | |||
| 784 | protected function createNewWalletV2($options) { |
||
| 907 | |||
| 908 | protected function createNewWalletV3($options) { |
||
| 1052 | |||
| 1053 | /** |
||
| 1054 | * @param array $bip32Key |
||
| 1055 | * @throws BlocktrailSDKException |
||
| 1056 | */ |
||
| 1057 | 3 | private function verifyPublicBIP32Key(array $bip32Key) { |
|
| 1067 | |||
| 1068 | /** |
||
| 1069 | * @param array $walletData |
||
| 1070 | * @throws BlocktrailSDKException |
||
| 1071 | */ |
||
| 1072 | 3 | private function verifyPublicOnly(array $walletData) { |
|
| 1076 | |||
| 1077 | /** |
||
| 1078 | * create wallet using the API |
||
| 1079 | * |
||
| 1080 | * @param string $identifier the wallet identifier to create |
||
| 1081 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1082 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1083 | * @param string $primaryMnemonic mnemonic to store |
||
| 1084 | * @param string $checksum checksum to store |
||
| 1085 | * @param int $keyIndex account that we expect to use |
||
| 1086 | * @param bool $segwit opt in to segwit |
||
| 1087 | * @return mixed |
||
| 1088 | */ |
||
| 1089 | public function storeNewWalletV1($identifier, $primaryPublicKey, $backupPublicKey, $primaryMnemonic, $checksum, $keyIndex, $segwit = false) { |
||
| 1103 | |||
| 1104 | /** |
||
| 1105 | * create wallet using the API |
||
| 1106 | * |
||
| 1107 | * @param string $identifier the wallet identifier to create |
||
| 1108 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1109 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1110 | * @param $encryptedPrimarySeed |
||
| 1111 | * @param $encryptedSecret |
||
| 1112 | * @param $recoverySecret |
||
| 1113 | * @param string $checksum checksum to store |
||
| 1114 | * @param int $keyIndex account that we expect to use |
||
| 1115 | * @param bool $segwit opt in to segwit |
||
| 1116 | * @return mixed |
||
| 1117 | * @throws \Exception |
||
| 1118 | */ |
||
| 1119 | 3 | public function storeNewWalletV2($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false) { |
|
| 1136 | |||
| 1137 | /** |
||
| 1138 | * create wallet using the API |
||
| 1139 | * |
||
| 1140 | * @param string $identifier the wallet identifier to create |
||
| 1141 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1142 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1143 | * @param $encryptedPrimarySeed |
||
| 1144 | * @param $encryptedSecret |
||
| 1145 | * @param $recoverySecret |
||
| 1146 | * @param string $checksum checksum to store |
||
| 1147 | * @param int $keyIndex account that we expect to use |
||
| 1148 | * @param bool $segwit opt in to segwit |
||
| 1149 | * @return mixed |
||
| 1150 | * @throws \Exception |
||
| 1151 | */ |
||
| 1152 | public function storeNewWalletV3($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false) { |
||
| 1171 | |||
| 1172 | /** |
||
| 1173 | * upgrade wallet to use a new account number |
||
| 1174 | * the account number specifies which blocktrail cosigning key is used |
||
| 1175 | * |
||
| 1176 | * @param string $identifier the wallet identifier to be upgraded |
||
| 1177 | * @param int $keyIndex the new account to use |
||
| 1178 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1179 | * @return mixed |
||
| 1180 | */ |
||
| 1181 | public function upgradeKeyIndex($identifier, $keyIndex, $primaryPublicKey) { |
||
| 1190 | |||
| 1191 | /** |
||
| 1192 | * @param array $options |
||
| 1193 | * @return AddressReaderBase |
||
| 1194 | */ |
||
| 1195 | private function makeAddressReader(array $options) { |
||
| 1206 | |||
| 1207 | /** |
||
| 1208 | * initialize a previously created wallet |
||
| 1209 | * |
||
| 1210 | * Takes an options object, or accepts identifier/password for backwards compatiblity. |
||
| 1211 | * |
||
| 1212 | * Some of the options: |
||
| 1213 | * - "readonly/readOnly/read-only" can be to a boolean value, |
||
| 1214 | * so the wallet is loaded in read-only mode (no private key) |
||
| 1215 | * - "check_backup_key" can be set to your own backup key: |
||
| 1216 | * Format: ["M', "xpub..."] |
||
| 1217 | * Setting this will allow the SDK to check the server hasn't |
||
| 1218 | * a different key (one it happens to control) |
||
| 1219 | |||
| 1220 | * Either takes one argument: |
||
| 1221 | * @param array $options |
||
| 1222 | * |
||
| 1223 | * Or takes two arguments (old, deprecated syntax): |
||
| 1224 | * (@nonPHP-doc) @param string $identifier the wallet identifier to be initialized |
||
| 1225 | * (@nonPHP-doc) @param string $password the password to decrypt the mnemonic with |
||
| 1226 | * |
||
| 1227 | * @return WalletInterface |
||
| 1228 | * @throws \Exception |
||
| 1229 | */ |
||
| 1230 | 23 | public function initWallet($options) { |
|
| 1342 | |||
| 1343 | /** |
||
| 1344 | * get the wallet data from the server |
||
| 1345 | * |
||
| 1346 | * @param string $identifier the identifier of the wallet |
||
| 1347 | * @return mixed |
||
| 1348 | */ |
||
| 1349 | 23 | public function getWallet($identifier) { |
|
| 1353 | |||
| 1354 | /** |
||
| 1355 | * update the wallet data on the server |
||
| 1356 | * |
||
| 1357 | * @param string $identifier |
||
| 1358 | * @param $data |
||
| 1359 | * @return mixed |
||
| 1360 | */ |
||
| 1361 | public function updateWallet($identifier, $data) { |
||
| 1365 | |||
| 1366 | /** |
||
| 1367 | * delete a wallet from the server |
||
| 1368 | * the checksum address and a signature to verify you ownership of the key of that checksum address |
||
| 1369 | * is required to be able to delete a wallet |
||
| 1370 | * |
||
| 1371 | * @param string $identifier the identifier of the wallet |
||
| 1372 | * @param string $checksumAddress the address for your master private key (and the checksum used when creating the wallet) |
||
| 1373 | * @param string $signature a signature of the checksum address as message signed by the private key matching that address |
||
| 1374 | * @param bool $force ignore warnings (such as a non-zero balance) |
||
| 1375 | * @return mixed |
||
| 1376 | */ |
||
| 1377 | public function deleteWallet($identifier, $checksumAddress, $signature, $force = false) { |
||
| 1384 | |||
| 1385 | /** |
||
| 1386 | * create new backup key; |
||
| 1387 | * 1) a BIP39 mnemonic |
||
| 1388 | * 2) a seed from that mnemonic with a blank password |
||
| 1389 | * 3) a private key from that seed |
||
| 1390 | * |
||
| 1391 | * @return array [mnemonic, seed, key] |
||
| 1392 | */ |
||
| 1393 | protected function newBackupSeed() { |
||
| 1398 | |||
| 1399 | /** |
||
| 1400 | * create new primary key; |
||
| 1401 | * 1) a BIP39 mnemonic |
||
| 1402 | * 2) a seed from that mnemonic with the password |
||
| 1403 | * 3) a private key from that seed |
||
| 1404 | * |
||
| 1405 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1406 | * @return array [mnemonic, seed, key] |
||
| 1407 | * @TODO: require a strong password? |
||
| 1408 | */ |
||
| 1409 | protected function newPrimarySeed($passphrase) { |
||
| 1414 | |||
| 1415 | /** |
||
| 1416 | * create a new key; |
||
| 1417 | * 1) a BIP39 mnemonic |
||
| 1418 | * 2) a seed from that mnemonic with the password |
||
| 1419 | * 3) a private key from that seed |
||
| 1420 | * |
||
| 1421 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1422 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1423 | * @return array |
||
| 1424 | */ |
||
| 1425 | protected function generateNewSeed($passphrase = "", $forceEntropy = null) { |
||
| 1442 | |||
| 1443 | /** |
||
| 1444 | * generate a new mnemonic from some random entropy (512 bit) |
||
| 1445 | * |
||
| 1446 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1447 | * @return string |
||
| 1448 | * @throws \Exception |
||
| 1449 | */ |
||
| 1450 | protected function generateNewMnemonic($forceEntropy = null) { |
||
| 1460 | |||
| 1461 | /** |
||
| 1462 | * get the balance for the wallet |
||
| 1463 | * |
||
| 1464 | * @param string $identifier the identifier of the wallet |
||
| 1465 | * @return array |
||
| 1466 | */ |
||
| 1467 | public function getWalletBalance($identifier) { |
||
| 1471 | |||
| 1472 | /** |
||
| 1473 | * get a new derivation number for specified parent path |
||
| 1474 | * 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 |
||
| 1475 | * |
||
| 1476 | * returns the path |
||
| 1477 | * |
||
| 1478 | * @param string $identifier the identifier of the wallet |
||
| 1479 | * @param string $path the parent path for which to get a new derivation |
||
| 1480 | * @return string |
||
| 1481 | */ |
||
| 1482 | public function getNewDerivation($identifier, $path) { |
||
| 1486 | |||
| 1487 | /** |
||
| 1488 | * get a new derivation number for specified parent path |
||
| 1489 | * 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 |
||
| 1490 | * |
||
| 1491 | * @param string $identifier the identifier of the wallet |
||
| 1492 | * @param string $path the parent path for which to get a new derivation |
||
| 1493 | * @return mixed |
||
| 1494 | */ |
||
| 1495 | public function _getNewDerivation($identifier, $path) { |
||
| 1499 | |||
| 1500 | /** |
||
| 1501 | * get the path (and redeemScript) to specified address |
||
| 1502 | * |
||
| 1503 | * @param string $identifier |
||
| 1504 | * @param string $address |
||
| 1505 | * @return array |
||
| 1506 | * @throws \Exception |
||
| 1507 | */ |
||
| 1508 | public function getPathForAddress($identifier, $address) { |
||
| 1512 | |||
| 1513 | /** |
||
| 1514 | * send the transaction using the API |
||
| 1515 | * |
||
| 1516 | * @param string $identifier the identifier of the wallet |
||
| 1517 | * @param string|array $rawTransaction raw hex of the transaction (should be partially signed) |
||
| 1518 | * @param array $paths list of the paths that were used for the UTXO |
||
| 1519 | * @param bool $checkFee let the server verify the fee after signing |
||
| 1520 | * @param null $twoFactorToken |
||
| 1521 | * @return string the complete raw transaction |
||
| 1522 | * @throws \Exception |
||
| 1523 | */ |
||
| 1524 | public function sendTransaction($identifier, $rawTransaction, $paths, $checkFee = false, $twoFactorToken = null) { |
||
| 1555 | |||
| 1556 | /** |
||
| 1557 | * use the API to get the best inputs to use based on the outputs |
||
| 1558 | * |
||
| 1559 | * the return array has the following format: |
||
| 1560 | * [ |
||
| 1561 | * "utxos" => [ |
||
| 1562 | * [ |
||
| 1563 | * "hash" => "<txHash>", |
||
| 1564 | * "idx" => "<index of the output of that <txHash>", |
||
| 1565 | * "scriptpubkey_hex" => "<scriptPubKey-hex>", |
||
| 1566 | * "value" => 32746327, |
||
| 1567 | * "address" => "1address", |
||
| 1568 | * "path" => "m/44'/1'/0'/0/13", |
||
| 1569 | * "redeem_script" => "<redeemScript-hex>", |
||
| 1570 | * ], |
||
| 1571 | * ], |
||
| 1572 | * "fee" => 10000, |
||
| 1573 | * "change"=> 1010109201, |
||
| 1574 | * ] |
||
| 1575 | * |
||
| 1576 | * @param string $identifier the identifier of the wallet |
||
| 1577 | * @param array $outputs the outputs you want to create - array[address => satoshi-value] |
||
| 1578 | * @param bool $lockUTXO when TRUE the UTXOs selected will be locked for a few seconds |
||
| 1579 | * so you have some time to spend them without race-conditions |
||
| 1580 | * @param bool $allowZeroConf |
||
| 1581 | * @param string $feeStrategy |
||
| 1582 | * @param null|int $forceFee |
||
| 1583 | * @return array |
||
| 1584 | * @throws \Exception |
||
| 1585 | */ |
||
| 1586 | public function coinSelection($identifier, $outputs, $lockUTXO = false, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null) { |
||
| 1606 | |||
| 1607 | /** |
||
| 1608 | * |
||
| 1609 | * @param string $identifier the identifier of the wallet |
||
| 1610 | * @param bool $allowZeroConf |
||
| 1611 | * @param string $feeStrategy |
||
| 1612 | * @param null|int $forceFee |
||
| 1613 | * @param int $outputCnt |
||
| 1614 | * @return array |
||
| 1615 | * @throws \Exception |
||
| 1616 | */ |
||
| 1617 | public function walletMaxSpendable($identifier, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null, $outputCnt = 1) { |
||
| 1636 | |||
| 1637 | /** |
||
| 1638 | * @return array ['optimal_fee' => 10000, 'low_priority_fee' => 5000] |
||
| 1639 | */ |
||
| 1640 | public function feePerKB() { |
||
| 1644 | |||
| 1645 | /** |
||
| 1646 | * get the current price index |
||
| 1647 | * |
||
| 1648 | * @return array eg; ['USD' => 287.30] |
||
| 1649 | */ |
||
| 1650 | 1 | public function price() { |
|
| 1654 | |||
| 1655 | /** |
||
| 1656 | * setup webhook for wallet |
||
| 1657 | * |
||
| 1658 | * @param string $identifier the wallet identifier for which to create the webhook |
||
| 1659 | * @param string $webhookIdentifier the webhook identifier to use |
||
| 1660 | * @param string $url the url to receive the webhook events |
||
| 1661 | * @return array |
||
| 1662 | */ |
||
| 1663 | public function setupWalletWebhook($identifier, $webhookIdentifier, $url) { |
||
| 1667 | |||
| 1668 | /** |
||
| 1669 | * delete webhook for wallet |
||
| 1670 | * |
||
| 1671 | * @param string $identifier the wallet identifier for which to delete the webhook |
||
| 1672 | * @param string $webhookIdentifier the webhook identifier to delete |
||
| 1673 | * @return array |
||
| 1674 | */ |
||
| 1675 | public function deleteWalletWebhook($identifier, $webhookIdentifier) { |
||
| 1679 | |||
| 1680 | /** |
||
| 1681 | * lock a specific unspent output |
||
| 1682 | * |
||
| 1683 | * @param $identifier |
||
| 1684 | * @param $txHash |
||
| 1685 | * @param $txIdx |
||
| 1686 | * @param int $ttl |
||
| 1687 | * @return bool |
||
| 1688 | */ |
||
| 1689 | public function lockWalletUTXO($identifier, $txHash, $txIdx, $ttl = 3) { |
||
| 1693 | |||
| 1694 | /** |
||
| 1695 | * unlock a specific unspent output |
||
| 1696 | * |
||
| 1697 | * @param $identifier |
||
| 1698 | * @param $txHash |
||
| 1699 | * @param $txIdx |
||
| 1700 | * @return bool |
||
| 1701 | */ |
||
| 1702 | public function unlockWalletUTXO($identifier, $txHash, $txIdx) { |
||
| 1706 | |||
| 1707 | /** |
||
| 1708 | * get all transactions for wallet (paginated) |
||
| 1709 | * |
||
| 1710 | * @param string $identifier the wallet identifier for which to get transactions |
||
| 1711 | * @param integer $page pagination: page number |
||
| 1712 | * @param integer $limit pagination: records per page (max 500) |
||
| 1713 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1714 | * @return array associative array containing the response |
||
| 1715 | */ |
||
| 1716 | public function walletTransactions($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 1725 | |||
| 1726 | /** |
||
| 1727 | * get all addresses for wallet (paginated) |
||
| 1728 | * |
||
| 1729 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1730 | * @param integer $page pagination: page number |
||
| 1731 | * @param integer $limit pagination: records per page (max 500) |
||
| 1732 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1733 | * @return array associative array containing the response |
||
| 1734 | */ |
||
| 1735 | public function walletAddresses($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 1744 | |||
| 1745 | /** |
||
| 1746 | * get all UTXOs for wallet (paginated) |
||
| 1747 | * |
||
| 1748 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1749 | * @param integer $page pagination: page number |
||
| 1750 | * @param integer $limit pagination: records per page (max 500) |
||
| 1751 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1752 | * @param boolean $zeroconf include zero confirmation transactions |
||
| 1753 | * @return array associative array containing the response |
||
| 1754 | */ |
||
| 1755 | public function walletUTXOs($identifier, $page = 1, $limit = 20, $sortDir = 'asc', $zeroconf = true) { |
||
| 1765 | |||
| 1766 | /** |
||
| 1767 | * get a paginated list of all wallets associated with the api user |
||
| 1768 | * |
||
| 1769 | * @param integer $page pagination: page number |
||
| 1770 | * @param integer $limit pagination: records per page |
||
| 1771 | * @return array associative array containing the response |
||
| 1772 | */ |
||
| 1773 | public function allWallets($page = 1, $limit = 20) { |
||
| 1781 | |||
| 1782 | /** |
||
| 1783 | * send raw transaction |
||
| 1784 | * |
||
| 1785 | * @param $txHex |
||
| 1786 | * @return bool |
||
| 1787 | */ |
||
| 1788 | public function sendRawTransaction($txHex) { |
||
| 1792 | |||
| 1793 | /** |
||
| 1794 | * testnet only ;-) |
||
| 1795 | * |
||
| 1796 | * @param $address |
||
| 1797 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1798 | * @return mixed |
||
| 1799 | * @throws \Exception |
||
| 1800 | */ |
||
| 1801 | public function faucetWithdrawal($address, $amount = 10000) { |
||
| 1808 | |||
| 1809 | /** |
||
| 1810 | * Exists for BC. Remove at major bump. |
||
| 1811 | * |
||
| 1812 | * @see faucetWithdrawal |
||
| 1813 | * @deprecated |
||
| 1814 | * @param $address |
||
| 1815 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1816 | * @return mixed |
||
| 1817 | * @throws \Exception |
||
| 1818 | */ |
||
| 1819 | public function faucetWithdrawl($address, $amount = 10000) { |
||
| 1822 | |||
| 1823 | /** |
||
| 1824 | * verify a message signed bitcoin-core style |
||
| 1825 | * |
||
| 1826 | * @param string $message |
||
| 1827 | * @param string $address |
||
| 1828 | * @param string $signature |
||
| 1829 | * @return boolean |
||
| 1830 | */ |
||
| 1831 | 2 | public function verifyMessage($message, $address, $signature) { |
|
| 1845 | |||
| 1846 | /** |
||
| 1847 | * Take a base58 or cashaddress, and return only |
||
| 1848 | * the cash address. |
||
| 1849 | * This function only works on bitcoin cash. |
||
| 1850 | * @param string $input |
||
| 1851 | * @return string |
||
| 1852 | * @throws BlocktrailSDKException |
||
| 1853 | */ |
||
| 1854 | public function getLegacyBitcoinCashAddress($input) { |
||
| 1871 | |||
| 1872 | /** |
||
| 1873 | * convert a Satoshi value to a BTC value |
||
| 1874 | * |
||
| 1875 | * @param int $satoshi |
||
| 1876 | * @return float |
||
| 1877 | */ |
||
| 1878 | 1 | public static function toBTC($satoshi) { |
|
| 1881 | |||
| 1882 | /** |
||
| 1883 | * convert a Satoshi value to a BTC value and return it as a string |
||
| 1884 | |||
| 1885 | * @param int $satoshi |
||
| 1886 | * @return string |
||
| 1887 | */ |
||
| 1888 | public static function toBTCString($satoshi) { |
||
| 1891 | |||
| 1892 | /** |
||
| 1893 | * convert a BTC value to a Satoshi value |
||
| 1894 | * |
||
| 1895 | * @param float $btc |
||
| 1896 | * @return string |
||
| 1897 | */ |
||
| 1898 | 1 | public static function toSatoshiString($btc) { |
|
| 1901 | |||
| 1902 | /** |
||
| 1903 | * convert a BTC value to a Satoshi value |
||
| 1904 | * |
||
| 1905 | * @param float $btc |
||
| 1906 | * @return string |
||
| 1907 | */ |
||
| 1908 | 1 | public static function toSatoshi($btc) { |
|
| 1911 | |||
| 1912 | /** |
||
| 1913 | * json_decode helper that throws exceptions when it fails to decode |
||
| 1914 | * |
||
| 1915 | * @param $json |
||
| 1916 | * @param bool $assoc |
||
| 1917 | * @return mixed |
||
| 1918 | * @throws \Exception |
||
| 1919 | */ |
||
| 1920 | 3 | public static function jsonDecode($json, $assoc = false) { |
|
| 1933 | |||
| 1934 | /** |
||
| 1935 | * sort public keys for multisig script |
||
| 1936 | * |
||
| 1937 | * @param PublicKeyInterface[] $pubKeys |
||
| 1938 | * @return PublicKeyInterface[] |
||
| 1939 | */ |
||
| 1940 | public static function sortMultisigKeys(array $pubKeys) { |
||
| 1950 | |||
| 1951 | /** |
||
| 1952 | * read and decode the json payload from a webhook's POST request. |
||
| 1953 | * |
||
| 1954 | * @param bool $returnObject flag to indicate if an object or associative array should be returned |
||
| 1955 | * @return mixed|null |
||
| 1956 | * @throws \Exception |
||
| 1957 | */ |
||
| 1958 | public static function getWebhookPayload($returnObject = false) { |
||
| 1966 | |||
| 1967 | public static function normalizeBIP32KeyArray($keys) { |
||
| 1972 | |||
| 1973 | /** |
||
| 1974 | * @param array|BIP32Key $key |
||
| 1975 | * @return BIP32Key |
||
| 1976 | * @throws \Exception |
||
| 1977 | */ |
||
| 1978 | public static function normalizeBIP32Key($key) { |
||
| 1996 | } |
||
| 1997 |
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: