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 | 5 | 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 | * @return string the txid / transaction hash |
||
| 602 | * @throws \Exception |
||
| 603 | */ |
||
| 604 | 9 | public function pay(array $outputs, $changeAddress = null, $allowZeroConf = false, $randomizeChangeIdx = true, $feeStrategy = self::FEE_STRATEGY_OPTIMAL, $forceFee = null) { |
|
| 630 | |||
| 631 | /** |
||
| 632 | * determine max spendable from wallet after fees |
||
| 633 | * |
||
| 634 | * @param bool $allowZeroConf |
||
| 635 | * @param string $feeStrategy |
||
| 636 | * @param null|int $forceFee set a fixed fee instead of automatically calculating the correct fee, not recommended! |
||
| 637 | * @param int $outputCnt |
||
| 638 | * @return string |
||
| 639 | * @throws BlocktrailSDKException |
||
| 640 | */ |
||
| 641 | public function getMaxSpendable($allowZeroConf = false, $feeStrategy = self::FEE_STRATEGY_OPTIMAL, $forceFee = null, $outputCnt = 1) { |
||
| 644 | |||
| 645 | /** |
||
| 646 | * parse outputs into normalized struct |
||
| 647 | * |
||
| 648 | * @param array $outputs [address => value, ] or [[address, value], ] or [['address' => address, 'value' => value], ] |
||
| 649 | * @return array [['address' => address, 'value' => value], ] |
||
| 650 | */ |
||
| 651 | 1 | public static function normalizeOutputsStruct(array $outputs) { |
|
| 679 | |||
| 680 | /** |
||
| 681 | * 'fund' the txBuilder with UTXOs (modified in place) |
||
| 682 | * |
||
| 683 | * @param TransactionBuilder $txBuilder |
||
| 684 | * @param bool|true $lockUTXOs |
||
| 685 | * @param bool|false $allowZeroConf |
||
| 686 | * @param null|int $forceFee |
||
| 687 | * @return TransactionBuilder |
||
| 688 | */ |
||
| 689 | 11 | public function coinSelectionForTxBuilder(TransactionBuilder $txBuilder, $lockUTXOs = true, $allowZeroConf = false, $forceFee = null) { |
|
| 718 | |||
| 719 | /** |
||
| 720 | * build inputs and outputs lists for TransactionBuilder |
||
| 721 | * |
||
| 722 | * @param TransactionBuilder $txBuilder |
||
| 723 | * @return [TransactionInterface, SignInfo[]] |
||
| 724 | * @throws \Exception |
||
| 725 | */ |
||
| 726 | 7 | public function buildTx(TransactionBuilder $txBuilder) { |
|
| 727 | 7 | $send = $txBuilder->getOutputs(); |
|
| 728 | 7 | $utxos = $txBuilder->getUtxos(); |
|
| 729 | 7 | $signInfo = []; |
|
| 730 | |||
| 731 | 7 | $txb = new TxBuilder(); |
|
| 732 | |||
| 733 | 7 | foreach ($utxos as $utxo) { |
|
| 734 | 7 | if (!$utxo->address || !$utxo->value || !$utxo->scriptPubKey) { |
|
| 735 | 1 | $tx = $this->sdk->transaction($utxo->hash); |
|
| 736 | |||
| 737 | 1 | if (!$tx || !isset($tx['outputs'][$utxo->index])) { |
|
| 738 | throw new \Exception("Invalid output [{$utxo->hash}][{$utxo->index}]"); |
||
| 739 | } |
||
| 740 | |||
| 741 | 1 | $output = $tx['outputs'][$utxo->index]; |
|
| 742 | |||
| 743 | 1 | if (!$utxo->address) { |
|
| 744 | $utxo->address = $this->addressReader->fromString($output['address']); |
||
| 745 | } |
||
| 746 | 1 | if (!$utxo->value) { |
|
| 747 | $utxo->value = $output['value']; |
||
| 748 | } |
||
| 749 | 1 | if (!$utxo->scriptPubKey) { |
|
| 750 | 1 | $utxo->scriptPubKey = ScriptFactory::fromHex($output['script_hex']); |
|
| 751 | } |
||
| 752 | } |
||
| 753 | |||
| 754 | 7 | if (SignInfo::MODE_SIGN === $utxo->signMode) { |
|
| 755 | 7 | if (!$utxo->path) { |
|
| 756 | $utxo->path = $this->getPathForAddress($utxo->address->getAddress()); |
||
| 757 | } |
||
| 758 | |||
| 759 | 7 | if (!$utxo->redeemScript || !$utxo->witnessScript) { |
|
| 760 | 6 | list(, $redeemScript, $witnessScript) = $this->getRedeemScriptByPath($utxo->path); |
|
| 761 | 6 | $utxo->redeemScript = $redeemScript; |
|
| 762 | 6 | $utxo->witnessScript = $witnessScript; |
|
| 763 | } |
||
| 764 | } |
||
| 765 | |||
| 766 | 7 | $signInfo[] = $utxo->getSignInfo(); |
|
| 767 | } |
||
| 768 | |||
| 769 | $utxoSum = array_sum(array_map(function (UTXO $utxo) { |
||
| 770 | 7 | return $utxo->value; |
|
| 771 | 7 | }, $utxos)); |
|
| 772 | 7 | if ($utxoSum < array_sum(array_column($send, 'value'))) { |
|
| 773 | 1 | throw new \Exception("Atempting to spend more than sum of UTXOs"); |
|
| 774 | } |
||
| 775 | |||
| 776 | 7 | list($fee, $change) = $this->determineFeeAndChange($txBuilder, $this->getHighPriorityFeePerKB(), $this->getOptimalFeePerKB(), $this->getLowPriorityFeePerKB()); |
|
| 777 | |||
| 778 | 7 | if ($txBuilder->getValidateFee() !== null) { |
|
| 779 | // sanity check to make sure the API isn't giving us crappy data |
||
| 780 | 5 | if (abs($txBuilder->getValidateFee() - $fee) > (Wallet::BASE_FEE * 5)) { |
|
| 781 | throw new \Exception("the fee suggested by the coin selection ({$txBuilder->getValidateFee()}) seems incorrect ({$fee})"); |
||
| 782 | } |
||
| 783 | } |
||
| 784 | |||
| 785 | 7 | if ($change > 0) { |
|
| 786 | 6 | $send[] = [ |
|
| 787 | 6 | 'address' => $txBuilder->getChangeAddress() ?: $this->getNewChangeAddress(), |
|
| 788 | 6 | 'value' => $change |
|
| 789 | ]; |
||
| 790 | } |
||
| 791 | |||
| 792 | 7 | foreach ($utxos as $utxo) { |
|
| 793 | 7 | $txb->spendOutPoint(new OutPoint(Buffer::hex($utxo->hash), $utxo->index)); |
|
| 794 | } |
||
| 795 | |||
| 796 | // outputs should be randomized to make the change harder to detect |
||
| 797 | 7 | if ($txBuilder->shouldRandomizeChangeOuput()) { |
|
| 798 | 7 | shuffle($send); |
|
| 799 | } |
||
| 800 | |||
| 801 | 7 | foreach ($send as $out) { |
|
| 802 | 7 | assert(isset($out['value'])); |
|
| 803 | |||
| 804 | 7 | if (isset($out['scriptPubKey'])) { |
|
| 805 | 7 | $txb->output($out['value'], $out['scriptPubKey']); |
|
| 806 | 6 | } elseif (isset($out['address'])) { |
|
| 807 | 6 | $txb->output($out['value'], $this->addressReader->fromString($out['address'])->getScriptPubKey()); |
|
| 808 | } else { |
||
| 809 | 7 | throw new \Exception(); |
|
| 810 | } |
||
| 811 | } |
||
| 812 | |||
| 813 | 7 | return [$txb->get(), $signInfo]; |
|
| 814 | } |
||
| 815 | |||
| 816 | 7 | public function determineFeeAndChange(TransactionBuilder $txBuilder, $highPriorityFeePerKB, $optimalFeePerKB, $lowPriorityFeePerKB) { |
|
| 817 | 7 | $send = (new OutputsNormalizer($this->addressReader))->normalize($txBuilder->getOutputs()); |
|
| 818 | 7 | $utxos = $txBuilder->getUtxos(); |
|
| 819 | |||
| 820 | 7 | $fee = $txBuilder->getFee(); |
|
| 821 | 7 | $change = null; |
|
| 822 | |||
| 823 | // if the fee is fixed we just need to calculate the change |
||
| 824 | 7 | if ($fee !== null) { |
|
| 825 | 2 | $change = $this->determineChange($utxos, $send, $fee); |
|
| 826 | |||
| 827 | // if change is not dust we need to add a change output |
||
| 828 | 2 | if ($change > Blocktrail::DUST) { |
|
| 829 | 1 | $send[] = ['address' => 'change', 'value' => $change]; |
|
| 830 | } else { |
||
| 831 | // if change is dust we add it to the fee |
||
| 832 | 1 | $fee += $change; |
|
| 833 | 1 | $change = 0; |
|
| 834 | } |
||
| 835 | |||
| 836 | 2 | return [$fee, $change]; |
|
| 837 | } else { |
||
| 838 | 7 | $fee = $this->determineFee($utxos, $send, $txBuilder->getFeeStrategy(), $highPriorityFeePerKB, $optimalFeePerKB, $lowPriorityFeePerKB); |
|
| 839 | |||
| 840 | 7 | $change = $this->determineChange($utxos, $send, $fee); |
|
| 841 | |||
| 842 | 7 | if ($change > 0) { |
|
| 843 | 6 | $changeIdx = count($send); |
|
| 844 | // set dummy change output |
||
| 845 | 6 | $send[$changeIdx] = ['address' => 'change', 'value' => $change]; |
|
| 846 | |||
| 847 | // recaculate fee now that we know that we have a change output |
||
| 848 | 6 | $fee2 = $this->determineFee($utxos, $send, $txBuilder->getFeeStrategy(), $highPriorityFeePerKB, $optimalFeePerKB, $lowPriorityFeePerKB); |
|
| 849 | |||
| 850 | // unset dummy change output |
||
| 851 | 6 | unset($send[$changeIdx]); |
|
| 852 | |||
| 853 | // if adding the change output made the fee bump up and the change is smaller than the fee |
||
| 854 | // then we're not doing change |
||
| 855 | 6 | if ($fee2 > $fee && $fee2 > $change) { |
|
| 856 | $change = 0; |
||
| 857 | } else { |
||
| 858 | 6 | $change = $this->determineChange($utxos, $send, $fee2); |
|
| 859 | |||
| 860 | // if change is not dust we need to add a change output |
||
| 861 | 6 | if ($change > Blocktrail::DUST) { |
|
| 862 | 6 | $send[$changeIdx] = ['address' => 'change', 'value' => $change]; |
|
| 863 | } else { |
||
| 864 | // if change is dust we do nothing (implicitly it's added to the fee) |
||
| 865 | 1 | $change = 0; |
|
| 866 | } |
||
| 867 | } |
||
| 868 | } |
||
| 869 | |||
| 870 | |||
| 871 | 7 | $fee = $this->determineFee($utxos, $send, $txBuilder->getFeeStrategy(), $highPriorityFeePerKB, $optimalFeePerKB, $lowPriorityFeePerKB); |
|
| 872 | |||
| 873 | 7 | return [$fee, $change]; |
|
| 874 | } |
||
| 875 | } |
||
| 876 | |||
| 877 | /** |
||
| 878 | * create, sign and send transction based on TransactionBuilder |
||
| 879 | * |
||
| 880 | * @param TransactionBuilder $txBuilder |
||
| 881 | * @param bool $apiCheckFee let the API check if the fee is correct |
||
| 882 | * @return string |
||
| 883 | * @throws \Exception |
||
| 884 | */ |
||
| 885 | 4 | public function sendTx(TransactionBuilder $txBuilder, $apiCheckFee = true) { |
|
| 890 | |||
| 891 | /** |
||
| 892 | * !! INTERNAL METHOD, public for testing purposes !! |
||
| 893 | * create, sign and send transction based on inputs and outputs |
||
| 894 | * |
||
| 895 | * @param Transaction $tx |
||
| 896 | * @param SignInfo[] $signInfo |
||
| 897 | * @param bool $apiCheckFee let the API check if the fee is correct |
||
| 898 | * @return string |
||
| 899 | * @throws \Exception |
||
| 900 | * @internal |
||
| 901 | */ |
||
| 902 | 4 | public function _sendTx(Transaction $tx, array $signInfo, $apiCheckFee = true) { |
|
| 924 | |||
| 925 | /** |
||
| 926 | * only supports estimating fee for 2of3 multsig UTXOs and P2PKH/P2SH outputs |
||
| 927 | * |
||
| 928 | * @todo: mark this as deprecated, insist on the utxo's or qualified scripts. |
||
| 929 | * @param int $utxoCnt number of unspent inputs in transaction |
||
| 930 | * @param int $outputCnt number of outputs in transaction |
||
| 931 | * @return float |
||
| 932 | * @access public reminder that people might use this! |
||
| 933 | */ |
||
| 934 | 1 | public static function estimateFee($utxoCnt, $outputCnt) { |
|
| 939 | |||
| 940 | /** |
||
| 941 | * @param int $size size in bytes |
||
| 942 | * @return int fee in satoshi |
||
| 943 | */ |
||
| 944 | 5 | public static function baseFeeForSize($size) { |
|
| 949 | |||
| 950 | /** |
||
| 951 | * @todo: variable varint |
||
| 952 | * @todo: deprecate |
||
| 953 | * @param int $txinSize |
||
| 954 | * @param int $txoutSize |
||
| 955 | * @return float |
||
| 956 | */ |
||
| 957 | 2 | public static function estimateSize($txinSize, $txoutSize) { |
|
| 960 | |||
| 961 | /** |
||
| 962 | * only supports estimating size for P2PKH/P2SH outputs |
||
| 963 | * |
||
| 964 | * @param int $outputCnt number of outputs in transaction |
||
| 965 | * @return float |
||
| 966 | */ |
||
| 967 | 2 | public static function estimateSizeOutputs($outputCnt) { |
|
| 970 | |||
| 971 | /** |
||
| 972 | * only supports estimating size for 2of3 multsig UTXOs |
||
| 973 | * |
||
| 974 | * @param int $utxoCnt number of unspent inputs in transaction |
||
| 975 | * @return float |
||
| 976 | */ |
||
| 977 | 3 | public static function estimateSizeUTXOs($utxoCnt) { |
|
| 1013 | |||
| 1014 | /** |
||
| 1015 | * determine how much fee is required based on the inputs and outputs |
||
| 1016 | * this is an estimation, not a proper 100% correct calculation |
||
| 1017 | * |
||
| 1018 | * @param UTXO[] $utxos |
||
| 1019 | * @param array[] $outputs |
||
| 1020 | * @param $feeStrategy |
||
| 1021 | * @param $highPriorityFeePerKB |
||
| 1022 | * @param $optimalFeePerKB |
||
| 1023 | * @param $lowPriorityFeePerKB |
||
| 1024 | * @return int |
||
| 1025 | * @throws BlocktrailSDKException |
||
| 1026 | */ |
||
| 1027 | 7 | protected function determineFee($utxos, $outputs, $feeStrategy, $highPriorityFeePerKB, $optimalFeePerKB, $lowPriorityFeePerKB) { |
|
| 1051 | |||
| 1052 | /** |
||
| 1053 | * determine how much change is left over based on the inputs and outputs and the fee |
||
| 1054 | * |
||
| 1055 | * @param UTXO[] $utxos |
||
| 1056 | * @param array[] $outputs |
||
| 1057 | * @param int $fee |
||
| 1058 | * @return int |
||
| 1059 | */ |
||
| 1060 | 7 | protected function determineChange($utxos, $outputs, $fee) { |
|
| 1068 | |||
| 1069 | /** |
||
| 1070 | * sign a raw transaction with the private keys that we have |
||
| 1071 | * |
||
| 1072 | * @param Transaction $tx |
||
| 1073 | * @param SignInfo[] $signInfo |
||
| 1074 | * @return TransactionInterface |
||
| 1075 | * @throws \Exception |
||
| 1076 | */ |
||
| 1077 | 4 | protected function signTransaction(Transaction $tx, array $signInfo) { |
|
| 1109 | |||
| 1110 | /** |
||
| 1111 | * send the transaction using the API |
||
| 1112 | * |
||
| 1113 | * @param string|array $signed |
||
| 1114 | * @param string[] $paths |
||
| 1115 | * @param bool $checkFee |
||
| 1116 | * @return string the complete raw transaction |
||
| 1117 | * @throws \Exception |
||
| 1118 | */ |
||
| 1119 | 4 | protected function sendTransaction($signed, $paths, $checkFee = false) { |
|
| 1122 | |||
| 1123 | /** |
||
| 1124 | * @param \array[] $outputs |
||
| 1125 | * @param bool $lockUTXO |
||
| 1126 | * @param bool $allowZeroConf |
||
| 1127 | * @param int|null|string $feeStrategy |
||
| 1128 | * @param null $forceFee |
||
| 1129 | * @return array |
||
| 1130 | */ |
||
| 1131 | 12 | public function coinSelection($outputs, $lockUTXO = true, $allowZeroConf = false, $feeStrategy = self::FEE_STRATEGY_OPTIMAL, $forceFee = null) { |
|
| 1149 | |||
| 1150 | 7 | public function getHighPriorityFeePerKB() { |
|
| 1157 | |||
| 1158 | 7 | public function getOptimalFeePerKB() { |
|
| 1165 | |||
| 1166 | 7 | public function getLowPriorityFeePerKB() { |
|
| 1173 | |||
| 1174 | 3 | public function updateFeePerKB() { |
|
| 1183 | |||
| 1184 | /** |
||
| 1185 | * delete the wallet |
||
| 1186 | * |
||
| 1187 | * @param bool $force ignore warnings (such as non-zero balance) |
||
| 1188 | * @return mixed |
||
| 1189 | * @throws \Exception |
||
| 1190 | */ |
||
| 1191 | 10 | public function deleteWallet($force = false) { |
|
| 1199 | |||
| 1200 | /** |
||
| 1201 | * create checksum to verify ownership of the master primary key |
||
| 1202 | * |
||
| 1203 | * @return string[] [address, signature] |
||
| 1204 | */ |
||
| 1205 | 10 | protected function createChecksumVerificationSignature() { |
|
| 1216 | |||
| 1217 | /** |
||
| 1218 | * setup a webhook for our wallet |
||
| 1219 | * |
||
| 1220 | * @param string $url URL to receive webhook events |
||
| 1221 | * @param string $identifier identifier for the webhook, defaults to WALLET-{$this->identifier} |
||
| 1222 | * @return array |
||
| 1223 | */ |
||
| 1224 | 1 | public function setupWebhook($url, $identifier = null) { |
|
| 1228 | |||
| 1229 | /** |
||
| 1230 | * @param string $identifier identifier for the webhook, defaults to WALLET-{$this->identifier} |
||
| 1231 | * @return mixed |
||
| 1232 | */ |
||
| 1233 | 1 | public function deleteWebhook($identifier = null) { |
|
| 1237 | |||
| 1238 | /** |
||
| 1239 | * lock a specific unspent output |
||
| 1240 | * |
||
| 1241 | * @param $txHash |
||
| 1242 | * @param $txIdx |
||
| 1243 | * @param int $ttl |
||
| 1244 | * @return bool |
||
| 1245 | */ |
||
| 1246 | public function lockUTXO($txHash, $txIdx, $ttl = 3) { |
||
| 1249 | |||
| 1250 | /** |
||
| 1251 | * unlock a specific unspent output |
||
| 1252 | * |
||
| 1253 | * @param $txHash |
||
| 1254 | * @param $txIdx |
||
| 1255 | * @return bool |
||
| 1256 | */ |
||
| 1257 | public function unlockUTXO($txHash, $txIdx) { |
||
| 1260 | |||
| 1261 | /** |
||
| 1262 | * get all transactions for the wallet (paginated) |
||
| 1263 | * |
||
| 1264 | * @param integer $page pagination: page number |
||
| 1265 | * @param integer $limit pagination: records per page (max 500) |
||
| 1266 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1267 | * @return array associative array containing the response |
||
| 1268 | */ |
||
| 1269 | 1 | public function transactions($page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 1272 | |||
| 1273 | /** |
||
| 1274 | * get all addresses for the wallet (paginated) |
||
| 1275 | * |
||
| 1276 | * @param integer $page pagination: page number |
||
| 1277 | * @param integer $limit pagination: records per page (max 500) |
||
| 1278 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1279 | * @return array associative array containing the response |
||
| 1280 | */ |
||
| 1281 | 1 | public function addresses($page = 1, $limit = 20, $sortDir = 'asc') { |
|
| 1284 | |||
| 1285 | /** |
||
| 1286 | * get all UTXOs for the wallet (paginated) |
||
| 1287 | * |
||
| 1288 | * @param integer $page pagination: page number |
||
| 1289 | * @param integer $limit pagination: records per page (max 500) |
||
| 1290 | * @param string $sortDir pagination: sort direction (asc|desc) |
||
| 1291 | * @param boolean $zeroconf include zero confirmation transactions |
||
| 1292 | * @return array associative array containing the response |
||
| 1293 | */ |
||
| 1294 | 1 | public function utxos($page = 1, $limit = 20, $sortDir = 'asc', $zeroconf = true) { |
|
| 1297 | } |
||
| 1298 |
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.