Completed
Branch master (95c3c5)
by
unknown
05:09
created

WalletV3Sweeper::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Blocktrail\SDK;
4
5
use BitWasp\Bitcoin\Mnemonic\MnemonicFactory;
6
use BitWasp\Buffertools\BufferInterface;
7
use Blocktrail\SDK\Exceptions\BlocktrailSDKException;
8
use Blocktrail\SDK\V3Crypt\Encryption;
9
use Blocktrail\SDK\V3Crypt\Mnemonic;
10
11
class WalletV3Sweeper extends WalletSweeper
12
{
13
    /**
14
     * @param string              $encryptedPrimaryMnemonic
15
     * @param string              $encryptedSecretMnemonic
16
     * @param string              $backupMnemonic
17
     * @param BufferInterface     $passphrase
18
     * @param array               $blocktrailPublicKeys
19
     * @param UnspentOutputFinder $utxoFinder
20
     * @param string              $network
21
     * @param bool                $testnet
22
     * @throws BlocktrailSDKException
23
     */
24
    public function __construct($encryptedPrimaryMnemonic, $encryptedSecretMnemonic, $backupMnemonic, BufferInterface $passphrase, array $blocktrailPublicKeys, UnspentOutputFinder $utxoFinder, $network = 'btc', $testnet = false) {
25
        // cleanup copy paste errors from mnemonics
26
        $encryptedPrimaryMnemonic = str_replace("  ", " ", str_replace("\r\n", " ", str_replace("\n", " ", trim($encryptedPrimaryMnemonic))));
27
        $encryptedSecretMnemonic = str_replace("  ", " ", str_replace("\r\n", " ", str_replace("\n", " ", trim($encryptedSecretMnemonic))));
28
        $backupMnemonic = str_replace("  ", " ", str_replace("\r\n", " ", str_replace("\n", " ", trim($backupMnemonic))));
29
30
        if (!($secret = Encryption::decrypt(Mnemonic::decode($encryptedSecretMnemonic), $passphrase))) {
31
            throw new BlocktrailSDKException("Failed to decret password encrypted secret");
32
        }
33
34
        if (!($primarySeed = Encryption::decrypt(Mnemonic::decode($encryptedPrimaryMnemonic), $secret))) {
35
            throw new BlocktrailSDKException("failed to decrypt encrypted primary seed! (weird!)");
36
        }
37
38
        $backupMnemonic = MnemonicFactory::bip39()->mnemonicToEntropy($backupMnemonic);
39
        parent::__construct($primarySeed, $backupMnemonic, $blocktrailPublicKeys, $utxoFinder, $network, $testnet);
40
    }
41
}
42