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 Connection\RestClient |
||
| 56 | */ |
||
| 57 | protected $btccomRawTxClient; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string currently only supporting; bitcoin |
||
| 61 | */ |
||
| 62 | protected $network; |
||
| 63 | |||
| 64 | /** |
||
| 65 | 118 | * @var bool |
|
| 66 | */ |
||
| 67 | 118 | protected $testnet; |
|
| 68 | |||
| 69 | 118 | /** |
|
| 70 | 118 | * @var ConverterInterface |
|
| 71 | 118 | */ |
|
| 72 | protected $converter; |
||
| 73 | |||
| 74 | /** |
||
| 75 | 118 | * @param string $apiKey the API_KEY to use for authentication |
|
| 76 | 118 | * @param string $apiSecret the API_SECRET to use for authentication |
|
| 77 | * @param string $network the cryptocurrency 'network' to consume, eg BTC, LTC, etc |
||
| 78 | 118 | * @param bool $testnet testnet yes/no |
|
| 79 | 118 | * @param string $apiVersion the version of the API to consume |
|
| 80 | * @param null $apiEndpoint overwrite the endpoint used |
||
| 81 | * this will cause the $network, $testnet and $apiVersion to be ignored! |
||
| 82 | */ |
||
| 83 | public function __construct($apiKey, $apiSecret, $network = 'BTC', $testnet = false, $apiVersion = 'v1', $apiEndpoint = null) { |
||
| 109 | 118 | ||
| 110 | /** |
||
| 111 | 4 | * normalize network string |
|
| 112 | 4 | * |
|
| 113 | * @param $network |
||
| 114 | 4 | * @param $testnet |
|
| 115 | 4 | * @return array |
|
| 116 | * @throws \Exception |
||
| 117 | */ |
||
| 118 | protected function normalizeNetwork($network, $testnet) { |
||
| 122 | 118 | ||
| 123 | /** |
||
| 124 | * set BitcoinLib to the correct magic-byte defaults for the selected network |
||
| 125 | * |
||
| 126 | * @param $network |
||
| 127 | * @param bool $testnet |
||
| 128 | * @param bool $regtest |
||
| 129 | */ |
||
| 130 | protected function setBitcoinLibMagicBytes($network, $testnet, $regtest) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * enable CURL debugging output |
||
| 155 | * |
||
| 156 | * @param bool $debug |
||
| 157 | * |
||
| 158 | * @codeCoverageIgnore |
||
| 159 | */ |
||
| 160 | 2 | public function setCurlDebugging($debug = true) { |
|
| 165 | |||
| 166 | /** |
||
| 167 | * enable verbose errors |
||
| 168 | * |
||
| 169 | * @param bool $verboseErrors |
||
| 170 | * |
||
| 171 | * @codeCoverageIgnore |
||
| 172 | */ |
||
| 173 | public function setVerboseErrors($verboseErrors = true) { |
||
| 178 | 1 | ||
| 179 | /** |
||
| 180 | * set cURL default option on Guzzle client |
||
| 181 | * @param string $key |
||
| 182 | * @param bool $value |
||
| 183 | * |
||
| 184 | * @codeCoverageIgnore |
||
| 185 | */ |
||
| 186 | public function setCurlDefaultOption($key, $value) { |
||
| 191 | 1 | ||
| 192 | 1 | /** |
|
| 193 | 1 | * @return RestClientInterface |
|
| 194 | */ |
||
| 195 | 1 | public function getRestClient() { |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @return RestClient |
||
| 201 | */ |
||
| 202 | public function getDataRestClient() { |
||
| 205 | |||
| 206 | /** |
||
| 207 | 1 | * @param RestClientInterface $restClient |
|
| 208 | */ |
||
| 209 | 1 | public function setRestClient(RestClientInterface $restClient) { |
|
| 212 | |||
| 213 | 1 | /** |
|
| 214 | 1 | * get a single address |
|
| 215 | * @param string $address address hash |
||
| 216 | * @return array associative array containing the response |
||
| 217 | */ |
||
| 218 | public function address($address) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * get all transactions for an address (paginated) |
||
| 225 | 1 | * @param string $address address hash |
|
| 226 | * @param integer $page pagination: page number |
||
| 227 | 1 | * @param integer $limit pagination: records per page (max 500) |
|
| 228 | 1 | * @param string $sortDir pagination: sort direction (asc|desc) |
|
| 229 | 1 | * @return array associative array containing the response |
|
| 230 | */ |
||
| 231 | 1 | public function addressTransactions($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 240 | |||
| 241 | /** |
||
| 242 | * get all unconfirmed transactions for an address (paginated) |
||
| 243 | * @param string $address address hash |
||
| 244 | * @param integer $page pagination: page number |
||
| 245 | * @param integer $limit pagination: records per page (max 500) |
||
| 246 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 247 | * @return array associative array containing the response |
||
| 248 | */ |
||
| 249 | public function addressUnconfirmedTransactions($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * get all unspent outputs for an address (paginated) |
||
| 261 | 2 | * @param string $address address hash |
|
| 262 | 2 | * @param integer $page pagination: page number |
|
| 263 | * @param integer $limit pagination: records per page (max 500) |
||
| 264 | 2 | * @param string $sortDir pagination: sort direction (asc|desc) |
|
| 265 | * @return array associative array containing the response |
||
| 266 | 2 | */ |
|
| 267 | public function addressUnspentOutputs($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 276 | 1 | ||
| 277 | /** |
||
| 278 | 1 | * get all unspent outputs for a batch of addresses (paginated) |
|
| 279 | 1 | * |
|
| 280 | 1 | * @param string[] $addresses |
|
| 281 | * @param integer $page pagination: page number |
||
| 282 | 1 | * @param integer $limit pagination: records per page (max 500) |
|
| 283 | 1 | * @param string $sortDir pagination: sort direction (asc|desc) |
|
| 284 | * @return array associative array containing the response |
||
| 285 | * @throws \Exception |
||
| 286 | */ |
||
| 287 | public function batchAddressUnspentOutputs($addresses, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 290 | 1 | ||
| 291 | 1 | /** |
|
| 292 | 1 | * verify ownership of an address |
|
| 293 | * @param string $address address hash |
||
| 294 | * @param string $signature a signed message (the address hash) using the private key of the address |
||
| 295 | * @return array associative array containing the response |
||
| 296 | */ |
||
| 297 | public function verifyAddress($address, $signature) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * get all blocks (paginated) |
||
| 307 | * @param integer $page pagination: page number |
||
| 308 | * @param integer $limit pagination: records per page |
||
| 309 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 310 | * @return array associative array containing the response |
||
| 311 | */ |
||
| 312 | public function allBlocks($page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * get the latest block |
||
| 324 | * @return array associative array containing the response |
||
| 325 | */ |
||
| 326 | public function blockLatest() { |
||
| 330 | 5 | ||
| 331 | /** |
||
| 332 | * get an individual block |
||
| 333 | * @param string|integer $block a block hash or a block height |
||
| 334 | * @return array associative array containing the response |
||
| 335 | */ |
||
| 336 | public function block($block) { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * get all transaction in a block (paginated) |
||
| 343 | * @param string|integer $block a block hash or a block height |
||
| 344 | * @param integer $page pagination: page number |
||
| 345 | * @param integer $limit pagination: records per page |
||
| 346 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 347 | * @return array associative array containing the response |
||
| 348 | */ |
||
| 349 | 1 | public function blockTransactions($block, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 358 | |||
| 359 | /** |
||
| 360 | * get a single transaction |
||
| 361 | * @param string $txhash transaction hash |
||
| 362 | * @return array associative array containing the response |
||
| 363 | 1 | */ |
|
| 364 | 1 | public function transaction($txhash) { |
|
| 374 | 1 | ||
| 375 | /** |
||
| 376 | 1 | * get a single transaction |
|
| 377 | 1 | * @param string[] $txhashes list of transaction hashes (up to 20) |
|
| 378 | * @return array[] array containing the response |
||
| 379 | 1 | */ |
|
| 380 | 1 | public function transactions($txhashes) { |
|
| 384 | |||
| 385 | /** |
||
| 386 | * get a paginated list of all webhooks associated with the api user |
||
| 387 | * @param integer $page pagination: page number |
||
| 388 | * @param integer $limit pagination: records per page |
||
| 389 | * @return array associative array containing the response |
||
| 390 | 1 | */ |
|
| 391 | public function allWebhooks($page = 1, $limit = 20) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * get an existing webhook by it's identifier |
||
| 402 | * @param string $identifier a unique identifier associated with the webhook |
||
| 403 | * @return array associative array containing the response |
||
| 404 | 1 | */ |
|
| 405 | 1 | public function getWebhook($identifier) { |
|
| 409 | |||
| 410 | /** |
||
| 411 | * create a new webhook |
||
| 412 | * @param string $url the url to receive the webhook events |
||
| 413 | * @param string $identifier a unique identifier to associate with this webhook |
||
| 414 | * @return array associative array containing the response |
||
| 415 | */ |
||
| 416 | 2 | public function setupWebhook($url, $identifier = null) { |
|
| 424 | |||
| 425 | /** |
||
| 426 | * update an existing webhook |
||
| 427 | * @param string $identifier the unique identifier of the webhook to update |
||
| 428 | * @param string $newUrl the new url to receive the webhook events |
||
| 429 | * @param string $newIdentifier a new unique identifier to associate with this webhook |
||
| 430 | * @return array associative array containing the response |
||
| 431 | */ |
||
| 432 | 1 | public function updateWebhook($identifier, $newUrl = null, $newIdentifier = null) { |
|
| 440 | |||
| 441 | /** |
||
| 442 | * deletes an existing webhook and any event subscriptions associated with it |
||
| 443 | * @param string $identifier the unique identifier of the webhook to delete |
||
| 444 | * @return boolean true on success |
||
| 445 | */ |
||
| 446 | public function deleteWebhook($identifier) { |
||
| 450 | |||
| 451 | 1 | /** |
|
| 452 | 1 | * get a paginated list of all the events a webhook is subscribed to |
|
| 453 | 1 | * @param string $identifier the unique identifier of the webhook |
|
| 454 | * @param integer $page pagination: page number |
||
| 455 | 1 | * @param integer $limit pagination: records per page |
|
| 456 | 1 | * @return array associative array containing the response |
|
| 457 | */ |
||
| 458 | public function getWebhookEvents($identifier, $page = 1, $limit = 20) { |
||
| 466 | |||
| 467 | /** |
||
| 468 | * subscribes a webhook to transaction events of one particular transaction |
||
| 469 | 1 | * @param string $identifier the unique identifier of the webhook to be triggered |
|
| 470 | 1 | * @param string $transaction the transaction hash |
|
| 471 | 1 | * @param integer $confirmations the amount of confirmations to send. |
|
| 472 | 1 | * @return array associative array containing the response |
|
| 473 | 1 | */ |
|
| 474 | 1 | public function subscribeTransaction($identifier, $transaction, $confirmations = 6) { |
|
| 483 | |||
| 484 | /** |
||
| 485 | * subscribes a webhook to transaction events on a particular address |
||
| 486 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 487 | 1 | * @param string $address the address hash |
|
| 488 | * @param integer $confirmations the amount of confirmations to send. |
||
| 489 | 1 | * @return array associative array containing the response |
|
| 490 | */ |
||
| 491 | 1 | public function subscribeAddressTransactions($identifier, $address, $confirmations = 6) { |
|
| 500 | |||
| 501 | 1 | /** |
|
| 502 | 1 | * batch subscribes a webhook to multiple transaction events |
|
| 503 | 1 | * |
|
| 504 | * @param string $identifier the unique identifier of the webhook |
||
| 505 | * @param array $batchData A 2D array of event data: |
||
| 506 | * [address => $address, confirmations => $confirmations] |
||
| 507 | * where $address is the address to subscibe to |
||
| 508 | * and optionally $confirmations is the amount of confirmations |
||
| 509 | * @return boolean true on success |
||
| 510 | */ |
||
| 511 | public function batchSubscribeAddressTransactions($identifier, $batchData) { |
||
| 523 | 1 | ||
| 524 | 1 | /** |
|
| 525 | * subscribes a webhook to a new block event |
||
| 526 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 527 | * @return array associative array containing the response |
||
| 528 | */ |
||
| 529 | public function subscribeNewBlocks($identifier) { |
||
| 536 | |||
| 537 | /** |
||
| 538 | * removes an transaction event subscription from a webhook |
||
| 539 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 540 | * @param string $transaction the transaction hash of the event subscription |
||
| 541 | * @return boolean true on success |
||
| 542 | */ |
||
| 543 | public function unsubscribeTransaction($identifier, $transaction) { |
||
| 547 | |||
| 548 | 1 | /** |
|
| 549 | 1 | * removes an address transaction event subscription from a webhook |
|
| 550 | 1 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
|
| 551 | * @param string $address the address hash of the event subscription |
||
| 552 | * @return boolean true on success |
||
| 553 | */ |
||
| 554 | 7 | public function unsubscribeAddressTransactions($identifier, $address) { |
|
| 558 | 1 | ||
| 559 | /** |
||
| 560 | * removes a block event subscription from a webhook |
||
| 561 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 562 | 7 | * @return boolean true on success |
|
| 563 | 1 | */ |
|
| 564 | public function unsubscribeNewBlocks($identifier) { |
||
| 568 | |||
| 569 | /** |
||
| 570 | 7 | * create a new wallet |
|
| 571 | 3 | * - will generate a new primary seed (with password) and backup seed (without password) |
|
| 572 | * - send the primary seed (BIP39 'encrypted') and backup public key to the server |
||
| 573 | * - receive the blocktrail co-signing public key from the server |
||
| 574 | 7 | * |
|
| 575 | 7 | * Either takes one argument: |
|
| 576 | 1 | * @param array $options |
|
| 577 | * |
||
| 578 | 6 | * Or takes three arguments (old, deprecated syntax): |
|
| 579 | 2 | * (@nonPHP-doc) @param $identifier |
|
| 580 | * (@nonPHP-doc) @param $password |
||
| 581 | 4 | * (@nonPHP-doc) @param int $keyIndex override for the blocktrail cosigning key to use |
|
| 582 | 4 | * |
|
| 583 | * @return array[WalletInterface, array] list($wallet, $backupInfo) |
||
| 584 | * @throws \Exception |
||
| 585 | */ |
||
| 586 | public function createNewWallet($options) { |
||
| 630 | |||
| 631 | protected function createNewWalletV1($options) { |
||
| 751 | |||
| 752 | public static function randomBits($bits) { |
||
| 755 | |||
| 756 | public static function randomBytes($bytes) { |
||
| 759 | 1 | ||
| 760 | 1 | protected function createNewWalletV2($options) { |
|
| 883 | |||
| 884 | 4 | protected function createNewWalletV3($options) { |
|
| 1025 | 1 | ||
| 1026 | 1 | /** |
|
| 1027 | 1 | * @param array $bip32Key |
|
| 1028 | 1 | * @throws BlocktrailSDKException |
|
| 1029 | */ |
||
| 1030 | 1 | private function verifyPublicBIP32Key(array $bip32Key) { |
|
| 1040 | |||
| 1041 | /** |
||
| 1042 | * @param array $walletData |
||
| 1043 | * @throws BlocktrailSDKException |
||
| 1044 | */ |
||
| 1045 | private function verifyPublicOnly(array $walletData) { |
||
| 1049 | |||
| 1050 | 5 | /** |
|
| 1051 | * create wallet using the API |
||
| 1052 | 5 | * |
|
| 1053 | * @param string $identifier the wallet identifier to create |
||
| 1054 | 5 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
|
| 1055 | 5 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
|
| 1056 | 5 | * @param string $primaryMnemonic mnemonic to store |
|
| 1057 | 5 | * @param string $checksum checksum to store |
|
| 1058 | 5 | * @param int $keyIndex account that we expect to use |
|
| 1059 | 5 | * @param bool $segwit opt in to segwit |
|
| 1060 | 5 | * @return mixed |
|
| 1061 | 5 | */ |
|
| 1062 | public function storeNewWalletV1($identifier, $primaryPublicKey, $backupPublicKey, $primaryMnemonic, $checksum, $keyIndex, $segwit = false) { |
||
| 1076 | |||
| 1077 | /** |
||
| 1078 | * create wallet using the API |
||
| 1079 | * |
||
| 1080 | * @param string $identifier the wallet identifier to create |
||
| 1081 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1082 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1083 | 4 | * @param $encryptedPrimarySeed |
|
| 1084 | * @param $encryptedSecret |
||
| 1085 | * @param $recoverySecret |
||
| 1086 | 4 | * @param string $checksum checksum to store |
|
| 1087 | * @param int $keyIndex account that we expect to use |
||
| 1088 | 4 | * @param bool $segwit opt in to segwit |
|
| 1089 | 4 | * @return mixed |
|
| 1090 | 4 | * @throws \Exception |
|
| 1091 | 4 | */ |
|
| 1092 | 4 | public function storeNewWalletV2($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false) { |
|
| 1109 | |||
| 1110 | /** |
||
| 1111 | * create wallet using the API |
||
| 1112 | 5 | * |
|
| 1113 | * @param string $identifier the wallet identifier to create |
||
| 1114 | 5 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
|
| 1115 | 5 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
|
| 1116 | * @param $encryptedPrimarySeed |
||
| 1117 | * @param $encryptedSecret |
||
| 1118 | 5 | * @param $recoverySecret |
|
| 1119 | 5 | * @param string $checksum checksum to store |
|
| 1120 | * @param int $keyIndex account that we expect to use |
||
| 1121 | * @param bool $segwit opt in to segwit |
||
| 1122 | * @return mixed |
||
| 1123 | * @throws \Exception |
||
| 1124 | */ |
||
| 1125 | public function storeNewWalletV3($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false) { |
||
| 1144 | |||
| 1145 | /** |
||
| 1146 | * upgrade wallet to use a new account number |
||
| 1147 | * the account number specifies which blocktrail cosigning key is used |
||
| 1148 | * |
||
| 1149 | * @param string $identifier the wallet identifier to be upgraded |
||
| 1150 | * @param int $keyIndex the new account to use |
||
| 1151 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1152 | * @return mixed |
||
| 1153 | */ |
||
| 1154 | public function upgradeKeyIndex($identifier, $keyIndex, $primaryPublicKey) { |
||
| 1163 | 1 | ||
| 1164 | /** |
||
| 1165 | 1 | * @param array $options |
|
| 1166 | 1 | * @return AddressReaderBase |
|
| 1167 | */ |
||
| 1168 | private function makeAddressReader(array $options) { |
||
| 1179 | |||
| 1180 | /** |
||
| 1181 | * initialize a previously created wallet |
||
| 1182 | 23 | * |
|
| 1183 | 1 | * Takes an options object, or accepts identifier/password for backwards compatiblity. |
|
| 1184 | 1 | * |
|
| 1185 | * Some of the options: |
||
| 1186 | 1 | * - "readonly/readOnly/read-only" can be to a boolean value, |
|
| 1187 | 1 | * so the wallet is loaded in read-only mode (no private key) |
|
| 1188 | * - "check_backup_key" can be set to your own backup key: |
||
| 1189 | * Format: ["M', "xpub..."] |
||
| 1190 | * Setting this will allow the SDK to check the server hasn't |
||
| 1191 | 23 | * a different key (one it happens to control) |
|
| 1192 | |||
| 1193 | 23 | * Either takes one argument: |
|
| 1194 | 23 | * @param array $options |
|
| 1195 | 17 | * |
|
| 1196 | 17 | * Or takes two arguments (old, deprecated syntax): |
|
| 1197 | 17 | * (@nonPHP-doc) @param string $identifier the wallet identifier to be initialized |
|
| 1198 | 17 | * (@nonPHP-doc) @param string $password the password to decrypt the mnemonic with |
|
| 1199 | 17 | * |
|
| 1200 | 17 | * @return WalletInterface |
|
| 1201 | 17 | * @throws \Exception |
|
| 1202 | 17 | */ |
|
| 1203 | 17 | public function initWallet($options) { |
|
| 1315 | |||
| 1316 | /** |
||
| 1317 | * get the wallet data from the server |
||
| 1318 | * |
||
| 1319 | * @param string $identifier the identifier of the wallet |
||
| 1320 | * @return mixed |
||
| 1321 | */ |
||
| 1322 | public function getWallet($identifier) { |
||
| 1326 | |||
| 1327 | 1 | /** |
|
| 1328 | * update the wallet data on the server |
||
| 1329 | * |
||
| 1330 | * @param string $identifier |
||
| 1331 | * @param $data |
||
| 1332 | * @return mixed |
||
| 1333 | */ |
||
| 1334 | public function updateWallet($identifier, $data) { |
||
| 1338 | |||
| 1339 | /** |
||
| 1340 | 1 | * delete a wallet from the server |
|
| 1341 | 1 | * the checksum address and a signature to verify you ownership of the key of that checksum address |
|
| 1342 | * is required to be able to delete a wallet |
||
| 1343 | 1 | * |
|
| 1344 | * @param string $identifier the identifier of the wallet |
||
| 1345 | * @param string $checksumAddress the address for your master private key (and the checksum used when creating the wallet) |
||
| 1346 | * @param string $signature a signature of the checksum address as message signed by the private key matching that address |
||
| 1347 | * @param bool $force ignore warnings (such as a non-zero balance) |
||
| 1348 | * @return mixed |
||
| 1349 | */ |
||
| 1350 | public function deleteWallet($identifier, $checksumAddress, $signature, $force = false) { |
||
| 1357 | |||
| 1358 | /** |
||
| 1359 | 1 | * create new backup key; |
|
| 1360 | * 1) a BIP39 mnemonic |
||
| 1361 | 1 | * 2) a seed from that mnemonic with a blank password |
|
| 1362 | * 3) a private key from that seed |
||
| 1363 | 1 | * |
|
| 1364 | * @return array [mnemonic, seed, key] |
||
| 1365 | 1 | */ |
|
| 1366 | protected function newBackupSeed() { |
||
| 1371 | 1 | ||
| 1372 | /** |
||
| 1373 | * create new primary key; |
||
| 1374 | * 1) a BIP39 mnemonic |
||
| 1375 | * 2) a seed from that mnemonic with the password |
||
| 1376 | * 3) a private key from that seed |
||
| 1377 | * |
||
| 1378 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1379 | * @return array [mnemonic, seed, key] |
||
| 1380 | * @TODO: require a strong password? |
||
| 1381 | 1 | */ |
|
| 1382 | 1 | protected function newPrimarySeed($passphrase) { |
|
| 1387 | |||
| 1388 | /** |
||
| 1389 | 1 | * create a new key; |
|
| 1390 | * 1) a BIP39 mnemonic |
||
| 1391 | * 2) a seed from that mnemonic with the password |
||
| 1392 | * 3) a private key from that seed |
||
| 1393 | * |
||
| 1394 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1395 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1396 | * @return array |
||
| 1397 | */ |
||
| 1398 | 9 | protected function generateNewSeed($passphrase = "", $forceEntropy = null) { |
|
| 1415 | |||
| 1416 | /** |
||
| 1417 | * generate a new mnemonic from some random entropy (512 bit) |
||
| 1418 | * |
||
| 1419 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1420 | * @return string |
||
| 1421 | * @throws \Exception |
||
| 1422 | */ |
||
| 1423 | protected function generateNewMnemonic($forceEntropy = null) { |
||
| 1433 | |||
| 1434 | /** |
||
| 1435 | * get the balance for the wallet |
||
| 1436 | * |
||
| 1437 | * @param string $identifier the identifier of the wallet |
||
| 1438 | * @return array |
||
| 1439 | */ |
||
| 1440 | 16 | public function getWalletBalance($identifier) { |
|
| 1444 | |||
| 1445 | /** |
||
| 1446 | * get a new derivation number for specified parent path |
||
| 1447 | * 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 |
||
| 1448 | * |
||
| 1449 | * returns the path |
||
| 1450 | * |
||
| 1451 | * @param string $identifier the identifier of the wallet |
||
| 1452 | * @param string $path the parent path for which to get a new derivation |
||
| 1453 | 1 | * @return string |
|
| 1454 | 1 | */ |
|
| 1455 | 1 | public function getNewDerivation($identifier, $path) { |
|
| 1459 | |||
| 1460 | /** |
||
| 1461 | * get a new derivation number for specified parent path |
||
| 1462 | * 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 |
||
| 1463 | * |
||
| 1464 | * @param string $identifier the identifier of the wallet |
||
| 1465 | * @param string $path the parent path for which to get a new derivation |
||
| 1466 | * @return mixed |
||
| 1467 | */ |
||
| 1468 | 4 | public function _getNewDerivation($identifier, $path) { |
|
| 1472 | |||
| 1473 | 4 | /** |
|
| 1474 | 4 | * get the path (and redeemScript) to specified address |
|
| 1475 | 4 | * |
|
| 1476 | 4 | * @param string $identifier |
|
| 1477 | 4 | * @param string $address |
|
| 1478 | * @return array |
||
| 1479 | 4 | * @throws \Exception |
|
| 1480 | */ |
||
| 1481 | public function getPathForAddress($identifier, $address) { |
||
| 1485 | |||
| 1486 | 4 | /** |
|
| 1487 | * send the transaction using the API |
||
| 1488 | 4 | * |
|
| 1489 | 4 | * @param string $identifier the identifier of the wallet |
|
| 1490 | * @param string|array $rawTransaction raw hex of the transaction (should be partially signed) |
||
| 1491 | 4 | * @param array $paths list of the paths that were used for the UTXO |
|
| 1492 | * @param bool $checkFee let the server verify the fee after signing |
||
| 1493 | * @param null $twoFactorToken |
||
| 1494 | * @return string the complete raw transaction |
||
| 1495 | * @throws \Exception |
||
| 1496 | 4 | */ |
|
| 1497 | public function sendTransaction($identifier, $rawTransaction, $paths, $checkFee = false, $twoFactorToken = null) { |
||
| 1528 | |||
| 1529 | 12 | /** |
|
| 1530 | * use the API to get the best inputs to use based on the outputs |
||
| 1531 | 12 | * |
|
| 1532 | 12 | * the return array has the following format: |
|
| 1533 | 12 | * [ |
|
| 1534 | * "utxos" => [ |
||
| 1535 | * [ |
||
| 1536 | 12 | * "hash" => "<txHash>", |
|
| 1537 | 1 | * "idx" => "<index of the output of that <txHash>", |
|
| 1538 | * "scriptpubkey_hex" => "<scriptPubKey-hex>", |
||
| 1539 | * "value" => 32746327, |
||
| 1540 | 12 | * "address" => "1address", |
|
| 1541 | 12 | * "path" => "m/44'/1'/0'/0/13", |
|
| 1542 | 12 | * "redeem_script" => "<redeemScript-hex>", |
|
| 1543 | 12 | * ], |
|
| 1544 | 12 | * ], |
|
| 1545 | * "fee" => 10000, |
||
| 1546 | * "change"=> 1010109201, |
||
| 1547 | 6 | * ] |
|
| 1548 | * |
||
| 1549 | * @param string $identifier the identifier of the wallet |
||
| 1550 | * @param array $outputs the outputs you want to create - array[address => satoshi-value] |
||
| 1551 | * @param bool $lockUTXO when TRUE the UTXOs selected will be locked for a few seconds |
||
| 1552 | * so you have some time to spend them without race-conditions |
||
| 1553 | * @param bool $allowZeroConf |
||
| 1554 | * @param string $feeStrategy |
||
| 1555 | * @param null|int $forceFee |
||
| 1556 | * @return array |
||
| 1557 | * @throws \Exception |
||
| 1558 | */ |
||
| 1559 | public function coinSelection($identifier, $outputs, $lockUTXO = false, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null) { |
||
| 1579 | |||
| 1580 | /** |
||
| 1581 | * |
||
| 1582 | * @param string $identifier the identifier of the wallet |
||
| 1583 | 3 | * @param bool $allowZeroConf |
|
| 1584 | 3 | * @param string $feeStrategy |
|
| 1585 | 3 | * @param null|int $forceFee |
|
| 1586 | * @param int $outputCnt |
||
| 1587 | * @return array |
||
| 1588 | * @throws \Exception |
||
| 1589 | */ |
||
| 1590 | public function walletMaxSpendable($identifier, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null, $outputCnt = 1) { |
||
| 1609 | |||
| 1610 | /** |
||
| 1611 | * @return array ['optimal_fee' => 10000, 'low_priority_fee' => 5000] |
||
| 1612 | */ |
||
| 1613 | public function feePerKB() { |
||
| 1617 | |||
| 1618 | 1 | /** |
|
| 1619 | 1 | * get the current price index |
|
| 1620 | 1 | * |
|
| 1621 | * @return array eg; ['USD' => 287.30] |
||
| 1622 | */ |
||
| 1623 | public function price() { |
||
| 1627 | |||
| 1628 | /** |
||
| 1629 | * setup webhook for wallet |
||
| 1630 | * |
||
| 1631 | * @param string $identifier the wallet identifier for which to create the webhook |
||
| 1632 | * @param string $webhookIdentifier the webhook identifier to use |
||
| 1633 | * @param string $url the url to receive the webhook events |
||
| 1634 | * @return array |
||
| 1635 | */ |
||
| 1636 | public function setupWalletWebhook($identifier, $webhookIdentifier, $url) { |
||
| 1640 | |||
| 1641 | /** |
||
| 1642 | * delete webhook for wallet |
||
| 1643 | * |
||
| 1644 | * @param string $identifier the wallet identifier for which to delete the webhook |
||
| 1645 | * @param string $webhookIdentifier the webhook identifier to delete |
||
| 1646 | * @return array |
||
| 1647 | */ |
||
| 1648 | public function deleteWalletWebhook($identifier, $webhookIdentifier) { |
||
| 1652 | |||
| 1653 | /** |
||
| 1654 | * lock a specific unspent output |
||
| 1655 | * |
||
| 1656 | * @param $identifier |
||
| 1657 | * @param $txHash |
||
| 1658 | * @param $txIdx |
||
| 1659 | 1 | * @param int $ttl |
|
| 1660 | * @return bool |
||
| 1661 | 1 | */ |
|
| 1662 | 1 | public function lockWalletUTXO($identifier, $txHash, $txIdx, $ttl = 3) { |
|
| 1666 | 1 | ||
| 1667 | /** |
||
| 1668 | * unlock a specific unspent output |
||
| 1669 | * |
||
| 1670 | * @param $identifier |
||
| 1671 | * @param $txHash |
||
| 1672 | * @param $txIdx |
||
| 1673 | * @return bool |
||
| 1674 | */ |
||
| 1675 | public function unlockWalletUTXO($identifier, $txHash, $txIdx) { |
||
| 1679 | |||
| 1680 | 1 | /** |
|
| 1681 | 1 | * get all transactions for wallet (paginated) |
|
| 1682 | 1 | * |
|
| 1683 | * @param string $identifier the wallet identifier for which to get transactions |
||
| 1684 | 1 | * @param integer $page pagination: page number |
|
| 1685 | 1 | * @param integer $limit pagination: records per page (max 500) |
|
| 1686 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1687 | * @return array associative array containing the response |
||
| 1688 | */ |
||
| 1689 | public function walletTransactions($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 1698 | 1 | ||
| 1699 | /** |
||
| 1700 | 1 | * get all addresses for wallet (paginated) |
|
| 1701 | 1 | * |
|
| 1702 | 1 | * @param string $identifier the wallet identifier for which to get addresses |
|
| 1703 | 1 | * @param integer $page pagination: page number |
|
| 1704 | * @param integer $limit pagination: records per page (max 500) |
||
| 1705 | 1 | * @param string $sortDir pagination: sort direction (asc|desc) |
|
| 1706 | 1 | * @return array associative array containing the response |
|
| 1707 | */ |
||
| 1708 | public function walletAddresses($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 1717 | |||
| 1718 | 2 | /** |
|
| 1719 | 2 | * get all UTXOs for wallet (paginated) |
|
| 1720 | * |
||
| 1721 | 2 | * @param string $identifier the wallet identifier for which to get addresses |
|
| 1722 | 2 | * @param integer $page pagination: page number |
|
| 1723 | * @param integer $limit pagination: records per page (max 500) |
||
| 1724 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1725 | * @param boolean $zeroconf include zero confirmation transactions |
||
| 1726 | * @return array associative array containing the response |
||
| 1727 | */ |
||
| 1728 | public function walletUTXOs($identifier, $page = 1, $limit = 20, $sortDir = 'asc', $zeroconf = true) { |
||
| 1738 | |||
| 1739 | /** |
||
| 1740 | * get a paginated list of all wallets associated with the api user |
||
| 1741 | * |
||
| 1742 | * @param integer $page pagination: page number |
||
| 1743 | * @param integer $limit pagination: records per page |
||
| 1744 | * @return array associative array containing the response |
||
| 1745 | */ |
||
| 1746 | public function allWallets($page = 1, $limit = 20) { |
||
| 1754 | |||
| 1755 | /** |
||
| 1756 | * send raw transaction |
||
| 1757 | * |
||
| 1758 | * @param $txHex |
||
| 1759 | * @return bool |
||
| 1760 | */ |
||
| 1761 | public function sendRawTransaction($txHex) { |
||
| 1765 | |||
| 1766 | /** |
||
| 1767 | * testnet only ;-) |
||
| 1768 | * |
||
| 1769 | * @param $address |
||
| 1770 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1771 | * @return mixed |
||
| 1772 | * @throws \Exception |
||
| 1773 | */ |
||
| 1774 | 1 | public function faucetWithdrawal($address, $amount = 10000) { |
|
| 1781 | |||
| 1782 | /** |
||
| 1783 | * Exists for BC. Remove at major bump. |
||
| 1784 | * |
||
| 1785 | 1 | * @see faucetWithdrawal |
|
| 1786 | 1 | * @deprecated |
|
| 1787 | * @param $address |
||
| 1788 | 1 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
|
| 1789 | 1 | * @return mixed |
|
| 1790 | * @throws \Exception |
||
| 1791 | */ |
||
| 1792 | public function faucetWithdrawl($address, $amount = 10000) { |
||
| 1795 | |||
| 1796 | /** |
||
| 1797 | * verify a message signed bitcoin-core style |
||
| 1798 | * |
||
| 1799 | * @param string $message |
||
| 1800 | 1 | * @param string $address |
|
| 1801 | 1 | * @param string $signature |
|
| 1802 | * @return boolean |
||
| 1803 | 1 | */ |
|
| 1804 | 1 | public function verifyMessage($message, $address, $signature) { |
|
| 1818 | |||
| 1819 | /** |
||
| 1820 | * Take a base58 or cashaddress, and return only |
||
| 1821 | * the cash address. |
||
| 1822 | * This function only works on bitcoin cash. |
||
| 1823 | * @param string $input |
||
| 1824 | * @return string |
||
| 1825 | * @throws BlocktrailSDKException |
||
| 1826 | */ |
||
| 1827 | public function getLegacyBitcoinCashAddress($input) { |
||
| 1844 | 12 | ||
| 1845 | 12 | /** |
|
| 1846 | * convert a Satoshi value to a BTC value |
||
| 1847 | * |
||
| 1848 | * @param int $satoshi |
||
| 1849 | * @return float |
||
| 1850 | */ |
||
| 1851 | public static function toBTC($satoshi) { |
||
| 1854 | 12 | ||
| 1855 | 12 | /** |
|
| 1856 | * convert a Satoshi value to a BTC value and return it as a string |
||
| 1857 | |||
| 1858 | * @param int $satoshi |
||
| 1859 | * @return string |
||
| 1860 | */ |
||
| 1861 | public static function toBTCString($satoshi) { |
||
| 1864 | |||
| 1865 | /** |
||
| 1866 | 32 | * convert a BTC value to a Satoshi value |
|
| 1867 | 32 | * |
|
| 1868 | * @param float $btc |
||
| 1869 | * @return string |
||
| 1870 | */ |
||
| 1871 | 32 | public static function toSatoshiString($btc) { |
|
| 1874 | |||
| 1875 | /** |
||
| 1876 | * convert a BTC value to a Satoshi value |
||
| 1877 | 32 | * |
|
| 1878 | * @param float $btc |
||
| 1879 | * @return string |
||
| 1880 | */ |
||
| 1881 | public static function toSatoshi($btc) { |
||
| 1884 | |||
| 1885 | /** |
||
| 1886 | 18 | * json_decode helper that throws exceptions when it fails to decode |
|
| 1887 | 18 | * |
|
| 1888 | * @param $json |
||
| 1889 | 18 | * @param bool $assoc |
|
| 1890 | 18 | * @return mixed |
|
| 1891 | 18 | * @throws \Exception |
|
| 1892 | 18 | */ |
|
| 1893 | public static function jsonDecode($json, $assoc = false) { |
||
| 1906 | |||
| 1907 | /** |
||
| 1908 | * sort public keys for multisig script |
||
| 1909 | * |
||
| 1910 | * @param PublicKeyInterface[] $pubKeys |
||
| 1911 | * @return PublicKeyInterface[] |
||
| 1912 | */ |
||
| 1913 | public static function sortMultisigKeys(array $pubKeys) { |
||
| 1923 | |||
| 1924 | 26 | /** |
|
| 1925 | 26 | * read and decode the json payload from a webhook's POST request. |
|
| 1926 | 10 | * |
|
| 1927 | * @param bool $returnObject flag to indicate if an object or associative array should be returned |
||
| 1928 | * @return mixed|null |
||
| 1929 | 26 | * @throws \Exception |
|
| 1930 | 26 | */ |
|
| 1931 | 26 | public static function getWebhookPayload($returnObject = false) { |
|
| 1939 | |||
| 1940 | public static function normalizeBIP32KeyArray($keys) { |
||
| 1945 | |||
| 1946 | /** |
||
| 1947 | * @param array|BIP32Key $key |
||
| 1948 | * @return BIP32Key |
||
| 1949 | * @throws \Exception |
||
| 1950 | */ |
||
| 1951 | public static function normalizeBIP32Key($key) { |
||
| 1969 | } |
||
| 1970 |
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: