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 |
||
| 34 | class BlocktrailSDK implements BlocktrailSDKInterface { |
||
| 35 | /** |
||
| 36 | * @var Connection\RestClientInterface |
||
| 37 | */ |
||
| 38 | protected $client; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string currently only supporting; bitcoin |
||
| 42 | */ |
||
| 43 | protected $network; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var bool |
||
| 47 | */ |
||
| 48 | protected $testnet; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param string $apiKey the API_KEY to use for authentication |
||
| 52 | * @param string $apiSecret the API_SECRET to use for authentication |
||
| 53 | * @param string $network the cryptocurrency 'network' to consume, eg BTC, LTC, etc |
||
| 54 | * @param bool $testnet testnet yes/no |
||
| 55 | * @param string $apiVersion the version of the API to consume |
||
| 56 | * @param null $apiEndpoint overwrite the endpoint used |
||
| 57 | * this will cause the $network, $testnet and $apiVersion to be ignored! |
||
| 58 | */ |
||
| 59 | 87 | public function __construct($apiKey, $apiSecret, $network = 'BTC', $testnet = false, $apiVersion = 'v1', $apiEndpoint = null) { |
|
| 74 | |||
| 75 | /** |
||
| 76 | * normalize network string |
||
| 77 | * |
||
| 78 | * @param $network |
||
| 79 | * @param $testnet |
||
| 80 | * @return array |
||
| 81 | * @throws \Exception |
||
| 82 | */ |
||
| 83 | 87 | protected function normalizeNetwork($network, $testnet) { |
|
| 86 | |||
| 87 | /** |
||
| 88 | * set BitcoinLib to the correct magic-byte defaults for the selected network |
||
| 89 | * |
||
| 90 | * @param $network |
||
| 91 | * @param $testnet |
||
| 92 | */ |
||
| 93 | 87 | protected function setBitcoinLibMagicBytes($network, $testnet) { |
|
| 97 | |||
| 98 | /** |
||
| 99 | * enable CURL debugging output |
||
| 100 | * |
||
| 101 | * @param bool $debug |
||
| 102 | * |
||
| 103 | * @codeCoverageIgnore |
||
| 104 | */ |
||
| 105 | public function setCurlDebugging($debug = true) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * enable verbose errors |
||
| 111 | * |
||
| 112 | * @param bool $verboseErrors |
||
| 113 | * |
||
| 114 | * @codeCoverageIgnore |
||
| 115 | */ |
||
| 116 | public function setVerboseErrors($verboseErrors = true) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * set cURL default option on Guzzle client |
||
| 122 | * @param string $key |
||
| 123 | * @param bool $value |
||
| 124 | * |
||
| 125 | * @codeCoverageIgnore |
||
| 126 | */ |
||
| 127 | public function setCurlDefaultOption($key, $value) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return RestClientInterface |
||
| 133 | */ |
||
| 134 | 2 | public function getRestClient() { |
|
| 137 | |||
| 138 | /** |
||
| 139 | * @param RestClientInterface $restClient |
||
| 140 | */ |
||
| 141 | public function setRestClient(RestClientInterface $restClient) { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * get a single address |
||
| 147 | * @param string $address address hash |
||
| 148 | * @return array associative array containing the response |
||
| 149 | */ |
||
| 150 | 1 | public function address($address) { |
|
| 154 | |||
| 155 | /** |
||
| 156 | * get all transactions for an address (paginated) |
||
| 157 | * @param string $address address hash |
||
| 158 | * @param integer $page pagination: page number |
||
| 159 | * @param integer $limit pagination: records per page (max 500) |
||
| 160 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 161 | * @return array associative array containing the response |
||
| 162 | */ |
||
| 163 | 1 | public function addressTransactions($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 172 | |||
| 173 | /** |
||
| 174 | * get all unconfirmed transactions for an address (paginated) |
||
| 175 | * @param string $address address hash |
||
| 176 | * @param integer $page pagination: page number |
||
| 177 | * @param integer $limit pagination: records per page (max 500) |
||
| 178 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 179 | * @return array associative array containing the response |
||
| 180 | */ |
||
| 181 | 1 | public function addressUnconfirmedTransactions($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 190 | |||
| 191 | /** |
||
| 192 | * get all unspent outputs for an address (paginated) |
||
| 193 | * @param string $address address hash |
||
| 194 | * @param integer $page pagination: page number |
||
| 195 | * @param integer $limit pagination: records per page (max 500) |
||
| 196 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 197 | * @return array associative array containing the response |
||
| 198 | */ |
||
| 199 | 1 | public function addressUnspentOutputs($address, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 208 | |||
| 209 | /** |
||
| 210 | * get all unspent outputs for a batch of addresses (paginated) |
||
| 211 | * |
||
| 212 | * @param string[] $addresses |
||
| 213 | * @param integer $page pagination: page number |
||
| 214 | * @param integer $limit pagination: records per page (max 500) |
||
| 215 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 216 | * @return array associative array containing the response |
||
| 217 | * @throws \Exception |
||
| 218 | */ |
||
| 219 | public function batchAddressUnspentOutputs($addresses, $page = 1, $limit = 20, $sortDir = 'asc') { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * verify ownership of an address |
||
| 231 | * @param string $address address hash |
||
| 232 | * @param string $signature a signed message (the address hash) using the private key of the address |
||
| 233 | * @return array associative array containing the response |
||
| 234 | */ |
||
| 235 | 2 | public function verifyAddress($address, $signature) { |
|
| 242 | |||
| 243 | /** |
||
| 244 | * get all blocks (paginated) |
||
| 245 | * @param integer $page pagination: page number |
||
| 246 | * @param integer $limit pagination: records per page |
||
| 247 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 248 | * @return array associative array containing the response |
||
| 249 | */ |
||
| 250 | 1 | public function allBlocks($page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 259 | |||
| 260 | /** |
||
| 261 | * get the latest block |
||
| 262 | * @return array associative array containing the response |
||
| 263 | */ |
||
| 264 | 1 | public function blockLatest() { |
|
| 268 | |||
| 269 | /** |
||
| 270 | * get an individual block |
||
| 271 | * @param string|integer $block a block hash or a block height |
||
| 272 | * @return array associative array containing the response |
||
| 273 | */ |
||
| 274 | 1 | public function block($block) { |
|
| 278 | |||
| 279 | /** |
||
| 280 | * get all transaction in a block (paginated) |
||
| 281 | * @param string|integer $block a block hash or a block height |
||
| 282 | * @param integer $page pagination: page number |
||
| 283 | * @param integer $limit pagination: records per page |
||
| 284 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 285 | * @return array associative array containing the response |
||
| 286 | */ |
||
| 287 | 1 | public function blockTransactions($block, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 296 | |||
| 297 | /** |
||
| 298 | * get a single transaction |
||
| 299 | * @param string $txhash transaction hash |
||
| 300 | * @return array associative array containing the response |
||
| 301 | */ |
||
| 302 | 5 | public function transaction($txhash) { |
|
| 306 | |||
| 307 | /** |
||
| 308 | * get a single transaction |
||
| 309 | * @param string[] $txhashes list of transaction hashes (up to 20) |
||
| 310 | * @return array[] array containing the response |
||
| 311 | */ |
||
| 312 | public function transactions($txhashes) { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * get a paginated list of all webhooks associated with the api user |
||
| 319 | * @param integer $page pagination: page number |
||
| 320 | * @param integer $limit pagination: records per page |
||
| 321 | * @return array associative array containing the response |
||
| 322 | */ |
||
| 323 | 1 | public function allWebhooks($page = 1, $limit = 20) { |
|
| 331 | |||
| 332 | /** |
||
| 333 | * get an existing webhook by it's identifier |
||
| 334 | * @param string $identifier a unique identifier associated with the webhook |
||
| 335 | * @return array associative array containing the response |
||
| 336 | */ |
||
| 337 | 1 | public function getWebhook($identifier) { |
|
| 341 | |||
| 342 | /** |
||
| 343 | * create a new webhook |
||
| 344 | * @param string $url the url to receive the webhook events |
||
| 345 | * @param string $identifier a unique identifier to associate with this webhook |
||
| 346 | * @return array associative array containing the response |
||
| 347 | */ |
||
| 348 | 1 | public function setupWebhook($url, $identifier = null) { |
|
| 356 | |||
| 357 | /** |
||
| 358 | * update an existing webhook |
||
| 359 | * @param string $identifier the unique identifier of the webhook to update |
||
| 360 | * @param string $newUrl the new url to receive the webhook events |
||
| 361 | * @param string $newIdentifier a new unique identifier to associate with this webhook |
||
| 362 | * @return array associative array containing the response |
||
| 363 | */ |
||
| 364 | 1 | public function updateWebhook($identifier, $newUrl = null, $newIdentifier = null) { |
|
| 372 | |||
| 373 | /** |
||
| 374 | * deletes an existing webhook and any event subscriptions associated with it |
||
| 375 | * @param string $identifier the unique identifier of the webhook to delete |
||
| 376 | * @return boolean true on success |
||
| 377 | */ |
||
| 378 | 1 | public function deleteWebhook($identifier) { |
|
| 382 | |||
| 383 | /** |
||
| 384 | * get a paginated list of all the events a webhook is subscribed to |
||
| 385 | * @param string $identifier the unique identifier of the webhook |
||
| 386 | * @param integer $page pagination: page number |
||
| 387 | * @param integer $limit pagination: records per page |
||
| 388 | * @return array associative array containing the response |
||
| 389 | */ |
||
| 390 | 2 | public function getWebhookEvents($identifier, $page = 1, $limit = 20) { |
|
| 398 | |||
| 399 | /** |
||
| 400 | * subscribes a webhook to transaction events of one particular transaction |
||
| 401 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 402 | * @param string $transaction the transaction hash |
||
| 403 | * @param integer $confirmations the amount of confirmations to send. |
||
| 404 | * @return array associative array containing the response |
||
| 405 | */ |
||
| 406 | 1 | public function subscribeTransaction($identifier, $transaction, $confirmations = 6) { |
|
| 415 | |||
| 416 | /** |
||
| 417 | * subscribes a webhook to transaction events on a particular address |
||
| 418 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 419 | * @param string $address the address hash |
||
| 420 | * @param integer $confirmations the amount of confirmations to send. |
||
| 421 | * @return array associative array containing the response |
||
| 422 | */ |
||
| 423 | 1 | public function subscribeAddressTransactions($identifier, $address, $confirmations = 6) { |
|
| 432 | |||
| 433 | /** |
||
| 434 | * batch subscribes a webhook to multiple transaction events |
||
| 435 | * |
||
| 436 | * @param string $identifier the unique identifier of the webhook |
||
| 437 | * @param array $batchData A 2D array of event data: |
||
| 438 | * [address => $address, confirmations => $confirmations] |
||
| 439 | * where $address is the address to subscibe to |
||
| 440 | * and optionally $confirmations is the amount of confirmations |
||
| 441 | * @return boolean true on success |
||
| 442 | */ |
||
| 443 | 1 | public function batchSubscribeAddressTransactions($identifier, $batchData) { |
|
| 455 | |||
| 456 | /** |
||
| 457 | * subscribes a webhook to a new block event |
||
| 458 | * @param string $identifier the unique identifier of the webhook to be triggered |
||
| 459 | * @return array associative array containing the response |
||
| 460 | */ |
||
| 461 | 1 | public function subscribeNewBlocks($identifier) { |
|
| 468 | |||
| 469 | /** |
||
| 470 | * removes an transaction event subscription from a webhook |
||
| 471 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 472 | * @param string $transaction the transaction hash of the event subscription |
||
| 473 | * @return boolean true on success |
||
| 474 | */ |
||
| 475 | 1 | public function unsubscribeTransaction($identifier, $transaction) { |
|
| 479 | |||
| 480 | /** |
||
| 481 | * removes an address transaction event subscription from a webhook |
||
| 482 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 483 | * @param string $address the address hash of the event subscription |
||
| 484 | * @return boolean true on success |
||
| 485 | */ |
||
| 486 | 1 | public function unsubscribeAddressTransactions($identifier, $address) { |
|
| 490 | |||
| 491 | /** |
||
| 492 | * removes a block event subscription from a webhook |
||
| 493 | * @param string $identifier the unique identifier of the webhook associated with the event subscription |
||
| 494 | * @return boolean true on success |
||
| 495 | */ |
||
| 496 | 1 | public function unsubscribeNewBlocks($identifier) { |
|
| 500 | |||
| 501 | /** |
||
| 502 | * create a new wallet |
||
| 503 | * - will generate a new primary seed (with password) and backup seed (without password) |
||
| 504 | * - send the primary seed (BIP39 'encrypted') and backup public key to the server |
||
| 505 | * - receive the blocktrail co-signing public key from the server |
||
| 506 | * |
||
| 507 | * Either takes one argument: |
||
| 508 | * @param array $options |
||
| 509 | * |
||
| 510 | * Or takes three arguments (old, deprecated syntax): |
||
| 511 | * (@nonPHP-doc) @param $identifier |
||
| 512 | * (@nonPHP-doc) @param $password |
||
| 513 | * (@nonPHP-doc) @param int $keyIndex override for the blocktrail cosigning key to use |
||
| 514 | * |
||
| 515 | * @return array[WalletInterface, array] list($wallet, $backupInfo) |
||
| 516 | * @throws \Exception |
||
| 517 | */ |
||
| 518 | 7 | public function createNewWallet($options) { |
|
| 562 | |||
| 563 | 1 | protected function createNewWalletV1($options) { |
|
| 681 | |||
| 682 | 5 | public static function randomBits($bits) { |
|
| 685 | |||
| 686 | 5 | public static function randomBytes($bytes) { |
|
| 689 | |||
| 690 | 2 | protected function createNewWalletV2($options) { |
|
| 811 | |||
| 812 | 4 | protected function createNewWalletV3($options) { |
|
| 951 | |||
| 952 | /** |
||
| 953 | * @param array $bip32Key |
||
| 954 | * @throws BlocktrailSDKException |
||
| 955 | */ |
||
| 956 | 10 | private function verifyPublicBIP32Key(array $bip32Key) { |
|
| 966 | |||
| 967 | /** |
||
| 968 | * @param array $walletData |
||
| 969 | * @throws BlocktrailSDKException |
||
| 970 | */ |
||
| 971 | 10 | private function verifyPublicOnly(array $walletData) { |
|
| 975 | |||
| 976 | /** |
||
| 977 | * create wallet using the API |
||
| 978 | * |
||
| 979 | * @param string $identifier the wallet identifier to create |
||
| 980 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 981 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 982 | * @param string $primaryMnemonic mnemonic to store |
||
| 983 | * @param string $checksum checksum to store |
||
| 984 | * @param int $keyIndex account that we expect to use |
||
| 985 | * @param bool $segwit opt in to segwit |
||
| 986 | * @return mixed |
||
| 987 | */ |
||
| 988 | 1 | public function storeNewWalletV1($identifier, $primaryPublicKey, $backupPublicKey, $primaryMnemonic, $checksum, $keyIndex, $segwit = false) { |
|
| 1002 | |||
| 1003 | /** |
||
| 1004 | * create wallet using the API |
||
| 1005 | * |
||
| 1006 | * @param string $identifier the wallet identifier to create |
||
| 1007 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1008 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1009 | * @param $encryptedPrimarySeed |
||
| 1010 | * @param $encryptedSecret |
||
| 1011 | * @param $recoverySecret |
||
| 1012 | * @param string $checksum checksum to store |
||
| 1013 | * @param int $keyIndex account that we expect to use |
||
| 1014 | * @param bool $segwit opt in to segwit |
||
| 1015 | * @return mixed |
||
| 1016 | * @throws \Exception |
||
| 1017 | */ |
||
| 1018 | 5 | public function storeNewWalletV2($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false) { |
|
| 1035 | |||
| 1036 | /** |
||
| 1037 | * create wallet using the API |
||
| 1038 | * |
||
| 1039 | * @param string $identifier the wallet identifier to create |
||
| 1040 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1041 | * @param array $backupPublicKey BIP32 extended public key - [backup key, path "M"] |
||
| 1042 | * @param $encryptedPrimarySeed |
||
| 1043 | * @param $encryptedSecret |
||
| 1044 | * @param $recoverySecret |
||
| 1045 | * @param string $checksum checksum to store |
||
| 1046 | * @param int $keyIndex account that we expect to use |
||
| 1047 | * @param bool $segwit opt in to segwit |
||
| 1048 | * @return mixed |
||
| 1049 | * @throws \Exception |
||
| 1050 | */ |
||
| 1051 | 4 | public function storeNewWalletV3($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex, $segwit = false) { |
|
| 1070 | |||
| 1071 | /** |
||
| 1072 | * upgrade wallet to use a new account number |
||
| 1073 | * the account number specifies which blocktrail cosigning key is used |
||
| 1074 | * |
||
| 1075 | * @param string $identifier the wallet identifier to be upgraded |
||
| 1076 | * @param int $keyIndex the new account to use |
||
| 1077 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1078 | * @return mixed |
||
| 1079 | */ |
||
| 1080 | 5 | public function upgradeKeyIndex($identifier, $keyIndex, $primaryPublicKey) { |
|
| 1089 | |||
| 1090 | /** |
||
| 1091 | * initialize a previously created wallet |
||
| 1092 | * |
||
| 1093 | * Takes an options object, or accepts identifier/password for backwards compatiblity. |
||
| 1094 | * |
||
| 1095 | * Some of the options: |
||
| 1096 | * - "readonly/readOnly/read-only" can be to a boolean value, |
||
| 1097 | * so the wallet is loaded in read-only mode (no private key) |
||
| 1098 | * - "check_backup_key" can be set to your own backup key: |
||
| 1099 | * Format: ["M', "xpub..."] |
||
| 1100 | * Setting this will allow the SDK to check the server hasn't |
||
| 1101 | * a different key (one it happens to control) |
||
| 1102 | |||
| 1103 | * Either takes one argument: |
||
| 1104 | * @param array $options |
||
| 1105 | * |
||
| 1106 | * Or takes two arguments (old, deprecated syntax): |
||
| 1107 | * (@nonPHP-doc) @param string $identifier the wallet identifier to be initialized |
||
| 1108 | * (@nonPHP-doc) @param string $password the password to decrypt the mnemonic with |
||
| 1109 | * |
||
| 1110 | * @return WalletInterface |
||
| 1111 | * @throws \Exception |
||
| 1112 | */ |
||
| 1113 | 18 | public function initWallet($options) { |
|
| 1220 | |||
| 1221 | /** |
||
| 1222 | * get the wallet data from the server |
||
| 1223 | * |
||
| 1224 | * @param string $identifier the identifier of the wallet |
||
| 1225 | * @return mixed |
||
| 1226 | */ |
||
| 1227 | 18 | public function getWallet($identifier) { |
|
| 1231 | |||
| 1232 | /** |
||
| 1233 | * update the wallet data on the server |
||
| 1234 | * |
||
| 1235 | * @param string $identifier |
||
| 1236 | * @param $data |
||
| 1237 | * @return mixed |
||
| 1238 | */ |
||
| 1239 | 3 | public function updateWallet($identifier, $data) { |
|
| 1243 | |||
| 1244 | /** |
||
| 1245 | * delete a wallet from the server |
||
| 1246 | * the checksum address and a signature to verify you ownership of the key of that checksum address |
||
| 1247 | * is required to be able to delete a wallet |
||
| 1248 | * |
||
| 1249 | * @param string $identifier the identifier of the wallet |
||
| 1250 | * @param string $checksumAddress the address for your master private key (and the checksum used when creating the wallet) |
||
| 1251 | * @param string $signature a signature of the checksum address as message signed by the private key matching that address |
||
| 1252 | * @param bool $force ignore warnings (such as a non-zero balance) |
||
| 1253 | * @return mixed |
||
| 1254 | */ |
||
| 1255 | 10 | public function deleteWallet($identifier, $checksumAddress, $signature, $force = false) { |
|
| 1262 | |||
| 1263 | /** |
||
| 1264 | * create new backup key; |
||
| 1265 | * 1) a BIP39 mnemonic |
||
| 1266 | * 2) a seed from that mnemonic with a blank password |
||
| 1267 | * 3) a private key from that seed |
||
| 1268 | * |
||
| 1269 | * @return array [mnemonic, seed, key] |
||
| 1270 | */ |
||
| 1271 | 1 | protected function newBackupSeed() { |
|
| 1276 | |||
| 1277 | /** |
||
| 1278 | * create new primary key; |
||
| 1279 | * 1) a BIP39 mnemonic |
||
| 1280 | * 2) a seed from that mnemonic with the password |
||
| 1281 | * 3) a private key from that seed |
||
| 1282 | * |
||
| 1283 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1284 | * @return array [mnemonic, seed, key] |
||
| 1285 | * @TODO: require a strong password? |
||
| 1286 | */ |
||
| 1287 | 1 | protected function newPrimarySeed($passphrase) { |
|
| 1292 | |||
| 1293 | /** |
||
| 1294 | * create a new key; |
||
| 1295 | * 1) a BIP39 mnemonic |
||
| 1296 | * 2) a seed from that mnemonic with the password |
||
| 1297 | * 3) a private key from that seed |
||
| 1298 | * |
||
| 1299 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1300 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1301 | * @return array |
||
| 1302 | */ |
||
| 1303 | 1 | protected function generateNewSeed($passphrase = "", $forceEntropy = null) { |
|
| 1320 | |||
| 1321 | /** |
||
| 1322 | * generate a new mnemonic from some random entropy (512 bit) |
||
| 1323 | * |
||
| 1324 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1325 | * @return string |
||
| 1326 | * @throws \Exception |
||
| 1327 | */ |
||
| 1328 | 1 | protected function generateNewMnemonic($forceEntropy = null) { |
|
| 1338 | |||
| 1339 | /** |
||
| 1340 | * get the balance for the wallet |
||
| 1341 | * |
||
| 1342 | * @param string $identifier the identifier of the wallet |
||
| 1343 | * @return array |
||
| 1344 | */ |
||
| 1345 | 9 | public function getWalletBalance($identifier) { |
|
| 1349 | |||
| 1350 | /** |
||
| 1351 | * do HD wallet discovery for the wallet |
||
| 1352 | * |
||
| 1353 | * this can be REALLY slow, so we've set the timeout to 120s ... |
||
| 1354 | * |
||
| 1355 | * @param string $identifier the identifier of the wallet |
||
| 1356 | * @param int $gap the gap setting to use for discovery |
||
| 1357 | * @return mixed |
||
| 1358 | */ |
||
| 1359 | 2 | public function doWalletDiscovery($identifier, $gap = 200) { |
|
| 1363 | |||
| 1364 | /** |
||
| 1365 | * get a new derivation number for specified parent path |
||
| 1366 | * 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 |
||
| 1367 | * |
||
| 1368 | * returns the path |
||
| 1369 | * |
||
| 1370 | * @param string $identifier the identifier of the wallet |
||
| 1371 | * @param string $path the parent path for which to get a new derivation |
||
| 1372 | * @return string |
||
| 1373 | */ |
||
| 1374 | 1 | public function getNewDerivation($identifier, $path) { |
|
| 1378 | |||
| 1379 | /** |
||
| 1380 | * get a new derivation number for specified parent path |
||
| 1381 | * 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 |
||
| 1382 | * |
||
| 1383 | * @param string $identifier the identifier of the wallet |
||
| 1384 | * @param string $path the parent path for which to get a new derivation |
||
| 1385 | * @return mixed |
||
| 1386 | */ |
||
| 1387 | 13 | public function _getNewDerivation($identifier, $path) { |
|
| 1391 | |||
| 1392 | /** |
||
| 1393 | * get the path (and redeemScript) to specified address |
||
| 1394 | * |
||
| 1395 | * @param string $identifier |
||
| 1396 | * @param string $address |
||
| 1397 | * @return array |
||
| 1398 | * @throws \Exception |
||
| 1399 | */ |
||
| 1400 | 1 | public function getPathForAddress($identifier, $address) { |
|
| 1404 | |||
| 1405 | /** |
||
| 1406 | * send the transaction using the API |
||
| 1407 | * |
||
| 1408 | * @param string $identifier the identifier of the wallet |
||
| 1409 | * @param string|array $rawTransaction raw hex of the transaction (should be partially signed) |
||
| 1410 | * @param array $paths list of the paths that were used for the UTXO |
||
| 1411 | * @param bool $checkFee let the server verify the fee after signing |
||
| 1412 | * @return string the complete raw transaction |
||
| 1413 | * @throws \Exception |
||
| 1414 | */ |
||
| 1415 | 4 | public function sendTransaction($identifier, $rawTransaction, $paths, $checkFee = false) { |
|
| 1445 | |||
| 1446 | /** |
||
| 1447 | * use the API to get the best inputs to use based on the outputs |
||
| 1448 | * |
||
| 1449 | * the return array has the following format: |
||
| 1450 | * [ |
||
| 1451 | * "utxos" => [ |
||
| 1452 | * [ |
||
| 1453 | * "hash" => "<txHash>", |
||
| 1454 | * "idx" => "<index of the output of that <txHash>", |
||
| 1455 | * "scriptpubkey_hex" => "<scriptPubKey-hex>", |
||
| 1456 | * "value" => 32746327, |
||
| 1457 | * "address" => "1address", |
||
| 1458 | * "path" => "m/44'/1'/0'/0/13", |
||
| 1459 | * "redeem_script" => "<redeemScript-hex>", |
||
| 1460 | * ], |
||
| 1461 | * ], |
||
| 1462 | * "fee" => 10000, |
||
| 1463 | * "change"=> 1010109201, |
||
| 1464 | * ] |
||
| 1465 | * |
||
| 1466 | * @param string $identifier the identifier of the wallet |
||
| 1467 | * @param array $outputs the outputs you want to create - array[address => satoshi-value] |
||
| 1468 | * @param bool $lockUTXO when TRUE the UTXOs selected will be locked for a few seconds |
||
| 1469 | * so you have some time to spend them without race-conditions |
||
| 1470 | * @param bool $allowZeroConf |
||
| 1471 | * @param string $feeStrategy |
||
| 1472 | * @param null|int $forceFee |
||
| 1473 | * @return array |
||
| 1474 | * @throws \Exception |
||
| 1475 | */ |
||
| 1476 | 11 | public function coinSelection($identifier, $outputs, $lockUTXO = false, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null) { |
|
| 1496 | |||
| 1497 | /** |
||
| 1498 | * |
||
| 1499 | * @param string $identifier the identifier of the wallet |
||
| 1500 | * @param bool $allowZeroConf |
||
| 1501 | * @param string $feeStrategy |
||
| 1502 | * @param null|int $forceFee |
||
| 1503 | * @param int $outputCnt |
||
| 1504 | * @return array |
||
| 1505 | * @throws \Exception |
||
| 1506 | */ |
||
| 1507 | public function walletMaxSpendable($identifier, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null, $outputCnt = 1) { |
||
| 1526 | |||
| 1527 | /** |
||
| 1528 | * @return array ['optimal_fee' => 10000, 'low_priority_fee' => 5000] |
||
| 1529 | */ |
||
| 1530 | 3 | public function feePerKB() { |
|
| 1534 | |||
| 1535 | /** |
||
| 1536 | * get the current price index |
||
| 1537 | * |
||
| 1538 | * @return array eg; ['USD' => 287.30] |
||
| 1539 | */ |
||
| 1540 | 1 | public function price() { |
|
| 1544 | |||
| 1545 | /** |
||
| 1546 | * setup webhook for wallet |
||
| 1547 | * |
||
| 1548 | * @param string $identifier the wallet identifier for which to create the webhook |
||
| 1549 | * @param string $webhookIdentifier the webhook identifier to use |
||
| 1550 | * @param string $url the url to receive the webhook events |
||
| 1551 | * @return array |
||
| 1552 | */ |
||
| 1553 | 1 | public function setupWalletWebhook($identifier, $webhookIdentifier, $url) { |
|
| 1557 | |||
| 1558 | /** |
||
| 1559 | * delete webhook for wallet |
||
| 1560 | * |
||
| 1561 | * @param string $identifier the wallet identifier for which to delete the webhook |
||
| 1562 | * @param string $webhookIdentifier the webhook identifier to delete |
||
| 1563 | * @return array |
||
| 1564 | */ |
||
| 1565 | 1 | public function deleteWalletWebhook($identifier, $webhookIdentifier) { |
|
| 1569 | |||
| 1570 | /** |
||
| 1571 | * lock a specific unspent output |
||
| 1572 | * |
||
| 1573 | * @param $identifier |
||
| 1574 | * @param $txHash |
||
| 1575 | * @param $txIdx |
||
| 1576 | * @param int $ttl |
||
| 1577 | * @return bool |
||
| 1578 | */ |
||
| 1579 | public function lockWalletUTXO($identifier, $txHash, $txIdx, $ttl = 3) { |
||
| 1583 | |||
| 1584 | /** |
||
| 1585 | * unlock a specific unspent output |
||
| 1586 | * |
||
| 1587 | * @param $identifier |
||
| 1588 | * @param $txHash |
||
| 1589 | * @param $txIdx |
||
| 1590 | * @return bool |
||
| 1591 | */ |
||
| 1592 | public function unlockWalletUTXO($identifier, $txHash, $txIdx) { |
||
| 1596 | |||
| 1597 | /** |
||
| 1598 | * get all transactions for wallet (paginated) |
||
| 1599 | * |
||
| 1600 | * @param string $identifier the wallet identifier for which to get transactions |
||
| 1601 | * @param integer $page pagination: page number |
||
| 1602 | * @param integer $limit pagination: records per page (max 500) |
||
| 1603 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1604 | * @return array associative array containing the response |
||
| 1605 | */ |
||
| 1606 | 1 | public function walletTransactions($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 1615 | |||
| 1616 | /** |
||
| 1617 | * get all addresses for wallet (paginated) |
||
| 1618 | * |
||
| 1619 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1620 | * @param integer $page pagination: page number |
||
| 1621 | * @param integer $limit pagination: records per page (max 500) |
||
| 1622 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1623 | * @return array associative array containing the response |
||
| 1624 | */ |
||
| 1625 | 1 | public function walletAddresses($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 1634 | |||
| 1635 | /** |
||
| 1636 | * get all UTXOs for wallet (paginated) |
||
| 1637 | * |
||
| 1638 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1639 | * @param integer $page pagination: page number |
||
| 1640 | * @param integer $limit pagination: records per page (max 500) |
||
| 1641 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1642 | * @param boolean $zeroconf include zero confirmation transactions |
||
| 1643 | * @return array associative array containing the response |
||
| 1644 | */ |
||
| 1645 | 1 | public function walletUTXOs($identifier, $page = 1, $limit = 20, $sortDir = 'asc', $zeroconf = true) { |
|
| 1655 | |||
| 1656 | /** |
||
| 1657 | * get a paginated list of all wallets associated with the api user |
||
| 1658 | * |
||
| 1659 | * @param integer $page pagination: page number |
||
| 1660 | * @param integer $limit pagination: records per page |
||
| 1661 | * @return array associative array containing the response |
||
| 1662 | */ |
||
| 1663 | 2 | public function allWallets($page = 1, $limit = 20) { |
|
| 1671 | |||
| 1672 | /** |
||
| 1673 | * send raw transaction |
||
| 1674 | * |
||
| 1675 | * @param $txHex |
||
| 1676 | * @return bool |
||
| 1677 | */ |
||
| 1678 | public function sendRawTransaction($txHex) { |
||
| 1682 | |||
| 1683 | /** |
||
| 1684 | * testnet only ;-) |
||
| 1685 | * |
||
| 1686 | * @param $address |
||
| 1687 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1688 | * @return mixed |
||
| 1689 | * @throws \Exception |
||
| 1690 | */ |
||
| 1691 | public function faucetWithdrawal($address, $amount = 10000) { |
||
| 1698 | |||
| 1699 | /** |
||
| 1700 | * Exists for BC. Remove at major bump. |
||
| 1701 | * |
||
| 1702 | * @see faucetWithdrawal |
||
| 1703 | * @deprecated |
||
| 1704 | * @param $address |
||
| 1705 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1706 | * @return mixed |
||
| 1707 | * @throws \Exception |
||
| 1708 | */ |
||
| 1709 | public function faucetWithdrawl($address, $amount = 10000) { |
||
| 1712 | |||
| 1713 | /** |
||
| 1714 | * verify a message signed bitcoin-core style |
||
| 1715 | * |
||
| 1716 | * @param string $message |
||
| 1717 | * @param string $address |
||
| 1718 | * @param string $signature |
||
| 1719 | * @return boolean |
||
| 1720 | */ |
||
| 1721 | 1 | public function verifyMessage($message, $address, $signature) { |
|
| 1738 | |||
| 1739 | /** |
||
| 1740 | * convert a Satoshi value to a BTC value |
||
| 1741 | * |
||
| 1742 | * @param int $satoshi |
||
| 1743 | * @return float |
||
| 1744 | */ |
||
| 1745 | public static function toBTC($satoshi) { |
||
| 1748 | |||
| 1749 | /** |
||
| 1750 | * convert a Satoshi value to a BTC value and return it as a string |
||
| 1751 | |||
| 1752 | * @param int $satoshi |
||
| 1753 | * @return string |
||
| 1754 | */ |
||
| 1755 | public static function toBTCString($satoshi) { |
||
| 1758 | |||
| 1759 | /** |
||
| 1760 | * convert a BTC value to a Satoshi value |
||
| 1761 | * |
||
| 1762 | * @param float $btc |
||
| 1763 | * @return string |
||
| 1764 | */ |
||
| 1765 | 12 | public static function toSatoshiString($btc) { |
|
| 1768 | |||
| 1769 | /** |
||
| 1770 | * convert a BTC value to a Satoshi value |
||
| 1771 | * |
||
| 1772 | * @param float $btc |
||
| 1773 | * @return string |
||
| 1774 | */ |
||
| 1775 | 12 | public static function toSatoshi($btc) { |
|
| 1778 | |||
| 1779 | /** |
||
| 1780 | * json_decode helper that throws exceptions when it fails to decode |
||
| 1781 | * |
||
| 1782 | * @param $json |
||
| 1783 | * @param bool $assoc |
||
| 1784 | * @return mixed |
||
| 1785 | * @throws \Exception |
||
| 1786 | */ |
||
| 1787 | 27 | protected static function jsonDecode($json, $assoc = false) { |
|
| 1800 | |||
| 1801 | /** |
||
| 1802 | * sort public keys for multisig script |
||
| 1803 | * |
||
| 1804 | * @param PublicKeyInterface[] $pubKeys |
||
| 1805 | * @return PublicKeyInterface[] |
||
| 1806 | */ |
||
| 1807 | 16 | public static function sortMultisigKeys(array $pubKeys) { |
|
| 1817 | |||
| 1818 | /** |
||
| 1819 | * read and decode the json payload from a webhook's POST request. |
||
| 1820 | * |
||
| 1821 | * @param bool $returnObject flag to indicate if an object or associative array should be returned |
||
| 1822 | * @return mixed|null |
||
| 1823 | * @throws \Exception |
||
| 1824 | */ |
||
| 1825 | public static function getWebhookPayload($returnObject = false) { |
||
| 1833 | |||
| 1834 | public static function normalizeBIP32KeyArray($keys) { |
||
| 1839 | |||
| 1840 | /** |
||
| 1841 | * @param array|BIP32Key $key |
||
| 1842 | * @return BIP32Key |
||
| 1843 | * @throws \Exception |
||
| 1844 | */ |
||
| 1845 | 21 | public static function normalizeBIP32Key($key) { |
|
| 1863 | } |
||
| 1864 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: