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 |
||
| 33 | class BlocktrailSDK implements BlocktrailSDKInterface { |
||
| 34 | /** |
||
| 35 | * @var Connection\RestClient |
||
| 36 | */ |
||
| 37 | protected $client; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string currently only supporting; bitcoin |
||
| 41 | */ |
||
| 42 | protected $network; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | protected $testnet; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param string $apiKey the API_KEY to use for authentication |
||
| 51 | * @param string $apiSecret the API_SECRET to use for authentication |
||
| 52 | * @param string $network the cryptocurrency 'network' to consume, eg BTC, LTC, etc |
||
| 53 | * @param bool $testnet testnet yes/no |
||
| 54 | * @param string $apiVersion the version of the API to consume |
||
| 55 | * @param null $apiEndpoint overwrite the endpoint used |
||
| 56 | * this will cause the $network, $testnet and $apiVersion to be ignored! |
||
| 57 | */ |
||
| 58 | 92 | public function __construct($apiKey, $apiSecret, $network = 'BTC', $testnet = false, $apiVersion = 'v1', $apiEndpoint = null) { |
|
| 73 | |||
| 74 | /** |
||
| 75 | * normalize network string |
||
| 76 | * |
||
| 77 | * @param $network |
||
| 78 | * @param $testnet |
||
| 79 | * @return array |
||
| 80 | * @throws \Exception |
||
| 81 | */ |
||
| 82 | 92 | protected function normalizeNetwork($network, $testnet) { |
|
| 85 | |||
| 86 | /** |
||
| 87 | * set BitcoinLib to the correct magic-byte defaults for the selected network |
||
| 88 | * |
||
| 89 | * @param $network |
||
| 90 | * @param $testnet |
||
| 91 | */ |
||
| 92 | 92 | protected function setBitcoinLibMagicBytes($network, $testnet) { |
|
| 96 | |||
| 97 | /** |
||
| 98 | * enable CURL debugging output |
||
| 99 | * |
||
| 100 | * @param bool $debug |
||
| 101 | * |
||
| 102 | * @codeCoverageIgnore |
||
| 103 | */ |
||
| 104 | public function setCurlDebugging($debug = true) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * enable verbose errors |
||
| 110 | * |
||
| 111 | * @param bool $verboseErrors |
||
| 112 | * |
||
| 113 | * @codeCoverageIgnore |
||
| 114 | */ |
||
| 115 | public function setVerboseErrors($verboseErrors = true) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * set cURL default option on Guzzle client |
||
| 121 | * @param string $key |
||
| 122 | * @param bool $value |
||
| 123 | * |
||
| 124 | * @codeCoverageIgnore |
||
| 125 | */ |
||
| 126 | public function setCurlDefaultOption($key, $value) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return RestClient |
||
| 132 | */ |
||
| 133 | 2 | public function getRestClient() { |
|
| 136 | |||
| 137 | /** |
||
| 138 | * get a single address |
||
| 139 | * @param string $address address hash |
||
| 140 | * @return array associative array containing the response |
||
| 141 | */ |
||
| 142 | 1 | public function address($address) { |
|
| 146 | |||
| 147 | /** |
||
| 148 | * get all transactions for an address (paginated) |
||
| 149 | * @param string $address address hash |
||
| 150 | * @param integer $page pagination: page number |
||
| 151 | * @param integer $limit pagination: records per page (max 500) |
||
| 152 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 153 | * @return array associative array containing the response |
||
| 154 | */ |
||
| 155 | 1 | public function addressTransactions($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 164 | |||
| 165 | /** |
||
| 166 | * get all unconfirmed transactions for an address (paginated) |
||
| 167 | * @param string $address address hash |
||
| 168 | * @param integer $page pagination: page number |
||
| 169 | * @param integer $limit pagination: records per page (max 500) |
||
| 170 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 171 | * @return array associative array containing the response |
||
| 172 | */ |
||
| 173 | 1 | public function addressUnconfirmedTransactions($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 182 | |||
| 183 | /** |
||
| 184 | * get all unspent outputs for an address (paginated) |
||
| 185 | * @param string $address address hash |
||
| 186 | * @param integer $page pagination: page number |
||
| 187 | * @param integer $limit pagination: records per page (max 500) |
||
| 188 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 189 | * @return array associative array containing the response |
||
| 190 | */ |
||
| 191 | 1 | public function addressUnspentOutputs($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 200 | |||
| 201 | /** |
||
| 202 | * get all unspent outputs for a batch of addresses (paginated) |
||
| 203 | * |
||
| 204 | * @param string[] $addresses |
||
| 205 | * @param integer $page pagination: page number |
||
| 206 | * @param integer $limit pagination: records per page (max 500) |
||
| 207 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 208 | * @return array associative array containing the response |
||
| 209 | * @throws \Exception |
||
| 210 | */ |
||
| 211 | public function batchAddressUnspentOutputs($addresses, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * verify ownership of an address |
||
| 223 | * @param string $address address hash |
||
| 224 | * @param string $signature a signed message (the address hash) using the private key of the address |
||
| 225 | * @return array associative array containing the response |
||
| 226 | */ |
||
| 227 | 2 | public function verifyAddress($address, $signature) { |
|
| 234 | |||
| 235 | /** |
||
| 236 | * get all blocks (paginated) |
||
| 237 | * @param integer $page pagination: page number |
||
| 238 | * @param integer $limit pagination: records per page |
||
| 239 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 240 | * @return array associative array containing the response |
||
| 241 | */ |
||
| 242 | 1 | public function allBlocks($page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 251 | |||
| 252 | /** |
||
| 253 | * get the latest block |
||
| 254 | * @return array associative array containing the response |
||
| 255 | */ |
||
| 256 | 1 | public function blockLatest() { |
|
| 260 | |||
| 261 | /** |
||
| 262 | * get an individual block |
||
| 263 | * @param string|integer $block a block hash or a block height |
||
| 264 | * @return array associative array containing the response |
||
| 265 | */ |
||
| 266 | 1 | public function block($block) { |
|
| 270 | |||
| 271 | /** |
||
| 272 | * get all transaction in a block (paginated) |
||
| 273 | * @param string|integer $block a block hash or a block height |
||
| 274 | * @param integer $page pagination: page number |
||
| 275 | * @param integer $limit pagination: records per page |
||
| 276 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 277 | * @return array associative array containing the response |
||
| 278 | */ |
||
| 279 | 1 | public function blockTransactions($block, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 288 | |||
| 289 | /** |
||
| 290 | * get a single transaction |
||
| 291 | * @param string $txhash transaction hash |
||
| 292 | * @return array associative array containing the response |
||
| 293 | */ |
||
| 294 | 5 | public function transaction($txhash) { |
|
| 298 | |||
| 299 | /** |
||
| 300 | * get a single transaction |
||
| 301 | * @param string[] $txhashes list of transaction hashes (up to 20) |
||
| 302 | * @return array[] array containing the response |
||
| 303 | */ |
||
| 304 | public function transactions($txhashes) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * get a paginated list of all webhooks associated with the api user |
||
| 311 | * @param integer $page pagination: page number |
||
| 312 | * @param integer $limit pagination: records per page |
||
| 313 | * @return array associative array containing the response |
||
| 314 | */ |
||
| 315 | 1 | public function allWebhooks($page = 1, $limit = 20) { |
|
| 323 | |||
| 324 | /** |
||
| 325 | * get an existing webhook by it's identifier |
||
| 326 | * @param string $identifier a unique identifier associated with the webhook |
||
| 327 | * @return array associative array containing the response |
||
| 328 | */ |
||
| 329 | 1 | public function getWebhook($identifier) { |
|
| 333 | |||
| 334 | /** |
||
| 335 | * create a new webhook |
||
| 336 | * @param string $url the url to receive the webhook events |
||
| 337 | * @param string $identifier a unique identifier to associate with this webhook |
||
| 338 | * @return array associative array containing the response |
||
| 339 | */ |
||
| 340 | 1 | public function setupWebhook($url, $identifier = null) { |
|
| 348 | |||
| 349 | /** |
||
| 350 | * update an existing webhook |
||
| 351 | * @param string $identifier the unique identifier of the webhook to update |
||
| 352 | * @param string $newUrl the new url to receive the webhook events |
||
| 353 | * @param string $newIdentifier a new unique identifier to associate with this webhook |
||
| 354 | * @return array associative array containing the response |
||
| 355 | */ |
||
| 356 | 1 | public function updateWebhook($identifier, $newUrl = null, $newIdentifier = null) { |
|
| 364 | |||
| 365 | /** |
||
| 366 | * deletes an existing webhook and any event subscriptions associated with it |
||
| 367 | * @param string $identifier the unique identifier of the webhook to delete |
||
| 368 | * @return boolean true on success |
||
| 369 | */ |
||
| 370 | 1 | public function deleteWebhook($identifier) { |
|
| 374 | |||
| 375 | /** |
||
| 376 | * get a paginated list of all the events a webhook is subscribed to |
||
| 377 | * @param string $identifier the unique identifier of the webhook |
||
| 378 | * @param integer $page pagination: page number |
||
| 379 | * @param integer $limit pagination: records per page |
||
| 380 | * @return array associative array containing the response |
||
| 381 | */ |
||
| 382 | 2 | public function getWebhookEvents($identifier, $page = 1, $limit = 20) { |
|
| 390 | |||
| 391 | /** |
||
| 392 | * subscribes a webhook to transaction events of one particular transaction |
||
| 393 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 394 | * @param string $transaction the transaction hash |
||
| 395 | * @param integer $confirmations the amount of confirmations to send. |
||
| 396 | * @return array associative array containing the response |
||
| 397 | */ |
||
| 398 | 1 | public function subscribeTransaction($identifier, $transaction, $confirmations = 6) { |
|
| 407 | |||
| 408 | /** |
||
| 409 | * subscribes a webhook to transaction events on a particular address |
||
| 410 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 411 | * @param string $address the address hash |
||
| 412 | * @param integer $confirmations the amount of confirmations to send. |
||
| 413 | * @return array associative array containing the response |
||
| 414 | */ |
||
| 415 | 1 | public function subscribeAddressTransactions($identifier, $address, $confirmations = 6) { |
|
| 424 | |||
| 425 | /** |
||
| 426 | * batch subscribes a webhook to multiple transaction events |
||
| 427 | * |
||
| 428 | * @param string $identifier the unique identifier of the webhook |
||
| 429 | * @param array $batchData A 2D array of event data: |
||
| 430 | * [address => $address, confirmations => $confirmations] |
||
| 431 | * where $address is the address to subscibe to |
||
| 432 | * and optionally $confirmations is the amount of confirmations |
||
| 433 | * @return boolean true on success |
||
| 434 | */ |
||
| 435 | 1 | public function batchSubscribeAddressTransactions($identifier, $batchData) { |
|
| 447 | |||
| 448 | /** |
||
| 449 | * subscribes a webhook to a new block event |
||
| 450 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 451 | * @return array associative array containing the response |
||
| 452 | */ |
||
| 453 | 1 | public function subscribeNewBlocks($identifier) { |
|
| 460 | |||
| 461 | /** |
||
| 462 | * removes an transaction event subscription from a webhook |
||
| 463 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 464 | * @param string $transaction the transaction hash of the event subscription |
||
| 465 | * @return boolean true on success |
||
| 466 | */ |
||
| 467 | 1 | public function unsubscribeTransaction($identifier, $transaction) { |
|
| 471 | |||
| 472 | /** |
||
| 473 | * removes an address transaction event subscription from a webhook |
||
| 474 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 475 | * @param string $address the address hash of the event subscription |
||
| 476 | * @return boolean true on success |
||
| 477 | */ |
||
| 478 | 1 | public function unsubscribeAddressTransactions($identifier, $address) { |
|
| 482 | |||
| 483 | /** |
||
| 484 | * removes a block event subscription from a webhook |
||
| 485 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 486 | * @return boolean true on success |
||
| 487 | */ |
||
| 488 | 1 | public function unsubscribeNewBlocks($identifier) { |
|
| 492 | |||
| 493 | /** |
||
| 494 | * create a new wallet |
||
| 495 | * - will generate a new primary seed (with password) and backup seed (without password) |
||
| 496 | * - send the primary seed (BIP39 'encrypted') and backup public key to the server |
||
| 497 | * - receive the blocktrail co-signing public key from the server |
||
| 498 | * |
||
| 499 | * Either takes one argument: |
||
| 500 | * @param array $options |
||
| 501 | * |
||
| 502 | * Or takes three arguments (old, deprecated syntax): |
||
| 503 | * (@nonPHP-doc) @param $identifier |
||
| 504 | * (@nonPHP-doc) @param $password |
||
| 505 | * (@nonPHP-doc) @param int $keyIndex override for the blocktrail cosigning key to use |
||
|
|
|||
| 506 | * |
||
| 507 | * @return array[WalletInterface, array] list($wallet, $backupInfo) |
||
| 508 | * @throws \Exception |
||
| 509 | */ |
||
| 510 | 7 | public function createNewWallet($options) { |
|
| 554 | |||
| 555 | 1 | protected function createNewWalletV1($options) { |
|
| 556 | 1 | $walletPath = WalletPath::create($options['key_index']); |
|
| 557 | |||
| 558 | 1 | $storePrimaryMnemonic = isset($options['store_primary_mnemonic']) ? $options['store_primary_mnemonic'] : null; |
|
| 559 | |||
| 560 | 1 | if (isset($options['primary_mnemonic']) && isset($options['primary_private_key'])) { |
|
| 561 | throw new \InvalidArgumentException("Can't specify Primary Mnemonic and Primary PrivateKey"); |
||
| 562 | } |
||
| 563 | |||
| 564 | 1 | $primaryMnemonic = null; |
|
| 565 | 1 | $primaryPrivateKey = null; |
|
| 566 | 1 | if (!isset($options['primary_mnemonic']) && !isset($options['primary_private_key'])) { |
|
| 567 | 1 | if (!$options['passphrase']) { |
|
| 568 | throw new \InvalidArgumentException("Can't generate Primary Mnemonic without a passphrase"); |
||
| 569 | } else { |
||
| 570 | // create new primary seed |
||
| 571 | /** @var HierarchicalKey $primaryPrivateKey */ |
||
| 572 | 1 | list($primaryMnemonic, , $primaryPrivateKey) = $this->newPrimarySeed($options['passphrase']); |
|
| 573 | 1 | if ($storePrimaryMnemonic !== false) { |
|
| 574 | 1 | $storePrimaryMnemonic = true; |
|
| 575 | } |
||
| 576 | } |
||
| 577 | } elseif (isset($options['primary_mnemonic'])) { |
||
| 578 | $primaryMnemonic = $options['primary_mnemonic']; |
||
| 579 | } elseif (isset($options['primary_private_key'])) { |
||
| 580 | $primaryPrivateKey = $options['primary_private_key']; |
||
| 581 | } |
||
| 582 | |||
| 583 | 1 | if ($storePrimaryMnemonic && $primaryMnemonic && !$options['passphrase']) { |
|
| 584 | throw new \InvalidArgumentException("Can't store Primary Mnemonic on server without a passphrase"); |
||
| 585 | } |
||
| 586 | |||
| 587 | 1 | if ($primaryPrivateKey) { |
|
| 588 | 1 | if (is_string($primaryPrivateKey)) { |
|
| 589 | 1 | $primaryPrivateKey = [$primaryPrivateKey, "m"]; |
|
| 590 | } |
||
| 591 | } else { |
||
| 592 | $primaryPrivateKey = HierarchicalKeyFactory::fromEntropy((new Bip39SeedGenerator())->getSeed($primaryMnemonic, $options['passphrase'])); |
||
| 593 | } |
||
| 594 | |||
| 595 | 1 | if (!$storePrimaryMnemonic) { |
|
| 596 | $primaryMnemonic = false; |
||
| 597 | } |
||
| 598 | |||
| 599 | // create primary public key from the created private key |
||
| 600 | 1 | $path = $walletPath->keyIndexPath()->publicPath(); |
|
| 601 | 1 | $primaryPublicKey = BIP32Key::create($primaryPrivateKey, "m")->buildKey($path); |
|
| 602 | |||
| 603 | 1 | if (isset($options['backup_mnemonic']) && $options['backup_public_key']) { |
|
| 604 | throw new \InvalidArgumentException("Can't specify Backup Mnemonic and Backup PublicKey"); |
||
| 605 | } |
||
| 606 | |||
| 607 | 1 | $backupMnemonic = null; |
|
| 608 | 1 | $backupPublicKey = null; |
|
| 609 | 1 | if (!isset($options['backup_mnemonic']) && !isset($options['backup_public_key'])) { |
|
| 610 | /** @var HierarchicalKey $backupPrivateKey */ |
||
| 611 | 1 | list($backupMnemonic, , ) = $this->newBackupSeed(); |
|
| 612 | } else if (isset($options['backup_mnemonic'])) { |
||
| 613 | $backupMnemonic = $options['backup_mnemonic']; |
||
| 614 | } elseif (isset($options['backup_public_key'])) { |
||
| 615 | $backupPublicKey = $options['backup_public_key']; |
||
| 616 | } |
||
| 617 | |||
| 618 | 1 | if ($backupPublicKey) { |
|
| 619 | if (is_string($backupPublicKey)) { |
||
| 620 | $backupPublicKey = [$backupPublicKey, "m"]; |
||
| 621 | } |
||
| 622 | } else { |
||
| 623 | 1 | $backupPrivateKey = HierarchicalKeyFactory::fromEntropy((new Bip39SeedGenerator())->getSeed($backupMnemonic, "")); |
|
| 624 | 1 | $backupPublicKey = BIP32Key::create($backupPrivateKey->toPublic(), "M"); |
|
| 625 | } |
||
| 626 | |||
| 627 | // create a checksum of our private key which we'll later use to verify we used the right password |
||
| 628 | 1 | $checksum = $primaryPrivateKey->getPublicKey()->getAddress()->getAddress(); |
|
| 629 | |||
| 630 | // send the public keys to the server to store them |
||
| 631 | // and the mnemonic, which is safe because it's useless without the password |
||
| 632 | 1 | $data = $this->storeNewWalletV1( |
|
| 633 | 1 | $options['identifier'], |
|
| 634 | 1 | $primaryPublicKey->tuple(), |
|
| 635 | 1 | $backupPublicKey->tuple(), |
|
| 636 | 1 | $primaryMnemonic, |
|
| 637 | 1 | $checksum, |
|
| 638 | 1 | $options['key_index'], |
|
| 639 | 1 | array_key_exists('segwit', $options) ? $options['segwit'] : false |
|
| 640 | ); |
||
| 641 | |||
| 642 | // received the blocktrail public keys |
||
| 643 | $blocktrailPublicKeys = Util::arrayMapWithIndex(function ($keyIndex, $pubKeyTuple) { |
||
| 644 | 1 | return [$keyIndex, BIP32Key::create(HierarchicalKeyFactory::fromExtended($pubKeyTuple[0]), $pubKeyTuple[1])]; |
|
| 645 | 1 | }, $data['blocktrail_public_keys']); |
|
| 646 | |||
| 647 | 1 | $wallet = new WalletV1( |
|
| 648 | 1 | $this, |
|
| 649 | 1 | $options['identifier'], |
|
| 650 | 1 | $primaryMnemonic, |
|
| 651 | 1 | [$options['key_index'] => $primaryPublicKey], |
|
| 652 | 1 | $backupPublicKey, |
|
| 653 | 1 | $blocktrailPublicKeys, |
|
| 654 | 1 | $options['key_index'], |
|
| 655 | 1 | $this->network, |
|
| 656 | 1 | $this->testnet, |
|
| 657 | 1 | array_key_exists('segwit', $data) ? $data['segwit'] : false, |
|
| 658 | 1 | $checksum |
|
| 659 | ); |
||
| 660 | |||
| 661 | 1 | $wallet->unlock($options); |
|
| 662 | |||
| 663 | // return wallet and backup mnemonic |
||
| 664 | return [ |
||
| 665 | 1 | $wallet, |
|
| 666 | [ |
||
| 667 | 1 | 'primary_mnemonic' => $primaryMnemonic, |
|
| 668 | 1 | 'backup_mnemonic' => $backupMnemonic, |
|
| 669 | 1 | 'blocktrail_public_keys' => $blocktrailPublicKeys, |
|
| 670 | ], |
||
| 671 | ]; |
||
| 672 | } |
||
| 673 | |||
| 674 | 5 | public static function randomBits($bits) { |
|
| 677 | |||
| 678 | 5 | public static function randomBytes($bytes) { |
|
| 681 | |||
| 682 | 2 | protected function createNewWalletV2($options) { |
|
| 803 | |||
| 804 | 4 | protected function createNewWalletV3($options) { |
|
| 943 | |||
| 944 | /** |
||
| 945 | * @param array $bip32Key |
||
| 946 | * @throws BlocktrailSDKException |
||
| 947 | */ |
||
| 948 | 10 | private function verifyPublicBIP32Key(array $bip32Key) { |
|
| 958 | |||
| 959 | /** |
||
| 960 | * @param array $walletData |
||
| 961 | * @throws BlocktrailSDKException |
||
| 962 | */ |
||
| 963 | 10 | private function verifyPublicOnly(array $walletData) { |
|
| 967 | |||
| 968 | /** |
||
| 969 | * create wallet using the API |
||
| 970 | * |
||
| 971 | * @param string $identifier the wallet identifier to create |
||
| 972 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 973 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 974 | * @param string $primaryMnemonic mnemonic to store |
||
| 975 | * @param string $checksum checksum to store |
||
| 976 | * @param int $keyIndex account that we expect to use |
||
| 977 | * @param bool $segwit opt in to segwit |
||
| 978 | * @return mixed |
||
| 979 | */ |
||
| 980 | 1 | public function storeNewWalletV1($identifier, $primaryPublicKey, $backupPublicKey, $primaryMnemonic, $checksum, $keyIndex, $segwit = false) { |
|
| 994 | |||
| 995 | /** |
||
| 996 | * create wallet using the API |
||
| 997 | * |
||
| 998 | * @param string $identifier the wallet identifier to create |
||
| 999 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1000 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1001 | * @param $encryptedPrimarySeed |
||
| 1002 | * @param $encryptedSecret |
||
| 1003 | * @param $recoverySecret |
||
| 1004 | * @param string $checksum checksum to store |
||
| 1005 | * @param int $keyIndex account that we expect to use |
||
| 1006 | * @param bool $segwit opt in to segwit |
||
| 1007 | * @return mixed |
||
| 1008 | * @throws \Exception |
||
| 1009 | */ |
||
| 1010 | 5 | public function storeNewWalletV2($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false) { |
|
| 1027 | |||
| 1028 | /** |
||
| 1029 | * create wallet using the API |
||
| 1030 | * |
||
| 1031 | * @param string $identifier the wallet identifier to create |
||
| 1032 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1033 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1034 | * @param $encryptedPrimarySeed |
||
| 1035 | * @param $encryptedSecret |
||
| 1036 | * @param $recoverySecret |
||
| 1037 | * @param string $checksum checksum to store |
||
| 1038 | * @param int $keyIndex account that we expect to use |
||
| 1039 | * @param bool $segwit opt in to segwit |
||
| 1040 | * @return mixed |
||
| 1041 | * @throws \Exception |
||
| 1042 | */ |
||
| 1043 | 4 | public function storeNewWalletV3($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false) { |
|
| 1062 | |||
| 1063 | /** |
||
| 1064 | * upgrade wallet to use a new account number |
||
| 1065 | * the account number specifies which blocktrail cosigning key is used |
||
| 1066 | * |
||
| 1067 | * @param string $identifier the wallet identifier to be upgraded |
||
| 1068 | * @param int $keyIndex the new account to use |
||
| 1069 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1070 | * @return mixed |
||
| 1071 | */ |
||
| 1072 | 3 | public function upgradeKeyIndex($identifier, $keyIndex, $primaryPublicKey) { |
|
| 1081 | |||
| 1082 | /** |
||
| 1083 | * initialize a previously created wallet |
||
| 1084 | * |
||
| 1085 | * Takes an options object, or accepts identifier/password for backwards compatiblity. |
||
| 1086 | * |
||
| 1087 | * Some of the options: |
||
| 1088 | * - "readonly/readOnly/read-only" can be to a boolean value, |
||
| 1089 | * so the wallet is loaded in read-only mode (no private key) |
||
| 1090 | * - "check_backup_key" can be set to your own backup key: |
||
| 1091 | * Format: ["M', "xpub..."] |
||
| 1092 | * Setting this will allow the SDK to check the server hasn't |
||
| 1093 | * a different key (one it happens to control) |
||
| 1094 | |||
| 1095 | * Either takes one argument: |
||
| 1096 | * @param array $options |
||
| 1097 | * |
||
| 1098 | * Or takes two arguments (old, deprecated syntax): |
||
| 1099 | * (@nonPHP-doc) @param string $identifier the wallet identifier to be initialized |
||
| 1100 | * (@nonPHP-doc) @param string $password the password to decrypt the mnemonic with |
||
| 1101 | * |
||
| 1102 | * @return WalletInterface |
||
| 1103 | * @throws \Exception |
||
| 1104 | */ |
||
| 1105 | 19 | public function initWallet($options) { |
|
| 1212 | |||
| 1213 | /** |
||
| 1214 | * get the wallet data from the server |
||
| 1215 | * |
||
| 1216 | * @param string $identifier the identifier of the wallet |
||
| 1217 | * @return mixed |
||
| 1218 | */ |
||
| 1219 | 19 | public function getWallet($identifier) { |
|
| 1223 | |||
| 1224 | /** |
||
| 1225 | * update the wallet data on the server |
||
| 1226 | * |
||
| 1227 | * @param string $identifier |
||
| 1228 | * @param $data |
||
| 1229 | * @return mixed |
||
| 1230 | */ |
||
| 1231 | 2 | public function updateWallet($identifier, $data) { |
|
| 1235 | |||
| 1236 | /** |
||
| 1237 | * delete a wallet from the server |
||
| 1238 | * the checksum address and a signature to verify you ownership of the key of that checksum address |
||
| 1239 | * is required to be able to delete a wallet |
||
| 1240 | * |
||
| 1241 | * @param string $identifier the identifier of the wallet |
||
| 1242 | * @param string $checksumAddress the address for your master private key (and the checksum used when creating the wallet) |
||
| 1243 | * @param string $signature a signature of the checksum address as message signed by the private key matching that address |
||
| 1244 | * @param bool $force ignore warnings (such as a non-zero balance) |
||
| 1245 | * @return mixed |
||
| 1246 | */ |
||
| 1247 | 10 | public function deleteWallet($identifier, $checksumAddress, $signature, $force = false) { |
|
| 1254 | |||
| 1255 | /** |
||
| 1256 | * create new backup key; |
||
| 1257 | * 1) a BIP39 mnemonic |
||
| 1258 | * 2) a seed from that mnemonic with a blank password |
||
| 1259 | * 3) a private key from that seed |
||
| 1260 | * |
||
| 1261 | * @return array [mnemonic, seed, key] |
||
| 1262 | */ |
||
| 1263 | 1 | protected function newBackupSeed() { |
|
| 1268 | |||
| 1269 | /** |
||
| 1270 | * create new primary key; |
||
| 1271 | * 1) a BIP39 mnemonic |
||
| 1272 | * 2) a seed from that mnemonic with the password |
||
| 1273 | * 3) a private key from that seed |
||
| 1274 | * |
||
| 1275 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1276 | * @return array [mnemonic, seed, key] |
||
| 1277 | * @TODO: require a strong password? |
||
| 1278 | */ |
||
| 1279 | 1 | protected function newPrimarySeed($passphrase) { |
|
| 1284 | |||
| 1285 | /** |
||
| 1286 | * create a new key; |
||
| 1287 | * 1) a BIP39 mnemonic |
||
| 1288 | * 2) a seed from that mnemonic with the password |
||
| 1289 | * 3) a private key from that seed |
||
| 1290 | * |
||
| 1291 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1292 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1293 | * @return array |
||
| 1294 | */ |
||
| 1295 | 1 | protected function generateNewSeed($passphrase = "", $forceEntropy = null) { |
|
| 1312 | |||
| 1313 | /** |
||
| 1314 | * generate a new mnemonic from some random entropy (512 bit) |
||
| 1315 | * |
||
| 1316 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1317 | * @return string |
||
| 1318 | * @throws \Exception |
||
| 1319 | */ |
||
| 1320 | 1 | protected function generateNewMnemonic($forceEntropy = null) { |
|
| 1330 | |||
| 1331 | /** |
||
| 1332 | * get the balance for the wallet |
||
| 1333 | * |
||
| 1334 | * @param string $identifier the identifier of the wallet |
||
| 1335 | * @return array |
||
| 1336 | */ |
||
| 1337 | 9 | public function getWalletBalance($identifier) { |
|
| 1341 | |||
| 1342 | /** |
||
| 1343 | * do HD wallet discovery for the wallet |
||
| 1344 | * |
||
| 1345 | * this can be REALLY slow, so we've set the timeout to 120s ... |
||
| 1346 | * |
||
| 1347 | * @param string $identifier the identifier of the wallet |
||
| 1348 | * @param int $gap the gap setting to use for discovery |
||
| 1349 | * @return mixed |
||
| 1350 | */ |
||
| 1351 | 2 | public function doWalletDiscovery($identifier, $gap = 200) { |
|
| 1355 | |||
| 1356 | /** |
||
| 1357 | * get a new derivation number for specified parent path |
||
| 1358 | * 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 |
||
| 1359 | * |
||
| 1360 | * returns the path |
||
| 1361 | * |
||
| 1362 | * @param string $identifier the identifier of the wallet |
||
| 1363 | * @param string $path the parent path for which to get a new derivation |
||
| 1364 | * @return string |
||
| 1365 | */ |
||
| 1366 | 1 | public function getNewDerivation($identifier, $path) { |
|
| 1370 | |||
| 1371 | /** |
||
| 1372 | * get a new derivation number for specified parent path |
||
| 1373 | * 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 |
||
| 1374 | * |
||
| 1375 | * @param string $identifier the identifier of the wallet |
||
| 1376 | * @param string $path the parent path for which to get a new derivation |
||
| 1377 | * @return mixed |
||
| 1378 | */ |
||
| 1379 | 14 | public function _getNewDerivation($identifier, $path) { |
|
| 1383 | |||
| 1384 | /** |
||
| 1385 | * get the path (and redeemScript) to specified address |
||
| 1386 | * |
||
| 1387 | * @param string $identifier |
||
| 1388 | * @param string $address |
||
| 1389 | * @return array |
||
| 1390 | * @throws \Exception |
||
| 1391 | */ |
||
| 1392 | 1 | public function getPathForAddress($identifier, $address) { |
|
| 1396 | |||
| 1397 | /** |
||
| 1398 | * send the transaction using the API |
||
| 1399 | * |
||
| 1400 | * @param string $identifier the identifier of the wallet |
||
| 1401 | * @param string|array $rawTransaction raw hex of the transaction (should be partially signed) |
||
| 1402 | * @param array $paths list of the paths that were used for the UTXO |
||
| 1403 | * @param bool $checkFee let the server verify the fee after signing |
||
| 1404 | * @return string the complete raw transaction |
||
| 1405 | * @throws \Exception |
||
| 1406 | */ |
||
| 1407 | 4 | public function sendTransaction($identifier, $rawTransaction, $paths, $checkFee = false) { |
|
| 1437 | |||
| 1438 | /** |
||
| 1439 | * use the API to get the best inputs to use based on the outputs |
||
| 1440 | * |
||
| 1441 | * the return array has the following format: |
||
| 1442 | * [ |
||
| 1443 | * "utxos" => [ |
||
| 1444 | * [ |
||
| 1445 | * "hash" => "<txHash>", |
||
| 1446 | * "idx" => "<index of the output of that <txHash>", |
||
| 1447 | * "scriptpubkey_hex" => "<scriptPubKey-hex>", |
||
| 1448 | * "value" => 32746327, |
||
| 1449 | * "address" => "1address", |
||
| 1450 | * "path" => "m/44'/1'/0'/0/13", |
||
| 1451 | * "redeem_script" => "<redeemScript-hex>", |
||
| 1452 | * ], |
||
| 1453 | * ], |
||
| 1454 | * "fee" => 10000, |
||
| 1455 | * "change"=> 1010109201, |
||
| 1456 | * ] |
||
| 1457 | * |
||
| 1458 | * @param string $identifier the identifier of the wallet |
||
| 1459 | * @param array $outputs the outputs you want to create - array[address => satoshi-value] |
||
| 1460 | * @param bool $lockUTXO when TRUE the UTXOs selected will be locked for a few seconds |
||
| 1461 | * so you have some time to spend them without race-conditions |
||
| 1462 | * @param bool $allowZeroConf |
||
| 1463 | * @param string $feeStrategy |
||
| 1464 | * @param null|int $forceFee |
||
| 1465 | * @return array |
||
| 1466 | * @throws \Exception |
||
| 1467 | */ |
||
| 1468 | 10 | public function coinSelection($identifier, $outputs, $lockUTXO = false, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null) { |
|
| 1488 | |||
| 1489 | /** |
||
| 1490 | * |
||
| 1491 | * @param string $identifier the identifier of the wallet |
||
| 1492 | * @param bool $allowZeroConf |
||
| 1493 | * @param string $feeStrategy |
||
| 1494 | * @param null|int $forceFee |
||
| 1495 | * @param int $outputCnt |
||
| 1496 | * @return array |
||
| 1497 | * @throws \Exception |
||
| 1498 | */ |
||
| 1499 | public function walletMaxSpendable($identifier, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null, $outputCnt = 1) { |
||
| 1518 | |||
| 1519 | /** |
||
| 1520 | * @return array ['optimal_fee' => 10000, 'low_priority_fee' => 5000] |
||
| 1521 | */ |
||
| 1522 | 3 | public function feePerKB() { |
|
| 1526 | |||
| 1527 | /** |
||
| 1528 | * get the current price index |
||
| 1529 | * |
||
| 1530 | * @return array eg; ['USD' => 287.30] |
||
| 1531 | */ |
||
| 1532 | 1 | public function price() { |
|
| 1536 | |||
| 1537 | /** |
||
| 1538 | * setup webhook for wallet |
||
| 1539 | * |
||
| 1540 | * @param string $identifier the wallet identifier for which to create the webhook |
||
| 1541 | * @param string $webhookIdentifier the webhook identifier to use |
||
| 1542 | * @param string $url the url to receive the webhook events |
||
| 1543 | * @return array |
||
| 1544 | */ |
||
| 1545 | 1 | public function setupWalletWebhook($identifier, $webhookIdentifier, $url) { |
|
| 1549 | |||
| 1550 | /** |
||
| 1551 | * delete webhook for wallet |
||
| 1552 | * |
||
| 1553 | * @param string $identifier the wallet identifier for which to delete the webhook |
||
| 1554 | * @param string $webhookIdentifier the webhook identifier to delete |
||
| 1555 | * @return array |
||
| 1556 | */ |
||
| 1557 | 1 | public function deleteWalletWebhook($identifier, $webhookIdentifier) { |
|
| 1561 | |||
| 1562 | /** |
||
| 1563 | * lock a specific unspent output |
||
| 1564 | * |
||
| 1565 | * @param $identifier |
||
| 1566 | * @param $txHash |
||
| 1567 | * @param $txIdx |
||
| 1568 | * @param int $ttl |
||
| 1569 | * @return bool |
||
| 1570 | */ |
||
| 1571 | public function lockWalletUTXO($identifier, $txHash, $txIdx, $ttl = 3) { |
||
| 1575 | |||
| 1576 | /** |
||
| 1577 | * unlock a specific unspent output |
||
| 1578 | * |
||
| 1579 | * @param $identifier |
||
| 1580 | * @param $txHash |
||
| 1581 | * @param $txIdx |
||
| 1582 | * @return bool |
||
| 1583 | */ |
||
| 1584 | public function unlockWalletUTXO($identifier, $txHash, $txIdx) { |
||
| 1588 | |||
| 1589 | /** |
||
| 1590 | * get all transactions for wallet (paginated) |
||
| 1591 | * |
||
| 1592 | * @param string $identifier the wallet identifier for which to get transactions |
||
| 1593 | * @param integer $page pagination: page number |
||
| 1594 | * @param integer $limit pagination: records per page (max 500) |
||
| 1595 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1596 | * @return array associative array containing the response |
||
| 1597 | */ |
||
| 1598 | 1 | public function walletTransactions($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 1607 | |||
| 1608 | /** |
||
| 1609 | * get all addresses for wallet (paginated) |
||
| 1610 | * |
||
| 1611 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1612 | * @param integer $page pagination: page number |
||
| 1613 | * @param integer $limit pagination: records per page (max 500) |
||
| 1614 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1615 | * @return array associative array containing the response |
||
| 1616 | */ |
||
| 1617 | 1 | public function walletAddresses($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 1626 | |||
| 1627 | /** |
||
| 1628 | * get all UTXOs for wallet (paginated) |
||
| 1629 | * |
||
| 1630 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1631 | * @param integer $page pagination: page number |
||
| 1632 | * @param integer $limit pagination: records per page (max 500) |
||
| 1633 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1634 | * @param boolean $zeroconf include zero confirmation transactions |
||
| 1635 | * @return array associative array containing the response |
||
| 1636 | */ |
||
| 1637 | 1 | public function walletUTXOs($identifier, $page = 1, $limit = 20, $sortDir = 'asc', $zeroconf = true) { |
|
| 1647 | |||
| 1648 | /** |
||
| 1649 | * get a paginated list of all wallets associated with the api user |
||
| 1650 | * |
||
| 1651 | * @param integer $page pagination: page number |
||
| 1652 | * @param integer $limit pagination: records per page |
||
| 1653 | * @return array associative array containing the response |
||
| 1654 | */ |
||
| 1655 | 2 | public function allWallets($page = 1, $limit = 20) { |
|
| 1663 | |||
| 1664 | /** |
||
| 1665 | * send raw transaction |
||
| 1666 | * |
||
| 1667 | * @param $txHex |
||
| 1668 | * @return bool |
||
| 1669 | */ |
||
| 1670 | public function sendRawTransaction($txHex) { |
||
| 1674 | |||
| 1675 | /** |
||
| 1676 | * testnet only ;-) |
||
| 1677 | * |
||
| 1678 | * @param $address |
||
| 1679 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1680 | * @return mixed |
||
| 1681 | * @throws \Exception |
||
| 1682 | */ |
||
| 1683 | public function faucetWithdrawal($address, $amount = 10000) { |
||
| 1690 | |||
| 1691 | /** |
||
| 1692 | * Exists for BC. Remove at major bump. |
||
| 1693 | * |
||
| 1694 | * @see faucetWithdrawal |
||
| 1695 | * @deprecated |
||
| 1696 | * @param $address |
||
| 1697 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1698 | * @return mixed |
||
| 1699 | * @throws \Exception |
||
| 1700 | */ |
||
| 1701 | public function faucetWithdrawl($address, $amount = 10000) { |
||
| 1704 | |||
| 1705 | /** |
||
| 1706 | * verify a message signed bitcoin-core style |
||
| 1707 | * |
||
| 1708 | * @param string $message |
||
| 1709 | * @param string $address |
||
| 1710 | * @param string $signature |
||
| 1711 | * @return boolean |
||
| 1712 | */ |
||
| 1713 | 1 | public function verifyMessage($message, $address, $signature) { |
|
| 1730 | |||
| 1731 | /** |
||
| 1732 | * convert a Satoshi value to a BTC value |
||
| 1733 | * |
||
| 1734 | * @param int $satoshi |
||
| 1735 | * @return float |
||
| 1736 | */ |
||
| 1737 | public static function toBTC($satoshi) { |
||
| 1740 | |||
| 1741 | /** |
||
| 1742 | * convert a Satoshi value to a BTC value and return it as a string |
||
| 1743 | |||
| 1744 | * @param int $satoshi |
||
| 1745 | * @return string |
||
| 1746 | */ |
||
| 1747 | public static function toBTCString($satoshi) { |
||
| 1750 | |||
| 1751 | /** |
||
| 1752 | * convert a BTC value to a Satoshi value |
||
| 1753 | * |
||
| 1754 | * @param float $btc |
||
| 1755 | * @return string |
||
| 1756 | */ |
||
| 1757 | 12 | public static function toSatoshiString($btc) { |
|
| 1760 | |||
| 1761 | /** |
||
| 1762 | * convert a BTC value to a Satoshi value |
||
| 1763 | * |
||
| 1764 | * @param float $btc |
||
| 1765 | * @return string |
||
| 1766 | */ |
||
| 1767 | 12 | public static function toSatoshi($btc) { |
|
| 1770 | |||
| 1771 | /** |
||
| 1772 | * json_decode helper that throws exceptions when it fails to decode |
||
| 1773 | * |
||
| 1774 | * @param $json |
||
| 1775 | * @param bool $assoc |
||
| 1776 | * @return mixed |
||
| 1777 | * @throws \Exception |
||
| 1778 | */ |
||
| 1779 | 28 | protected static function jsonDecode($json, $assoc = false) { |
|
| 1792 | |||
| 1793 | /** |
||
| 1794 | * sort public keys for multisig script |
||
| 1795 | * |
||
| 1796 | * @param PublicKeyInterface[] $pubKeys |
||
| 1797 | * @return PublicKeyInterface[] |
||
| 1798 | */ |
||
| 1799 | 16 | public static function sortMultisigKeys(array $pubKeys) { |
|
| 1809 | |||
| 1810 | /** |
||
| 1811 | * read and decode the json payload from a webhook's POST request. |
||
| 1812 | * |
||
| 1813 | * @param bool $returnObject flag to indicate if an object or associative array should be returned |
||
| 1814 | * @return mixed|null |
||
| 1815 | * @throws \Exception |
||
| 1816 | */ |
||
| 1817 | public static function getWebhookPayload($returnObject = false) { |
||
| 1825 | |||
| 1826 | public static function normalizeBIP32KeyArray($keys) { |
||
| 1831 | |||
| 1832 | /** |
||
| 1833 | * @param array|BIP32Key $key |
||
| 1834 | * @return BIP32Key |
||
| 1835 | * @throws \Exception |
||
| 1836 | */ |
||
| 1837 | 22 | public static function normalizeBIP32Key($key) { |
|
| 1855 | } |
||
| 1856 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.