Complex classes like BlocktrailSDK often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BlocktrailSDK, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class BlocktrailSDK implements BlocktrailSDKInterface { |
||
| 44 | /** |
||
| 45 | * @var Connection\RestClientInterface |
||
| 46 | */ |
||
| 47 | protected $blocktrailClient; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var Connection\RestClient |
||
| 51 | */ |
||
| 52 | protected $dataClient; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string currently only supporting; bitcoin |
||
| 56 | */ |
||
| 57 | protected $network; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var bool |
||
| 61 | */ |
||
| 62 | protected $testnet; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var ConverterInterface |
||
| 66 | */ |
||
| 67 | protected $converter; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param string $apiKey the API_KEY to use for authentication |
||
| 71 | * @param string $apiSecret the API_SECRET to use for authentication |
||
| 72 | * @param string $network the cryptocurrency 'network' to consume, eg BTC, LTC, etc |
||
| 73 | * @param bool $testnet testnet yes/no |
||
| 74 | * @param string $apiVersion the version of the API to consume |
||
| 75 | * @param null $apiEndpoint overwrite the endpoint used |
||
| 76 | * this will cause the $network, $testnet and $apiVersion to be ignored! |
||
| 77 | */ |
||
| 78 | 118 | public function __construct($apiKey, $apiSecret, $network = 'BTC', $testnet = false, $apiVersion = 'v1', $apiEndpoint = null) { |
|
| 79 | |||
| 80 | 118 | list ($apiNetwork, $testnet) = Util::parseApiNetwork($network, $testnet); |
|
| 81 | |||
| 82 | 118 | if (is_null($apiEndpoint)) { |
|
| 83 | 118 | $apiEndpoint = getenv('BLOCKTRAIL_SDK_API_ENDPOINT') ?: "https://api.blocktrail.com"; |
|
| 84 | 118 | $apiEndpoint = "{$apiEndpoint}/{$apiVersion}/{$apiNetwork}/"; |
|
| 85 | } |
||
| 86 | |||
| 87 | 118 | $btccomEndpoint = getenv('BLOCKTRAIL_SDK_BTCCOM_API_ENDPOINT') ?: "https://chain.api.btc.com"; |
|
| 88 | 118 | $btccomEndpoint = "{$btccomEndpoint}/v3/"; |
|
| 89 | |||
| 90 | // normalize network and set bitcoinlib to the right magic-bytes |
||
| 91 | 118 | list($this->network, $this->testnet, $regtest) = $this->normalizeNetwork($network, $testnet); |
|
| 92 | 118 | $this->setBitcoinLibMagicBytes($this->network, $this->testnet, $regtest); |
|
| 93 | |||
| 94 | 118 | if ($this->testnet && strpos($btccomEndpoint, "tchain") === false) { |
|
| 95 | 33 | $btccomEndpoint = \str_replace("chain", "tchain", $btccomEndpoint); |
|
| 96 | } |
||
| 97 | |||
| 98 | 118 | $this->blocktrailClient = new RestClient($apiEndpoint, $apiVersion, $apiKey, $apiSecret); |
|
| 99 | 118 | $this->dataClient = new RestClient($btccomEndpoint, $apiVersion, $apiKey, $apiSecret); |
|
| 100 | |||
| 101 | 118 | $this->converter = new BtccomConverter(); |
|
| 102 | 118 | } |
|
| 103 | |||
| 104 | /** |
||
| 105 | * normalize network string |
||
| 106 | * |
||
| 107 | * @param $network |
||
| 108 | * @param $testnet |
||
| 109 | * @return array |
||
| 110 | * @throws \Exception |
||
| 111 | */ |
||
| 112 | 118 | protected function normalizeNetwork($network, $testnet) { |
|
| 113 | // [name, testnet, network] |
||
| 114 | 118 | return Util::normalizeNetwork($network, $testnet); |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * set BitcoinLib to the correct magic-byte defaults for the selected network |
||
| 119 | * |
||
| 120 | * @param $network |
||
| 121 | * @param bool $testnet |
||
| 122 | * @param bool $regtest |
||
| 123 | */ |
||
| 124 | 118 | protected function setBitcoinLibMagicBytes($network, $testnet, $regtest) { |
|
| 125 | |||
| 126 | 118 | if ($network === "bitcoin") { |
|
| 127 | 118 | if ($regtest) { |
|
| 128 | $useNetwork = NetworkFactory::bitcoinRegtest(); |
||
| 129 | 118 | } else if ($testnet) { |
|
| 130 | 29 | $useNetwork = NetworkFactory::bitcoinTestnet(); |
|
| 131 | } else { |
||
| 132 | 118 | $useNetwork = NetworkFactory::bitcoin(); |
|
| 133 | } |
||
| 134 | 4 | } else if ($network === "bitcoincash") { |
|
| 135 | 4 | if ($regtest) { |
|
| 136 | $useNetwork = new BitcoinCashRegtest(); |
||
| 137 | 4 | } else if ($testnet) { |
|
| 138 | 4 | $useNetwork = new BitcoinCashTestnet(); |
|
| 139 | } else { |
||
| 140 | $useNetwork = new BitcoinCash(); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | 118 | Bitcoin::setNetwork($useNetwork); |
|
|
|
|||
| 145 | 118 | } |
|
| 146 | |||
| 147 | /** |
||
| 148 | * enable CURL debugging output |
||
| 149 | * |
||
| 150 | * @param bool $debug |
||
| 151 | * |
||
| 152 | * @codeCoverageIgnore |
||
| 153 | */ |
||
| 154 | public function setCurlDebugging($debug = true) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * enable verbose errors |
||
| 161 | * |
||
| 162 | * @param bool $verboseErrors |
||
| 163 | * |
||
| 164 | * @codeCoverageIgnore |
||
| 165 | */ |
||
| 166 | public function setVerboseErrors($verboseErrors = true) { |
||
| 167 | $this->blocktrailClient->setVerboseErrors($verboseErrors); |
||
| 168 | $this->dataClient->setVerboseErrors($verboseErrors); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * set cURL default option on Guzzle client |
||
| 173 | * @param string $key |
||
| 174 | * @param bool $value |
||
| 175 | * |
||
| 176 | * @codeCoverageIgnore |
||
| 177 | */ |
||
| 178 | public function setCurlDefaultOption($key, $value) { |
||
| 179 | $this->blocktrailClient->setCurlDefaultOption($key, $value); |
||
| 180 | $this->dataClient->setCurlDefaultOption($key, $value); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return RestClientInterface |
||
| 185 | */ |
||
| 186 | 2 | public function getRestClient() { |
|
| 187 | 2 | return $this->blocktrailClient; |
|
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @return RestClient |
||
| 192 | */ |
||
| 193 | public function getDataRestClient() { |
||
| 194 | return $this->dataClient; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param RestClientInterface $restClient |
||
| 199 | */ |
||
| 200 | public function setRestClient(RestClientInterface $restClient) { |
||
| 201 | $this->client = $restClient; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * get a single address |
||
| 206 | * @param string $address address hash |
||
| 207 | * @return array associative array containing the response |
||
| 208 | */ |
||
| 209 | 1 | public function address($address) { |
|
| 213 | |||
| 214 | /** |
||
| 215 | * get all transactions for an address (paginated) |
||
| 216 | * @param string $address address hash |
||
| 217 | * @param integer $page pagination: page number |
||
| 218 | * @param integer $limit pagination: records per page (max 500) |
||
| 219 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 220 | * @return array associative array containing the response |
||
| 221 | */ |
||
| 222 | 1 | public function addressTransactions($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 231 | |||
| 232 | /** |
||
| 233 | * get all unconfirmed transactions for an address (paginated) |
||
| 234 | * @param string $address address hash |
||
| 235 | * @param integer $page pagination: page number |
||
| 236 | * @param integer $limit pagination: records per page (max 500) |
||
| 237 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 238 | * @return array associative array containing the response |
||
| 239 | */ |
||
| 240 | public function addressUnconfirmedTransactions($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * get all unspent outputs for an address (paginated) |
||
| 252 | * @param string $address address hash |
||
| 253 | * @param integer $page pagination: page number |
||
| 254 | * @param integer $limit pagination: records per page (max 500) |
||
| 255 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 256 | * @return array associative array containing the response |
||
| 257 | */ |
||
| 258 | 1 | public function addressUnspentOutputs($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 267 | |||
| 268 | /** |
||
| 269 | * get all unspent outputs for a batch of addresses (paginated) |
||
| 270 | * |
||
| 271 | * @param string[] $addresses |
||
| 272 | * @param integer $page pagination: page number |
||
| 273 | * @param integer $limit pagination: records per page (max 500) |
||
| 274 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 275 | * @return array associative array containing the response |
||
| 276 | * @throws \Exception |
||
| 277 | */ |
||
| 278 | public function batchAddressUnspentOutputs($addresses, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * verify ownership of an address |
||
| 296 | * @param string $address address hash |
||
| 297 | * @param string $signature a signed message (the address hash) using the private key of the address |
||
| 298 | * @return array associative array containing the response |
||
| 299 | */ |
||
| 300 | 1 | public function verifyAddress($address, $signature) { |
|
| 307 | |||
| 308 | /** |
||
| 309 | * get all blocks (paginated) |
||
| 310 | * @param integer $page pagination: page number |
||
| 311 | * @param integer $limit pagination: records per page |
||
| 312 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 313 | * @return array associative array containing the response |
||
| 314 | */ |
||
| 315 | 1 | public function allBlocks($page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 324 | |||
| 325 | /** |
||
| 326 | * get the latest block |
||
| 327 | * @return array associative array containing the response |
||
| 328 | */ |
||
| 329 | 1 | public function blockLatest() { |
|
| 333 | |||
| 334 | /** |
||
| 335 | * get an individual block |
||
| 336 | * @param string|integer $block a block hash or a block height |
||
| 337 | * @return array associative array containing the response |
||
| 338 | */ |
||
| 339 | 1 | public function block($block) { |
|
| 343 | |||
| 344 | /** |
||
| 345 | * get all transaction in a block (paginated) |
||
| 346 | * @param string|integer $block a block hash or a block height |
||
| 347 | * @param integer $page pagination: page number |
||
| 348 | * @param integer $limit pagination: records per page |
||
| 349 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 350 | * @return array associative array containing the response |
||
| 351 | */ |
||
| 352 | public function blockTransactions($block, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * get a single transaction |
||
| 364 | * @param string $txhash transaction hash |
||
| 365 | * @return array associative array containing the response |
||
| 366 | */ |
||
| 367 | 4 | public function transaction($txhash) { |
|
| 377 | |||
| 378 | /** |
||
| 379 | * get a single transaction |
||
| 380 | * @param string[] $txhashes list of transaction hashes (up to 20) |
||
| 381 | * @return array[] array containing the response |
||
| 382 | */ |
||
| 383 | 1 | public function transactions($txhashes) { |
|
| 387 | |||
| 388 | /** |
||
| 389 | * get a paginated list of all webhooks associated with the api user |
||
| 390 | * @param integer $page pagination: page number |
||
| 391 | * @param integer $limit pagination: records per page |
||
| 392 | * @return array associative array containing the response |
||
| 393 | */ |
||
| 394 | 1 | public function allWebhooks($page = 1, $limit = 20) { |
|
| 402 | |||
| 403 | /** |
||
| 404 | * get an existing webhook by it's identifier |
||
| 405 | * @param string $identifier a unique identifier associated with the webhook |
||
| 406 | * @return array associative array containing the response |
||
| 407 | */ |
||
| 408 | 1 | public function getWebhook($identifier) { |
|
| 412 | |||
| 413 | /** |
||
| 414 | * create a new webhook |
||
| 415 | * @param string $url the url to receive the webhook events |
||
| 416 | * @param string $identifier a unique identifier to associate with this webhook |
||
| 417 | * @return array associative array containing the response |
||
| 418 | */ |
||
| 419 | 1 | public function setupWebhook($url, $identifier = null) { |
|
| 427 | |||
| 428 | /** |
||
| 429 | * update an existing webhook |
||
| 430 | * @param string $identifier the unique identifier of the webhook to update |
||
| 431 | * @param string $newUrl the new url to receive the webhook events |
||
| 432 | * @param string $newIdentifier a new unique identifier to associate with this webhook |
||
| 433 | * @return array associative array containing the response |
||
| 434 | */ |
||
| 435 | 1 | public function updateWebhook($identifier, $newUrl = null, $newIdentifier = null) { |
|
| 443 | |||
| 444 | /** |
||
| 445 | * deletes an existing webhook and any event subscriptions associated with it |
||
| 446 | * @param string $identifier the unique identifier of the webhook to delete |
||
| 447 | * @return boolean true on success |
||
| 448 | */ |
||
| 449 | 1 | public function deleteWebhook($identifier) { |
|
| 453 | |||
| 454 | /** |
||
| 455 | * get a paginated list of all the events a webhook is subscribed to |
||
| 456 | * @param string $identifier the unique identifier of the webhook |
||
| 457 | * @param integer $page pagination: page number |
||
| 458 | * @param integer $limit pagination: records per page |
||
| 459 | * @return array associative array containing the response |
||
| 460 | */ |
||
| 461 | 2 | public function getWebhookEvents($identifier, $page = 1, $limit = 20) { |
|
| 469 | |||
| 470 | /** |
||
| 471 | * subscribes a webhook to transaction events of one particular transaction |
||
| 472 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 473 | * @param string $transaction the transaction hash |
||
| 474 | * @param integer $confirmations the amount of confirmations to send. |
||
| 475 | * @return array associative array containing the response |
||
| 476 | */ |
||
| 477 | 1 | public function subscribeTransaction($identifier, $transaction, $confirmations = 6) { |
|
| 486 | |||
| 487 | /** |
||
| 488 | * subscribes a webhook to transaction events on a particular address |
||
| 489 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 490 | * @param string $address the address hash |
||
| 491 | * @param integer $confirmations the amount of confirmations to send. |
||
| 492 | * @return array associative array containing the response |
||
| 493 | */ |
||
| 494 | 1 | public function subscribeAddressTransactions($identifier, $address, $confirmations = 6) { |
|
| 503 | |||
| 504 | /** |
||
| 505 | * batch subscribes a webhook to multiple transaction events |
||
| 506 | * |
||
| 507 | * @param string $identifier the unique identifier of the webhook |
||
| 508 | * @param array $batchData A 2D array of event data: |
||
| 509 | * [address => $address, confirmations => $confirmations] |
||
| 510 | * where $address is the address to subscibe to |
||
| 511 | * and optionally $confirmations is the amount of confirmations |
||
| 512 | * @return boolean true on success |
||
| 513 | */ |
||
| 514 | 1 | public function batchSubscribeAddressTransactions($identifier, $batchData) { |
|
| 526 | |||
| 527 | /** |
||
| 528 | * subscribes a webhook to a new block event |
||
| 529 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 530 | * @return array associative array containing the response |
||
| 531 | */ |
||
| 532 | 1 | public function subscribeNewBlocks($identifier) { |
|
| 539 | |||
| 540 | /** |
||
| 541 | * removes an transaction event subscription from a webhook |
||
| 542 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 543 | * @param string $transaction the transaction hash of the event subscription |
||
| 544 | * @return boolean true on success |
||
| 545 | */ |
||
| 546 | 1 | public function unsubscribeTransaction($identifier, $transaction) { |
|
| 550 | |||
| 551 | /** |
||
| 552 | * removes an address transaction event subscription from a webhook |
||
| 553 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 554 | * @param string $address the address hash of the event subscription |
||
| 555 | * @return boolean true on success |
||
| 556 | */ |
||
| 557 | 1 | public function unsubscribeAddressTransactions($identifier, $address) { |
|
| 561 | |||
| 562 | /** |
||
| 563 | * removes a block event subscription from a webhook |
||
| 564 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 565 | * @return boolean true on success |
||
| 566 | */ |
||
| 567 | 1 | public function unsubscribeNewBlocks($identifier) { |
|
| 571 | |||
| 572 | /** |
||
| 573 | * create a new wallet |
||
| 574 | * - will generate a new primary seed (with password) and backup seed (without password) |
||
| 575 | * - send the primary seed (BIP39 'encrypted') and backup public key to the server |
||
| 576 | * - receive the blocktrail co-signing public key from the server |
||
| 577 | * |
||
| 578 | * Either takes one argument: |
||
| 579 | * @param array $options |
||
| 580 | * |
||
| 581 | * Or takes three arguments (old, deprecated syntax): |
||
| 582 | * (@nonPHP-doc) @param $identifier |
||
| 583 | * (@nonPHP-doc) @param $password |
||
| 584 | * (@nonPHP-doc) @param int $keyIndex override for the blocktrail cosigning key to use |
||
| 585 | * |
||
| 586 | * @return array[WalletInterface, array] list($wallet, $backupInfo) |
||
| 587 | * @throws \Exception |
||
| 588 | */ |
||
| 589 | 7 | public function createNewWallet($options) { |
|
| 633 | |||
| 634 | 1 | protected function createNewWalletV1($options) { |
|
| 754 | |||
| 755 | 5 | public static function randomBits($bits) { |
|
| 758 | |||
| 759 | 5 | public static function randomBytes($bytes) { |
|
| 762 | |||
| 763 | 2 | protected function createNewWalletV2($options) { |
|
| 886 | |||
| 887 | 4 | protected function createNewWalletV3($options) { |
|
| 1031 | |||
| 1032 | /** |
||
| 1033 | * @param array $bip32Key |
||
| 1034 | * @throws BlocktrailSDKException |
||
| 1035 | */ |
||
| 1036 | 10 | private function verifyPublicBIP32Key(array $bip32Key) { |
|
| 1046 | |||
| 1047 | /** |
||
| 1048 | * @param array $walletData |
||
| 1049 | * @throws BlocktrailSDKException |
||
| 1050 | */ |
||
| 1051 | 10 | private function verifyPublicOnly(array $walletData) { |
|
| 1055 | |||
| 1056 | /** |
||
| 1057 | * create wallet using the API |
||
| 1058 | * |
||
| 1059 | * @param string $identifier the wallet identifier to create |
||
| 1060 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1061 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1062 | * @param string $primaryMnemonic mnemonic to store |
||
| 1063 | * @param string $checksum checksum to store |
||
| 1064 | * @param int $keyIndex account that we expect to use |
||
| 1065 | * @param bool $segwit opt in to segwit |
||
| 1066 | * @return mixed |
||
| 1067 | */ |
||
| 1068 | 1 | public function storeNewWalletV1($identifier, $primaryPublicKey, $backupPublicKey, $primaryMnemonic, $checksum, $keyIndex, $segwit = false) { |
|
| 1082 | |||
| 1083 | /** |
||
| 1084 | * create wallet using the API |
||
| 1085 | * |
||
| 1086 | * @param string $identifier the wallet identifier to create |
||
| 1087 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1088 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1089 | * @param $encryptedPrimarySeed |
||
| 1090 | * @param $encryptedSecret |
||
| 1091 | * @param $recoverySecret |
||
| 1092 | * @param string $checksum checksum to store |
||
| 1093 | * @param int $keyIndex account that we expect to use |
||
| 1094 | * @param bool $segwit opt in to segwit |
||
| 1095 | * @return mixed |
||
| 1096 | * @throws \Exception |
||
| 1097 | */ |
||
| 1098 | 5 | public function storeNewWalletV2($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false) { |
|
| 1115 | |||
| 1116 | /** |
||
| 1117 | * create wallet using the API |
||
| 1118 | * |
||
| 1119 | * @param string $identifier the wallet identifier to create |
||
| 1120 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1121 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1122 | * @param $encryptedPrimarySeed |
||
| 1123 | * @param $encryptedSecret |
||
| 1124 | * @param $recoverySecret |
||
| 1125 | * @param string $checksum checksum to store |
||
| 1126 | * @param int $keyIndex account that we expect to use |
||
| 1127 | * @param bool $segwit opt in to segwit |
||
| 1128 | * @return mixed |
||
| 1129 | * @throws \Exception |
||
| 1130 | */ |
||
| 1131 | 4 | public function storeNewWalletV3($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false) { |
|
| 1150 | |||
| 1151 | /** |
||
| 1152 | * upgrade wallet to use a new account number |
||
| 1153 | * the account number specifies which blocktrail cosigning key is used |
||
| 1154 | * |
||
| 1155 | * @param string $identifier the wallet identifier to be upgraded |
||
| 1156 | * @param int $keyIndex the new account to use |
||
| 1157 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1158 | * @return mixed |
||
| 1159 | */ |
||
| 1160 | 5 | public function upgradeKeyIndex($identifier, $keyIndex, $primaryPublicKey) { |
|
| 1169 | |||
| 1170 | /** |
||
| 1171 | * @param array $options |
||
| 1172 | * @return AddressReaderBase |
||
| 1173 | */ |
||
| 1174 | 23 | private function makeAddressReader(array $options) { |
|
| 1185 | |||
| 1186 | /** |
||
| 1187 | * initialize a previously created wallet |
||
| 1188 | * |
||
| 1189 | * Takes an options object, or accepts identifier/password for backwards compatiblity. |
||
| 1190 | * |
||
| 1191 | * Some of the options: |
||
| 1192 | * - "readonly/readOnly/read-only" can be to a boolean value, |
||
| 1193 | * so the wallet is loaded in read-only mode (no private key) |
||
| 1194 | * - "check_backup_key" can be set to your own backup key: |
||
| 1195 | * Format: ["M', "xpub..."] |
||
| 1196 | * Setting this will allow the SDK to check the server hasn't |
||
| 1197 | * a different key (one it happens to control) |
||
| 1198 | |||
| 1199 | * Either takes one argument: |
||
| 1200 | * @param array $options |
||
| 1201 | * |
||
| 1202 | * Or takes two arguments (old, deprecated syntax): |
||
| 1203 | * (@nonPHP-doc) @param string $identifier the wallet identifier to be initialized |
||
| 1204 | * (@nonPHP-doc) @param string $password the password to decrypt the mnemonic with |
||
| 1205 | * |
||
| 1206 | * @return WalletInterface |
||
| 1207 | * @throws \Exception |
||
| 1208 | */ |
||
| 1209 | 23 | public function initWallet($options) { |
|
| 1321 | |||
| 1322 | /** |
||
| 1323 | * get the wallet data from the server |
||
| 1324 | * |
||
| 1325 | * @param string $identifier the identifier of the wallet |
||
| 1326 | * @return mixed |
||
| 1327 | */ |
||
| 1328 | 23 | public function getWallet($identifier) { |
|
| 1332 | |||
| 1333 | /** |
||
| 1334 | * update the wallet data on the server |
||
| 1335 | * |
||
| 1336 | * @param string $identifier |
||
| 1337 | * @param $data |
||
| 1338 | * @return mixed |
||
| 1339 | */ |
||
| 1340 | 3 | public function updateWallet($identifier, $data) { |
|
| 1344 | |||
| 1345 | /** |
||
| 1346 | * delete a wallet from the server |
||
| 1347 | * the checksum address and a signature to verify you ownership of the key of that checksum address |
||
| 1348 | * is required to be able to delete a wallet |
||
| 1349 | * |
||
| 1350 | * @param string $identifier the identifier of the wallet |
||
| 1351 | * @param string $checksumAddress the address for your master private key (and the checksum used when creating the wallet) |
||
| 1352 | * @param string $signature a signature of the checksum address as message signed by the private key matching that address |
||
| 1353 | * @param bool $force ignore warnings (such as a non-zero balance) |
||
| 1354 | * @return mixed |
||
| 1355 | */ |
||
| 1356 | 10 | public function deleteWallet($identifier, $checksumAddress, $signature, $force = false) { |
|
| 1363 | |||
| 1364 | /** |
||
| 1365 | * create new backup key; |
||
| 1366 | * 1) a BIP39 mnemonic |
||
| 1367 | * 2) a seed from that mnemonic with a blank password |
||
| 1368 | * 3) a private key from that seed |
||
| 1369 | * |
||
| 1370 | * @return array [mnemonic, seed, key] |
||
| 1371 | */ |
||
| 1372 | 1 | protected function newBackupSeed() { |
|
| 1377 | |||
| 1378 | /** |
||
| 1379 | * create new primary key; |
||
| 1380 | * 1) a BIP39 mnemonic |
||
| 1381 | * 2) a seed from that mnemonic with the password |
||
| 1382 | * 3) a private key from that seed |
||
| 1383 | * |
||
| 1384 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1385 | * @return array [mnemonic, seed, key] |
||
| 1386 | * @TODO: require a strong password? |
||
| 1387 | */ |
||
| 1388 | 1 | protected function newPrimarySeed($passphrase) { |
|
| 1393 | |||
| 1394 | /** |
||
| 1395 | * create a new key; |
||
| 1396 | * 1) a BIP39 mnemonic |
||
| 1397 | * 2) a seed from that mnemonic with the password |
||
| 1398 | * 3) a private key from that seed |
||
| 1399 | * |
||
| 1400 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1401 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1402 | * @return array |
||
| 1403 | */ |
||
| 1404 | 1 | protected function generateNewSeed($passphrase = "", $forceEntropy = null) { |
|
| 1421 | |||
| 1422 | /** |
||
| 1423 | * generate a new mnemonic from some random entropy (512 bit) |
||
| 1424 | * |
||
| 1425 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1426 | * @return string |
||
| 1427 | * @throws \Exception |
||
| 1428 | */ |
||
| 1429 | 1 | protected function generateNewMnemonic($forceEntropy = null) { |
|
| 1439 | |||
| 1440 | /** |
||
| 1441 | * get the balance for the wallet |
||
| 1442 | * |
||
| 1443 | * @param string $identifier the identifier of the wallet |
||
| 1444 | * @return array |
||
| 1445 | */ |
||
| 1446 | 9 | public function getWalletBalance($identifier) { |
|
| 1450 | |||
| 1451 | /** |
||
| 1452 | * get a new derivation number for specified parent path |
||
| 1453 | * 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 |
||
| 1454 | * |
||
| 1455 | * returns the path |
||
| 1456 | * |
||
| 1457 | * @param string $identifier the identifier of the wallet |
||
| 1458 | * @param string $path the parent path for which to get a new derivation |
||
| 1459 | * @return string |
||
| 1460 | */ |
||
| 1461 | 1 | public function getNewDerivation($identifier, $path) { |
|
| 1465 | |||
| 1466 | /** |
||
| 1467 | * get a new derivation number for specified parent path |
||
| 1468 | * 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 |
||
| 1469 | * |
||
| 1470 | * @param string $identifier the identifier of the wallet |
||
| 1471 | * @param string $path the parent path for which to get a new derivation |
||
| 1472 | * @return mixed |
||
| 1473 | */ |
||
| 1474 | 16 | public function _getNewDerivation($identifier, $path) { |
|
| 1478 | |||
| 1479 | /** |
||
| 1480 | * get the path (and redeemScript) to specified address |
||
| 1481 | * |
||
| 1482 | * @param string $identifier |
||
| 1483 | * @param string $address |
||
| 1484 | * @return array |
||
| 1485 | * @throws \Exception |
||
| 1486 | */ |
||
| 1487 | 1 | public function getPathForAddress($identifier, $address) { |
|
| 1491 | |||
| 1492 | /** |
||
| 1493 | * send the transaction using the API |
||
| 1494 | * |
||
| 1495 | * @param string $identifier the identifier of the wallet |
||
| 1496 | * @param string|array $rawTransaction raw hex of the transaction (should be partially signed) |
||
| 1497 | * @param array $paths list of the paths that were used for the UTXO |
||
| 1498 | * @param bool $checkFee let the server verify the fee after signing |
||
| 1499 | * @param null $twoFactorToken |
||
| 1500 | * @return string the complete raw transaction |
||
| 1501 | * @throws \Exception |
||
| 1502 | */ |
||
| 1503 | 4 | public function sendTransaction($identifier, $rawTransaction, $paths, $checkFee = false, $twoFactorToken = null) { |
|
| 1534 | |||
| 1535 | /** |
||
| 1536 | * use the API to get the best inputs to use based on the outputs |
||
| 1537 | * |
||
| 1538 | * the return array has the following format: |
||
| 1539 | * [ |
||
| 1540 | * "utxos" => [ |
||
| 1541 | * [ |
||
| 1542 | * "hash" => "<txHash>", |
||
| 1543 | * "idx" => "<index of the output of that <txHash>", |
||
| 1544 | * "scriptpubkey_hex" => "<scriptPubKey-hex>", |
||
| 1545 | * "value" => 32746327, |
||
| 1546 | * "address" => "1address", |
||
| 1547 | * "path" => "m/44'/1'/0'/0/13", |
||
| 1548 | * "redeem_script" => "<redeemScript-hex>", |
||
| 1549 | * ], |
||
| 1550 | * ], |
||
| 1551 | * "fee" => 10000, |
||
| 1552 | * "change"=> 1010109201, |
||
| 1553 | * ] |
||
| 1554 | * |
||
| 1555 | * @param string $identifier the identifier of the wallet |
||
| 1556 | * @param array $outputs the outputs you want to create - array[address => satoshi-value] |
||
| 1557 | * @param bool $lockUTXO when TRUE the UTXOs selected will be locked for a few seconds |
||
| 1558 | * so you have some time to spend them without race-conditions |
||
| 1559 | * @param bool $allowZeroConf |
||
| 1560 | * @param string $feeStrategy |
||
| 1561 | * @param null|int $forceFee |
||
| 1562 | * @return array |
||
| 1563 | * @throws \Exception |
||
| 1564 | */ |
||
| 1565 | 12 | public function coinSelection($identifier, $outputs, $lockUTXO = false, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null) { |
|
| 1585 | |||
| 1586 | /** |
||
| 1587 | * |
||
| 1588 | * @param string $identifier the identifier of the wallet |
||
| 1589 | * @param bool $allowZeroConf |
||
| 1590 | * @param string $feeStrategy |
||
| 1591 | * @param null|int $forceFee |
||
| 1592 | * @param int $outputCnt |
||
| 1593 | * @return array |
||
| 1594 | * @throws \Exception |
||
| 1595 | */ |
||
| 1596 | public function walletMaxSpendable($identifier, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null, $outputCnt = 1) { |
||
| 1615 | |||
| 1616 | /** |
||
| 1617 | * @return array ['optimal_fee' => 10000, 'low_priority_fee' => 5000] |
||
| 1618 | */ |
||
| 1619 | 4 | public function feePerKB() { |
|
| 1623 | |||
| 1624 | /** |
||
| 1625 | * get the current price index |
||
| 1626 | * |
||
| 1627 | * @return array eg; ['USD' => 287.30] |
||
| 1628 | */ |
||
| 1629 | 1 | public function price() { |
|
| 1633 | |||
| 1634 | /** |
||
| 1635 | * setup webhook for wallet |
||
| 1636 | * |
||
| 1637 | * @param string $identifier the wallet identifier for which to create the webhook |
||
| 1638 | * @param string $webhookIdentifier the webhook identifier to use |
||
| 1639 | * @param string $url the url to receive the webhook events |
||
| 1640 | * @return array |
||
| 1641 | */ |
||
| 1642 | 1 | public function setupWalletWebhook($identifier, $webhookIdentifier, $url) { |
|
| 1646 | |||
| 1647 | /** |
||
| 1648 | * delete webhook for wallet |
||
| 1649 | * |
||
| 1650 | * @param string $identifier the wallet identifier for which to delete the webhook |
||
| 1651 | * @param string $webhookIdentifier the webhook identifier to delete |
||
| 1652 | * @return array |
||
| 1653 | */ |
||
| 1654 | 1 | public function deleteWalletWebhook($identifier, $webhookIdentifier) { |
|
| 1658 | |||
| 1659 | /** |
||
| 1660 | * lock a specific unspent output |
||
| 1661 | * |
||
| 1662 | * @param $identifier |
||
| 1663 | * @param $txHash |
||
| 1664 | * @param $txIdx |
||
| 1665 | * @param int $ttl |
||
| 1666 | * @return bool |
||
| 1667 | */ |
||
| 1668 | public function lockWalletUTXO($identifier, $txHash, $txIdx, $ttl = 3) { |
||
| 1672 | |||
| 1673 | /** |
||
| 1674 | * unlock a specific unspent output |
||
| 1675 | * |
||
| 1676 | * @param $identifier |
||
| 1677 | * @param $txHash |
||
| 1678 | * @param $txIdx |
||
| 1679 | * @return bool |
||
| 1680 | */ |
||
| 1681 | public function unlockWalletUTXO($identifier, $txHash, $txIdx) { |
||
| 1685 | |||
| 1686 | /** |
||
| 1687 | * get all transactions for wallet (paginated) |
||
| 1688 | * |
||
| 1689 | * @param string $identifier the wallet identifier for which to get transactions |
||
| 1690 | * @param integer $page pagination: page number |
||
| 1691 | * @param integer $limit pagination: records per page (max 500) |
||
| 1692 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1693 | * @return array associative array containing the response |
||
| 1694 | */ |
||
| 1695 | 1 | public function walletTransactions($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 1704 | |||
| 1705 | /** |
||
| 1706 | * get all addresses for wallet (paginated) |
||
| 1707 | * |
||
| 1708 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1709 | * @param integer $page pagination: page number |
||
| 1710 | * @param integer $limit pagination: records per page (max 500) |
||
| 1711 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1712 | * @return array associative array containing the response |
||
| 1713 | */ |
||
| 1714 | 1 | public function walletAddresses($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 1723 | |||
| 1724 | /** |
||
| 1725 | * get all UTXOs for wallet (paginated) |
||
| 1726 | * |
||
| 1727 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 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 | * @param boolean $zeroconf include zero confirmation transactions |
||
| 1732 | * @return array associative array containing the response |
||
| 1733 | */ |
||
| 1734 | 1 | public function walletUTXOs($identifier, $page = 1, $limit = 20, $sortDir = 'asc', $zeroconf = true) { |
|
| 1744 | |||
| 1745 | /** |
||
| 1746 | * get a paginated list of all wallets associated with the api user |
||
| 1747 | * |
||
| 1748 | * @param integer $page pagination: page number |
||
| 1749 | * @param integer $limit pagination: records per page |
||
| 1750 | * @return array associative array containing the response |
||
| 1751 | */ |
||
| 1752 | 2 | public function allWallets($page = 1, $limit = 20) { |
|
| 1760 | |||
| 1761 | /** |
||
| 1762 | * send raw transaction |
||
| 1763 | * |
||
| 1764 | * @param $txHex |
||
| 1765 | * @return bool |
||
| 1766 | */ |
||
| 1767 | public function sendRawTransaction($txHex) { |
||
| 1771 | |||
| 1772 | /** |
||
| 1773 | * testnet only ;-) |
||
| 1774 | * |
||
| 1775 | * @param $address |
||
| 1776 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1777 | * @return mixed |
||
| 1778 | * @throws \Exception |
||
| 1779 | */ |
||
| 1780 | public function faucetWithdrawal($address, $amount = 10000) { |
||
| 1787 | |||
| 1788 | /** |
||
| 1789 | * Exists for BC. Remove at major bump. |
||
| 1790 | * |
||
| 1791 | * @see faucetWithdrawal |
||
| 1792 | * @deprecated |
||
| 1793 | * @param $address |
||
| 1794 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1795 | * @return mixed |
||
| 1796 | * @throws \Exception |
||
| 1797 | */ |
||
| 1798 | public function faucetWithdrawl($address, $amount = 10000) { |
||
| 1801 | |||
| 1802 | /** |
||
| 1803 | * verify a message signed bitcoin-core style |
||
| 1804 | * |
||
| 1805 | * @param string $message |
||
| 1806 | * @param string $address |
||
| 1807 | * @param string $signature |
||
| 1808 | * @return boolean |
||
| 1809 | */ |
||
| 1810 | 2 | public function verifyMessage($message, $address, $signature) { |
|
| 1824 | |||
| 1825 | /** |
||
| 1826 | * Take a base58 or cashaddress, and return only |
||
| 1827 | * the cash address. |
||
| 1828 | * This function only works on bitcoin cash. |
||
| 1829 | * @param string $input |
||
| 1830 | * @return string |
||
| 1831 | * @throws BlocktrailSDKException |
||
| 1832 | */ |
||
| 1833 | 1 | public function getLegacyBitcoinCashAddress($input) { |
|
| 1850 | |||
| 1851 | /** |
||
| 1852 | * convert a Satoshi value to a BTC value |
||
| 1853 | * |
||
| 1854 | * @param int $satoshi |
||
| 1855 | * @return float |
||
| 1856 | */ |
||
| 1857 | 1 | public static function toBTC($satoshi) { |
|
| 1860 | |||
| 1861 | /** |
||
| 1862 | * convert a Satoshi value to a BTC value and return it as a string |
||
| 1863 | |||
| 1864 | * @param int $satoshi |
||
| 1865 | * @return string |
||
| 1866 | */ |
||
| 1867 | public static function toBTCString($satoshi) { |
||
| 1870 | |||
| 1871 | /** |
||
| 1872 | * convert a BTC value to a Satoshi value |
||
| 1873 | * |
||
| 1874 | * @param float $btc |
||
| 1875 | * @return string |
||
| 1876 | */ |
||
| 1877 | 13 | public static function toSatoshiString($btc) { |
|
| 1880 | |||
| 1881 | /** |
||
| 1882 | * convert a BTC value to a Satoshi value |
||
| 1883 | * |
||
| 1884 | * @param float $btc |
||
| 1885 | * @return string |
||
| 1886 | */ |
||
| 1887 | 13 | public static function toSatoshi($btc) { |
|
| 1890 | |||
| 1891 | /** |
||
| 1892 | * json_decode helper that throws exceptions when it fails to decode |
||
| 1893 | * |
||
| 1894 | * @param $json |
||
| 1895 | * @param bool $assoc |
||
| 1896 | * @return mixed |
||
| 1897 | * @throws \Exception |
||
| 1898 | */ |
||
| 1899 | 31 | public static function jsonDecode($json, $assoc = false) { |
|
| 1912 | |||
| 1913 | /** |
||
| 1914 | * sort public keys for multisig script |
||
| 1915 | * |
||
| 1916 | * @param PublicKeyInterface[] $pubKeys |
||
| 1917 | * @return PublicKeyInterface[] |
||
| 1918 | */ |
||
| 1919 | 18 | public static function sortMultisigKeys(array $pubKeys) { |
|
| 1929 | |||
| 1930 | /** |
||
| 1931 | * read and decode the json payload from a webhook's POST request. |
||
| 1932 | * |
||
| 1933 | * @param bool $returnObject flag to indicate if an object or associative array should be returned |
||
| 1934 | * @return mixed|null |
||
| 1935 | * @throws \Exception |
||
| 1936 | */ |
||
| 1937 | public static function getWebhookPayload($returnObject = false) { |
||
| 1945 | |||
| 1946 | public static function normalizeBIP32KeyArray($keys) { |
||
| 1951 | |||
| 1952 | /** |
||
| 1953 | * @param array|BIP32Key $key |
||
| 1954 | * @return BIP32Key |
||
| 1955 | * @throws \Exception |
||
| 1956 | */ |
||
| 1957 | 26 | public static function normalizeBIP32Key($key) { |
|
| 1975 | } |
||
| 1976 |
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: