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) { |
|
| 109 | |||
| 110 | /** |
||
| 111 | * normalize network string |
||
| 112 | * |
||
| 113 | * @param $network |
||
| 114 | * @param $testnet |
||
| 115 | * @return array |
||
| 116 | * @throws \Exception |
||
| 117 | */ |
||
| 118 | 17 | protected function normalizeNetwork($network, $testnet) { |
|
| 122 | |||
| 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 | 17 | protected function setBitcoinLibMagicBytes($network, $testnet, $regtest) { |
|
| 152 | |||
| 153 | /** |
||
| 154 | * enable CURL debugging output |
||
| 155 | * |
||
| 156 | * @param bool $debug |
||
| 157 | * |
||
| 158 | * @codeCoverageIgnore |
||
| 159 | */ |
||
| 160 | public function setCurlDebugging($debug = true) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * enable verbose errors |
||
| 167 | * |
||
| 168 | * @param bool $verboseErrors |
||
| 169 | * |
||
| 170 | * @codeCoverageIgnore |
||
| 171 | */ |
||
| 172 | public function setVerboseErrors($verboseErrors = true) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * set cURL default option on Guzzle client |
||
| 179 | * @param string $key |
||
| 180 | * @param bool $value |
||
| 181 | * |
||
| 182 | * @codeCoverageIgnore |
||
| 183 | */ |
||
| 184 | public function setCurlDefaultOption($key, $value) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return RestClientInterface |
||
| 191 | */ |
||
| 192 | 1 | public function getRestClient() { |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @return RestClient |
||
| 198 | */ |
||
| 199 | public function getDataRestClient() { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param RestClientInterface $restClient |
||
| 205 | */ |
||
| 206 | public function setRestClient(RestClientInterface $restClient) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * get a single address |
||
| 212 | * @param string $address address hash |
||
| 213 | * @return array associative array containing the response |
||
| 214 | */ |
||
| 215 | 2 | public function address($address) { |
|
| 219 | |||
| 220 | /** |
||
| 221 | * get all transactions for an address (paginated) |
||
| 222 | * @param string $address address hash |
||
| 223 | * @param integer $page pagination: page number |
||
| 224 | * @param integer $limit pagination: records per page (max 500) |
||
| 225 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 226 | * @return array associative array containing the response |
||
| 227 | */ |
||
| 228 | 2 | public function addressTransactions($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 237 | |||
| 238 | /** |
||
| 239 | * get all unconfirmed transactions for an address (paginated) |
||
| 240 | * @param string $address address hash |
||
| 241 | * @param integer $page pagination: page number |
||
| 242 | * @param integer $limit pagination: records per page (max 500) |
||
| 243 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 244 | * @return array associative array containing the response |
||
| 245 | */ |
||
| 246 | public function addressUnconfirmedTransactions($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * get all unspent outputs for an address (paginated) |
||
| 258 | * @param string $address address hash |
||
| 259 | * @param integer $page pagination: page number |
||
| 260 | * @param integer $limit pagination: records per page (max 500) |
||
| 261 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 262 | * @return array associative array containing the response |
||
| 263 | */ |
||
| 264 | 2 | public function addressUnspentOutputs($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 273 | |||
| 274 | /** |
||
| 275 | * get all unspent outputs for a batch of addresses (paginated) |
||
| 276 | * |
||
| 277 | * @param string[] $addresses |
||
| 278 | * @param integer $page pagination: page number |
||
| 279 | * @param integer $limit pagination: records per page (max 500) |
||
| 280 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 281 | * @return array associative array containing the response |
||
| 282 | * @throws \Exception |
||
| 283 | */ |
||
| 284 | public function batchAddressUnspentOutputs($addresses, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * verify ownership of an address |
||
| 311 | * @param string $address address hash |
||
| 312 | * @param string $signature a signed message (the address hash) using the private key of the address |
||
| 313 | * @return array associative array containing the response |
||
| 314 | */ |
||
| 315 | 2 | public function verifyAddress($address, $signature) { |
|
| 322 | |||
| 323 | /** |
||
| 324 | * get all blocks (paginated) |
||
| 325 | * @param integer $page pagination: page number |
||
| 326 | * @param integer $limit pagination: records per page |
||
| 327 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 328 | * @return array associative array containing the response |
||
| 329 | */ |
||
| 330 | 2 | public function allBlocks($page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 339 | |||
| 340 | /** |
||
| 341 | * get the latest block |
||
| 342 | * @return array associative array containing the response |
||
| 343 | */ |
||
| 344 | 1 | public function blockLatest() { |
|
| 348 | |||
| 349 | /** |
||
| 350 | * get an individual block |
||
| 351 | * @param string|integer $block a block hash or a block height |
||
| 352 | * @return array associative array containing the response |
||
| 353 | */ |
||
| 354 | 4 | public function block($block) { |
|
| 358 | |||
| 359 | /** |
||
| 360 | * get all transaction in a block (paginated) |
||
| 361 | * @param string|integer $block a block hash or a block height |
||
| 362 | * @param integer $page pagination: page number |
||
| 363 | * @param integer $limit pagination: records per page |
||
| 364 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 365 | * @return array associative array containing the response |
||
| 366 | */ |
||
| 367 | 1 | public function blockTransactions($block, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 376 | |||
| 377 | /** |
||
| 378 | * get a single transaction |
||
| 379 | * @param string $txhash transaction hash |
||
| 380 | * @return array associative array containing the response |
||
| 381 | */ |
||
| 382 | 2 | public function transaction($txhash) { |
|
| 392 | |||
| 393 | /** |
||
| 394 | * get a single transaction |
||
| 395 | * @param string[] $txhashes list of transaction hashes (up to 20) |
||
| 396 | * @return array[] array containing the response |
||
| 397 | */ |
||
| 398 | 2 | public function transactions($txhashes) { |
|
| 402 | |||
| 403 | /** |
||
| 404 | * get a paginated list of all webhooks associated with the api user |
||
| 405 | * @param integer $page pagination: page number |
||
| 406 | * @param integer $limit pagination: records per page |
||
| 407 | * @return array associative array containing the response |
||
| 408 | */ |
||
| 409 | public function allWebhooks($page = 1, $limit = 20) { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * get an existing webhook by it's identifier |
||
| 420 | * @param string $identifier a unique identifier associated with the webhook |
||
| 421 | * @return array associative array containing the response |
||
| 422 | */ |
||
| 423 | public function getWebhook($identifier) { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * create a new webhook |
||
| 430 | * @param string $url the url to receive the webhook events |
||
| 431 | * @param string $identifier a unique identifier to associate with this webhook |
||
| 432 | * @return array associative array containing the response |
||
| 433 | */ |
||
| 434 | public function setupWebhook($url, $identifier = null) { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * update an existing webhook |
||
| 445 | * @param string $identifier the unique identifier of the webhook to update |
||
| 446 | * @param string $newUrl the new url to receive the webhook events |
||
| 447 | * @param string $newIdentifier a new unique identifier to associate with this webhook |
||
| 448 | * @return array associative array containing the response |
||
| 449 | */ |
||
| 450 | public function updateWebhook($identifier, $newUrl = null, $newIdentifier = null) { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * deletes an existing webhook and any event subscriptions associated with it |
||
| 461 | * @param string $identifier the unique identifier of the webhook to delete |
||
| 462 | * @return boolean true on success |
||
| 463 | */ |
||
| 464 | public function deleteWebhook($identifier) { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * get a paginated list of all the events a webhook is subscribed to |
||
| 471 | * @param string $identifier the unique identifier of the webhook |
||
| 472 | * @param integer $page pagination: page number |
||
| 473 | * @param integer $limit pagination: records per page |
||
| 474 | * @return array associative array containing the response |
||
| 475 | */ |
||
| 476 | public function getWebhookEvents($identifier, $page = 1, $limit = 20) { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * subscribes a webhook to transaction events of one particular transaction |
||
| 487 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 488 | * @param string $transaction the transaction hash |
||
| 489 | * @param integer $confirmations the amount of confirmations to send. |
||
| 490 | * @return array associative array containing the response |
||
| 491 | */ |
||
| 492 | public function subscribeTransaction($identifier, $transaction, $confirmations = 6) { |
||
| 501 | |||
| 502 | /** |
||
| 503 | * subscribes a webhook to transaction events on a particular address |
||
| 504 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 505 | * @param string $address the address hash |
||
| 506 | * @param integer $confirmations the amount of confirmations to send. |
||
| 507 | * @return array associative array containing the response |
||
| 508 | */ |
||
| 509 | public function subscribeAddressTransactions($identifier, $address, $confirmations = 6) { |
||
| 518 | |||
| 519 | /** |
||
| 520 | * batch subscribes a webhook to multiple transaction events |
||
| 521 | * |
||
| 522 | * @param string $identifier the unique identifier of the webhook |
||
| 523 | * @param array $batchData A 2D array of event data: |
||
| 524 | * [address => $address, confirmations => $confirmations] |
||
| 525 | * where $address is the address to subscibe to |
||
| 526 | * and optionally $confirmations is the amount of confirmations |
||
| 527 | * @return boolean true on success |
||
| 528 | */ |
||
| 529 | public function batchSubscribeAddressTransactions($identifier, $batchData) { |
||
| 541 | |||
| 542 | /** |
||
| 543 | * subscribes a webhook to a new block event |
||
| 544 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 545 | * @return array associative array containing the response |
||
| 546 | */ |
||
| 547 | public function subscribeNewBlocks($identifier) { |
||
| 554 | |||
| 555 | /** |
||
| 556 | * removes an transaction event subscription from a webhook |
||
| 557 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 558 | * @param string $transaction the transaction hash of the event subscription |
||
| 559 | * @return boolean true on success |
||
| 560 | */ |
||
| 561 | public function unsubscribeTransaction($identifier, $transaction) { |
||
| 565 | |||
| 566 | /** |
||
| 567 | * removes an address transaction event subscription from a webhook |
||
| 568 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 569 | * @param string $address the address hash of the event subscription |
||
| 570 | * @return boolean true on success |
||
| 571 | */ |
||
| 572 | public function unsubscribeAddressTransactions($identifier, $address) { |
||
| 576 | |||
| 577 | /** |
||
| 578 | * removes a block event subscription from a webhook |
||
| 579 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 580 | * @return boolean true on success |
||
| 581 | */ |
||
| 582 | public function unsubscribeNewBlocks($identifier) { |
||
| 586 | |||
| 587 | /** |
||
| 588 | * create a new wallet |
||
| 589 | * - will generate a new primary seed (with password) and backup seed (without password) |
||
| 590 | * - send the primary seed (BIP39 'encrypted') and backup public key to the server |
||
| 591 | * - receive the blocktrail co-signing public key from the server |
||
| 592 | * |
||
| 593 | * Either takes one argument: |
||
| 594 | * @param array $options |
||
| 595 | * |
||
| 596 | * Or takes three arguments (old, deprecated syntax): |
||
| 597 | * (@nonPHP-doc) @param $identifier |
||
| 598 | * (@nonPHP-doc) @param $password |
||
| 599 | * (@nonPHP-doc) @param int $keyIndex override for the blocktrail cosigning key to use |
||
| 600 | * |
||
| 601 | * @return array[WalletInterface, array] list($wallet, $backupInfo) |
||
| 602 | * @throws \Exception |
||
| 603 | */ |
||
| 604 | public function createNewWallet($options) { |
||
| 648 | |||
| 649 | protected function createNewWalletV1($options) { |
||
| 769 | |||
| 770 | public function randomBits($bits) { |
||
| 773 | |||
| 774 | public function randomBytes($bytes) { |
||
| 777 | |||
| 778 | protected function createNewWalletV2($options) { |
||
| 898 | |||
| 899 | protected function createNewWalletV3($options) { |
||
| 1037 | |||
| 1038 | public function newV2PrimarySeed() { |
||
| 1041 | |||
| 1042 | public function newV2BackupSeed() { |
||
| 1045 | |||
| 1046 | public function newV2Secret($passphrase) { |
||
| 1052 | |||
| 1053 | public function newV2EncryptedPrimarySeed($primarySeed, $secret) { |
||
| 1056 | |||
| 1057 | public function newV2RecoverySecret($secret) { |
||
| 1063 | |||
| 1064 | public function newV3PrimarySeed() { |
||
| 1067 | |||
| 1068 | public function newV3BackupSeed() { |
||
| 1071 | |||
| 1072 | public function newV3Secret($passphrase) { |
||
| 1079 | |||
| 1080 | public function newV3EncryptedPrimarySeed(Buffer $primarySeed, Buffer $secret) { |
||
| 1084 | |||
| 1085 | public function newV3RecoverySecret(Buffer $secret) { |
||
| 1092 | |||
| 1093 | /** |
||
| 1094 | * @param array $bip32Key |
||
| 1095 | * @throws BlocktrailSDKException |
||
| 1096 | */ |
||
| 1097 | private function verifyPublicBIP32Key(array $bip32Key) { |
||
| 1107 | |||
| 1108 | /** |
||
| 1109 | * @param array $walletData |
||
| 1110 | * @throws BlocktrailSDKException |
||
| 1111 | */ |
||
| 1112 | private function verifyPublicOnly(array $walletData) { |
||
| 1116 | |||
| 1117 | /** |
||
| 1118 | * create wallet using the API |
||
| 1119 | * |
||
| 1120 | * @param string $identifier the wallet identifier to create |
||
| 1121 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1122 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1123 | * @param string $primaryMnemonic mnemonic to store |
||
| 1124 | * @param string $checksum checksum to store |
||
| 1125 | * @param int $keyIndex account that we expect to use |
||
| 1126 | * @param bool $segwit opt in to segwit |
||
| 1127 | * @return mixed |
||
| 1128 | */ |
||
| 1129 | public function storeNewWalletV1($identifier, $primaryPublicKey, $backupPublicKey, $primaryMnemonic, $checksum, $keyIndex, $segwit = false) { |
||
| 1143 | |||
| 1144 | /** |
||
| 1145 | * create wallet using the API |
||
| 1146 | * |
||
| 1147 | * @param string $identifier the wallet identifier to create |
||
| 1148 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1149 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1150 | * @param $encryptedPrimarySeed |
||
| 1151 | * @param $encryptedSecret |
||
| 1152 | * @param $recoverySecret |
||
| 1153 | * @param string $checksum checksum to store |
||
| 1154 | * @param int $keyIndex account that we expect to use |
||
| 1155 | * @param bool $segwit opt in to segwit |
||
| 1156 | * @return mixed |
||
| 1157 | * @throws \Exception |
||
| 1158 | */ |
||
| 1159 | public function storeNewWalletV2($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false) { |
||
| 1176 | |||
| 1177 | /** |
||
| 1178 | * create wallet using the API |
||
| 1179 | * |
||
| 1180 | * @param string $identifier the wallet identifier to create |
||
| 1181 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1182 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1183 | * @param $encryptedPrimarySeed |
||
| 1184 | * @param $encryptedSecret |
||
| 1185 | * @param $recoverySecret |
||
| 1186 | * @param string $checksum checksum to store |
||
| 1187 | * @param int $keyIndex account that we expect to use |
||
| 1188 | * @param bool $segwit opt in to segwit |
||
| 1189 | * @return mixed |
||
| 1190 | * @throws \Exception |
||
| 1191 | */ |
||
| 1192 | public function storeNewWalletV3($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false) { |
||
| 1211 | |||
| 1212 | /** |
||
| 1213 | * upgrade wallet to use a new account number |
||
| 1214 | * the account number specifies which blocktrail cosigning key is used |
||
| 1215 | * |
||
| 1216 | * @param string $identifier the wallet identifier to be upgraded |
||
| 1217 | * @param int $keyIndex the new account to use |
||
| 1218 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1219 | * @return mixed |
||
| 1220 | */ |
||
| 1221 | public function upgradeKeyIndex($identifier, $keyIndex, $primaryPublicKey) { |
||
| 1230 | |||
| 1231 | /** |
||
| 1232 | * @param array $options |
||
| 1233 | * @return AddressReaderBase |
||
| 1234 | */ |
||
| 1235 | private function makeAddressReader(array $options) { |
||
| 1246 | |||
| 1247 | /** |
||
| 1248 | * initialize a previously created wallet |
||
| 1249 | * |
||
| 1250 | * Takes an options object, or accepts identifier/password for backwards compatiblity. |
||
| 1251 | * |
||
| 1252 | * Some of the options: |
||
| 1253 | * - "readonly/readOnly/read-only" can be to a boolean value, |
||
| 1254 | * so the wallet is loaded in read-only mode (no private key) |
||
| 1255 | * - "check_backup_key" can be set to your own backup key: |
||
| 1256 | * Format: ["M', "xpub..."] |
||
| 1257 | * Setting this will allow the SDK to check the server hasn't |
||
| 1258 | * a different key (one it happens to control) |
||
| 1259 | |||
| 1260 | * Either takes one argument: |
||
| 1261 | * @param array $options |
||
| 1262 | * |
||
| 1263 | * Or takes two arguments (old, deprecated syntax): |
||
| 1264 | * (@nonPHP-doc) @param string $identifier the wallet identifier to be initialized |
||
| 1265 | * (@nonPHP-doc) @param string $password the password to decrypt the mnemonic with |
||
| 1266 | * |
||
| 1267 | * @return WalletInterface |
||
| 1268 | * @throws \Exception |
||
| 1269 | */ |
||
| 1270 | public function initWallet($options) { |
||
| 1382 | |||
| 1383 | /** |
||
| 1384 | * get the wallet data from the server |
||
| 1385 | * |
||
| 1386 | * @param string $identifier the identifier of the wallet |
||
| 1387 | * @return mixed |
||
| 1388 | */ |
||
| 1389 | public function getWallet($identifier) { |
||
| 1393 | |||
| 1394 | /** |
||
| 1395 | * update the wallet data on the server |
||
| 1396 | * |
||
| 1397 | * @param string $identifier |
||
| 1398 | * @param $data |
||
| 1399 | * @return mixed |
||
| 1400 | */ |
||
| 1401 | public function updateWallet($identifier, $data) { |
||
| 1405 | |||
| 1406 | /** |
||
| 1407 | * delete a wallet from the server |
||
| 1408 | * the checksum address and a signature to verify you ownership of the key of that checksum address |
||
| 1409 | * is required to be able to delete a wallet |
||
| 1410 | * |
||
| 1411 | * @param string $identifier the identifier of the wallet |
||
| 1412 | * @param string $checksumAddress the address for your master private key (and the checksum used when creating the wallet) |
||
| 1413 | * @param string $signature a signature of the checksum address as message signed by the private key matching that address |
||
| 1414 | * @param bool $force ignore warnings (such as a non-zero balance) |
||
| 1415 | * @return mixed |
||
| 1416 | */ |
||
| 1417 | public function deleteWallet($identifier, $checksumAddress, $signature, $force = false) { |
||
| 1424 | |||
| 1425 | /** |
||
| 1426 | * create new backup key; |
||
| 1427 | * 1) a BIP39 mnemonic |
||
| 1428 | * 2) a seed from that mnemonic with a blank password |
||
| 1429 | * 3) a private key from that seed |
||
| 1430 | * |
||
| 1431 | * @return array [mnemonic, seed, key] |
||
| 1432 | */ |
||
| 1433 | protected function newV1BackupSeed() { |
||
| 1438 | |||
| 1439 | /** |
||
| 1440 | * create new primary key; |
||
| 1441 | * 1) a BIP39 mnemonic |
||
| 1442 | * 2) a seed from that mnemonic with the password |
||
| 1443 | * 3) a private key from that seed |
||
| 1444 | * |
||
| 1445 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1446 | * @return array [mnemonic, seed, key] |
||
| 1447 | * @TODO: require a strong password? |
||
| 1448 | */ |
||
| 1449 | protected function newV1PrimarySeed($passphrase) { |
||
| 1454 | |||
| 1455 | /** |
||
| 1456 | * create a new key; |
||
| 1457 | * 1) a BIP39 mnemonic |
||
| 1458 | * 2) a seed from that mnemonic with the password |
||
| 1459 | * 3) a private key from that seed |
||
| 1460 | * |
||
| 1461 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1462 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1463 | * @return array |
||
| 1464 | */ |
||
| 1465 | protected function generateNewSeed($passphrase = "", $forceEntropy = null) { |
||
| 1482 | |||
| 1483 | /** |
||
| 1484 | * generate a new mnemonic from some random entropy (512 bit) |
||
| 1485 | * |
||
| 1486 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1487 | * @return string |
||
| 1488 | * @throws \Exception |
||
| 1489 | */ |
||
| 1490 | public function generateNewMnemonic($forceEntropy = null) { |
||
| 1500 | |||
| 1501 | /** |
||
| 1502 | * get the balance for the wallet |
||
| 1503 | * |
||
| 1504 | * @param string $identifier the identifier of the wallet |
||
| 1505 | * @return array |
||
| 1506 | */ |
||
| 1507 | public function getWalletBalance($identifier) { |
||
| 1511 | |||
| 1512 | /** |
||
| 1513 | * get a new derivation number for specified parent path |
||
| 1514 | * 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 |
||
| 1515 | * |
||
| 1516 | * returns the path |
||
| 1517 | * |
||
| 1518 | * @param string $identifier the identifier of the wallet |
||
| 1519 | * @param string $path the parent path for which to get a new derivation |
||
| 1520 | * @return string |
||
| 1521 | */ |
||
| 1522 | public function getNewDerivation($identifier, $path) { |
||
| 1526 | |||
| 1527 | /** |
||
| 1528 | * get a new derivation number for specified parent path |
||
| 1529 | * 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 |
||
| 1530 | * |
||
| 1531 | * @param string $identifier the identifier of the wallet |
||
| 1532 | * @param string $path the parent path for which to get a new derivation |
||
| 1533 | * @return mixed |
||
| 1534 | */ |
||
| 1535 | public function _getNewDerivation($identifier, $path) { |
||
| 1539 | |||
| 1540 | /** |
||
| 1541 | * get the path (and redeemScript) to specified address |
||
| 1542 | * |
||
| 1543 | * @param string $identifier |
||
| 1544 | * @param string $address |
||
| 1545 | * @return array |
||
| 1546 | * @throws \Exception |
||
| 1547 | */ |
||
| 1548 | public function getPathForAddress($identifier, $address) { |
||
| 1552 | |||
| 1553 | /** |
||
| 1554 | * send the transaction using the API |
||
| 1555 | * |
||
| 1556 | * @param string $identifier the identifier of the wallet |
||
| 1557 | * @param string|array $rawTransaction raw hex of the transaction (should be partially signed) |
||
| 1558 | * @param array $paths list of the paths that were used for the UTXO |
||
| 1559 | * @param bool $checkFee let the server verify the fee after signing |
||
| 1560 | * @param null $twoFactorToken |
||
| 1561 | * @return string the complete raw transaction |
||
| 1562 | * @throws \Exception |
||
| 1563 | */ |
||
| 1564 | public function sendTransaction($identifier, $rawTransaction, $paths, $checkFee = false, $twoFactorToken = null) { |
||
| 1595 | |||
| 1596 | /** |
||
| 1597 | * use the API to get the best inputs to use based on the outputs |
||
| 1598 | * |
||
| 1599 | * the return array has the following format: |
||
| 1600 | * [ |
||
| 1601 | * "utxos" => [ |
||
| 1602 | * [ |
||
| 1603 | * "hash" => "<txHash>", |
||
| 1604 | * "idx" => "<index of the output of that <txHash>", |
||
| 1605 | * "scriptpubkey_hex" => "<scriptPubKey-hex>", |
||
| 1606 | * "value" => 32746327, |
||
| 1607 | * "address" => "1address", |
||
| 1608 | * "path" => "m/44'/1'/0'/0/13", |
||
| 1609 | * "redeem_script" => "<redeemScript-hex>", |
||
| 1610 | * ], |
||
| 1611 | * ], |
||
| 1612 | * "fee" => 10000, |
||
| 1613 | * "change"=> 1010109201, |
||
| 1614 | * ] |
||
| 1615 | * |
||
| 1616 | * @param string $identifier the identifier of the wallet |
||
| 1617 | * @param array $outputs the outputs you want to create - array[address => satoshi-value] |
||
| 1618 | * @param bool $lockUTXO when TRUE the UTXOs selected will be locked for a few seconds |
||
| 1619 | * so you have some time to spend them without race-conditions |
||
| 1620 | * @param bool $allowZeroConf |
||
| 1621 | * @param string $feeStrategy |
||
| 1622 | * @param null|int $forceFee |
||
| 1623 | * @return array |
||
| 1624 | * @throws \Exception |
||
| 1625 | */ |
||
| 1626 | public function coinSelection($identifier, $outputs, $lockUTXO = false, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null) { |
||
| 1648 | |||
| 1649 | /** |
||
| 1650 | * |
||
| 1651 | * @param string $identifier the identifier of the wallet |
||
| 1652 | * @param bool $allowZeroConf |
||
| 1653 | * @param string $feeStrategy |
||
| 1654 | * @param null|int $forceFee |
||
| 1655 | * @param int $outputCnt |
||
| 1656 | * @return array |
||
| 1657 | * @throws \Exception |
||
| 1658 | */ |
||
| 1659 | public function walletMaxSpendable($identifier, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null, $outputCnt = 1) { |
||
| 1678 | |||
| 1679 | /** |
||
| 1680 | * @return array ['optimal_fee' => 10000, 'low_priority_fee' => 5000] |
||
| 1681 | */ |
||
| 1682 | public function feePerKB() { |
||
| 1686 | |||
| 1687 | /** |
||
| 1688 | * get the current price index |
||
| 1689 | * |
||
| 1690 | * @return array eg; ['USD' => 287.30] |
||
| 1691 | */ |
||
| 1692 | 2 | public function price() { |
|
| 1696 | |||
| 1697 | /** |
||
| 1698 | * setup webhook for wallet |
||
| 1699 | * |
||
| 1700 | * @param string $identifier the wallet identifier for which to create the webhook |
||
| 1701 | * @param string $webhookIdentifier the webhook identifier to use |
||
| 1702 | * @param string $url the url to receive the webhook events |
||
| 1703 | * @return array |
||
| 1704 | */ |
||
| 1705 | public function setupWalletWebhook($identifier, $webhookIdentifier, $url) { |
||
| 1709 | |||
| 1710 | /** |
||
| 1711 | * delete webhook for wallet |
||
| 1712 | * |
||
| 1713 | * @param string $identifier the wallet identifier for which to delete the webhook |
||
| 1714 | * @param string $webhookIdentifier the webhook identifier to delete |
||
| 1715 | * @return array |
||
| 1716 | */ |
||
| 1717 | public function deleteWalletWebhook($identifier, $webhookIdentifier) { |
||
| 1721 | |||
| 1722 | /** |
||
| 1723 | * lock a specific unspent output |
||
| 1724 | * |
||
| 1725 | * @param $identifier |
||
| 1726 | * @param $txHash |
||
| 1727 | * @param $txIdx |
||
| 1728 | * @param int $ttl |
||
| 1729 | * @return bool |
||
| 1730 | */ |
||
| 1731 | public function lockWalletUTXO($identifier, $txHash, $txIdx, $ttl = 3) { |
||
| 1735 | |||
| 1736 | /** |
||
| 1737 | * unlock a specific unspent output |
||
| 1738 | * |
||
| 1739 | * @param $identifier |
||
| 1740 | * @param $txHash |
||
| 1741 | * @param $txIdx |
||
| 1742 | * @return bool |
||
| 1743 | */ |
||
| 1744 | public function unlockWalletUTXO($identifier, $txHash, $txIdx) { |
||
| 1748 | |||
| 1749 | /** |
||
| 1750 | * get all transactions for wallet (paginated) |
||
| 1751 | * |
||
| 1752 | * @param string $identifier the wallet identifier for which to get transactions |
||
| 1753 | * @param integer $page pagination: page number |
||
| 1754 | * @param integer $limit pagination: records per page (max 500) |
||
| 1755 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1756 | * @return array associative array containing the response |
||
| 1757 | */ |
||
| 1758 | public function walletTransactions($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 1767 | |||
| 1768 | /** |
||
| 1769 | * get all addresses for wallet (paginated) |
||
| 1770 | * |
||
| 1771 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1772 | * @param integer $page pagination: page number |
||
| 1773 | * @param integer $limit pagination: records per page (max 500) |
||
| 1774 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1775 | * @return array associative array containing the response |
||
| 1776 | */ |
||
| 1777 | public function walletAddresses($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 1786 | |||
| 1787 | /** |
||
| 1788 | * get all UTXOs for wallet (paginated) |
||
| 1789 | * |
||
| 1790 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1791 | * @param integer $page pagination: page number |
||
| 1792 | * @param integer $limit pagination: records per page (max 500) |
||
| 1793 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1794 | * @param boolean $zeroconf include zero confirmation transactions |
||
| 1795 | * @return array associative array containing the response |
||
| 1796 | */ |
||
| 1797 | public function walletUTXOs($identifier, $page = 1, $limit = 20, $sortDir = 'asc', $zeroconf = true) { |
||
| 1807 | |||
| 1808 | /** |
||
| 1809 | * get a paginated list of all wallets associated with the api user |
||
| 1810 | * |
||
| 1811 | * @param integer $page pagination: page number |
||
| 1812 | * @param integer $limit pagination: records per page |
||
| 1813 | * @return array associative array containing the response |
||
| 1814 | */ |
||
| 1815 | public function allWallets($page = 1, $limit = 20) { |
||
| 1823 | |||
| 1824 | /** |
||
| 1825 | * send raw transaction |
||
| 1826 | * |
||
| 1827 | * @param $txHex |
||
| 1828 | * @return bool |
||
| 1829 | */ |
||
| 1830 | public function sendRawTransaction($txHex) { |
||
| 1834 | |||
| 1835 | /** |
||
| 1836 | * testnet only ;-) |
||
| 1837 | * |
||
| 1838 | * @param $address |
||
| 1839 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1840 | * @return mixed |
||
| 1841 | * @throws \Exception |
||
| 1842 | */ |
||
| 1843 | public function faucetWithdrawal($address, $amount = 10000) { |
||
| 1850 | |||
| 1851 | /** |
||
| 1852 | * Exists for BC. Remove at major bump. |
||
| 1853 | * |
||
| 1854 | * @see faucetWithdrawal |
||
| 1855 | * @deprecated |
||
| 1856 | * @param $address |
||
| 1857 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1858 | * @return mixed |
||
| 1859 | * @throws \Exception |
||
| 1860 | */ |
||
| 1861 | public function faucetWithdrawl($address, $amount = 10000) { |
||
| 1864 | |||
| 1865 | /** |
||
| 1866 | * verify a message signed bitcoin-core style |
||
| 1867 | * |
||
| 1868 | * @param string $message |
||
| 1869 | * @param string $address |
||
| 1870 | * @param string $signature |
||
| 1871 | * @return boolean |
||
| 1872 | */ |
||
| 1873 | 2 | public function verifyMessage($message, $address, $signature) { |
|
| 1887 | |||
| 1888 | /** |
||
| 1889 | * Take a base58 or cashaddress, and return only |
||
| 1890 | * the cash address. |
||
| 1891 | * This function only works on bitcoin cash. |
||
| 1892 | * @param string $input |
||
| 1893 | * @return string |
||
| 1894 | * @throws BlocktrailSDKException |
||
| 1895 | */ |
||
| 1896 | public function getLegacyBitcoinCashAddress($input) { |
||
| 1913 | |||
| 1914 | /** |
||
| 1915 | * convert a Satoshi value to a BTC value |
||
| 1916 | * |
||
| 1917 | * @param int $satoshi |
||
| 1918 | * @return float |
||
| 1919 | */ |
||
| 1920 | 1 | public static function toBTC($satoshi) { |
|
| 1923 | |||
| 1924 | /** |
||
| 1925 | * convert a Satoshi value to a BTC value and return it as a string |
||
| 1926 | |||
| 1927 | * @param int $satoshi |
||
| 1928 | * @return string |
||
| 1929 | */ |
||
| 1930 | public static function toBTCString($satoshi) { |
||
| 1933 | |||
| 1934 | /** |
||
| 1935 | * convert a BTC value to a Satoshi value |
||
| 1936 | * |
||
| 1937 | * @param float $btc |
||
| 1938 | * @return string |
||
| 1939 | */ |
||
| 1940 | 1 | public static function toSatoshiString($btc) { |
|
| 1943 | |||
| 1944 | /** |
||
| 1945 | * convert a BTC value to a Satoshi value |
||
| 1946 | * |
||
| 1947 | * @param float $btc |
||
| 1948 | * @return string |
||
| 1949 | */ |
||
| 1950 | 1 | public static function toSatoshi($btc) { |
|
| 1953 | |||
| 1954 | /** |
||
| 1955 | * json_decode helper that throws exceptions when it fails to decode |
||
| 1956 | * |
||
| 1957 | * @param $json |
||
| 1958 | * @param bool $assoc |
||
| 1959 | * @return mixed |
||
| 1960 | * @throws \Exception |
||
| 1961 | */ |
||
| 1962 | 4 | public static function jsonDecode($json, $assoc = false) { |
|
| 1975 | |||
| 1976 | /** |
||
| 1977 | * sort public keys for multisig script |
||
| 1978 | * |
||
| 1979 | * @param PublicKeyInterface[] $pubKeys |
||
| 1980 | * @return PublicKeyInterface[] |
||
| 1981 | */ |
||
| 1982 | public static function sortMultisigKeys(array $pubKeys) { |
||
| 1992 | |||
| 1993 | /** |
||
| 1994 | * read and decode the json payload from a webhook's POST request. |
||
| 1995 | * |
||
| 1996 | * @param bool $returnObject flag to indicate if an object or associative array should be returned |
||
| 1997 | * @return mixed|null |
||
| 1998 | * @throws \Exception |
||
| 1999 | */ |
||
| 2000 | public static function getWebhookPayload($returnObject = false) { |
||
| 2008 | |||
| 2009 | public static function normalizeBIP32KeyArray($keys) { |
||
| 2014 | |||
| 2015 | /** |
||
| 2016 | * @param array|BIP32Key $key |
||
| 2017 | * @return BIP32Key |
||
| 2018 | * @throws \Exception |
||
| 2019 | */ |
||
| 2020 | public static function normalizeBIP32Key($key) { |
||
| 2038 | |||
| 2039 | public function shuffle($arr) { |
||
| 2042 | } |
||
| 2043 |
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: