|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Blocktrail\SDK; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use BitWasp\Bitcoin\Mnemonic\MnemonicFactory; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
use BitWasp\Buffertools\Buffer; |
|
10
|
|
|
use Blocktrail\CryptoJSAES\CryptoJSAES; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
use Blocktrail\SDK\Exceptions\BlocktrailSDKException; |
|
14
|
|
|
|
|
15
|
|
|
class WalletV2Sweeper extends WalletSweeper { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param string $encryptedPrimarySeed |
|
19
|
|
|
* @param string $passwordEncryptedSecret |
|
20
|
|
|
* @param string $backupSeed |
|
21
|
|
|
* @param string $passphrase |
|
22
|
|
|
* @param array $blocktrailPublicKeys |
|
23
|
|
|
* @param UnspentOutputFinder $utxoFinder |
|
24
|
|
|
* @param string $network |
|
25
|
|
|
* @param bool $testnet |
|
26
|
|
|
* @throws BlocktrailSDKException |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct($encryptedPrimarySeed, $passwordEncryptedSecret, $backupSeed, $passphrase, array $blocktrailPublicKeys, UnspentOutputFinder $utxoFinder, $network = 'btc', $testnet = false) { |
|
29
|
|
|
// cleanup copy paste errors from mnemonics |
|
30
|
|
|
$encryptedPrimarySeed = str_replace(" ", " ", str_replace("\r\n", " ", str_replace("\n", " ", trim($encryptedPrimarySeed)))); |
|
31
|
|
|
$passwordEncryptedSecret = str_replace(" ", " ", str_replace("\r\n", " ", str_replace("\n", " ", trim($passwordEncryptedSecret)))); |
|
32
|
|
|
$backupSeed = str_replace(" ", " ", str_replace("\r\n", " ", str_replace("\n", " ", trim($backupSeed)))); |
|
33
|
|
|
|
|
34
|
|
|
$bip39 = MnemonicFactory::bip39(); |
|
35
|
|
|
|
|
36
|
|
View Code Duplication |
if (!($secret = CryptoJSAES::decrypt(base64_encode($bip39->mnemonicToEntropy($passwordEncryptedSecret)->getBinary()), $passphrase))) { |
|
|
|
|
|
|
37
|
|
|
throw new BlocktrailSDKException("Failed to decret password encrypted secret"); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
View Code Duplication |
if (!($primarySeed = CryptoJSAES::decrypt(base64_encode($bip39->mnemonicToEntropy($encryptedPrimarySeed)->getBinary()), $secret))) { |
|
|
|
|
|
|
41
|
|
|
throw new BlocktrailSDKException("failed to decrypt encrypted primary seed! (weird!)"); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$backupSeed = $bip39->mnemonicToEntropy($backupSeed); |
|
45
|
|
|
|
|
46
|
|
|
parent::__construct(new Buffer($primarySeed), $backupSeed, $blocktrailPublicKeys, $utxoFinder, $network, $testnet); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.