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 | 81 | 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 | 81 | 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 | 81 | 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 | 4 | 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) { |
|
| 673 | |||
| 674 | 5 | public static function randomBits($bits) { |
|
| 677 | |||
| 678 | 5 | public static function randomBytes($bytes) { |
|
| 681 | |||
| 682 | 2 | protected function createNewWalletV2($options) { |
|
| 802 | |||
| 803 | 4 | protected function createNewWalletV3($options) { |
|
| 941 | |||
| 942 | /** |
||
| 943 | * @param array $bip32Key |
||
| 944 | * @throws BlocktrailSDKException |
||
| 945 | */ |
||
| 946 | 10 | private function verifyPublicBIP32Key(array $bip32Key) { |
|
| 956 | |||
| 957 | /** |
||
| 958 | * @param array $walletData |
||
| 959 | * @throws BlocktrailSDKException |
||
| 960 | */ |
||
| 961 | 10 | private function verifyPublicOnly(array $walletData) { |
|
| 965 | |||
| 966 | /** |
||
| 967 | * create wallet using the API |
||
| 968 | * |
||
| 969 | * @param string $identifier the wallet identifier to create |
||
| 970 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 971 | * @param string $backupPublicKey plain public key |
||
| 972 | * @param string $primaryMnemonic mnemonic to store |
||
| 973 | * @param string $checksum checksum to store |
||
| 974 | * @param int $keyIndex account that we expect to use |
||
| 975 | * @return mixed |
||
| 976 | */ |
||
| 977 | 1 | public function storeNewWalletV1($identifier, $primaryPublicKey, $backupPublicKey, $primaryMnemonic, $checksum, $keyIndex) { |
|
| 990 | |||
| 991 | /** |
||
| 992 | * create wallet using the API |
||
| 993 | * |
||
| 994 | * @param string $identifier the wallet identifier to create |
||
| 995 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 996 | * @param string $backupPublicKey plain public key |
||
| 997 | * @param $encryptedPrimarySeed |
||
| 998 | * @param $encryptedSecret |
||
| 999 | * @param $recoverySecret |
||
| 1000 | * @param string $checksum checksum to store |
||
| 1001 | * @param int $keyIndex account that we expect to use |
||
| 1002 | * @return mixed |
||
| 1003 | * @throws \Exception |
||
| 1004 | */ |
||
| 1005 | 5 | public function storeNewWalletV2($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex) { |
|
| 1021 | |||
| 1022 | /** |
||
| 1023 | * create wallet using the API |
||
| 1024 | * |
||
| 1025 | * @param string $identifier the wallet identifier to create |
||
| 1026 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1027 | * @param string $backupPublicKey plain public key |
||
| 1028 | * @param $encryptedPrimarySeed |
||
| 1029 | * @param $encryptedSecret |
||
| 1030 | * @param $recoverySecret |
||
| 1031 | * @param string $checksum checksum to store |
||
| 1032 | * @param int $keyIndex account that we expect to use |
||
| 1033 | * @return mixed |
||
| 1034 | * @throws \Exception |
||
| 1035 | */ |
||
| 1036 | 4 | public function storeNewWalletV3($identifier, $primaryPublicKey, $backupPublicKey, $encryptedPrimarySeed, $encryptedSecret, $recoverySecret, $checksum, $keyIndex) { |
|
| 1054 | |||
| 1055 | /** |
||
| 1056 | * upgrade wallet to use a new account number |
||
| 1057 | * the account number specifies which blocktrail cosigning key is used |
||
| 1058 | * |
||
| 1059 | * @param string $identifier the wallet identifier to be upgraded |
||
| 1060 | * @param int $keyIndex the new account to use |
||
| 1061 | * @param array $primaryPublicKey BIP32 extended public key - [key, path] |
||
| 1062 | * @return mixed |
||
| 1063 | */ |
||
| 1064 | 5 | public function upgradeKeyIndex($identifier, $keyIndex, $primaryPublicKey) { |
|
| 1073 | |||
| 1074 | /** |
||
| 1075 | * initialize a previously created wallet |
||
| 1076 | * |
||
| 1077 | * Takes an options object, or accepts identifier/password for backwards compatiblity. |
||
| 1078 | * |
||
| 1079 | * Some of the options: |
||
| 1080 | * - "readonly/readOnly/read-only" can be to a boolean value, |
||
| 1081 | * so the wallet is loaded in read-only mode (no private key) |
||
| 1082 | * - "check_backup_key" can be set to your own backup key: |
||
| 1083 | * Format: ["M', "xpub..."] |
||
| 1084 | * Setting this will allow the SDK to check the server hasn't |
||
| 1085 | * a different key (one it happens to control) |
||
| 1086 | |||
| 1087 | * Either takes one argument: |
||
| 1088 | * @param array $options |
||
| 1089 | * |
||
| 1090 | * Or takes two arguments (old, deprecated syntax): |
||
| 1091 | * (@nonPHP-doc) @param string $identifier the wallet identifier to be initialized |
||
| 1092 | * (@nonPHP-doc) @param string $password the password to decrypt the mnemonic with |
||
| 1093 | * |
||
| 1094 | * @return WalletInterface |
||
| 1095 | * @throws \Exception |
||
| 1096 | */ |
||
| 1097 | 16 | public function initWallet($options) { |
|
| 1204 | |||
| 1205 | /** |
||
| 1206 | * get the wallet data from the server |
||
| 1207 | * |
||
| 1208 | * @param string $identifier the identifier of the wallet |
||
| 1209 | * @return mixed |
||
| 1210 | */ |
||
| 1211 | 16 | public function getWallet($identifier) { |
|
| 1215 | |||
| 1216 | /** |
||
| 1217 | * update the wallet data on the server |
||
| 1218 | * |
||
| 1219 | * @param string $identifier |
||
| 1220 | * @param $data |
||
| 1221 | * @return mixed |
||
| 1222 | */ |
||
| 1223 | 3 | public function updateWallet($identifier, $data) { |
|
| 1227 | |||
| 1228 | /** |
||
| 1229 | * delete a wallet from the server |
||
| 1230 | * the checksum address and a signature to verify you ownership of the key of that checksum address |
||
| 1231 | * is required to be able to delete a wallet |
||
| 1232 | * |
||
| 1233 | * @param string $identifier the identifier of the wallet |
||
| 1234 | * @param string $checksumAddress the address for your master private key (and the checksum used when creating the wallet) |
||
| 1235 | * @param string $signature a signature of the checksum address as message signed by the private key matching that address |
||
| 1236 | * @param bool $force ignore warnings (such as a non-zero balance) |
||
| 1237 | * @return mixed |
||
| 1238 | */ |
||
| 1239 | 10 | public function deleteWallet($identifier, $checksumAddress, $signature, $force = false) { |
|
| 1246 | |||
| 1247 | /** |
||
| 1248 | * create new backup key; |
||
| 1249 | * 1) a BIP39 mnemonic |
||
| 1250 | * 2) a seed from that mnemonic with a blank password |
||
| 1251 | * 3) a private key from that seed |
||
| 1252 | * |
||
| 1253 | * @return array [mnemonic, seed, key] |
||
| 1254 | */ |
||
| 1255 | 1 | protected function newBackupSeed() { |
|
| 1260 | |||
| 1261 | /** |
||
| 1262 | * create new primary key; |
||
| 1263 | * 1) a BIP39 mnemonic |
||
| 1264 | * 2) a seed from that mnemonic with the password |
||
| 1265 | * 3) a private key from that seed |
||
| 1266 | * |
||
| 1267 | * @param string $passphrase the password to use in the BIP39 creation of the seed |
||
| 1268 | * @return array [mnemonic, seed, key] |
||
| 1269 | * @TODO: require a strong password? |
||
| 1270 | */ |
||
| 1271 | 1 | protected function newPrimarySeed($passphrase) { |
|
| 1276 | |||
| 1277 | /** |
||
| 1278 | * create a new 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 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1285 | * @return array |
||
| 1286 | */ |
||
| 1287 | 1 | protected function generateNewSeed($passphrase = "", $forceEntropy = null) { |
|
| 1304 | |||
| 1305 | /** |
||
| 1306 | * generate a new mnemonic from some random entropy (512 bit) |
||
| 1307 | * |
||
| 1308 | * @param string $forceEntropy forced entropy instead of random entropy for testing purposes |
||
| 1309 | * @return string |
||
| 1310 | * @throws \Exception |
||
| 1311 | */ |
||
| 1312 | 1 | protected function generateNewMnemonic($forceEntropy = null) { |
|
| 1322 | |||
| 1323 | /** |
||
| 1324 | * get the balance for the wallet |
||
| 1325 | * |
||
| 1326 | * @param string $identifier the identifier of the wallet |
||
| 1327 | * @return array |
||
| 1328 | */ |
||
| 1329 | 9 | public function getWalletBalance($identifier) { |
|
| 1333 | |||
| 1334 | /** |
||
| 1335 | * do HD wallet discovery for the wallet |
||
| 1336 | * |
||
| 1337 | * this can be REALLY slow, so we've set the timeout to 120s ... |
||
| 1338 | * |
||
| 1339 | * @param string $identifier the identifier of the wallet |
||
| 1340 | * @param int $gap the gap setting to use for discovery |
||
| 1341 | * @return mixed |
||
| 1342 | */ |
||
| 1343 | 2 | public function doWalletDiscovery($identifier, $gap = 200) { |
|
| 1347 | |||
| 1348 | /** |
||
| 1349 | * get a new derivation number for specified parent path |
||
| 1350 | * 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 |
||
| 1351 | * |
||
| 1352 | * returns the path |
||
| 1353 | * |
||
| 1354 | * @param string $identifier the identifier of the wallet |
||
| 1355 | * @param string $path the parent path for which to get a new derivation |
||
| 1356 | * @return string |
||
| 1357 | */ |
||
| 1358 | 1 | public function getNewDerivation($identifier, $path) { |
|
| 1362 | |||
| 1363 | /** |
||
| 1364 | * get a new derivation number for specified parent path |
||
| 1365 | * 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 |
||
| 1366 | * |
||
| 1367 | * @param string $identifier the identifier of the wallet |
||
| 1368 | * @param string $path the parent path for which to get a new derivation |
||
| 1369 | * @return mixed |
||
| 1370 | */ |
||
| 1371 | 12 | public function _getNewDerivation($identifier, $path) { |
|
| 1375 | |||
| 1376 | /** |
||
| 1377 | * get the path (and redeemScript) to specified address |
||
| 1378 | * |
||
| 1379 | * @param string $identifier |
||
| 1380 | * @param string $address |
||
| 1381 | * @return array |
||
| 1382 | * @throws \Exception |
||
| 1383 | */ |
||
| 1384 | public function getPathForAddress($identifier, $address) { |
||
| 1388 | |||
| 1389 | /** |
||
| 1390 | * send the transaction using the API |
||
| 1391 | * |
||
| 1392 | * @param string $identifier the identifier of the wallet |
||
| 1393 | * @param string|array $rawTransaction raw hex of the transaction (should be partially signed) |
||
| 1394 | * @param array $paths list of the paths that were used for the UTXO |
||
| 1395 | * @param bool $checkFee let the server verify the fee after signing |
||
| 1396 | * @return string the complete raw transaction |
||
| 1397 | * @throws \Exception |
||
| 1398 | */ |
||
| 1399 | 4 | public function sendTransaction($identifier, $rawTransaction, $paths, $checkFee = false) { |
|
| 1429 | |||
| 1430 | /** |
||
| 1431 | * use the API to get the best inputs to use based on the outputs |
||
| 1432 | * |
||
| 1433 | * the return array has the following format: |
||
| 1434 | * [ |
||
| 1435 | * "utxos" => [ |
||
| 1436 | * [ |
||
| 1437 | * "hash" => "<txHash>", |
||
| 1438 | * "idx" => "<index of the output of that <txHash>", |
||
| 1439 | * "scriptpubkey_hex" => "<scriptPubKey-hex>", |
||
| 1440 | * "value" => 32746327, |
||
| 1441 | * "address" => "1address", |
||
| 1442 | * "path" => "m/44'/1'/0'/0/13", |
||
| 1443 | * "redeem_script" => "<redeemScript-hex>", |
||
| 1444 | * ], |
||
| 1445 | * ], |
||
| 1446 | * "fee" => 10000, |
||
| 1447 | * "change"=> 1010109201, |
||
| 1448 | * ] |
||
| 1449 | * |
||
| 1450 | * @param string $identifier the identifier of the wallet |
||
| 1451 | * @param array $outputs the outputs you want to create - array[address => satoshi-value] |
||
| 1452 | * @param bool $lockUTXO when TRUE the UTXOs selected will be locked for a few seconds |
||
| 1453 | * so you have some time to spend them without race-conditions |
||
| 1454 | * @param bool $allowZeroConf |
||
| 1455 | * @param string $feeStrategy |
||
| 1456 | * @param null|int $forceFee |
||
| 1457 | * @return array |
||
| 1458 | * @throws \Exception |
||
| 1459 | */ |
||
| 1460 | 11 | public function coinSelection($identifier, $outputs, $lockUTXO = false, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null) { |
|
| 1461 | $args = [ |
||
| 1462 | 11 | 'lock' => (int)!!$lockUTXO, |
|
| 1463 | 11 | 'zeroconf' => (int)!!$allowZeroConf, |
|
| 1464 | 11 | 'fee_strategy' => $feeStrategy, |
|
| 1465 | ]; |
||
| 1466 | |||
| 1467 | 11 | if ($forceFee !== null) { |
|
| 1468 | $args['forcefee'] = (int)$forceFee; |
||
| 1469 | } |
||
| 1470 | |||
| 1471 | 11 | $response = $this->client->post( |
|
| 1472 | 11 | "wallet/{$identifier}/coin-selection", |
|
| 1473 | 11 | $args, |
|
| 1474 | 11 | $outputs, |
|
| 1475 | 11 | RestClient::AUTH_HTTP_SIG |
|
| 1476 | ); |
||
| 1477 | |||
| 1478 | 5 | return self::jsonDecode($response->body(), true); |
|
| 1479 | } |
||
| 1480 | |||
| 1481 | /** |
||
| 1482 | * |
||
| 1483 | * @param string $identifier the identifier of the wallet |
||
| 1484 | * @param bool $allowZeroConf |
||
| 1485 | * @param string $feeStrategy |
||
| 1486 | * @param null|int $forceFee |
||
| 1487 | * @param int $outputCnt |
||
| 1488 | * @return array |
||
| 1489 | * @throws \Exception |
||
| 1490 | */ |
||
| 1491 | public function walletMaxSpendable($identifier, $allowZeroConf = false, $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL, $forceFee = null, $outputCnt = 1) { |
||
| 1510 | |||
| 1511 | /** |
||
| 1512 | * @return array ['optimal_fee' => 10000, 'low_priority_fee' => 5000] |
||
| 1513 | */ |
||
| 1514 | 2 | public function feePerKB() { |
|
| 1518 | |||
| 1519 | /** |
||
| 1520 | * get the current price index |
||
| 1521 | * |
||
| 1522 | * @return array eg; ['USD' => 287.30] |
||
| 1523 | */ |
||
| 1524 | 1 | public function price() { |
|
| 1528 | |||
| 1529 | /** |
||
| 1530 | * setup webhook for wallet |
||
| 1531 | * |
||
| 1532 | * @param string $identifier the wallet identifier for which to create the webhook |
||
| 1533 | * @param string $webhookIdentifier the webhook identifier to use |
||
| 1534 | * @param string $url the url to receive the webhook events |
||
| 1535 | * @return array |
||
| 1536 | */ |
||
| 1537 | 1 | public function setupWalletWebhook($identifier, $webhookIdentifier, $url) { |
|
| 1541 | |||
| 1542 | /** |
||
| 1543 | * delete webhook for wallet |
||
| 1544 | * |
||
| 1545 | * @param string $identifier the wallet identifier for which to delete the webhook |
||
| 1546 | * @param string $webhookIdentifier the webhook identifier to delete |
||
| 1547 | * @return array |
||
| 1548 | */ |
||
| 1549 | 1 | public function deleteWalletWebhook($identifier, $webhookIdentifier) { |
|
| 1553 | |||
| 1554 | /** |
||
| 1555 | * lock a specific unspent output |
||
| 1556 | * |
||
| 1557 | * @param $identifier |
||
| 1558 | * @param $txHash |
||
| 1559 | * @param $txIdx |
||
| 1560 | * @param int $ttl |
||
| 1561 | * @return bool |
||
| 1562 | */ |
||
| 1563 | public function lockWalletUTXO($identifier, $txHash, $txIdx, $ttl = 3) { |
||
| 1567 | |||
| 1568 | /** |
||
| 1569 | * unlock a specific unspent output |
||
| 1570 | * |
||
| 1571 | * @param $identifier |
||
| 1572 | * @param $txHash |
||
| 1573 | * @param $txIdx |
||
| 1574 | * @return bool |
||
| 1575 | */ |
||
| 1576 | public function unlockWalletUTXO($identifier, $txHash, $txIdx) { |
||
| 1580 | |||
| 1581 | /** |
||
| 1582 | * get all transactions for wallet (paginated) |
||
| 1583 | * |
||
| 1584 | * @param string $identifier the wallet identifier for which to get transactions |
||
| 1585 | * @param integer $page pagination: page number |
||
| 1586 | * @param integer $limit pagination: records per page (max 500) |
||
| 1587 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1588 | * @return array associative array containing the response |
||
| 1589 | */ |
||
| 1590 | 1 | public function walletTransactions($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 1599 | |||
| 1600 | /** |
||
| 1601 | * get all addresses for wallet (paginated) |
||
| 1602 | * |
||
| 1603 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1604 | * @param integer $page pagination: page number |
||
| 1605 | * @param integer $limit pagination: records per page (max 500) |
||
| 1606 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1607 | * @return array associative array containing the response |
||
| 1608 | */ |
||
| 1609 | 1 | public function walletAddresses($identifier, $page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 1618 | |||
| 1619 | /** |
||
| 1620 | * get all UTXOs for wallet (paginated) |
||
| 1621 | * |
||
| 1622 | * @param string $identifier the wallet identifier for which to get addresses |
||
| 1623 | * @param integer $page pagination: page number |
||
| 1624 | * @param integer $limit pagination: records per page (max 500) |
||
| 1625 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1626 | * @param boolean $zeroconf include zero confirmation transactions |
||
| 1627 | * @return array associative array containing the response |
||
| 1628 | */ |
||
| 1629 | 1 | public function walletUTXOs($identifier, $page = 1, $limit = 20, $sortDir = 'asc', $zeroconf = true) { |
|
| 1639 | |||
| 1640 | /** |
||
| 1641 | * get a paginated list of all wallets associated with the api user |
||
| 1642 | * |
||
| 1643 | * @param integer $page pagination: page number |
||
| 1644 | * @param integer $limit pagination: records per page |
||
| 1645 | * @return array associative array containing the response |
||
| 1646 | */ |
||
| 1647 | 2 | public function allWallets($page = 1, $limit = 20) { |
|
| 1655 | |||
| 1656 | /** |
||
| 1657 | * send raw transaction |
||
| 1658 | * |
||
| 1659 | * @param $txHex |
||
| 1660 | * @return bool |
||
| 1661 | */ |
||
| 1662 | public function sendRawTransaction($txHex) { |
||
| 1666 | |||
| 1667 | /** |
||
| 1668 | * testnet only ;-) |
||
| 1669 | * |
||
| 1670 | * @param $address |
||
| 1671 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1672 | * @return mixed |
||
| 1673 | * @throws \Exception |
||
| 1674 | */ |
||
| 1675 | public function faucetWithdrawal($address, $amount = 10000) { |
||
| 1682 | |||
| 1683 | /** |
||
| 1684 | * Exists for BC. Remove at major bump. |
||
| 1685 | * |
||
| 1686 | * @see faucetWithdrawal |
||
| 1687 | * @deprecated |
||
| 1688 | * @param $address |
||
| 1689 | * @param int $amount defaults to 0.0001 BTC, max 0.001 BTC |
||
| 1690 | * @return mixed |
||
| 1691 | * @throws \Exception |
||
| 1692 | */ |
||
| 1693 | public function faucetWithdrawl($address, $amount = 10000) { |
||
| 1696 | |||
| 1697 | /** |
||
| 1698 | * verify a message signed bitcoin-core style |
||
| 1699 | * |
||
| 1700 | * @param string $message |
||
| 1701 | * @param string $address |
||
| 1702 | * @param string $signature |
||
| 1703 | * @return boolean |
||
| 1704 | */ |
||
| 1705 | 1 | public function verifyMessage($message, $address, $signature) { |
|
| 1722 | |||
| 1723 | /** |
||
| 1724 | * convert a Satoshi value to a BTC value |
||
| 1725 | * |
||
| 1726 | * @param int $satoshi |
||
| 1727 | * @return float |
||
| 1728 | */ |
||
| 1729 | public static function toBTC($satoshi) { |
||
| 1732 | |||
| 1733 | /** |
||
| 1734 | * convert a Satoshi value to a BTC value and return it as a string |
||
| 1735 | |||
| 1736 | * @param int $satoshi |
||
| 1737 | * @return string |
||
| 1738 | */ |
||
| 1739 | public static function toBTCString($satoshi) { |
||
| 1742 | |||
| 1743 | /** |
||
| 1744 | * convert a BTC value to a Satoshi value |
||
| 1745 | * |
||
| 1746 | * @param float $btc |
||
| 1747 | * @return string |
||
| 1748 | */ |
||
| 1749 | 12 | public static function toSatoshiString($btc) { |
|
| 1752 | |||
| 1753 | /** |
||
| 1754 | * convert a BTC value to a Satoshi value |
||
| 1755 | * |
||
| 1756 | * @param float $btc |
||
| 1757 | * @return string |
||
| 1758 | */ |
||
| 1759 | 12 | public static function toSatoshi($btc) { |
|
| 1762 | |||
| 1763 | /** |
||
| 1764 | * json_decode helper that throws exceptions when it fails to decode |
||
| 1765 | * |
||
| 1766 | * @param $json |
||
| 1767 | * @param bool $assoc |
||
| 1768 | * @return mixed |
||
| 1769 | * @throws \Exception |
||
| 1770 | */ |
||
| 1771 | 25 | protected static function jsonDecode($json, $assoc = false) { |
|
| 1784 | |||
| 1785 | /** |
||
| 1786 | * sort public keys for multisig script |
||
| 1787 | * |
||
| 1788 | * @param PublicKeyInterface[] $pubKeys |
||
| 1789 | * @return PublicKeyInterface[] |
||
| 1790 | */ |
||
| 1791 | 14 | public static function sortMultisigKeys(array $pubKeys) { |
|
| 1801 | |||
| 1802 | /** |
||
| 1803 | * read and decode the json payload from a webhook's POST request. |
||
| 1804 | * |
||
| 1805 | * @param bool $returnObject flag to indicate if an object or associative array should be returned |
||
| 1806 | * @return mixed|null |
||
| 1807 | * @throws \Exception |
||
| 1808 | */ |
||
| 1809 | public static function getWebhookPayload($returnObject = false) { |
||
| 1817 | |||
| 1818 | public static function normalizeBIP32KeyArray($keys) { |
||
| 1823 | |||
| 1824 | 19 | public static function normalizeBIP32Key($key) { |
|
| 1842 | } |
||
| 1843 |
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: