Completed
Pull Request — master (#127)
by thomas
19:50
created

WalletV2Sweeper   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 10
cts 12
cp 0.8333
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 7

1 Method

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