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 | 17 | public function __construct($apiKey, $apiSecret, $network = 'BTC', $testnet = false, $apiVersion = 'v1', $apiEndpoint = null) { |
|
| 79 | |||
| 80 | 17 | list ($apiNetwork, $testnet) = Util::parseApiNetwork($network, $testnet); |
|
| 81 | |||
| 82 | 17 | if (is_null($apiEndpoint)) { |
|
| 83 | 17 | $apiEndpoint = getenv('BLOCKTRAIL_SDK_API_ENDPOINT') ?: "https://wallet-api.btc.com"; |
|
| 84 | 17 | $apiEndpoint = "{$apiEndpoint}/{$apiVersion}/{$apiNetwork}/"; |
|
| 85 | } |
||
| 86 | |||
| 87 | // normalize network and set bitcoinlib to the right magic-bytes |
||
| 88 | 17 | list($this->network, $this->testnet, $regtest) = $this->normalizeNetwork($network, $testnet); |
|
| 89 | 17 | $this->setBitcoinLibMagicBytes($this->network, $this->testnet, $regtest); |
|
| 90 | |||
| 91 | 17 | $btccomEndpoint = getenv('BLOCKTRAIL_SDK_BTCCOM_API_ENDPOINT'); |
|
| 92 | 17 | if (!$btccomEndpoint) { |
|
| 93 | $btccomEndpoint = "https://" . ($this->network === "BCC" ? "bch-chain" : "chain") . ".api.btc.com"; |
||
| 94 | } |
||
| 95 | 17 | $btccomEndpoint = "{$btccomEndpoint}/v3/"; |
|
| 96 | |||
| 97 | 17 | if ($this->testnet && strpos($btccomEndpoint, "tchain") === false) { |
|
| 98 | 11 | $btccomEndpoint = \str_replace("chain", "tchain", $btccomEndpoint); |
|
| 99 | } |
||
| 100 | |||
| 101 | 17 | $this->blocktrailClient = new RestClient($apiEndpoint, $apiVersion, $apiKey, $apiSecret); |
|
| 102 | 17 | $this->dataClient = new RestClient($btccomEndpoint, $apiVersion, $apiKey, $apiSecret); |
|
| 103 | |||
| 104 | 17 | $this->converter = new BtccomConverter(); |
|
| 105 | 17 | } |
|
| 106 | |||
| 107 | /** |
||
| 108 | * normalize network string |
||
| 109 | * |
||
| 110 | * @param $network |
||
| 111 | * @param $testnet |
||
| 112 | * @return array |
||
| 113 | * @throws \Exception |
||
| 114 | */ |
||
| 115 | 17 | 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 | 17 | protected function setBitcoinLibMagicBytes($network, $testnet, $regtest) { |
|
| 128 | |||
| 129 | 17 | if ($network === "bitcoin") { |
|
| 130 | 17 | if ($regtest) { |
|
| 131 | 11 | $useNetwork = NetworkFactory::bitcoinRegtest(); |
|
| 132 | 6 | } else if ($testnet) { |
|
| 133 | $useNetwork = NetworkFactory::bitcoinTestnet(); |
||
| 134 | } else { |
||
| 135 | 17 | $useNetwork = NetworkFactory::bitcoin(); |
|
| 136 | } |
||
| 137 | } else if ($network === "bitcoincash") { |
||
| 138 | if ($regtest) { |
||
| 139 | $useNetwork = new BitcoinCashRegtest(); |
||
| 140 | } else if ($testnet) { |
||
| 141 | $useNetwork = new BitcoinCashTestnet(); |
||
| 142 | } else { |
||
| 143 | $useNetwork = new BitcoinCash(); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | 17 | Bitcoin::setNetwork($useNetwork); |
|
|
|
|||
| 148 | 17 | } |
|
| 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 | 1 | 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 | 2 | 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 | 2 | public function addressTransactions($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 226 | $queryString = [ |
||
| 227 | 2 | 'page' => $page, |
|
| 228 | 2 | 'limit' => $limit, |
|
| 229 | 2 | 'sort_dir' => $sortDir, |
|
| 230 | ]; |
||
| 231 | 2 | $response = $this->dataClient->get($this->converter->getUrlForAddressTransactions($address), $this->converter->paginationParams($queryString)); |
|
| 232 | 2 | 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 | 2 | public function addressUnspentOutputs($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 262 | $queryString = [ |
||
| 263 | 2 | 'page' => $page, |
|
| 264 | 2 | 'limit' => $limit, |
|
| 265 | 2 | 'sort_dir' => $sortDir |
|
| 266 | ]; |
||
| 267 | 2 | $response = $this->dataClient->get($this->converter->getUrlForAddressUnspent($address), $this->converter->paginationParams($queryString)); |
|
| 268 | 2 | 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 | 2 | public function verifyAddress($address, $signature) { |
|
| 313 | 2 | if ($this->verifyMessage($address, $address, $signature)) { |
|
| 314 | 2 | return ['result' => true, 'msg' => 'Successfully verified']; |
|
| 315 | } else { |
||
| 316 | return ['result' => false]; |
||
| 317 | } |
||
| 318 | } |
||
| 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 | 2 | public function allBlocks($page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 328 | $queryString = [ |
||
| 329 | 2 | 'page' => $page, |
|
| 330 | 2 | 'limit' => $limit, |
|
| 331 | 2 | 'sort_dir' => $sortDir |
|
| 332 | ]; |
||
| 333 | 2 | $response = $this->dataClient->get($this->converter->getUrlForAllBlocks(), $this->converter->paginationParams($queryString)); |
|
| 334 | 2 | return $this->converter->convertBlocks($response->body()); |
|
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * get the latest block |
||
| 339 | * @return array associative array containing the response |
||
| 340 | */ |
||
| 341 | 1 | public function blockLatest() { |
|
| 342 | 1 | $response = $this->dataClient->get($this->converter->getUrlForBlock("latest")); |
|
| 343 | 1 | return $this->converter->convertBlock($response->body()); |
|
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * get an individual block |
||
| 348 | * @param string|integer $block a block hash or a block height |
||
| 349 | * @return array associative array containing the response |
||
| 350 | */ |
||
| 351 | 4 | public function block($block) { |
|
| 355 | |||
| 356 | /** |
||
| 357 | * get all transaction in a block (paginated) |
||
| 358 | * @param string|integer $block a block hash or a block height |
||
| 359 | * @param integer $page pagination: page number |
||
| 360 | * @param integer $limit pagination: records per page |
||
| 361 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 362 | * @return array associative array containing the response |
||
| 363 | */ |
||
| 364 | 1 | public function blockTransactions($block, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 365 | $queryString = [ |
||
| 366 | 1 | 'page' => $page, |
|
| 367 | 1 | 'limit' => $limit, |
|
| 368 | 1 | 'sort_dir' => $sortDir |
|
| 369 | ]; |
||
| 370 | 1 | $response = $this->dataClient->get($this->converter->getUrlForBlockTransaction($block), $this->converter->paginationParams($queryString)); |
|
| 371 | 1 | return $this->converter->convertBlockTxs($response->body()); |
|
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * get a single transaction |
||
| 376 | * @param string $txhash transaction hash |
||
| 377 | * @return array associative array containing the response |
||
| 378 | */ |
||
| 379 | 2 | public function transaction($txhash) { |
|
| 380 | 2 | $response = $this->dataClient->get($this->converter->getUrlForTransaction($txhash)); |
|
| 381 | 2 | $res = $this->converter->convertTx($response->body(), null); |
|
| 382 | |||
| 383 | 2 | if ($this->converter instanceof BtccomConverter) { |
|
| 384 | 1 | $res['raw'] = \json_decode($this->dataClient->get("tx/{$txhash}/raw")->body(), true)['data']; |
|
| 385 | } |
||
| 386 | |||
| 387 | 2 | return $res; |
|
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * get a single transaction |
||
| 392 | * @param string[] $txhashes list of transaction hashes (up to 20) |
||
| 393 | * @return array[] array containing the response |
||
| 394 | */ |
||
| 395 | 2 | public function transactions($txhashes) { |
|
| 396 | 2 | $response = $this->dataClient->get($this->converter->getUrlForTransactions($txhashes)); |
|
| 397 | 2 | return $this->converter->convertTxs($response->body()); |
|
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * get a paginated list of all webhooks associated with the api user |
||
| 402 | * @param integer $page pagination: page number |
||
| 403 | * @param integer $limit pagination: records per page |
||
| 404 | * @return array associative array containing the response |
||
| 405 | */ |
||
| 406 | public function allWebhooks($page = 1, $limit = 20) { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * get an existing webhook by it's identifier |
||
| 417 | * @param string $identifier a unique identifier associated with the webhook |
||
| 418 | * @return array associative array containing the response |
||
| 419 | */ |
||
| 420 | public function getWebhook($identifier) { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * create a new webhook |
||
| 427 | * @param string $url the url to receive the webhook events |
||
| 428 | * @param string $identifier a unique identifier to associate with this webhook |
||
| 429 | * @return array associative array containing the response |
||
| 430 | */ |
||
| 431 | public function setupWebhook($url, $identifier = null) { |
||
| 432 | $postData = [ |
||
| 433 | 'url' => $url, |
||
| 434 | 'identifier' => $identifier |
||
| 435 | ]; |
||
| 436 | $response = $this->blocktrailClient->post("webhook", null, $postData, RestClient::AUTH_HTTP_SIG); |
||
| 437 | return self::jsonDecode($response->body(), true); |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * update an existing webhook |
||
| 442 | * @param string $identifier the unique identifier of the webhook to update |
||
| 443 | * @param string $newUrl the new url to receive the webhook events |
||
| 444 | * @param string $newIdentifier a new unique identifier to associate with this webhook |
||
| 445 | * @return array associative array containing the response |
||
| 446 | */ |
||
| 447 | public function updateWebhook($identifier, $newUrl = null, $newIdentifier = null) { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * deletes an existing webhook and any event subscriptions associated with it |
||
| 458 | * @param string $identifier the unique identifier of the webhook to delete |
||
| 459 | * @return boolean true on success |
||
| 460 | */ |
||
| 461 | public function deleteWebhook($identifier) { |
||
| 465 | |||
| 466 | /** |
||
| 467 | * get a paginated list of all the events a webhook is subscribed to |
||
| 468 | * @param string $identifier the unique identifier of the webhook |
||
| 469 | * @param integer $page pagination: page number |
||
| 470 | * @param integer $limit pagination: records per page |
||
| 471 | * @return array associative array containing the response |
||
| 472 | */ |
||
| 473 | public function getWebhookEvents($identifier, $page = 1, $limit = 20) { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * subscribes a webhook to transaction events of one particular transaction |
||
| 484 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 485 | * @param string $transaction the transaction hash |
||
| 486 | * @param integer $confirmations the amount of confirmations to send. |
||
| 487 | * @return array associative array containing the response |
||
| 488 | */ |
||
| 489 | public function subscribeTransaction($identifier, $transaction, $confirmations = 6) { |
||
| 498 | |||
| 499 | /** |
||
| 500 | * subscribes a webhook to transaction events on a particular address |
||
| 501 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 502 | * @param string $address the address hash |
||
| 503 | * @param integer $confirmations the amount of confirmations to send. |
||
| 504 | * @return array associative array containing the response |
||
| 505 | */ |
||
| 506 | public function subscribeAddressTransactions($identifier, $address, $confirmations = 6) { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * batch subscribes a webhook to multiple transaction events |
||
| 518 | * |
||
| 519 | * @param string $identifier the unique identifier of the webhook |
||
| 520 | * @param array $batchData A 2D array of event data: |
||
| 521 | * [address => $address, confirmations => $confirmations] |
||
| 522 | * where $address is the address to subscibe to |
||
| 523 | * and optionally $confirmations is the amount of confirmations |
||
| 524 | * @return boolean true on success |
||
| 525 | */ |
||
| 526 | public function batchSubscribeAddressTransactions($identifier, $batchData) { |
||
| 538 | |||
| 539 | /** |
||
| 540 | * subscribes a webhook to a new block event |
||
| 541 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 542 | * @return array associative array containing the response |
||
| 543 | */ |
||
| 544 | public function subscribeNewBlocks($identifier) { |
||
| 551 | |||
| 552 | /** |
||
| 553 | * removes an transaction event subscription from a webhook |
||
| 554 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 555 | * @param string $transaction the transaction hash of the event subscription |
||
| 556 | * @return boolean true on success |
||
| 557 | */ |
||
| 558 | public function unsubscribeTransaction($identifier, $transaction) { |
||
| 562 | |||
| 563 | /** |
||
| 564 | * removes an address transaction event subscription from a webhook |
||
| 565 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 566 | * @param string $address the address hash of the event subscription |
||
| 567 | * @return boolean true on success |
||
| 568 | */ |
||
| 569 | public function unsubscribeAddressTransactions($identifier, $address) { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * removes a block event subscription from a webhook |
||
| 576 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 577 | * @return boolean true on success |
||
| 578 | */ |
||
| 579 | public function unsubscribeNewBlocks($identifier) { |
||
| 583 | |||
| 584 | /** |
||
| 585 | * create a new wallet |
||
| 586 | * - will generate a new primary seed (with password) and backup seed (without password) |
||
| 587 | * - send the primary seed (BIP39 'encrypted') and backup public key to the server |
||
| 588 | * - receive the blocktrail co-signing public key from the server |
||
| 589 | * |
||
| 590 | * Either takes one argument: |
||
| 591 | * @param array $options |
||
| 592 | * |
||
| 593 | * Or takes three arguments (old, deprecated syntax): |
||
| 594 | * (@nonPHP-doc) @param $identifier |
||
| 595 | * (@nonPHP-doc) @param $password |
||
| 596 | * (@nonPHP-doc) @param int $keyIndex override for the blocktrail cosigning key to use |
||
| 597 | * |
||
| 598 | * @return array[WalletInterface, array] list($wallet, $backupInfo) |
||
| 599 | * @throws \Exception |
||
| 600 | */ |
||
| 601 | public function createNewWallet($options) { |
||
| 645 | |||
| 646 | protected function createNewWalletV1($options) { |
||
| 647 | $walletPath = WalletPath::create($options['key_index']); |
||
| 648 | |||
| 649 | $storePrimaryMnemonic = isset($options['store_primary_mnemonic']) ? $options['store_primary_mnemonic'] : null; |
||
| 650 | |||
| 651 | if (isset($options['primary_mnemonic']) && isset($options['primary_private_key'])) { |
||
| 652 | throw new \InvalidArgumentException("Can't specify Primary Mnemonic and Primary PrivateKey"); |
||
| 653 | } |
||
| 654 | |||
| 655 | $primaryMnemonic = null; |
||
| 656 | $primaryPrivateKey = null; |
||
| 657 | if (!isset($options['primary_mnemonic']) && !isset($options['primary_private_key'])) { |
||
| 658 | if (!$options['passphrase']) { |
||
| 659 | throw new \InvalidArgumentException("Can't generate Primary Mnemonic without a passphrase"); |
||
| 660 | } else { |
||
| 661 | // create new primary seed |
||
| 662 | /** @var HierarchicalKey $primaryPrivateKey */ |
||
| 663 | list($primaryMnemonic, , $primaryPrivateKey) = $this->newV1PrimarySeed($options['passphrase']); |
||
| 664 | if ($storePrimaryMnemonic !== false) { |
||
| 665 | $storePrimaryMnemonic = true; |
||
| 666 | } |
||
| 667 | } |
||
| 668 | } elseif (isset($options['primary_mnemonic'])) { |
||
| 669 | $primaryMnemonic = $options['primary_mnemonic']; |
||
| 670 | } elseif (isset($options['primary_private_key'])) { |
||
| 671 | $primaryPrivateKey = $options['primary_private_key']; |
||
| 672 | } |
||
| 673 | |||
| 674 | if ($storePrimaryMnemonic && $primaryMnemonic && !$options['passphrase']) { |
||
| 675 | throw new \InvalidArgumentException("Can't store Primary Mnemonic on server without a passphrase"); |
||
| 676 | } |
||
| 677 | |||
| 678 | if ($primaryPrivateKey) { |
||
| 679 | if (is_string($primaryPrivateKey)) { |
||
| 680 | $primaryPrivateKey = [$primaryPrivateKey, "m"]; |
||
| 681 | } |
||
| 682 | } else { |
||
| 683 | $primaryPrivateKey = HierarchicalKeyFactory::fromEntropy((new Bip39SeedGenerator())->getSeed($primaryMnemonic, $options['passphrase'])); |
||
| 684 | } |
||
| 685 | |||
| 686 | if (!$storePrimaryMnemonic) { |
||
| 687 | $primaryMnemonic = false; |
||
| 688 | } |
||
| 689 | |||
| 690 | // create primary public key from the created private key |
||
| 691 | $path = $walletPath->keyIndexPath()->publicPath(); |
||
| 692 | $primaryPublicKey = BIP32Key::create($primaryPrivateKey, "m")->buildKey($path); |
||
| 693 | |||
| 694 | if (isset($options['backup_mnemonic']) && $options['backup_public_key']) { |
||
| 695 | throw new \InvalidArgumentException("Can't specify Backup Mnemonic and Backup PublicKey"); |
||
| 696 | } |
||
| 697 | |||
| 698 | $backupMnemonic = null; |
||
| 699 | $backupPublicKey = null; |
||
| 700 | if (!isset($options['backup_mnemonic']) && !isset($options['backup_public_key'])) { |
||
| 701 | /** @var HierarchicalKey $backupPrivateKey */ |
||
| 702 | list($backupMnemonic, , ) = $this->newV1BackupSeed(); |
||
| 703 | } else if (isset($options['backup_mnemonic'])) { |
||
| 704 | $backupMnemonic = $options['backup_mnemonic']; |
||
| 705 | } elseif (isset($options['backup_public_key'])) { |
||
| 706 | $backupPublicKey = $options['backup_public_key']; |
||
| 707 | } |
||
| 708 | |||
| 709 | if ($backupPublicKey) { |
||
| 710 | if (is_string($backupPublicKey)) { |
||
| 711 | $backupPublicKey = [$backupPublicKey, "m"]; |
||
| 712 | } |
||
| 713 | } else { |
||
| 714 | $backupPrivateKey = HierarchicalKeyFactory::fromEntropy((new Bip39SeedGenerator())->getSeed($backupMnemonic, "")); |
||
| 715 | $backupPublicKey = BIP32Key::create($backupPrivateKey->toPublic(), "M"); |
||
| 716 | } |
||
| 717 | |||
| 718 | // create a checksum of our private key which we'll later use to verify we used the right password |
||
| 719 | $checksum = $primaryPrivateKey->getPublicKey()->getAddress()->getAddress(); |
||
| 720 | $addressReader = $this->makeAddressReader($options); |
||
| 721 | |||
| 722 | // send the public keys to the server to store them |
||
| 723 | // and the mnemonic, which is safe because it's useless without the password |
||
| 724 | $data = $this->storeNewWalletV1( |
||
| 725 | $options['identifier'], |
||
| 726 | $primaryPublicKey->tuple(), |
||
| 727 | $backupPublicKey->tuple(), |
||
| 728 | $primaryMnemonic, |
||
| 729 | $checksum, |
||
| 730 | $options['key_index'], |
||
| 731 | array_key_exists('segwit', $options) ? $options['segwit'] : false |
||
| 732 | ); |
||
| 733 | |||
| 734 | // received the blocktrail public keys |
||
| 735 | $blocktrailPublicKeys = Util::arrayMapWithIndex(function ($keyIndex, $pubKeyTuple) { |
||
| 736 | return [$keyIndex, BIP32Key::create(HierarchicalKeyFactory::fromExtended($pubKeyTuple[0]), $pubKeyTuple[1])]; |
||
| 737 | }, $data['blocktrail_public_keys']); |
||
| 738 | |||
| 739 | $wallet = new WalletV1( |
||
| 740 | $this, |
||
| 741 | $options['identifier'], |
||
| 742 | $primaryMnemonic, |
||
| 743 | [$options['key_index'] => $primaryPublicKey], |
||
| 744 | $backupPublicKey, |
||
| 745 | $blocktrailPublicKeys, |
||
| 746 | $options['key_index'], |
||
| 747 | $this->network, |
||
| 748 | $this->testnet, |
||
| 749 | array_key_exists('segwit', $data) ? $data['segwit'] : false, |
||
| 750 | $addressReader, |
||
| 751 | $checksum |
||
| 752 | ); |
||
| 753 | |||
| 754 | $wallet->unlock($options); |
||
| 755 | |||
| 756 | // return wallet and backup mnemonic |
||
| 757 | return [ |
||
| 758 | $wallet, |
||
| 759 | [ |
||
| 760 | 'primary_mnemonic' => $primaryMnemonic, |
||
| 761 | 'backup_mnemonic' => $backupMnemonic, |
||
| 762 | 'blocktrail_public_keys' => $blocktrailPublicKeys, |
||
| 763 | ], |
||
| 764 | ]; |
||
| 765 | } |
||
| 766 | |||
| 767 | public function randomBits($bits) { |
||
| 770 | |||
| 771 | public function randomBytes($bytes) { |
||
| 774 | |||
| 775 | protected function createNewWalletV2($options) { |
||
| 776 | $walletPath = WalletPath::create($options['key_index']); |
||
| 777 | |||
| 778 | if (isset($options['store_primary_mnemonic'])) { |
||
| 779 | $options['store_data_on_server'] = $options['store_primary_mnemonic']; |
||
| 780 | } |
||
| 781 | |||
| 782 | if (!isset($options['store_data_on_server'])) { |
||
| 783 | if (isset($options['primary_private_key'])) { |
||
| 784 | $options['store_data_on_server'] = false; |
||
| 785 | } else { |
||
| 786 | $options['store_data_on_server'] = true; |
||
| 895 | |||
| 896 | protected function createNewWalletV3($options) { |
||
| 1034 | |||
| 1035 | public function newV2PrimarySeed() { |
||
| 1038 | |||
| 1039 | public function newV2BackupSeed() { |
||
| 1042 | |||
| 1043 | public function newV2Secret($passphrase) { |
||
| 1049 | |||
| 1050 | public function newV2EncryptedPrimarySeed($primarySeed, $secret) { |
||
| 1053 | |||
| 1054 | public function newV2RecoverySecret($secret) { |
||
| 1060 | |||
| 1061 | public function newV3PrimarySeed() { |
||
| 1064 | |||
| 1065 | public function newV3BackupSeed() { |
||
| 1068 | |||
| 1069 | public function newV3Secret($passphrase) { |
||
| 1076 | |||
| 1077 | public function newV3EncryptedPrimarySeed(Buffer $primarySeed, Buffer $secret) { |
||
| 1081 | |||
| 1082 | public function newV3RecoverySecret(Buffer $secret) { |
||
| 1089 | |||
| 1090 | /** |
||
| 1091 | * @param array $bip32Key |
||
| 1092 | * @throws BlocktrailSDKException |
||
| 1093 | */ |
||
| 1094 | private function verifyPublicBIP32Key(array $bip32Key) { |
||
| 1104 | |||
| 1105 | /** |
||
| 1106 | * @param array $walletData |
||
| 1107 | * @throws BlocktrailSDKException |
||
| 1108 | */ |
||
| 1109 | private function verifyPublicOnly(array $walletData) { |
||
| 1113 | |||
| 1114 | /** |
||
| 1115 | * create wallet using the API |
||
| 1116 | * |
||
| 1117 | * @param string $identifier the wallet identifier to create |
||
| 1118 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1119 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1120 | * @param string $primaryMnemonic mnemonic to store |
||
| 1121 | * @param string $checksum checksum to store |
||
| 1122 | * @param int $keyIndex account that we expect to use |
||
| 1123 | * @param bool $segwit opt in to segwit |
||
| 1124 | * @return mixed |
||
| 1125 | */ |
||
| 1126 | public function storeNewWalletV1($identifier, $primaryPublicKey, $backupPublicKey, $primaryMnemonic, $checksum, $keyIndex, $segwit = false) { |
||
| 1140 | |||
| 1141 | /** |
||
| 1142 | * create wallet using the API |
||
| 1143 | * |
||
| 1144 | * @param string $identifier the wallet identifier to create |
||
| 1145 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1146 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1147 | * @param $encryptedPrimarySeed |
||
| 1148 | * @param $encryptedSecret |
||
| 1149 | * @param $recoverySecret |
||
| 1150 | * @param string $checksum checksum to store |
||
| 1151 | * @param int $keyIndex account that we expect to use |
||
| 1152 | * @param bool $segwit opt in to segwit |
||
| 1153 | * @return mixed |
||
| 1154 | * @throws \Exception |
||
| 1155 | */ |
||
| 1156 | public function storeNewWalletV2($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false) { |
||
| 1173 | |||
| 1174 | /** |
||
| 1175 | * create wallet using the API |
||
| 1176 | * |
||
| 1177 | * @param string $identifier the wallet identifier to create |
||
| 1178 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1179 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1180 | * @param $encryptedPrimarySeed |
||
| 1181 | * @param $encryptedSecret |
||
| 1182 | * @param $recoverySecret |
||
| 1183 | * @param string $checksum checksum to store |
||
| 1184 | * @param int $keyIndex account that we expect to use |
||
| 1185 | * @param bool $segwit opt in to segwit |
||
| 1186 | * @return mixed |
||
| 1187 | * @throws \Exception |
||
| 1188 | */ |
||
| 1189 | public function storeNewWalletV3($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false) { |
||
| 1208 | |||
| 1209 | /** |
||
| 1210 | * upgrade wallet to use a new account number |
||
| 1211 | * the account number specifies which blocktrail cosigning key is used |
||
| 1212 | * |
||
| 1213 | * @param string $identifier the wallet identifier to be upgraded |
||
| 1214 | * @param int $keyIndex the new account to use |
||
| 1215 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1216 | * @return mixed |
||
| 1217 | */ |
||
| 1218 | public function upgradeKeyIndex($identifier, $keyIndex, $primaryPublicKey) { |
||
| 1227 | |||
| 1228 | /** |
||
| 1229 | * @param array $options |
||
| 1230 | * @return AddressReaderBase |
||
| 1231 | */ |
||
| 1232 | private function makeAddressReader(array $options) { |
||
| 1243 | |||
| 1244 | /** |
||
| 1245 | * initialize a previously created wallet |
||
| 1246 | * |
||
| 1247 | * Takes an options object, or accepts identifier/password for backwards compatiblity. |
||
| 1248 | * |
||
| 1249 | * Some of the options: |
||
| 1250 | * - "readonly/readOnly/read-only" can be to a boolean value, |
||
| 1251 | * so the wallet is loaded in read-only mode (no private key) |
||
| 1252 | * - "check_backup_key" can be set to your own backup key: |
||
| 1253 | * Format: ["M', "xpub..."] |
||
| 1254 | * Setting this will allow the SDK to check the server hasn't |
||
| 1255 | * a different key (one it happens to control) |
||
| 1256 | |||
| 1257 | * Either takes one argument: |
||
| 1258 | * @param array $options |
||
| 1259 | * |
||
| 1260 | * Or takes two arguments (old, deprecated syntax): |
||
| 1261 | * (@nonPHP-doc) @param string $identifier the wallet identifier to be initialized |
||
| 1262 | * (@nonPHP-doc) @param string $password the password to decrypt the mnemonic with |
||
| 1263 | * |
||
| 1264 | * @return WalletInterface |
||
| 1265 | * @throws \Exception |
||
| 1266 | */ |
||
| 1267 | public function initWallet($options) { |
||
| 1379 | |||
| 1380 | /** |
||
| 1381 | * get the wallet data from the server |
||
| 1382 | * |
||
| 1383 | * @param string $identifier the identifier of the wallet |
||
| 1384 | * @return mixed |
||
| 1385 | */ |
||
| 1386 | public function getWallet($identifier) { |
||
| 1390 | |||
| 1391 | /** |
||
| 1392 | * update the wallet data on the server |
||
| 1393 | * |
||
| 1394 | * @param string $identifier |
||
| 1395 | * @param $data |
||
| 1396 | * @return mixed |
||
| 1397 | */ |
||
| 1398 | public function updateWallet($identifier, $data) { |
||
| 1402 | |||
| 1403 | /** |
||
| 1404 | * delete a wallet from the server |
||
| 1405 | * the checksum address and a signature to verify you ownership of the key of that checksum address |
||
| 1406 | * is required to be able to delete a wallet |
||
| 1407 | * |
||
| 1408 | * @param string $identifier the identifier of the wallet |
||
| 1409 | * @param string $checksumAddress the address for your master private key (and the checksum used when creating the wallet) |
||
| 1410 | * @param string $signature a signature of the checksum address as message signed by the private key matching that address |
||
| 1411 | * @param bool $force ignore warnings (such as a non-zero balance) |
||
| 1412 | * @return mixed |
||
| 1413 | */ |
||
| 1414 | public function deleteWallet($identifier, $checksumAddress, $signature, $force = false) { |
||
| 1421 | |||
| 1422 | /** |
||
| 1423 | * create new backup key; |
||
| 1424 | * 1) a BIP39 mnemonic |
||
| 1425 | * 2) a seed from that mnemonic with a blank password |
||
| 1426 | * 3) a private key from that seed |
||
| 1427 | * |
||
| 1428 | * @return array [mnemonic, seed, key] |
||
| 1429 | */ |
||
| 1430 | protected function newV1BackupSeed() { |
||
| 1435 | |||
| 1436 | /** |
||
| 1437 | * create new primary key; |
||
| 1438 | * 1) a BIP39 mnemonic |
||
| 1439 | * 2) a seed from that mnemonic with the password |
||
| 1440 | * 3) a private key from that seed |
||
| 1441 | * |
||
| 1442 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1443 | * @return array [mnemonic, seed, key] |
||
| 1444 | * @TODO: require a strong password? |
||
| 1445 | */ |
||
| 1446 | protected function newV1PrimarySeed($passphrase) { |
||
| 1451 | |||
| 1452 | /** |
||
| 1453 | * create a new key; |
||
| 1454 | * 1) a BIP39 mnemonic |
||
| 1455 | * 2) a seed from that mnemonic with the password |
||
| 1456 | * 3) a private key from that seed |
||
| 1457 | * |
||
| 1458 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1459 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1460 | * @return array |
||
| 1461 | */ |
||
| 1462 | protected function generateNewSeed($passphrase = "", $forceEntropy = null) { |
||
| 1479 | |||
| 1480 | /** |
||
| 1481 | * generate a new mnemonic from some random entropy (512 bit) |
||
| 1482 | * |
||
| 1483 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1484 | * @return string |
||
| 1485 | * @throws \Exception |
||
| 1486 | */ |
||
| 1487 | public function generateNewMnemonic($forceEntropy = null) { |
||
| 1497 | |||
| 1498 | /** |
||
| 1499 | * get the balance for the wallet |
||
| 1500 | * |
||
| 1501 | * @param string $identifier the identifier of the wallet |
||
| 1502 | * @return array |
||
| 1503 | */ |
||
| 1504 | public function getWalletBalance($identifier) { |
||
| 1508 | |||
| 1509 | /** |
||
| 1510 | * get a new derivation number for specified parent path |
||
| 1511 | * 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 |
||
| 1512 | * |
||
| 1513 | * returns the path |
||
| 1514 | * |
||
| 1515 | * @param string $identifier the identifier of the wallet |
||
| 1516 | * @param string $path the parent path for which to get a new derivation |
||
| 1517 | * @return string |
||
| 1518 | */ |
||
| 1519 | public function getNewDerivation($identifier, $path) { |
||
| 1523 | |||
| 1524 | /** |
||
| 1525 | * get a new derivation number for specified parent path |
||
| 1526 | * 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 |
||
| 1527 | * |
||
| 1528 | * @param string $identifier the identifier of the wallet |
||
| 1529 | * @param string $path the parent path for which to get a new derivation |
||
| 1530 | * @return mixed |
||
| 1531 | */ |
||
| 1532 | public function _getNewDerivation($identifier, $path) { |
||
| 1536 | |||
| 1537 | /** |
||
| 1538 | * get the path (and redeemScript) to specified address |
||
| 1539 | * |
||
| 1540 | * @param string $identifier |
||
| 1541 | * @param string $address |
||
| 1542 | * @return array |
||
| 1543 | * @throws \Exception |
||
| 1544 | */ |
||
| 1545 | public function getPathForAddress($identifier, $address) { |
||
| 1549 | |||
| 1550 | /** |
||
| 1551 | * send the transaction using the API |
||
| 1552 | * |
||
| 1553 | * @param string $identifier the identifier of the wallet |
||
| 1554 | * @param string|array $rawTransaction raw hex of the transaction (should be partially signed) |
||
| 1555 | * @param array $paths list of the paths that were used for the UTXO |
||
| 1556 | * @param bool $checkFee let the server verify the fee after signing |
||
| 1557 | * @param null $twoFactorToken |
||
| 1558 | * @return string the complete raw transaction |
||
| 1559 | * @throws \Exception |
||
| 1560 | */ |
||
| 1561 | public function sendTransaction($identifier, $rawTransaction, $paths, $checkFee = false, $twoFactorToken = null) { |
||
| 1592 | |||
| 1593 | /** |
||
| 1594 | * use the API to get the best inputs to use based on the outputs |
||
| 1595 | * |
||
| 1596 | * the return array has the following format: |
||
| 1597 | * [ |
||
| 1598 | * "utxos" => [ |
||
| 1599 | * [ |
||
| 1600 | * "hash" => "<txHash>", |
||
| 1601 | * "idx" => "<index of the output of that <txHash>", |
||
| 1602 | * "scriptpubkey_hex" => "<scriptPubKey-hex>", |
||
| 1603 | * "value" => 32746327, |
||
| 1604 | * "address" => "1address", |
||
| 1605 | * "path" => "m/44'/1'/0'/0/13", |
||
| 1606 | * "redeem_script" => "<redeemScript-hex>", |
||
| 1607 | * ], |
||
| 1608 | * ], |
||
| 1609 | * "fee" => 10000, |
||
| 1610 | * "change"=> 1010109201, |
||
| 1611 | * ] |
||
| 1612 | * |
||
| 1613 | * @param string $identifier the identifier of the wallet |
||
| 1614 | * @param array $outputs the outputs you want to create - array[address => satoshi-value] |
||
| 1615 | * @param bool $lockUTXO when TRUE the UTXOs selected will be locked for a few seconds |
||
| 1616 | * so you have some time to spend them without race-conditions |
||
| 1617 | * @param bool $allowZeroConf |
||
| 1618 | * @param string $feeStrategy |
||
| 1619 | * @param null|int $forceFee |
||
| 1620 | * @return array |
||
| 1621 | * @throws \Exception |
||
| 1622 | */ |
||
| 1623 | public function coinSelection($identifier, $outputs, $lockUTXO = false, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null) { |
||
| 1645 | |||
| 1646 | /** |
||
| 1647 | * |
||
| 1648 | * @param string $identifier the identifier of the wallet |
||
| 1649 | * @param bool $allowZeroConf |
||
| 1650 | * @param string $feeStrategy |
||
| 1651 | * @param null|int $forceFee |
||
| 1652 | * @param int $outputCnt |
||
| 1653 | * @return array |
||
| 1654 | * @throws \Exception |
||
| 1655 | */ |
||
| 1656 | public function walletMaxSpendable($identifier, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null, $outputCnt = 1) { |
||
| 1675 | |||
| 1676 | /** |
||
| 1677 | * @return array ['optimal_fee' => 10000, 'low_priority_fee' => 5000] |
||
| 1678 | */ |
||
| 1679 | public function feePerKB() { |
||
| 1683 | |||
| 1684 | /** |
||
| 1685 | * get the current price index |
||
| 1686 | * |
||
| 1687 | * @return array eg; ['USD' => 287.30] |
||
| 1688 | */ |
||
| 1689 | 2 | public function price() { |
|
| 1693 | |||
| 1694 | /** |
||
| 1695 | * setup webhook for wallet |
||
| 1696 | * |
||
| 1697 | * @param string $identifier the wallet identifier for which to create the webhook |
||
| 1698 | * @param string $webhookIdentifier the webhook identifier to use |
||
| 1699 | * @param string $url the url to receive the webhook events |
||
| 1700 | * @return array |
||
| 1701 | */ |
||
| 1702 | public function setupWalletWebhook($identifier, $webhookIdentifier, $url) { |
||
| 1706 | |||
| 1707 | /** |
||
| 1708 | * delete webhook for wallet |
||
| 1709 | * |
||
| 1710 | * @param string $identifier the wallet identifier for which to delete the webhook |
||
| 1711 | * @param string $webhookIdentifier the webhook identifier to delete |
||
| 1712 | * @return array |
||
| 1713 | */ |
||
| 1714 | public function deleteWalletWebhook($identifier, $webhookIdentifier) { |
||
| 1718 | |||
| 1719 | /** |
||
| 1720 | * lock a specific unspent output |
||
| 1721 | * |
||
| 1722 | * @param $identifier |
||
| 1723 | * @param $txHash |
||
| 1724 | * @param $txIdx |
||
| 1725 | * @param int $ttl |
||
| 1726 | * @return bool |
||
| 1727 | */ |
||
| 1728 | public function lockWalletUTXO($identifier, $txHash, $txIdx, $ttl = 3) { |
||
| 1732 | |||
| 1733 | /** |
||
| 1734 | * unlock a specific unspent output |
||
| 1735 | * |
||
| 1736 | * @param $identifier |
||
| 1737 | * @param $txHash |
||
| 1738 | * @param $txIdx |
||
| 1739 | * @return bool |
||
| 1740 | */ |
||
| 1741 | public function unlockWalletUTXO($identifier, $txHash, $txIdx) { |
||
| 1745 | |||
| 1746 | /** |
||
| 1747 | * get all transactions for wallet (paginated) |
||
| 1748 | * |
||
| 1749 | * @param string $identifier the wallet identifier for which to get transactions |
||
| 1750 | * @param integer $page pagination: page number |
||
| 1751 | * @param integer $limit pagination: records per page (max 500) |
||
| 1752 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1753 | * @return array associative array containing the response |
||
| 1754 | */ |
||
| 1755 | public function walletTransactions($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 1764 | |||
| 1765 | /** |
||
| 1766 | * get all addresses for wallet (paginated) |
||
| 1767 | * |
||
| 1768 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1769 | * @param integer $page pagination: page number |
||
| 1770 | * @param integer $limit pagination: records per page (max 500) |
||
| 1771 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1772 | * @return array associative array containing the response |
||
| 1773 | */ |
||
| 1774 | public function walletAddresses($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 1783 | |||
| 1784 | /** |
||
| 1785 | * get all UTXOs for wallet (paginated) |
||
| 1786 | * |
||
| 1787 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1788 | * @param integer $page pagination: page number |
||
| 1789 | * @param integer $limit pagination: records per page (max 500) |
||
| 1790 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1791 | * @param boolean $zeroconf include zero confirmation transactions |
||
| 1792 | * @return array associative array containing the response |
||
| 1793 | */ |
||
| 1794 | public function walletUTXOs($identifier, $page = 1, $limit = 20, $sortDir = 'asc', $zeroconf = true) { |
||
| 1804 | |||
| 1805 | /** |
||
| 1806 | * get a paginated list of all wallets associated with the api user |
||
| 1807 | * |
||
| 1808 | * @param integer $page pagination: page number |
||
| 1809 | * @param integer $limit pagination: records per page |
||
| 1810 | * @return array associative array containing the response |
||
| 1811 | */ |
||
| 1812 | public function allWallets($page = 1, $limit = 20) { |
||
| 1820 | |||
| 1821 | /** |
||
| 1822 | * send raw transaction |
||
| 1823 | * |
||
| 1824 | * @param $txHex |
||
| 1825 | * @return bool |
||
| 1826 | */ |
||
| 1827 | public function sendRawTransaction($txHex) { |
||
| 1831 | |||
| 1832 | /** |
||
| 1833 | * testnet only ;-) |
||
| 1834 | * |
||
| 1835 | * @param $address |
||
| 1836 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1837 | * @return mixed |
||
| 1838 | * @throws \Exception |
||
| 1839 | */ |
||
| 1840 | public function faucetWithdrawal($address, $amount = 10000) { |
||
| 1847 | |||
| 1848 | /** |
||
| 1849 | * Exists for BC. Remove at major bump. |
||
| 1850 | * |
||
| 1851 | * @see faucetWithdrawal |
||
| 1852 | * @deprecated |
||
| 1853 | * @param $address |
||
| 1854 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1855 | * @return mixed |
||
| 1856 | * @throws \Exception |
||
| 1857 | */ |
||
| 1858 | public function faucetWithdrawl($address, $amount = 10000) { |
||
| 1861 | |||
| 1862 | /** |
||
| 1863 | * verify a message signed bitcoin-core style |
||
| 1864 | * |
||
| 1865 | * @param string $message |
||
| 1866 | * @param string $address |
||
| 1867 | * @param string $signature |
||
| 1868 | * @return boolean |
||
| 1869 | */ |
||
| 1870 | 2 | public function verifyMessage($message, $address, $signature) { |
|
| 1884 | |||
| 1885 | /** |
||
| 1886 | * Take a base58 or cashaddress, and return only |
||
| 1887 | * the cash address. |
||
| 1888 | * This function only works on bitcoin cash. |
||
| 1889 | * @param string $input |
||
| 1890 | * @return string |
||
| 1891 | * @throws BlocktrailSDKException |
||
| 1892 | */ |
||
| 1893 | public function getLegacyBitcoinCashAddress($input) { |
||
| 1910 | |||
| 1911 | /** |
||
| 1912 | * convert a Satoshi value to a BTC value |
||
| 1913 | * |
||
| 1914 | * @param int $satoshi |
||
| 1915 | * @return float |
||
| 1916 | */ |
||
| 1917 | 1 | public static function toBTC($satoshi) { |
|
| 1920 | |||
| 1921 | /** |
||
| 1922 | * convert a Satoshi value to a BTC value and return it as a string |
||
| 1923 | |||
| 1924 | * @param int $satoshi |
||
| 1925 | * @return string |
||
| 1926 | */ |
||
| 1927 | public static function toBTCString($satoshi) { |
||
| 1930 | |||
| 1931 | /** |
||
| 1932 | * convert a BTC value to a Satoshi value |
||
| 1933 | * |
||
| 1934 | * @param float $btc |
||
| 1935 | * @return string |
||
| 1936 | */ |
||
| 1937 | 1 | public static function toSatoshiString($btc) { |
|
| 1940 | |||
| 1941 | /** |
||
| 1942 | * convert a BTC value to a Satoshi value |
||
| 1943 | * |
||
| 1944 | * @param float $btc |
||
| 1945 | * @return string |
||
| 1946 | */ |
||
| 1947 | 1 | public static function toSatoshi($btc) { |
|
| 1950 | |||
| 1951 | /** |
||
| 1952 | * json_decode helper that throws exceptions when it fails to decode |
||
| 1953 | * |
||
| 1954 | * @param $json |
||
| 1955 | * @param bool $assoc |
||
| 1956 | * @return mixed |
||
| 1957 | * @throws \Exception |
||
| 1958 | */ |
||
| 1959 | 4 | public static function jsonDecode($json, $assoc = false) { |
|
| 1972 | |||
| 1973 | /** |
||
| 1974 | * sort public keys for multisig script |
||
| 1975 | * |
||
| 1976 | * @param PublicKeyInterface[] $pubKeys |
||
| 1977 | * @return PublicKeyInterface[] |
||
| 1978 | */ |
||
| 1979 | public static function sortMultisigKeys(array $pubKeys) { |
||
| 1989 | |||
| 1990 | /** |
||
| 1991 | * read and decode the json payload from a webhook's POST request. |
||
| 1992 | * |
||
| 1993 | * @param bool $returnObject flag to indicate if an object or associative array should be returned |
||
| 1994 | * @return mixed|null |
||
| 1995 | * @throws \Exception |
||
| 1996 | */ |
||
| 1997 | public static function getWebhookPayload($returnObject = false) { |
||
| 2005 | |||
| 2006 | public static function normalizeBIP32KeyArray($keys) { |
||
| 2011 | |||
| 2012 | /** |
||
| 2013 | * @param array|BIP32Key $key |
||
| 2014 | * @return BIP32Key |
||
| 2015 | * @throws \Exception |
||
| 2016 | */ |
||
| 2017 | public static function normalizeBIP32Key($key) { |
||
| 2035 | |||
| 2036 | public function shuffle($arr) { |
||
| 2039 | } |
||
| 2040 |
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: