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

WalletV3Sweeper   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 9
cts 11
cp 0.8182
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 3
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 Btccom\JustEncrypt\Encryption;
9
use Btccom\JustEncrypt\EncryptionMnemonic;
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 1
    public function __construct($encryptedPrimaryMnemonic, $encryptedSecretMnemonic, $backupMnemonic, BufferInterface $passphrase, array $blocktrailPublicKeys, UnspentOutputFinder $utxoFinder, $network = 'btc', $testnet = false) {
25
        // cleanup copy paste errors from mnemonics
26 1
        $encryptedPrimaryMnemonic = str_replace("  ", " ", str_replace("\r\n", " ", str_replace("\n", " ", trim($encryptedPrimaryMnemonic))));
27 1
        $encryptedSecretMnemonic = str_replace("  ", " ", str_replace("\r\n", " ", str_replace("\n", " ", trim($encryptedSecretMnemonic))));
28 1
        $backupMnemonic = str_replace("  ", " ", str_replace("\r\n", " ", str_replace("\n", " ", trim($backupMnemonic))));
29
30 1
        if (!($secret = Encryption::decrypt(EncryptionMnemonic::decode($encryptedSecretMnemonic), $passphrase))) {
31
            throw new BlocktrailSDKException("Failed to decret password encrypted secret");
32
        }
33
34 1
        if (!($primarySeed = Encryption::decrypt(EncryptionMnemonic::decode($encryptedPrimaryMnemonic), $secret))) {
35
            throw new BlocktrailSDKException("failed to decrypt encrypted primary seed! (weird!)");
36
        }
37
38 1
        $backupMnemonic = MnemonicFactory::bip39()->mnemonicToEntropy($backupMnemonic);
39 1
        parent::__construct($primarySeed, $backupMnemonic, $blocktrailPublicKeys, $utxoFinder, $network, $testnet);
40 1
    }
41
}
42