1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Key\Deterministic; |
4
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Address\BaseAddressCreator; |
6
|
|
|
use BitWasp\Bitcoin\Key\KeyToScript\ScriptAndSignData; |
7
|
|
|
use BitWasp\Bitcoin\Key\KeyToScript\ScriptDataFactory; |
8
|
|
|
|
9
|
|
|
class HierarchicalKeyScriptDecorator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var ScriptDataFactory |
13
|
|
|
*/ |
14
|
|
|
private $scriptFactory; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var ScriptAndSignData |
18
|
|
|
*/ |
19
|
|
|
private $scriptAndSignData; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var HierarchicalKey |
23
|
|
|
*/ |
24
|
|
|
private $hdKey; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* HierarchicalKeyScriptDecorator constructor. |
28
|
|
|
* @param ScriptDataFactory $scriptFactory |
29
|
|
|
* @param HierarchicalKey $hdKey |
30
|
|
|
*/ |
31
|
3 |
|
public function __construct(ScriptDataFactory $scriptFactory, HierarchicalKey $hdKey) |
32
|
|
|
{ |
33
|
3 |
|
$this->scriptFactory = $scriptFactory; |
34
|
3 |
|
$this->hdKey = $hdKey; |
35
|
3 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return HierarchicalKey |
39
|
|
|
*/ |
40
|
2 |
|
public function getHdKey() |
41
|
|
|
{ |
42
|
2 |
|
return $this->hdKey; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return ScriptDataFactory |
47
|
|
|
*/ |
48
|
2 |
|
public function getScriptDataFactory() |
49
|
|
|
{ |
50
|
2 |
|
return $this->scriptFactory; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return \BitWasp\Bitcoin\Key\KeyToScript\ScriptAndSignData |
55
|
|
|
*/ |
56
|
3 |
|
public function getScriptAndSignData() |
57
|
|
|
{ |
58
|
3 |
|
if (null === $this->scriptAndSignData) { |
59
|
3 |
|
$this->scriptAndSignData = $this->scriptFactory->convertKey($this->hdKey->getPublicKey()); |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
return $this->scriptAndSignData; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param BaseAddressCreator $addressCreator |
67
|
|
|
* @return \BitWasp\Bitcoin\Address\Address |
68
|
|
|
*/ |
69
|
3 |
|
public function getAddress(BaseAddressCreator $addressCreator) |
70
|
|
|
{ |
71
|
3 |
|
$scriptAndSignData = $this->getScriptAndSignData(); |
72
|
3 |
|
return $addressCreator->fromOutputScript($scriptAndSignData->getScriptPubKey()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return HierarchicalKeyScriptDecorator |
77
|
|
|
*/ |
78
|
2 |
|
public function withoutPrivateKey() |
79
|
|
|
{ |
80
|
2 |
|
return new self($this->scriptFactory, $this->hdKey->withoutPrivateKey()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Derive a child key |
85
|
|
|
* |
86
|
|
|
* @param int $sequence |
87
|
|
|
* @return HierarchicalKeyScriptDecorator |
88
|
|
|
* @throws \Exception |
89
|
|
|
*/ |
90
|
|
|
public function deriveChild($sequence) |
91
|
|
|
{ |
92
|
|
|
return new self($this->scriptFactory, $this->hdKey->deriveChild($sequence)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param array|\stdClass|\Traversable $list |
97
|
|
|
* @return HierarchicalKeyScriptDecorator |
98
|
|
|
*/ |
99
|
|
|
public function deriveFromList(array $list) |
100
|
|
|
{ |
101
|
|
|
return new self($this->scriptFactory, $this->hdKey->deriveFromList($list)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Decodes a BIP32 path into actual 32bit sequence numbers and derives the child key |
106
|
|
|
* |
107
|
|
|
* @param string $path |
108
|
|
|
* @return HierarchicalKeyScriptDecorator |
109
|
|
|
* @throws \Exception |
110
|
|
|
*/ |
111
|
2 |
|
public function derivePath($path) |
112
|
|
|
{ |
113
|
2 |
|
return new self($this->scriptFactory, $this->hdKey->derivePath($path)); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|