|
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
|
|
|
|