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://wallet-api.btc.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 | echo $apiEndpoint.PHP_EOL; |
|
| 102 | 119 | $this->blocktrailClient = new RestClient($apiEndpoint, $apiVersion, $apiKey, $apiSecret); |
|
| 103 | $this->blocktrailClient->setVerboseErrors(true); |
||
| 104 | 119 | $this->blocktrailClient->setCurlDebugging(true); |
|
| 105 | 119 | ||
| 106 | $this->dataClient = new RestClient($btccomEndpoint, $apiVersion, $apiKey, $apiSecret); |
||
| 107 | $this->converter = new BtccomConverter(); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * normalize network string |
||
| 112 | * |
||
| 113 | * @param $network |
||
| 114 | * @param $testnet |
||
| 115 | 119 | * @return array |
|
| 116 | * @throws \Exception |
||
| 117 | 119 | */ |
|
| 118 | protected function normalizeNetwork($network, $testnet) { |
||
| 119 | // [name, testnet, network] |
||
| 120 | return Util::normalizeNetwork($network, $testnet); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * set BitcoinLib to the correct magic-byte defaults for the selected network |
||
| 125 | * |
||
| 126 | * @param $network |
||
| 127 | 119 | * @param bool $testnet |
|
| 128 | * @param bool $regtest |
||
| 129 | 119 | */ |
|
| 130 | 119 | protected function setBitcoinLibMagicBytes($network, $testnet, $regtest) { |
|
| 131 | |||
| 132 | 119 | if ($network === "bitcoin") { |
|
| 133 | 29 | if ($regtest) { |
|
| 134 | $useNetwork = NetworkFactory::bitcoinRegtest(); |
||
| 135 | 119 | } else if ($testnet) { |
|
| 136 | $useNetwork = NetworkFactory::bitcoinTestnet(); |
||
| 137 | 4 | } else { |
|
| 138 | 4 | $useNetwork = NetworkFactory::bitcoin(); |
|
| 139 | } |
||
| 140 | 4 | } else if ($network === "bitcoincash") { |
|
| 141 | 4 | if ($regtest) { |
|
| 142 | $useNetwork = new BitcoinCashRegtest(); |
||
| 143 | } else if ($testnet) { |
||
| 144 | $useNetwork = new BitcoinCashTestnet(); |
||
| 145 | } else { |
||
| 146 | $useNetwork = new BitcoinCash(); |
||
| 147 | 119 | } |
|
| 148 | 119 | } |
|
| 149 | |||
| 150 | Bitcoin::setNetwork($useNetwork); |
||
|
|
|||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * enable CURL debugging output |
||
| 155 | * |
||
| 156 | * @param bool $debug |
||
| 157 | * |
||
| 158 | * @codeCoverageIgnore |
||
| 159 | */ |
||
| 160 | public function setCurlDebugging($debug = true) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * enable verbose errors |
||
| 167 | * |
||
| 168 | * @param bool $verboseErrors |
||
| 169 | * |
||
| 170 | * @codeCoverageIgnore |
||
| 171 | */ |
||
| 172 | public function setVerboseErrors($verboseErrors = true) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * set cURL default option on Guzzle client |
||
| 179 | * @param string $key |
||
| 180 | * @param bool $value |
||
| 181 | * |
||
| 182 | * @codeCoverageIgnore |
||
| 183 | */ |
||
| 184 | public function setCurlDefaultOption($key, $value) { |
||
| 188 | |||
| 189 | 2 | /** |
|
| 190 | 2 | * @return RestClientInterface |
|
| 191 | */ |
||
| 192 | public function getRestClient() { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return RestClient |
||
| 198 | */ |
||
| 199 | public function getDataRestClient() { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param RestClientInterface $restClient |
||
| 205 | */ |
||
| 206 | public function setRestClient(RestClientInterface $restClient) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * get a single address |
||
| 212 | 1 | * @param string $address address hash |
|
| 213 | 1 | * @return array associative array containing the response |
|
| 214 | 1 | */ |
|
| 215 | public function address($address) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * get all transactions for an address (paginated) |
||
| 222 | * @param string $address address hash |
||
| 223 | * @param integer $page pagination: page number |
||
| 224 | * @param integer $limit pagination: records per page (max 500) |
||
| 225 | 1 | * @param string $sortDir pagination: sort direction (asc|desc) |
|
| 226 | * @return array associative array containing the response |
||
| 227 | 1 | */ |
|
| 228 | 1 | public function addressTransactions($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 237 | |||
| 238 | /** |
||
| 239 | * get all unconfirmed transactions for an address (paginated) |
||
| 240 | * @param string $address address hash |
||
| 241 | * @param integer $page pagination: page number |
||
| 242 | * @param integer $limit pagination: records per page (max 500) |
||
| 243 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 244 | * @return array associative array containing the response |
||
| 245 | */ |
||
| 246 | public function addressUnconfirmedTransactions($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * get all unspent outputs for an address (paginated) |
||
| 258 | * @param string $address address hash |
||
| 259 | * @param integer $page pagination: page number |
||
| 260 | * @param integer $limit pagination: records per page (max 500) |
||
| 261 | 1 | * @param string $sortDir pagination: sort direction (asc|desc) |
|
| 262 | * @return array associative array containing the response |
||
| 263 | 1 | */ |
|
| 264 | 1 | public function addressUnspentOutputs($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 273 | |||
| 274 | /** |
||
| 275 | * get all unspent outputs for a batch of addresses (paginated) |
||
| 276 | * |
||
| 277 | * @param string[] $addresses |
||
| 278 | * @param integer $page pagination: page number |
||
| 279 | * @param integer $limit pagination: records per page (max 500) |
||
| 280 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 281 | * @return array associative array containing the response |
||
| 282 | * @throws \Exception |
||
| 283 | */ |
||
| 284 | public function batchAddressUnspentOutputs($addresses, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * verify ownership of an address |
||
| 311 | * @param string $address address hash |
||
| 312 | 1 | * @param string $signature a signed message (the address hash) using the private key of the address |
|
| 313 | 1 | * @return array associative array containing the response |
|
| 314 | 1 | */ |
|
| 315 | public function verifyAddress($address, $signature) { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * get all blocks (paginated) |
||
| 325 | * @param integer $page pagination: page number |
||
| 326 | * @param integer $limit pagination: records per page |
||
| 327 | 1 | * @param string $sortDir pagination: sort direction (asc|desc) |
|
| 328 | * @return array associative array containing the response |
||
| 329 | 1 | */ |
|
| 330 | 1 | public function allBlocks($page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 339 | |||
| 340 | /** |
||
| 341 | 1 | * get the latest block |
|
| 342 | 1 | * @return array associative array containing the response |
|
| 343 | 1 | */ |
|
| 344 | public function blockLatest() { |
||
| 348 | |||
| 349 | /** |
||
| 350 | 1 | * get the wallet API's latest block ['hash' => x, 'height' => y] |
|
| 351 | 1 | * @return array associative array containing the response |
|
| 352 | */ |
||
| 353 | public function getWalletBlockLatest() { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * get an individual block |
||
| 360 | 1 | * @param string|integer $block a block hash or a block height |
|
| 361 | 1 | * @return array associative array containing the response |
|
| 362 | 1 | */ |
|
| 363 | public function block($block) { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * get all transaction in a block (paginated) |
||
| 370 | * @param string|integer $block a block hash or a block height |
||
| 371 | * @param integer $page pagination: page number |
||
| 372 | * @param integer $limit pagination: records per page |
||
| 373 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 374 | * @return array associative array containing the response |
||
| 375 | */ |
||
| 376 | public function blockTransactions($block, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * get a single transaction |
||
| 388 | 1 | * @param string $txhash transaction hash |
|
| 389 | 1 | * @return array associative array containing the response |
|
| 390 | 1 | */ |
|
| 391 | public function transaction($txhash) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * get a single transaction |
||
| 404 | 1 | * @param string[] $txhashes list of transaction hashes (up to 20) |
|
| 405 | 1 | * @return array[] array containing the response |
|
| 406 | 1 | */ |
|
| 407 | public function transactions($txhashes) { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * get a paginated list of all webhooks associated with the api user |
||
| 414 | * @param integer $page pagination: page number |
||
| 415 | * @param integer $limit pagination: records per page |
||
| 416 | * @return array associative array containing the response |
||
| 417 | */ |
||
| 418 | public function allWebhooks($page = 1, $limit = 20) { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * get an existing webhook by it's identifier |
||
| 429 | * @param string $identifier a unique identifier associated with the webhook |
||
| 430 | * @return array associative array containing the response |
||
| 431 | */ |
||
| 432 | public function getWebhook($identifier) { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * create a new webhook |
||
| 439 | * @param string $url the url to receive the webhook events |
||
| 440 | 1 | * @param string $identifier a unique identifier to associate with this webhook |
|
| 441 | * @return array associative array containing the response |
||
| 442 | 1 | */ |
|
| 443 | 1 | public function setupWebhook($url, $identifier = null) { |
|
| 451 | |||
| 452 | /** |
||
| 453 | * update an existing webhook |
||
| 454 | * @param string $identifier the unique identifier of the webhook to update |
||
| 455 | * @param string $newUrl the new url to receive the webhook events |
||
| 456 | * @param string $newIdentifier a new unique identifier to associate with this webhook |
||
| 457 | * @return array associative array containing the response |
||
| 458 | */ |
||
| 459 | public function updateWebhook($identifier, $newUrl = null, $newIdentifier = null) { |
||
| 467 | |||
| 468 | /** |
||
| 469 | * deletes an existing webhook and any event subscriptions associated with it |
||
| 470 | * @param string $identifier the unique identifier of the webhook to delete |
||
| 471 | * @return boolean true on success |
||
| 472 | */ |
||
| 473 | public function deleteWebhook($identifier) { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * get a paginated list of all the events a webhook is subscribed to |
||
| 480 | * @param string $identifier the unique identifier of the webhook |
||
| 481 | * @param integer $page pagination: page number |
||
| 482 | * @param integer $limit pagination: records per page |
||
| 483 | * @return array associative array containing the response |
||
| 484 | */ |
||
| 485 | public function getWebhookEvents($identifier, $page = 1, $limit = 20) { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * subscribes a webhook to transaction events of one particular transaction |
||
| 496 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 497 | * @param string $transaction the transaction hash |
||
| 498 | * @param integer $confirmations the amount of confirmations to send. |
||
| 499 | * @return array associative array containing the response |
||
| 500 | */ |
||
| 501 | public function subscribeTransaction($identifier, $transaction, $confirmations = 6) { |
||
| 510 | |||
| 511 | /** |
||
| 512 | * subscribes a webhook to transaction events on a particular address |
||
| 513 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 514 | * @param string $address the address hash |
||
| 515 | * @param integer $confirmations the amount of confirmations to send. |
||
| 516 | * @return array associative array containing the response |
||
| 517 | */ |
||
| 518 | public function subscribeAddressTransactions($identifier, $address, $confirmations = 6) { |
||
| 527 | |||
| 528 | /** |
||
| 529 | * batch subscribes a webhook to multiple transaction events |
||
| 530 | * |
||
| 531 | * @param string $identifier the unique identifier of the webhook |
||
| 532 | * @param array $batchData A 2D array of event data: |
||
| 533 | * [address => $address, confirmations => $confirmations] |
||
| 534 | * where $address is the address to subscibe to |
||
| 535 | * and optionally $confirmations is the amount of confirmations |
||
| 536 | * @return boolean true on success |
||
| 537 | */ |
||
| 538 | public function batchSubscribeAddressTransactions($identifier, $batchData) { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * subscribes a webhook to a new block event |
||
| 553 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 554 | * @return array associative array containing the response |
||
| 555 | */ |
||
| 556 | public function subscribeNewBlocks($identifier) { |
||
| 563 | |||
| 564 | /** |
||
| 565 | * removes an transaction event subscription from a webhook |
||
| 566 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 567 | * @param string $transaction the transaction hash of the event subscription |
||
| 568 | * @return boolean true on success |
||
| 569 | */ |
||
| 570 | public function unsubscribeTransaction($identifier, $transaction) { |
||
| 574 | |||
| 575 | /** |
||
| 576 | * removes an address transaction event subscription from a webhook |
||
| 577 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 578 | * @param string $address the address hash of the event subscription |
||
| 579 | * @return boolean true on success |
||
| 580 | */ |
||
| 581 | public function unsubscribeAddressTransactions($identifier, $address) { |
||
| 585 | |||
| 586 | /** |
||
| 587 | * removes a block event subscription from a webhook |
||
| 588 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 589 | * @return boolean true on success |
||
| 590 | */ |
||
| 591 | public function unsubscribeNewBlocks($identifier) { |
||
| 595 | |||
| 596 | /** |
||
| 597 | * create a new wallet |
||
| 598 | * - will generate a new primary seed (with password) and backup seed (without password) |
||
| 599 | * - send the primary seed (BIP39 'encrypted') and backup public key to the server |
||
| 600 | * - receive the blocktrail co-signing public key from the server |
||
| 601 | * |
||
| 602 | * Either takes one argument: |
||
| 603 | * @param array $options |
||
| 604 | * |
||
| 605 | * Or takes three arguments (old, deprecated syntax): |
||
| 606 | * (@nonPHP-doc) @param $identifier |
||
| 607 | * (@nonPHP-doc) @param $password |
||
| 608 | * (@nonPHP-doc) @param int $keyIndex override for the blocktrail cosigning key to use |
||
| 609 | * |
||
| 610 | * @return array[WalletInterface, array] list($wallet, $backupInfo) |
||
| 611 | * @throws \Exception |
||
| 612 | */ |
||
| 613 | public function createNewWallet($options) { |
||
| 657 | |||
| 658 | protected function createNewWalletV1($options) { |
||
| 778 | |||
| 779 | public function randomBits($bits) { |
||
| 782 | |||
| 783 | public function randomBytes($bytes) { |
||
| 786 | |||
| 787 | protected function createNewWalletV2($options) { |
||
| 907 | |||
| 908 | protected function createNewWalletV3($options) { |
||
| 1046 | |||
| 1047 | public function newV2PrimarySeed() { |
||
| 1050 | |||
| 1051 | public function newV2BackupSeed() { |
||
| 1054 | |||
| 1055 | public function newV2Secret($passphrase) { |
||
| 1061 | |||
| 1062 | public function newV2EncryptedPrimarySeed($primarySeed, $secret) { |
||
| 1065 | |||
| 1066 | 3 | public function newV2RecoverySecret($secret) { |
|
| 1072 | 3 | ||
| 1073 | 3 | public function newV3PrimarySeed() { |
|
| 1076 | |||
| 1077 | public function newV3BackupSeed() { |
||
| 1080 | |||
| 1081 | public function newV3Secret($passphrase) { |
||
| 1088 | |||
| 1089 | public function newV3EncryptedPrimarySeed(Buffer $primarySeed, Buffer $secret) { |
||
| 1093 | |||
| 1094 | public function newV3RecoverySecret(Buffer $secret) { |
||
| 1101 | |||
| 1102 | /** |
||
| 1103 | * @param array $bip32Key |
||
| 1104 | * @throws BlocktrailSDKException |
||
| 1105 | */ |
||
| 1106 | private function verifyPublicBIP32Key(array $bip32Key) { |
||
| 1116 | |||
| 1117 | /** |
||
| 1118 | * @param array $walletData |
||
| 1119 | 3 | * @throws BlocktrailSDKException |
|
| 1120 | */ |
||
| 1121 | 3 | private function verifyPublicOnly(array $walletData) { |
|
| 1125 | 3 | ||
| 1126 | 3 | /** |
|
| 1127 | 3 | * create wallet using the API |
|
| 1128 | 3 | * |
|
| 1129 | 3 | * @param string $identifier the wallet identifier to create |
|
| 1130 | 3 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
|
| 1131 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1132 | 3 | * @param string $primaryMnemonic mnemonic to store |
|
| 1133 | 3 | * @param string $checksum checksum to store |
|
| 1134 | * @param int $keyIndex account that we expect to use |
||
| 1135 | * @param bool $segwit opt in to segwit |
||
| 1136 | * @return mixed |
||
| 1137 | */ |
||
| 1138 | public function storeNewWalletV1($identifier, $primaryPublicKey, $backupPublicKey, $primaryMnemonic, $checksum, $keyIndex, $segwit = false) { |
||
| 1152 | |||
| 1153 | /** |
||
| 1154 | * create wallet using the API |
||
| 1155 | * |
||
| 1156 | * @param string $identifier the wallet identifier to create |
||
| 1157 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1158 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1159 | * @param $encryptedPrimarySeed |
||
| 1160 | * @param $encryptedSecret |
||
| 1161 | * @param $recoverySecret |
||
| 1162 | * @param string $checksum checksum to store |
||
| 1163 | * @param int $keyIndex account that we expect to use |
||
| 1164 | * @param bool $segwit opt in to segwit |
||
| 1165 | * @return mixed |
||
| 1166 | * @throws \Exception |
||
| 1167 | */ |
||
| 1168 | public function storeNewWalletV2($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false) { |
||
| 1185 | |||
| 1186 | /** |
||
| 1187 | * create wallet using the API |
||
| 1188 | * |
||
| 1189 | * @param string $identifier the wallet identifier to create |
||
| 1190 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1191 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1192 | * @param $encryptedPrimarySeed |
||
| 1193 | * @param $encryptedSecret |
||
| 1194 | * @param $recoverySecret |
||
| 1195 | * @param string $checksum checksum to store |
||
| 1196 | * @param int $keyIndex account that we expect to use |
||
| 1197 | * @param bool $segwit opt in to segwit |
||
| 1198 | * @return mixed |
||
| 1199 | * @throws \Exception |
||
| 1200 | */ |
||
| 1201 | public function storeNewWalletV3($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false) { |
||
| 1220 | |||
| 1221 | /** |
||
| 1222 | * upgrade wallet to use a new account number |
||
| 1223 | * the account number specifies which blocktrail cosigning key is used |
||
| 1224 | * |
||
| 1225 | * @param string $identifier the wallet identifier to be upgraded |
||
| 1226 | * @param int $keyIndex the new account to use |
||
| 1227 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1228 | * @return mixed |
||
| 1229 | */ |
||
| 1230 | 23 | public function upgradeKeyIndex($identifier, $keyIndex, $primaryPublicKey) { |
|
| 1239 | 23 | ||
| 1240 | 23 | /** |
|
| 1241 | 23 | * @param array $options |
|
| 1242 | 23 | * @return AddressReaderBase |
|
| 1243 | 23 | */ |
|
| 1244 | private function makeAddressReader(array $options) { |
||
| 1255 | |||
| 1256 | /** |
||
| 1257 | * initialize a previously created wallet |
||
| 1258 | * |
||
| 1259 | * Takes an options object, or accepts identifier/password for backwards compatiblity. |
||
| 1260 | * |
||
| 1261 | * Some of the options: |
||
| 1262 | * - "readonly/readOnly/read-only" can be to a boolean value, |
||
| 1263 | * so the wallet is loaded in read-only mode (no private key) |
||
| 1264 | * - "check_backup_key" can be set to your own backup key: |
||
| 1265 | * Format: ["M', "xpub..."] |
||
| 1266 | * Setting this will allow the SDK to check the server hasn't |
||
| 1267 | * a different key (one it happens to control) |
||
| 1268 | |||
| 1269 | * Either takes one argument: |
||
| 1270 | * @param array $options |
||
| 1271 | * |
||
| 1272 | * Or takes two arguments (old, deprecated syntax): |
||
| 1273 | * (@nonPHP-doc) @param string $identifier the wallet identifier to be initialized |
||
| 1274 | * (@nonPHP-doc) @param string $password the password to decrypt the mnemonic with |
||
| 1275 | * |
||
| 1276 | * @return WalletInterface |
||
| 1277 | * @throws \Exception |
||
| 1278 | */ |
||
| 1279 | public function initWallet($options) { |
||
| 1391 | |||
| 1392 | /** |
||
| 1393 | * get the wallet data from the server |
||
| 1394 | * |
||
| 1395 | * @param string $identifier the identifier of the wallet |
||
| 1396 | * @return mixed |
||
| 1397 | */ |
||
| 1398 | public function getWallet($identifier) { |
||
| 1402 | |||
| 1403 | /** |
||
| 1404 | * update the wallet data on the server |
||
| 1405 | * |
||
| 1406 | * @param string $identifier |
||
| 1407 | * @param $data |
||
| 1408 | * @return mixed |
||
| 1409 | */ |
||
| 1410 | public function updateWallet($identifier, $data) { |
||
| 1414 | |||
| 1415 | /** |
||
| 1416 | * delete a wallet from the server |
||
| 1417 | * the checksum address and a signature to verify you ownership of the key of that checksum address |
||
| 1418 | * is required to be able to delete a wallet |
||
| 1419 | * |
||
| 1420 | * @param string $identifier the identifier of the wallet |
||
| 1421 | * @param string $checksumAddress the address for your master private key (and the checksum used when creating the wallet) |
||
| 1422 | * @param string $signature a signature of the checksum address as message signed by the private key matching that address |
||
| 1423 | * @param bool $force ignore warnings (such as a non-zero balance) |
||
| 1424 | * @return mixed |
||
| 1425 | */ |
||
| 1426 | public function deleteWallet($identifier, $checksumAddress, $signature, $force = false) { |
||
| 1433 | |||
| 1434 | /** |
||
| 1435 | * create new backup key; |
||
| 1436 | * 1) a BIP39 mnemonic |
||
| 1437 | * 2) a seed from that mnemonic with a blank password |
||
| 1438 | * 3) a private key from that seed |
||
| 1439 | * |
||
| 1440 | * @return array [mnemonic, seed, key] |
||
| 1441 | */ |
||
| 1442 | protected function newV1BackupSeed() { |
||
| 1447 | |||
| 1448 | /** |
||
| 1449 | * create new primary key; |
||
| 1450 | * 1) a BIP39 mnemonic |
||
| 1451 | * 2) a seed from that mnemonic with the password |
||
| 1452 | * 3) a private key from that seed |
||
| 1453 | * |
||
| 1454 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1455 | * @return array [mnemonic, seed, key] |
||
| 1456 | * @TODO: require a strong password? |
||
| 1457 | */ |
||
| 1458 | protected function newV1PrimarySeed($passphrase) { |
||
| 1463 | |||
| 1464 | /** |
||
| 1465 | * create a new key; |
||
| 1466 | * 1) a BIP39 mnemonic |
||
| 1467 | * 2) a seed from that mnemonic with the password |
||
| 1468 | * 3) a private key from that seed |
||
| 1469 | * |
||
| 1470 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1471 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1472 | * @return array |
||
| 1473 | */ |
||
| 1474 | protected function generateNewSeed($passphrase = "", $forceEntropy = null) { |
||
| 1491 | |||
| 1492 | /** |
||
| 1493 | * generate a new mnemonic from some random entropy (512 bit) |
||
| 1494 | * |
||
| 1495 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1496 | * @return string |
||
| 1497 | * @throws \Exception |
||
| 1498 | */ |
||
| 1499 | public function generateNewMnemonic($forceEntropy = null) { |
||
| 1509 | |||
| 1510 | /** |
||
| 1511 | * get the balance for the wallet |
||
| 1512 | * |
||
| 1513 | * @param string $identifier the identifier of the wallet |
||
| 1514 | * @return array |
||
| 1515 | */ |
||
| 1516 | public function getWalletBalance($identifier) { |
||
| 1520 | |||
| 1521 | /** |
||
| 1522 | * get a new derivation number for specified parent path |
||
| 1523 | * 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 |
||
| 1524 | * |
||
| 1525 | * returns the path |
||
| 1526 | * |
||
| 1527 | * @param string $identifier the identifier of the wallet |
||
| 1528 | * @param string $path the parent path for which to get a new derivation |
||
| 1529 | * @return string |
||
| 1530 | */ |
||
| 1531 | public function getNewDerivation($identifier, $path) { |
||
| 1535 | |||
| 1536 | /** |
||
| 1537 | * get a new derivation number for specified parent path |
||
| 1538 | * 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 |
||
| 1539 | * |
||
| 1540 | * @param string $identifier the identifier of the wallet |
||
| 1541 | * @param string $path the parent path for which to get a new derivation |
||
| 1542 | * @return mixed |
||
| 1543 | */ |
||
| 1544 | public function _getNewDerivation($identifier, $path) { |
||
| 1548 | |||
| 1549 | /** |
||
| 1550 | * get the path (and redeemScript) to specified address |
||
| 1551 | * |
||
| 1552 | * @param string $identifier |
||
| 1553 | * @param string $address |
||
| 1554 | * @return array |
||
| 1555 | * @throws \Exception |
||
| 1556 | */ |
||
| 1557 | public function getPathForAddress($identifier, $address) { |
||
| 1561 | |||
| 1562 | /** |
||
| 1563 | * send the transaction using the API |
||
| 1564 | * |
||
| 1565 | * @param string $identifier the identifier of the wallet |
||
| 1566 | * @param string|array $rawTransaction raw hex of the transaction (should be partially signed) |
||
| 1567 | * @param array $paths list of the paths that were used for the UTXO |
||
| 1568 | * @param bool $checkFee let the server verify the fee after signing |
||
| 1569 | * @param null $twoFactorToken |
||
| 1570 | * @return string the complete raw transaction |
||
| 1571 | * @throws \Exception |
||
| 1572 | */ |
||
| 1573 | public function sendTransaction($identifier, $rawTransaction, $paths, $checkFee = false, $twoFactorToken = null) { |
||
| 1604 | |||
| 1605 | /** |
||
| 1606 | * use the API to get the best inputs to use based on the outputs |
||
| 1607 | * |
||
| 1608 | * the return array has the following format: |
||
| 1609 | * [ |
||
| 1610 | * "utxos" => [ |
||
| 1611 | * [ |
||
| 1612 | * "hash" => "<txHash>", |
||
| 1613 | * "idx" => "<index of the output of that <txHash>", |
||
| 1614 | * "scriptpubkey_hex" => "<scriptPubKey-hex>", |
||
| 1615 | * "value" => 32746327, |
||
| 1616 | * "address" => "1address", |
||
| 1617 | * "path" => "m/44'/1'/0'/0/13", |
||
| 1618 | * "redeem_script" => "<redeemScript-hex>", |
||
| 1619 | * ], |
||
| 1620 | * ], |
||
| 1621 | * "fee" => 10000, |
||
| 1622 | * "change"=> 1010109201, |
||
| 1623 | * ] |
||
| 1624 | * |
||
| 1625 | * @param string $identifier the identifier of the wallet |
||
| 1626 | * @param array $outputs the outputs you want to create - array[address => satoshi-value] |
||
| 1627 | * @param bool $lockUTXO when TRUE the UTXOs selected will be locked for a few seconds |
||
| 1628 | * so you have some time to spend them without race-conditions |
||
| 1629 | * @param bool $allowZeroConf |
||
| 1630 | * @param string $feeStrategy |
||
| 1631 | * @param null|int $forceFee |
||
| 1632 | * @return array |
||
| 1633 | * @throws \Exception |
||
| 1634 | */ |
||
| 1635 | public function coinSelection($identifier, $outputs, $lockUTXO = false, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null) { |
||
| 1657 | |||
| 1658 | /** |
||
| 1659 | * |
||
| 1660 | * @param string $identifier the identifier of the wallet |
||
| 1661 | * @param bool $allowZeroConf |
||
| 1662 | * @param string $feeStrategy |
||
| 1663 | * @param null|int $forceFee |
||
| 1664 | * @param int $outputCnt |
||
| 1665 | * @return array |
||
| 1666 | * @throws \Exception |
||
| 1667 | */ |
||
| 1668 | public function walletMaxSpendable($identifier, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null, $outputCnt = 1) { |
||
| 1687 | |||
| 1688 | /** |
||
| 1689 | * @return array ['optimal_fee' => 10000, 'low_priority_fee' => 5000] |
||
| 1690 | */ |
||
| 1691 | public function feePerKB() { |
||
| 1695 | |||
| 1696 | /** |
||
| 1697 | * get the current price index |
||
| 1698 | * |
||
| 1699 | * @return array eg; ['USD' => 287.30] |
||
| 1700 | */ |
||
| 1701 | public function price() { |
||
| 1705 | |||
| 1706 | /** |
||
| 1707 | * setup webhook for wallet |
||
| 1708 | * |
||
| 1709 | * @param string $identifier the wallet identifier for which to create the webhook |
||
| 1710 | * @param string $webhookIdentifier the webhook identifier to use |
||
| 1711 | * @param string $url the url to receive the webhook events |
||
| 1712 | * @return array |
||
| 1713 | */ |
||
| 1714 | public function setupWalletWebhook($identifier, $webhookIdentifier, $url) { |
||
| 1718 | |||
| 1719 | /** |
||
| 1720 | * delete webhook for wallet |
||
| 1721 | * |
||
| 1722 | * @param string $identifier the wallet identifier for which to delete the webhook |
||
| 1723 | * @param string $webhookIdentifier the webhook identifier to delete |
||
| 1724 | * @return array |
||
| 1725 | */ |
||
| 1726 | public function deleteWalletWebhook($identifier, $webhookIdentifier) { |
||
| 1730 | |||
| 1731 | /** |
||
| 1732 | * lock a specific unspent output |
||
| 1733 | * |
||
| 1734 | * @param $identifier |
||
| 1735 | * @param $txHash |
||
| 1736 | * @param $txIdx |
||
| 1737 | * @param int $ttl |
||
| 1738 | * @return bool |
||
| 1739 | */ |
||
| 1740 | public function lockWalletUTXO($identifier, $txHash, $txIdx, $ttl = 3) { |
||
| 1744 | |||
| 1745 | /** |
||
| 1746 | * unlock a specific unspent output |
||
| 1747 | * |
||
| 1748 | * @param $identifier |
||
| 1749 | * @param $txHash |
||
| 1750 | * @param $txIdx |
||
| 1751 | * @return bool |
||
| 1752 | */ |
||
| 1753 | public function unlockWalletUTXO($identifier, $txHash, $txIdx) { |
||
| 1757 | |||
| 1758 | /** |
||
| 1759 | * get all transactions for wallet (paginated) |
||
| 1760 | * |
||
| 1761 | * @param string $identifier the wallet identifier for which to get transactions |
||
| 1762 | * @param integer $page pagination: page number |
||
| 1763 | * @param integer $limit pagination: records per page (max 500) |
||
| 1764 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1765 | * @return array associative array containing the response |
||
| 1766 | */ |
||
| 1767 | public function walletTransactions($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 1776 | |||
| 1777 | /** |
||
| 1778 | * get all addresses for wallet (paginated) |
||
| 1779 | * |
||
| 1780 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1781 | * @param integer $page pagination: page number |
||
| 1782 | * @param integer $limit pagination: records per page (max 500) |
||
| 1783 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1784 | * @return array associative array containing the response |
||
| 1785 | */ |
||
| 1786 | public function walletAddresses($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 1795 | |||
| 1796 | /** |
||
| 1797 | * get all UTXOs for wallet (paginated) |
||
| 1798 | * |
||
| 1799 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1800 | * @param integer $page pagination: page number |
||
| 1801 | * @param integer $limit pagination: records per page (max 500) |
||
| 1802 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1803 | * @param boolean $zeroconf include zero confirmation transactions |
||
| 1804 | * @return array associative array containing the response |
||
| 1805 | */ |
||
| 1806 | public function walletUTXOs($identifier, $page = 1, $limit = 20, $sortDir = 'asc', $zeroconf = true) { |
||
| 1816 | |||
| 1817 | /** |
||
| 1818 | * get a paginated list of all wallets associated with the api user |
||
| 1819 | * |
||
| 1820 | * @param integer $page pagination: page number |
||
| 1821 | * @param integer $limit pagination: records per page |
||
| 1822 | * @return array associative array containing the response |
||
| 1823 | */ |
||
| 1824 | public function allWallets($page = 1, $limit = 20) { |
||
| 1832 | 2 | ||
| 1833 | 2 | /** |
|
| 1834 | 2 | * send raw transaction |
|
| 1835 | * |
||
| 1836 | * @param $txHex |
||
| 1837 | * @return bool |
||
| 1838 | */ |
||
| 1839 | 2 | public function sendRawTransaction($txHex) { |
|
| 1843 | 2 | ||
| 1844 | /** |
||
| 1845 | * testnet only ;-) |
||
| 1846 | * |
||
| 1847 | * @param $address |
||
| 1848 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1849 | * @return mixed |
||
| 1850 | * @throws \Exception |
||
| 1851 | */ |
||
| 1852 | public function faucetWithdrawal($address, $amount = 10000) { |
||
| 1859 | |||
| 1860 | /** |
||
| 1861 | * Exists for BC. Remove at major bump. |
||
| 1862 | * |
||
| 1863 | * @see faucetWithdrawal |
||
| 1864 | * @deprecated |
||
| 1865 | * @param $address |
||
| 1866 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1867 | * @return mixed |
||
| 1868 | * @throws \Exception |
||
| 1869 | */ |
||
| 1870 | public function faucetWithdrawl($address, $amount = 10000) { |
||
| 1873 | |||
| 1874 | /** |
||
| 1875 | * verify a message signed bitcoin-core style |
||
| 1876 | * |
||
| 1877 | * @param string $message |
||
| 1878 | 1 | * @param string $address |
|
| 1879 | 1 | * @param string $signature |
|
| 1880 | * @return boolean |
||
| 1881 | */ |
||
| 1882 | public function verifyMessage($message, $address, $signature) { |
||
| 1896 | |||
| 1897 | /** |
||
| 1898 | 1 | * Take a base58 or cashaddress, and return only |
|
| 1899 | 1 | * the cash address. |
|
| 1900 | * This function only works on bitcoin cash. |
||
| 1901 | * @param string $input |
||
| 1902 | * @return string |
||
| 1903 | * @throws BlocktrailSDKException |
||
| 1904 | */ |
||
| 1905 | public function getLegacyBitcoinCashAddress($input) { |
||
| 1922 | |||
| 1923 | /** |
||
| 1924 | * convert a Satoshi value to a BTC value |
||
| 1925 | 3 | * |
|
| 1926 | * @param int $satoshi |
||
| 1927 | 3 | * @return float |
|
| 1928 | */ |
||
| 1929 | public static function toBTC($satoshi) { |
||
| 1932 | |||
| 1933 | /** |
||
| 1934 | * convert a Satoshi value to a BTC value and return it as a string |
||
| 1935 | |||
| 1936 | * @param int $satoshi |
||
| 1937 | * @return string |
||
| 1938 | */ |
||
| 1939 | public static function toBTCString($satoshi) { |
||
| 1942 | |||
| 1943 | /** |
||
| 1944 | * convert a BTC value to a Satoshi value |
||
| 1945 | * |
||
| 1946 | * @param float $btc |
||
| 1947 | * @return string |
||
| 1948 | */ |
||
| 1949 | public static function toSatoshiString($btc) { |
||
| 1952 | |||
| 1953 | /** |
||
| 1954 | * convert a BTC value to a Satoshi value |
||
| 1955 | * |
||
| 1956 | * @param float $btc |
||
| 1957 | * @return string |
||
| 1958 | */ |
||
| 1959 | public static function toSatoshi($btc) { |
||
| 1962 | |||
| 1963 | /** |
||
| 1964 | * json_decode helper that throws exceptions when it fails to decode |
||
| 1965 | * |
||
| 1966 | * @param $json |
||
| 1967 | * @param bool $assoc |
||
| 1968 | * @return mixed |
||
| 1969 | * @throws \Exception |
||
| 1970 | */ |
||
| 1971 | public static function jsonDecode($json, $assoc = false) { |
||
| 1984 | |||
| 1985 | /** |
||
| 1986 | * sort public keys for multisig script |
||
| 1987 | * |
||
| 1988 | * @param PublicKeyInterface[] $pubKeys |
||
| 1989 | * @return PublicKeyInterface[] |
||
| 1990 | */ |
||
| 1991 | public static function sortMultisigKeys(array $pubKeys) { |
||
| 2001 | |||
| 2002 | /** |
||
| 2003 | * read and decode the json payload from a webhook's POST request. |
||
| 2004 | * |
||
| 2005 | * @param bool $returnObject flag to indicate if an object or associative array should be returned |
||
| 2006 | * @return mixed|null |
||
| 2007 | * @throws \Exception |
||
| 2008 | */ |
||
| 2009 | public static function getWebhookPayload($returnObject = false) { |
||
| 2017 | |||
| 2018 | public static function normalizeBIP32KeyArray($keys) { |
||
| 2023 | |||
| 2024 | /** |
||
| 2025 | * @param array|BIP32Key $key |
||
| 2026 | * @return BIP32Key |
||
| 2027 | * @throws \Exception |
||
| 2028 | */ |
||
| 2029 | public static function normalizeBIP32Key($key) { |
||
| 2047 | |||
| 2048 | public function shuffle($arr) { |
||
| 2051 | } |
||
| 2052 |
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: