Complex classes like Wallet 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 Wallet, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | abstract class Wallet implements WalletInterface { |
||
| 35 | |||
| 36 | const WALLET_VERSION_V1 = 'v1'; |
||
| 37 | const WALLET_VERSION_V2 = 'v2'; |
||
| 38 | const WALLET_VERSION_V3 = 'v3'; |
||
| 39 | |||
| 40 | const CHAIN_BTC_DEFAULT = 0; |
||
| 41 | const CHAIN_BCC_DEFAULT = 1; |
||
| 42 | const CHAIN_BTC_SEGWIT = 2; |
||
| 43 | |||
| 44 | const BASE_FEE = 10000; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * development / debug setting |
||
| 48 | * when getting a new derivation from the API, |
||
| 49 | * will verify address / redeeemScript with the values the API provides |
||
| 50 | */ |
||
| 51 | const VERIFY_NEW_DERIVATION = true; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var BlocktrailSDKInterface |
||
| 55 | */ |
||
| 56 | protected $sdk; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $identifier; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * BIP32 master primary private key (m/) |
||
| 65 | * |
||
| 66 | * @var BIP32Key |
||
| 67 | */ |
||
| 68 | protected $primaryPrivateKey; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var BIP32Key[] |
||
| 72 | */ |
||
| 73 | protected $primaryPublicKeys; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * BIP32 master backup public key (M/) |
||
| 77 | |||
| 78 | * @var BIP32Key |
||
| 79 | */ |
||
| 80 | protected $backupPublicKey; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * map of blocktrail BIP32 public keys |
||
| 84 | * keyed by key index |
||
| 85 | * path should be `M / key_index'` |
||
| 86 | * |
||
| 87 | * @var BIP32Key[] |
||
| 88 | */ |
||
| 89 | protected $blocktrailPublicKeys; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * the 'Blocktrail Key Index' that is used for new addresses |
||
| 93 | * |
||
| 94 | * @var int |
||
| 95 | */ |
||
| 96 | protected $keyIndex; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * 'bitcoin' |
||
| 100 | * |
||
| 101 | * @var string |
||
| 102 | */ |
||
| 103 | protected $network; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * testnet yes / no |
||
| 107 | * |
||
| 108 | * @var bool |
||
| 109 | */ |
||
| 110 | protected $testnet; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * cache of public keys, by path |
||
| 114 | * |
||
| 115 | * @var BIP32Key[] |
||
| 116 | */ |
||
| 117 | protected $pubKeys = []; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * cache of address / redeemScript, by path |
||
| 121 | * |
||
| 122 | * @var string[][] [[address, redeemScript)], ] |
||
| 123 | */ |
||
| 124 | protected $derivations = []; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * reverse cache of paths by address |
||
| 128 | * |
||
| 129 | * @var string[] |
||
| 130 | */ |
||
| 131 | protected $derivationsByAddress = []; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var string |
||
| 135 | */ |
||
| 136 | protected $checksum; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var bool |
||
| 140 | */ |
||
| 141 | protected $locked = true; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var bool |
||
| 145 | */ |
||
| 146 | protected $isSegwit = false; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var int |
||
| 150 | */ |
||
| 151 | protected $chainIndex; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var int |
||
| 155 | */ |
||
| 156 | protected $changeIndex; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @var AddressReaderBase |
||
| 160 | */ |
||
| 161 | protected $addressReader; |
||
| 162 | |||
| 163 | protected $highPriorityFeePerKB; |
||
| 164 | protected $optimalFeePerKB; |
||
| 165 | protected $lowPriorityFeePerKB; |
||
| 166 | protected $feePerKBAge; |
||
| 167 | protected $allowedSignModes = [SignInfo::MODE_DONTSIGN, SignInfo::MODE_SIGN]; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param BlocktrailSDKInterface $sdk SDK instance used to do requests |
||
| 171 | * @param string $identifier identifier of the wallet |
||
| 172 | * @param BIP32Key[] $primaryPublicKeys |
||
| 173 | * @param BIP32Key $backupPublicKey should be BIP32 master public key M/ |
||
| 174 | * @param BIP32Key[] $blocktrailPublicKeys |
||
| 175 | * @param int $keyIndex |
||
| 176 | * @param string $network |
||
| 177 | * @param bool $testnet |
||
| 178 | * @param bool $segwit |
||
| 179 | * @param string $checksum |
||
| 180 | * @throws BlocktrailSDKException |
||
| 181 | */ |
||
| 182 | 26 | public function __construct(BlocktrailSDKInterface $sdk, $identifier, array $primaryPublicKeys, $backupPublicKey, array $blocktrailPublicKeys, $keyIndex, $network, $testnet, $segwit, AddressReaderBase $addressReader, $checksum) { |
|
| 183 | 26 | $this->sdk = $sdk; |
|
| 184 | |||
| 185 | 26 | $this->identifier = $identifier; |
|
| 186 | 26 | $this->backupPublicKey = BlocktrailSDK::normalizeBIP32Key($backupPublicKey); |
|
| 187 | 26 | $this->primaryPublicKeys = BlocktrailSDK::normalizeBIP32KeyArray($primaryPublicKeys); |
|
| 188 | 26 | $this->blocktrailPublicKeys = BlocktrailSDK::normalizeBIP32KeyArray($blocktrailPublicKeys); |
|
| 189 | |||
| 190 | 26 | $this->network = $network; |
|
| 191 | 26 | $this->testnet = $testnet; |
|
| 192 | 26 | $this->keyIndex = $keyIndex; |
|
| 193 | 26 | $this->checksum = $checksum; |
|
| 194 | |||
| 195 | 26 | if ($network === "bitcoin") { |
|
| 196 | 22 | if ($segwit) { |
|
| 197 | 3 | $chainIdx = self::CHAIN_BTC_DEFAULT; |
|
| 198 | 3 | $changeIdx = self::CHAIN_BTC_SEGWIT; |
|
| 199 | } else { |
||
| 200 | 20 | $chainIdx = self::CHAIN_BTC_DEFAULT; |
|
| 201 | 22 | $changeIdx = self::CHAIN_BTC_DEFAULT; |
|
| 202 | } |
||
| 203 | } else { |
||
| 204 | 4 | if ($segwit && $network === "bitcoincash") { |
|
| 205 | throw new BlocktrailSDKException("Received segwit flag for bitcoincash - abort"); |
||
| 206 | } |
||
| 207 | 4 | $chainIdx = self::CHAIN_BCC_DEFAULT; |
|
| 208 | 4 | $changeIdx = self::CHAIN_BCC_DEFAULT; |
|
| 209 | } |
||
| 210 | |||
| 211 | 26 | $this->addressReader = $addressReader; |
|
| 212 | 26 | $this->isSegwit = (bool) $segwit; |
|
| 213 | 26 | $this->chainIndex = $chainIdx; |
|
| 214 | 26 | $this->changeIndex = $changeIdx; |
|
| 215 | 26 | } |
|
| 216 | |||
| 217 | /** |
||
| 218 | * @return AddressReaderBase |
||
| 219 | */ |
||
| 220 | 17 | public function getAddressReader() { |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @param int|null $chainIndex |
||
| 226 | * @return WalletPath |
||
| 227 | * @throws BlocktrailSDKException |
||
| 228 | */ |
||
| 229 | 17 | protected function getWalletPath($chainIndex = null) { |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | 3 | public function isSegwit() { |
|
| 246 | |||
| 247 | /** |
||
| 248 | * return the wallet identifier |
||
| 249 | * |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | 10 | public function getIdentifier() { |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Returns the wallets backup public key |
||
| 258 | * |
||
| 259 | * @return [xpub, path] |
||
|
|
|||
| 260 | */ |
||
| 261 | 1 | public function getBackupKey() { |
|
| 264 | |||
| 265 | /** |
||
| 266 | * return list of Blocktrail co-sign extended public keys |
||
| 267 | * |
||
| 268 | * @return array[] [ [xpub, path] ] |
||
| 269 | */ |
||
| 270 | 5 | public function getBlocktrailPublicKeys() { |
|
| 275 | |||
| 276 | /** |
||
| 277 | * check if wallet is locked |
||
| 278 | * |
||
| 279 | * @return bool |
||
| 280 | */ |
||
| 281 | 10 | public function isLocked() { |
|
| 284 | |||
| 285 | /** |
||
| 286 | * upgrade wallet to different blocktrail cosign key |
||
| 287 | * |
||
| 288 | * @param $keyIndex |
||
| 289 | * @return bool |
||
| 290 | * @throws \Exception |
||
| 291 | */ |
||
| 292 | 5 | public function upgradeKeyIndex($keyIndex) { |
|
| 319 | |||
| 320 | /** |
||
| 321 | * get a new BIP32 derivation for the next (unused) address |
||
| 322 | * by requesting it from the API |
||
| 323 | * |
||
| 324 | * @return string |
||
| 325 | * @param int|null $chainIndex |
||
| 326 | * @throws \Exception |
||
| 327 | */ |
||
| 328 | 17 | protected function getNewDerivation($chainIndex = null) { |
|
| 329 | 17 | $path = $this->getWalletPath($chainIndex)->path()->last("*"); |
|
| 330 | |||
| 331 | 16 | if (self::VERIFY_NEW_DERIVATION) { |
|
| 332 | 16 | $new = $this->sdk->_getNewDerivation($this->identifier, (string)$path); |
|
| 333 | |||
| 334 | 16 | $path = $new['path']; |
|
| 335 | 16 | $address = $new['address']; |
|
| 336 | |||
| 337 | 16 | $serverDecoded = $this->addressReader->fromString($address); |
|
| 338 | |||
| 339 | 16 | $redeemScript = $new['redeem_script']; |
|
| 340 | 16 | $witnessScript = array_key_exists('witness_script', $new) ? $new['witness_script'] : null; |
|
| 341 | |||
| 342 | /** @var ScriptInterface $checkRedeemScript */ |
||
| 343 | /** @var ScriptInterface $checkWitnessScript */ |
||
| 344 | 16 | list($checkAddress, $checkRedeemScript, $checkWitnessScript) = $this->getRedeemScriptByPath($path); |
|
| 345 | |||
| 346 | 16 | $oursDecoded = $this->addressReader->fromString($checkAddress); |
|
| 347 | |||
| 348 | 16 | if ($this->network === "bitcoincash" && |
|
| 349 | 16 | $serverDecoded instanceof Base58AddressInterface && |
|
| 350 | 16 | $oursDecoded instanceof CashAddress |
|
| 351 | ) { |
||
| 352 | // our address is a cashaddr, server gave us base58. |
||
| 353 | |||
| 354 | 1 | if (!$oursDecoded->getHash()->equals($serverDecoded->getHash())) { |
|
| 355 | throw new BlocktrailSDKException("Failed to verify legacy address from server [hash mismatch]"); |
||
| 356 | } |
||
| 357 | |||
| 358 | 1 | $matchedP2PKH = $serverDecoded instanceof PayToPubKeyHashAddress && $oursDecoded->getType() === ScriptType::P2PKH; |
|
| 359 | 1 | $matchedP2SH = $serverDecoded instanceof ScriptHashAddress && $oursDecoded->getType() === ScriptType::P2SH; |
|
| 360 | 1 | if (!($matchedP2PKH || $matchedP2SH)) { |
|
| 361 | throw new BlocktrailSDKException("Failed to verify legacy address from server [prefix mismatch]"); |
||
| 362 | } |
||
| 363 | |||
| 364 | // promote the legacy address to our cashaddr, as they are equivalent. |
||
| 365 | 1 | $address = $checkAddress; |
|
| 366 | } |
||
| 367 | |||
| 368 | 16 | if ($checkAddress != $address) { |
|
| 369 | throw new \Exception("Failed to verify that address from API [{$address}] matches address locally [{$checkAddress}]"); |
||
| 370 | } |
||
| 371 | |||
| 372 | 16 | if ($checkRedeemScript && $checkRedeemScript->getHex() != $redeemScript) { |
|
| 373 | throw new \Exception("Failed to verify that redeemScript from API [{$redeemScript}] matches address locally [{$checkRedeemScript->getHex()}]"); |
||
| 374 | } |
||
| 375 | |||
| 376 | 16 | if ($checkWitnessScript && $checkWitnessScript->getHex() != $witnessScript) { |
|
| 377 | 16 | throw new \Exception("Failed to verify that witnessScript from API [{$witnessScript}] matches address locally [{$checkWitnessScript->getHex()}]"); |
|
| 378 | } |
||
| 379 | } else { |
||
| 380 | $path = $this->sdk->getNewDerivation($this->identifier, (string)$path); |
||
| 381 | } |
||
| 382 | |||
| 383 | 16 | return (string)$path; |
|
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param string|BIP32Path $path |
||
| 388 | * @return BIP32Key|false |
||
| 389 | * @throws \Exception |
||
| 390 | * |
||
| 391 | * @TODO: hmm? |
||
| 392 | */ |
||
| 393 | 18 | protected function getParentPublicKey($path) { |
|
| 410 | |||
| 411 | /** |
||
| 412 | * get address for the specified path |
||
| 413 | * |
||
| 414 | * @param string|BIP32Path $path |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | 16 | public function getAddressByPath($path) { |
|
| 428 | |||
| 429 | /** |
||
| 430 | * @param string $path |
||
| 431 | * @return WalletScript |
||
| 432 | */ |
||
| 433 | 18 | public function getWalletScriptByPath($path) { |
|
| 445 | |||
| 446 | /** |
||
| 447 | * get address and redeemScript for specified path |
||
| 448 | * |
||
| 449 | * @param string $path |
||
| 450 | * @return array[string, ScriptInterface, ScriptInterface|null] [address, redeemScript, witnessScript] |
||
| 451 | */ |
||
| 452 | 17 | public function getRedeemScriptByPath($path) { |
|
| 459 | |||
| 460 | /** |
||
| 461 | * @param BIP32Key $key |
||
| 462 | * @param string|BIP32Path $path |
||
| 463 | * @return string |
||
| 464 | */ |
||
| 465 | protected function getAddressFromKey(BIP32Key $key, $path) { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param BIP32Key $key |
||
| 471 | * @param string|BIP32Path $path |
||
| 472 | * @return WalletScript |
||
| 473 | * @throws \Exception |
||
| 474 | */ |
||
| 475 | 18 | protected function getWalletScriptFromKey(BIP32Key $key, $path) { |
|
| 503 | |||
| 504 | /** |
||
| 505 | * get the path (and redeemScript) to specified address |
||
| 506 | * |
||
| 507 | * @param string $address |
||
| 508 | * @return array |
||
| 509 | */ |
||
| 510 | 1 | public function getPathForAddress($address) { |
|
| 511 | 1 | $decoded = $this->addressReader->fromString($address); |
|
| 512 | 1 | if ($decoded instanceof CashAddress) { |
|
| 513 | $address = $decoded->getLegacyAddress(); |
||
| 514 | } |
||
| 515 | |||
| 516 | 1 | return $this->sdk->getPathForAddress($this->identifier, $address); |
|
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @param string|BIP32Path $path |
||
| 521 | * @return BIP32Key |
||
| 522 | * @throws \Exception |
||
| 523 | */ |
||
| 524 | 18 | public function getBlocktrailPublicKey($path) { |
|
| 535 | |||
| 536 | /** |
||
| 537 | * generate a new derived key and return the new path and address for it |
||
| 538 | * |
||
| 539 | * @param int|null $chainIndex |
||
| 540 | * @return string[] [path, address] |
||
| 541 | */ |
||
| 542 | 17 | public function getNewAddressPair($chainIndex = null) { |
|
| 548 | |||
| 549 | /** |
||
| 550 | * generate a new derived private key and return the new address for it |
||
| 551 | * |
||
| 552 | * @param int|null $chainIndex |
||
| 553 | * @return string |
||
| 554 | */ |
||
| 555 | 9 | public function getNewAddress($chainIndex = null) { |
|
| 558 | |||
| 559 | /** |
||
| 560 | * generate a new derived private key and return the new address for it |
||
| 561 | * |
||
| 562 | * @return string |
||
| 563 | */ |
||
| 564 | 4 | public function getNewChangeAddress() { |
|
| 567 | |||
| 568 | /** |
||
| 569 | * get the balance for the wallet |
||
| 570 | * |
||
| 571 | * @return int[] [confirmed, unconfirmed] |
||
| 572 | */ |
||
| 573 | 9 | public function getBalance() { |
|
| 578 | |||
| 579 | /** |
||
| 580 | * do wallet discovery (slow) |
||
| 581 | * |
||
| 582 | * @param int $gap the gap setting to use for discovery |
||
| 583 | * @return int[] [confirmed, unconfirmed] |
||
| 584 | */ |
||
| 585 | 2 | public function doDiscovery($gap = 200) { |
|
| 590 | |||
| 591 | /** |
||
| 592 | * create, sign and send a transaction |
||
| 593 | * |
||
| 594 | * @param array $outputs [address => value, ] or [[address, value], ] or [['address' => address, 'value' => value], ] coins to send |
||
| 595 | * value should be INT |
||
| 596 | * @param string $changeAddress change address to use (autogenerated if NULL) |
||
| 597 | * @param bool $allowZeroConf |
||
| 598 | * @param bool $randomizeChangeIdx randomize the location of the change (for increased privacy / anonimity) |
||
| 599 | * @param string $feeStrategy |
||
| 600 | * @param null|int $forceFee set a fixed fee instead of automatically calculating the correct fee, not recommended! |
||
| 601 | * @param bool $apiCheckFee let the API apply sanity checks to the fee |
||
| 602 | * @return string the txid / transaction hash |
||
| 603 | * @throws \Exception |
||
| 604 | */ |
||
| 605 | 9 | public function pay(array $outputs, $changeAddress = null, $allowZeroConf = false, $randomizeChangeIdx = true, $feeStrategy = self::FEE_STRATEGY_OPTIMAL, $forceFee = null, $apiCheckFee = true) { |
|
| 633 | |||
| 634 | /** |
||
| 635 | * determine max spendable from wallet after fees |
||
| 636 | * |
||
| 637 | * @param bool $allowZeroConf |
||
| 638 | * @param string $feeStrategy |
||
| 639 | * @param null|int $forceFee set a fixed fee instead of automatically calculating the correct fee, not recommended! |
||
| 640 | * @param int $outputCnt |
||
| 641 | * @return string |
||
| 642 | * @throws BlocktrailSDKException |
||
| 643 | */ |
||
| 644 | public function getMaxSpendable($allowZeroConf = false, $feeStrategy = self::FEE_STRATEGY_OPTIMAL, $forceFee = null, $outputCnt = 1) { |
||
| 647 | |||
| 648 | /** |
||
| 649 | * parse outputs into normalized struct |
||
| 650 | * |
||
| 651 | * @param array $outputs [address => value, ] or [[address, value], ] or [['address' => address, 'value' => value], ] |
||
| 652 | * @return array [['address' => address, 'value' => value], ] |
||
| 653 | */ |
||
| 654 | 1 | public static function normalizeOutputsStruct(array $outputs) { |
|
| 682 | |||
| 683 | /** |
||
| 684 | * 'fund' the txBuilder with UTXOs (modified in place) |
||
| 685 | * |
||
| 686 | * @param TransactionBuilder $txBuilder |
||
| 687 | * @param bool|true $lockUTXOs |
||
| 688 | * @param bool|false $allowZeroConf |
||
| 689 | * @param null|int $forceFee |
||
| 690 | * @return TransactionBuilder |
||
| 691 | */ |
||
| 692 | 11 | public function coinSelectionForTxBuilder(TransactionBuilder $txBuilder, $lockUTXOs = true, $allowZeroConf = false, $forceFee = null) { |
|
| 721 | |||
| 722 | /** |
||
| 723 | * build inputs and outputs lists for TransactionBuilder |
||
| 724 | * |
||
| 725 | * @param TransactionBuilder $txBuilder |
||
| 726 | * @return [TransactionInterface, SignInfo[]] |
||
| 727 | * @throws \Exception |
||
| 728 | */ |
||
| 729 | 7 | public function buildTx(TransactionBuilder $txBuilder) { |
|
| 730 | 7 | $send = $txBuilder->getOutputs(); |
|
| 731 | 7 | $utxos = $txBuilder->getUtxos(); |
|
| 732 | 7 | $signInfo = []; |
|
| 733 | |||
| 734 | 7 | $txb = new TxBuilder(); |
|
| 735 | |||
| 736 | 7 | foreach ($utxos as $utxo) { |
|
| 737 | 7 | if (!$utxo->address || !$utxo->value || !$utxo->scriptPubKey) { |
|
| 738 | $tx = $this->sdk->transaction($utxo->hash); |
||
| 739 | |||
| 740 | if (!$tx || !isset($tx['outputs'][$utxo->index])) { |
||
| 741 | throw new \Exception("Invalid output [{$utxo->hash}][{$utxo->index}]"); |
||
| 742 | } |
||
| 743 | |||
| 744 | $output = $tx['outputs'][$utxo->index]; |
||
| 745 | |||
| 746 | if (!$utxo->address) { |
||
| 747 | $utxo->address = $this->addressReader->fromString($output['address']); |
||
| 748 | } |
||
| 749 | if (!$utxo->value) { |
||
| 750 | $utxo->value = $output['value']; |
||
| 751 | } |
||
| 752 | if (!$utxo->scriptPubKey) { |
||
| 753 | $utxo->scriptPubKey = ScriptFactory::fromHex($output['script_hex']); |
||
| 754 | } |
||
| 755 | } |
||
| 756 | |||
| 757 | 7 | if (SignInfo::MODE_SIGN === $utxo->signMode) { |
|
| 758 | 7 | if (!$utxo->path) { |
|
| 759 | $utxo->path = $this->getPathForAddress($utxo->address->getAddress()); |
||
| 760 | } |
||
| 761 | |||
| 762 | 7 | if (!$utxo->redeemScript || !$utxo->witnessScript) { |
|
| 763 | 6 | list(, $redeemScript, $witnessScript) = $this->getRedeemScriptByPath($utxo->path); |
|
| 764 | 6 | $utxo->redeemScript = $redeemScript; |
|
| 765 | 6 | $utxo->witnessScript = $witnessScript; |
|
| 766 | } |
||
| 767 | } |
||
| 768 | |||
| 769 | 7 | $signInfo[] = $utxo->getSignInfo(); |
|
| 770 | } |
||
| 771 | |||
| 772 | $utxoSum = array_sum(array_map(function (UTXO $utxo) { |
||
| 773 | 7 | return $utxo->value; |
|
| 774 | 7 | }, $utxos)); |
|
| 775 | 7 | if ($utxoSum < array_sum(array_column($send, 'value'))) { |
|
| 776 | 1 | throw new \Exception("Atempting to spend more than sum of UTXOs"); |
|
| 777 | } |
||
| 778 | |||
| 779 | 7 | list($fee, $change) = $this->determineFeeAndChange($txBuilder, $this->getHighPriorityFeePerKB(), $this->getOptimalFeePerKB(), $this->getLowPriorityFeePerKB()); |
|
| 780 | |||
| 781 | 7 | if ($txBuilder->getValidateFee() !== null) { |
|
| 782 | // sanity check to make sure the API isn't giving us crappy data |
||
| 783 | 5 | if (abs($txBuilder->getValidateFee() - $fee) > (Wallet::BASE_FEE * 5)) { |
|
| 784 | throw new \Exception("the fee suggested by the coin selection ({$txBuilder->getValidateFee()}) seems incorrect ({$fee})"); |
||
| 785 | } |
||
| 786 | } |
||
| 787 | |||
| 788 | 7 | if ($change > 0) { |
|
| 789 | 5 | $send[] = [ |
|
| 790 | 5 | 'address' => $txBuilder->getChangeAddress() ?: $this->getNewChangeAddress(), |
|
| 791 | 5 | 'value' => $change |
|
| 792 | ]; |
||
| 793 | } |
||
| 794 | |||
| 795 | 7 | foreach ($utxos as $utxo) { |
|
| 796 | 7 | $txb->spendOutPoint(new OutPoint(Buffer::hex($utxo->hash), $utxo->index)); |
|
| 797 | } |
||
| 798 | |||
| 799 | // outputs should be randomized to make the change harder to detect |
||
| 800 | 7 | if ($txBuilder->shouldRandomizeChangeOuput()) { |
|
| 801 | 7 | shuffle($send); |
|
| 802 | } |
||
| 803 | |||
| 804 | 7 | foreach ($send as $out) { |
|
| 805 | 7 | assert(isset($out['value'])); |
|
| 806 | |||
| 807 | 7 | if (isset($out['scriptPubKey'])) { |
|
| 808 | 7 | $txb->output($out['value'], $out['scriptPubKey']); |
|
| 809 | 5 | } elseif (isset($out['address'])) { |
|
| 810 | 5 | $txb->output($out['value'], $this->addressReader->fromString($out['address'])->getScriptPubKey()); |
|
| 811 | } else { |
||
| 812 | 7 | throw new \Exception(); |
|
| 813 | } |
||
| 814 | } |
||
| 815 | |||
| 816 | 7 | return [$txb->get(), $signInfo]; |
|
| 817 | } |
||
| 818 | |||
| 819 | 7 | public function determineFeeAndChange(TransactionBuilder $txBuilder, $highPriorityFeePerKB, $optimalFeePerKB, $lowPriorityFeePerKB) { |
|
| 820 | 7 | $send = (new OutputsNormalizer($this->addressReader))->normalize($txBuilder->getOutputs()); |
|
| 821 | 7 | $utxos = $txBuilder->getUtxos(); |
|
| 822 | |||
| 823 | 7 | $fee = $txBuilder->getFee(); |
|
| 824 | 7 | $change = null; |
|
| 825 | |||
| 826 | // if the fee is fixed we just need to calculate the change |
||
| 827 | 7 | if ($fee !== null) { |
|
| 828 | 2 | $change = $this->determineChange($utxos, $send, $fee); |
|
| 829 | |||
| 830 | // if change is not dust we need to add a change output |
||
| 831 | 2 | if ($change > Blocktrail::DUST) { |
|
| 832 | 1 | $send[] = ['address' => 'change', 'value' => $change]; |
|
| 833 | } else { |
||
| 834 | // if change is dust we add it to the fee |
||
| 835 | 1 | $fee += $change; |
|
| 836 | 1 | $change = 0; |
|
| 837 | } |
||
| 838 | |||
| 839 | 2 | return [$fee, $change]; |
|
| 840 | } else { |
||
| 841 | 7 | $fee = $this->determineFee($utxos, $send, $txBuilder->getFeeStrategy(), $highPriorityFeePerKB, $optimalFeePerKB, $lowPriorityFeePerKB); |
|
| 842 | |||
| 843 | 7 | $change = $this->determineChange($utxos, $send, $fee); |
|
| 844 | |||
| 845 | 7 | if ($change > 0) { |
|
| 846 | 6 | $changeIdx = count($send); |
|
| 847 | // set dummy change output |
||
| 848 | 6 | $send[$changeIdx] = ['address' => 'change', 'value' => $change]; |
|
| 849 | |||
| 850 | // recaculate fee now that we know that we have a change output |
||
| 851 | 6 | $fee2 = $this->determineFee($utxos, $send, $txBuilder->getFeeStrategy(), $highPriorityFeePerKB, $optimalFeePerKB, $lowPriorityFeePerKB); |
|
| 852 | |||
| 853 | // unset dummy change output |
||
| 854 | 6 | unset($send[$changeIdx]); |
|
| 855 | |||
| 856 | // if adding the change output made the fee bump up and the change is smaller than the fee |
||
| 857 | // then we're not doing change |
||
| 858 | 6 | if ($fee2 > $fee && $fee2 > $change) { |
|
| 859 | 1 | $change = 0; |
|
| 860 | } else { |
||
| 861 | 5 | $change = $this->determineChange($utxos, $send, $fee2); |
|
| 862 | |||
| 863 | // if change is not dust we need to add a change output |
||
| 864 | 5 | if ($change > Blocktrail::DUST) { |
|
| 865 | 5 | $send[$changeIdx] = ['address' => 'change', 'value' => $change]; |
|
| 866 | } else { |
||
| 867 | // if change is dust we do nothing (implicitly it's added to the fee) |
||
| 868 | 1 | $change = 0; |
|
| 869 | } |
||
| 870 | } |
||
| 871 | } |
||
| 872 | |||
| 873 | |||
| 874 | 7 | $fee = $this->determineFee($utxos, $send, $txBuilder->getFeeStrategy(), $highPriorityFeePerKB, $optimalFeePerKB, $lowPriorityFeePerKB); |
|
| 875 | |||
| 876 | 7 | return [$fee, $change]; |
|
| 877 | } |
||
| 878 | } |
||
| 879 | |||
| 880 | /** |
||
| 881 | * create, sign and send transction based on TransactionBuilder |
||
| 882 | * |
||
| 883 | * @param TransactionBuilder $txBuilder |
||
| 884 | * @param bool $apiCheckFee let the API check if the fee is correct |
||
| 885 | * @return string |
||
| 886 | * @throws \Exception |
||
| 887 | */ |
||
| 888 | 4 | public function sendTx(TransactionBuilder $txBuilder, $apiCheckFee = true) { |
|
| 893 | |||
| 894 | /** |
||
| 895 | * !! INTERNAL METHOD, public for testing purposes !! |
||
| 896 | * create, sign and send transction based on inputs and outputs |
||
| 897 | * |
||
| 898 | * @param Transaction $tx |
||
| 899 | * @param SignInfo[] $signInfo |
||
| 900 | * @param bool $apiCheckFee let the API check if the fee is correct |
||
| 901 | * @return string |
||
| 902 | * @throws \Exception |
||
| 903 | * @internal |
||
| 904 | */ |
||
| 905 | 4 | public function _sendTx(Transaction $tx, array $signInfo, $apiCheckFee = true) { |
|
| 927 | |||
| 928 | /** |
||
| 929 | * only supports estimating fee for 2of3 multsig UTXOs and P2PKH/P2SH outputs |
||
| 930 | * |
||
| 931 | * @todo: mark this as deprecated, insist on the utxo's or qualified scripts. |
||
| 932 | * @param int $utxoCnt number of unspent inputs in transaction |
||
| 933 | * @param int $outputCnt number of outputs in transaction |
||
| 934 | * @return float |
||
| 935 | * @access public reminder that people might use this! |
||
| 936 | */ |
||
| 937 | 1 | public static function estimateFee($utxoCnt, $outputCnt) { |
|
| 942 | |||
| 943 | /** |
||
| 944 | * @param int $size size in bytes |
||
| 945 | * @return int fee in satoshi |
||
| 946 | */ |
||
| 947 | 5 | public static function baseFeeForSize($size) { |
|
| 952 | |||
| 953 | /** |
||
| 954 | * @todo: variable varint |
||
| 955 | * @todo: deprecate |
||
| 956 | * @param int $txinSize |
||
| 957 | * @param int $txoutSize |
||
| 958 | * @return float |
||
| 959 | */ |
||
| 960 | 2 | public static function estimateSize($txinSize, $txoutSize) { |
|
| 963 | |||
| 964 | /** |
||
| 965 | * only supports estimating size for P2PKH/P2SH outputs |
||
| 966 | * |
||
| 967 | * @param int $outputCnt number of outputs in transaction |
||
| 968 | * @return float |
||
| 969 | */ |
||
| 970 | 2 | public static function estimateSizeOutputs($outputCnt) { |
|
| 973 | |||
| 974 | /** |
||
| 975 | * only supports estimating size for 2of3 multsig UTXOs |
||
| 976 | * |
||
| 977 | * @param int $utxoCnt number of unspent inputs in transaction |
||
| 978 | * @return float |
||
| 979 | */ |
||
| 980 | 3 | public static function estimateSizeUTXOs($utxoCnt) { |
|
| 1016 | |||
| 1017 | /** |
||
| 1018 | * determine how much fee is required based on the inputs and outputs |
||
| 1019 | * this is an estimation, not a proper 100% correct calculation |
||
| 1020 | * |
||
| 1021 | * @param UTXO[] $utxos |
||
| 1022 | * @param array[] $outputs |
||
| 1023 | * @param $feeStrategy |
||
| 1024 | * @param $highPriorityFeePerKB |
||
| 1025 | * @param $optimalFeePerKB |
||
| 1026 | * @param $lowPriorityFeePerKB |
||
| 1027 | * @return int |
||
| 1028 | * @throws BlocktrailSDKException |
||
| 1029 | */ |
||
| 1030 | 7 | protected function determineFee($utxos, $outputs, $feeStrategy, $highPriorityFeePerKB, $optimalFeePerKB, $lowPriorityFeePerKB) { |
|
| 1054 | |||
| 1055 | /** |
||
| 1056 | * determine how much change is left over based on the inputs and outputs and the fee |
||
| 1057 | * |
||
| 1058 | * @param UTXO[] $utxos |
||
| 1059 | * @param array[] $outputs |
||
| 1060 | * @param int $fee |
||
| 1061 | * @return int |
||
| 1062 | */ |
||
| 1063 | 7 | protected function determineChange($utxos, $outputs, $fee) { |
|
| 1071 | |||
| 1072 | /** |
||
| 1073 | * sign a raw transaction with the private keys that we have |
||
| 1074 | * |
||
| 1075 | * @param Transaction $tx |
||
| 1076 | * @param SignInfo[] $signInfo |
||
| 1077 | * @return TransactionInterface |
||
| 1078 | * @throws \Exception |
||
| 1079 | */ |
||
| 1080 | 4 | protected function signTransaction(Transaction $tx, array $signInfo) { |
|
| 1112 | |||
| 1113 | /** |
||
| 1114 | * send the transaction using the API |
||
| 1115 | * |
||
| 1116 | * @param string|array $signed |
||
| 1117 | * @param string[] $paths |
||
| 1118 | * @param bool $checkFee |
||
| 1119 | * @return string the complete raw transaction |
||
| 1120 | * @throws \Exception |
||
| 1121 | */ |
||
| 1122 | 4 | protected function sendTransaction($signed, $paths, $checkFee = false) { |
|
| 1125 | |||
| 1126 | /** |
||
| 1127 | * @param \array[] $outputs |
||
| 1128 | * @param bool $lockUTXO |
||
| 1129 | * @param bool $allowZeroConf |
||
| 1130 | * @param int|null|string $feeStrategy |
||
| 1131 | * @param null $forceFee |
||
| 1132 | * @return array |
||
| 1133 | */ |
||
| 1134 | 12 | public function coinSelection($outputs, $lockUTXO = true, $allowZeroConf = false, $feeStrategy = self::FEE_STRATEGY_OPTIMAL, $forceFee = null) { |
|
| 1152 | |||
| 1153 | 7 | public function getHighPriorityFeePerKB() { |
|
| 1160 | |||
| 1161 | 7 | public function getOptimalFeePerKB() { |
|
| 1168 | |||
| 1169 | 7 | public function getLowPriorityFeePerKB() { |
|
| 1176 | |||
| 1177 | 2 | public function updateFeePerKB() { |
|
| 1186 | |||
| 1187 | /** |
||
| 1188 | * delete the wallet |
||
| 1189 | * |
||
| 1190 | * @param bool $force ignore warnings (such as non-zero balance) |
||
| 1191 | * @return mixed |
||
| 1192 | * @throws \Exception |
||
| 1193 | */ |
||
| 1194 | 10 | public function deleteWallet($force = false) { |
|
| 1202 | |||
| 1203 | /** |
||
| 1204 | * create checksum to verify ownership of the master primary key |
||
| 1205 | * |
||
| 1206 | * @return string[] [address, signature] |
||
| 1207 | */ |
||
| 1208 | 10 | protected function createChecksumVerificationSignature() { |
|
| 1219 | |||
| 1220 | /** |
||
| 1221 | * setup a webhook for our wallet |
||
| 1222 | * |
||
| 1223 | * @param string $url URL to receive webhook events |
||
| 1224 | * @param string $identifier identifier for the webhook, defaults to WALLET-{$this->identifier} |
||
| 1225 | * @return array |
||
| 1226 | */ |
||
| 1227 | 1 | public function setupWebhook($url, $identifier = null) { |
|
| 1231 | |||
| 1232 | /** |
||
| 1233 | * @param string $identifier identifier for the webhook, defaults to WALLET-{$this->identifier} |
||
| 1234 | * @return mixed |
||
| 1235 | */ |
||
| 1236 | 1 | public function deleteWebhook($identifier = null) { |
|
| 1240 | |||
| 1241 | /** |
||
| 1242 | * lock a specific unspent output |
||
| 1243 | * |
||
| 1244 | * @param $txHash |
||
| 1245 | * @param $txIdx |
||
| 1246 | * @param int $ttl |
||
| 1247 | * @return bool |
||
| 1248 | */ |
||
| 1249 | public function lockUTXO($txHash, $txIdx, $ttl = 3) { |
||
| 1252 | |||
| 1253 | /** |
||
| 1254 | * unlock a specific unspent output |
||
| 1255 | * |
||
| 1256 | * @param $txHash |
||
| 1257 | * @param $txIdx |
||
| 1258 | * @return bool |
||
| 1259 | */ |
||
| 1260 | public function unlockUTXO($txHash, $txIdx) { |
||
| 1263 | |||
| 1264 | /** |
||
| 1265 | * get all transactions for the wallet (paginated) |
||
| 1266 | * |
||
| 1267 | * @param integer $page pagination: page number |
||
| 1268 | * @param integer $limit pagination: records per page (max 500) |
||
| 1269 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1270 | * @return array associative array containing the response |
||
| 1271 | */ |
||
| 1272 | 1 | public function transactions($page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 1275 | |||
| 1276 | /** |
||
| 1277 | * get all addresses for the wallet (paginated) |
||
| 1278 | * |
||
| 1279 | * @param integer $page pagination: page number |
||
| 1280 | * @param integer $limit pagination: records per page (max 500) |
||
| 1281 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1282 | * @return array associative array containing the response |
||
| 1283 | */ |
||
| 1284 | 1 | public function addresses($page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 1287 | |||
| 1288 | /** |
||
| 1289 | * get all UTXOs for the wallet (paginated) |
||
| 1290 | * |
||
| 1291 | * @param integer $page pagination: page number |
||
| 1292 | * @param integer $limit pagination: records per page (max 500) |
||
| 1293 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1294 | * @param boolean $zeroconf include zero confirmation transactions |
||
| 1295 | * @return array associative array containing the response |
||
| 1296 | */ |
||
| 1297 | 1 | public function utxos($page = 1, $limit = 20, $sortDir = 'asc', $zeroconf = true) { |
|
| 1300 | } |
||
| 1301 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.