1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Key\Factory; |
6
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Bitcoin; |
8
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface; |
9
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\KeyInterface; |
10
|
|
|
use BitWasp\Bitcoin\Key\Deterministic\ElectrumKey; |
11
|
|
|
use BitWasp\Bitcoin\Mnemonic\Electrum\ElectrumWordListInterface; |
12
|
|
|
use BitWasp\Bitcoin\Mnemonic\MnemonicFactory; |
13
|
|
|
use BitWasp\Buffertools\Buffer; |
14
|
|
|
use BitWasp\Buffertools\BufferInterface; |
15
|
|
|
|
16
|
|
|
class ElectrumKeyFactory |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var EcAdapterInterface |
20
|
|
|
*/ |
21
|
|
|
private $adapter; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var PrivateKeyFactory |
25
|
|
|
*/ |
26
|
|
|
private $privateFactory; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* ElectrumKeyFactory constructor. |
30
|
|
|
* @param EcAdapterInterface|null $ecAdapter |
31
|
|
|
*/ |
32
|
5 |
|
public function __construct(EcAdapterInterface $ecAdapter = null) |
33
|
|
|
{ |
34
|
5 |
|
$this->adapter = $ecAdapter ?: Bitcoin::getEcAdapter(); |
35
|
5 |
|
$this->privateFactory = PrivateKeyFactory::uncompressed($ecAdapter); |
36
|
5 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $mnemonic |
40
|
|
|
* @param ElectrumWordListInterface $wordList |
41
|
|
|
* @return ElectrumKey |
42
|
|
|
* @throws \Exception |
43
|
|
|
*/ |
44
|
2 |
|
public function fromMnemonic(string $mnemonic, ElectrumWordListInterface $wordList = null): ElectrumKey |
45
|
|
|
{ |
46
|
2 |
|
$mnemonicConverter = MnemonicFactory::electrum($wordList, $this->adapter); |
47
|
2 |
|
$entropy = $mnemonicConverter->mnemonicToEntropy($mnemonic); |
48
|
2 |
|
return $this->getKeyFromSeed($entropy); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param BufferInterface $seed |
53
|
|
|
* @return ElectrumKey |
54
|
|
|
* @throws \Exception |
55
|
|
|
*/ |
56
|
3 |
|
public function getKeyFromSeed(BufferInterface $seed): ElectrumKey |
57
|
|
|
{ |
58
|
|
|
// Really weird, did electrum actually hash hex string seeds? |
59
|
3 |
|
$binary = $oldseed = $seed->getHex(); |
60
|
|
|
|
61
|
|
|
// Perform sha256 hash 5 times per iteration |
62
|
3 |
|
for ($i = 0; $i < 5*20000; $i++) { |
63
|
|
|
// Hash should return binary data |
64
|
3 |
|
$binary = hash('sha256', $binary . $oldseed, true); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Convert binary data to hex. |
68
|
3 |
|
$secretExponent = new Buffer($binary, 32); |
69
|
|
|
|
70
|
3 |
|
return $this->fromSecretExponent($secretExponent); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param BufferInterface $secret |
75
|
|
|
* @return ElectrumKey |
76
|
|
|
*/ |
77
|
3 |
|
public function fromSecretExponent(BufferInterface $secret): ElectrumKey |
78
|
|
|
{ |
79
|
3 |
|
$masterKey = $this->privateFactory->fromBuffer($secret); |
80
|
3 |
|
return $this->fromKey($masterKey); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param KeyInterface $key |
85
|
|
|
* @return ElectrumKey |
86
|
|
|
*/ |
87
|
5 |
|
public function fromKey(KeyInterface $key): ElectrumKey |
88
|
|
|
{ |
89
|
5 |
|
return new ElectrumKey($key); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|