| Conditions | 18 |
| Paths | 2112 |
| Total Lines | 62 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 52 | public function unlock($options, callable $fn = null) { |
||
| 53 | // explode the wallet data |
||
| 54 | $password = isset($options['passphrase']) ? $options['passphrase'] : (isset($options['password']) ? $options['password'] : null); |
||
| 55 | $encryptedPrimarySeed = $this->encryptedPrimarySeed; |
||
| 56 | $encryptedSecret = $this->encryptedSecret; |
||
| 57 | $primaryPrivateKey = isset($options['primary_private_key']) ? $options['primary_private_key'] : null; |
||
| 58 | |||
| 59 | if (isset($options['secret'])) { |
||
| 60 | $this->secret = $options['secret']; |
||
| 61 | } |
||
| 62 | if (isset($options['primary_seed'])) { |
||
| 63 | $this->primarySeed = $options['primary_seed']; |
||
| 64 | } |
||
| 65 | |||
| 66 | if (!$primaryPrivateKey) { |
||
| 67 | if (!$password) { |
||
| 68 | throw new \InvalidArgumentException("Can't init wallet with Primary Seed without a passphrase"); |
||
| 69 | } else if (!$encryptedSecret) { |
||
| 70 | throw new \InvalidArgumentException("Can't init wallet with Primary Seed without a encrypted secret"); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | if ($primaryPrivateKey) { |
||
| 75 | if (!($primaryPrivateKey instanceof HierarchicalKey) && !($primaryPrivateKey instanceof BIP32Key)) { |
||
| 76 | $primaryPrivateKey = HierarchicalKeyFactory::fromExtended($primaryPrivateKey); |
||
| 77 | } |
||
| 78 | } else { |
||
| 79 | if (!($this->secret = CryptoJSAES::decrypt($encryptedSecret, $password))) { |
||
| 80 | throw new WalletDecryptException("Failed to decrypt secret with password"); |
||
| 81 | } |
||
| 82 | |||
| 83 | // convert the mnemonic to a seed using BIP39 standard |
||
| 84 | if (!($this->primarySeed = CryptoJSAES::decrypt($encryptedPrimarySeed, $this->secret))) { |
||
| 85 | throw new WalletDecryptException("Failed to decrypt primary seed with secret"); |
||
| 86 | } |
||
| 87 | |||
| 88 | $seedBuffer = new Buffer(base64_decode($this->primarySeed)); |
||
| 89 | |||
| 90 | // create BIP32 private key from the seed |
||
| 91 | $primaryPrivateKey = HierarchicalKeyFactory::fromEntropy($seedBuffer); |
||
| 92 | } |
||
| 93 | |||
| 94 | $this->primaryPrivateKey = $primaryPrivateKey instanceof BIP32Key ? $primaryPrivateKey : BIP32Key::create($primaryPrivateKey, "m"); |
||
| 95 | |||
| 96 | // create checksum (address) of the primary privatekey to compare to the stored checksum |
||
| 97 | $checksum = $this->primaryPrivateKey->publicKey()->getAddress()->getAddress(); |
||
| 98 | if ($checksum != $this->checksum) { |
||
| 99 | throw new \Exception("Checksum [{$checksum}] does not match [{$this->checksum}], most likely due to incorrect password"); |
||
| 100 | } |
||
| 101 | |||
| 102 | $this->locked = false; |
||
| 103 | |||
| 104 | // if the response suggests we should upgrade to a different blocktrail cosigning key then we should |
||
| 105 | if (isset($data['upgrade_key_index'])) { |
||
|
|
|||
| 106 | $this->upgradeKeyIndex($data['upgrade_key_index']); |
||
| 107 | } |
||
| 108 | |||
| 109 | if ($fn) { |
||
| 110 | $fn($this); |
||
| 111 | $this->lock(); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 154 |
This check looks for calls to
isset(...)orempty()on variables that are yet undefined. These calls will always produce the same result and can be removed.This is most likely caused by the renaming of a variable or the removal of a function/method parameter.