|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Key\Deterministic; |
|
4
|
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface; |
|
6
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\KeyInterface; |
|
7
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface; |
|
8
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface; |
|
9
|
|
|
use BitWasp\Bitcoin\Crypto\Hash; |
|
10
|
|
|
use BitWasp\Buffertools\Buffer; |
|
11
|
|
|
|
|
12
|
|
|
class ElectrumKey |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var EcAdapterInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
private $ecAdapter; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var null|PrivateKeyInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $masterPrivate; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var PublicKeyInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $masterPublic; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param EcAdapterInterface $ecAdapter |
|
31
|
|
|
* @param KeyInterface $masterKey |
|
32
|
|
|
*/ |
|
33
|
8 |
|
public function __construct(EcAdapterInterface $ecAdapter, KeyInterface $masterKey) |
|
34
|
|
|
{ |
|
35
|
8 |
|
if ($masterKey->isCompressed()) { |
|
36
|
2 |
|
throw new \RuntimeException('Electrum keys are not compressed'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
8 |
|
if ($masterKey instanceof PrivateKeyInterface) { |
|
40
|
6 |
|
$this->masterPrivate = $masterKey; |
|
41
|
6 |
|
$this->masterPublic = $masterKey->getPublicKey(); |
|
42
|
4 |
|
} elseif ($masterKey instanceof PublicKeyInterface) { |
|
43
|
4 |
|
$this->masterPublic = $masterKey; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
8 |
|
$this->ecAdapter = $ecAdapter; |
|
47
|
8 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return PrivateKeyInterface |
|
51
|
|
|
*/ |
|
52
|
4 |
|
public function getMasterPrivateKey() |
|
53
|
|
|
{ |
|
54
|
4 |
|
if (null === $this->masterPrivate) { |
|
55
|
2 |
|
throw new \RuntimeException("Cannot produce master private key from master public key"); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
return $this->masterPrivate; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return PublicKeyInterface |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public function getMasterPublicKey() |
|
65
|
|
|
{ |
|
66
|
2 |
|
return $this->masterPublic; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return Buffer |
|
71
|
|
|
*/ |
|
72
|
2 |
|
public function getMPK() |
|
73
|
|
|
{ |
|
74
|
2 |
|
return $this->getMasterPublicKey()->getBuffer()->slice(1); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param int $sequence |
|
79
|
|
|
* @param bool $change |
|
80
|
|
|
* @return \GMP |
|
81
|
|
|
*/ |
|
82
|
2 |
|
public function getSequenceOffset($sequence, $change = false) |
|
83
|
|
|
{ |
|
84
|
2 |
|
$seed = new Buffer(sprintf("%s:%s:%s", $sequence, $change ? '1' : '0', $this->getMPK()->getBinary()), null, $this->ecAdapter->getMath()); |
|
85
|
2 |
|
return Hash::sha256d($seed)->getGmp(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param int $sequence |
|
90
|
|
|
* @param bool $change |
|
91
|
|
|
* @return PrivateKeyInterface|PublicKeyInterface |
|
92
|
|
|
*/ |
|
93
|
2 |
|
public function deriveChild($sequence, $change = false) |
|
94
|
|
|
{ |
|
95
|
2 |
|
$key = is_null($this->masterPrivate) ? $this->masterPublic : $this->masterPrivate; |
|
96
|
2 |
|
return $key->tweakAdd($this->getSequenceOffset($sequence, $change)); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|