1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Address; |
4
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Base58; |
6
|
|
|
use BitWasp\Bitcoin\Bitcoin; |
7
|
|
|
use BitWasp\Bitcoin\Network\NetworkInterface; |
8
|
|
|
use BitWasp\Buffertools\Buffer; |
9
|
|
|
use BitWasp\Buffertools\BufferInterface; |
10
|
|
|
|
11
|
|
|
class WitnessPubKeyHashAddress implements AddressInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var int |
15
|
|
|
*/ |
16
|
|
|
private $witnessVersion; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var BufferInterface |
20
|
|
|
*/ |
21
|
|
|
private $hash; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* WitnessAddress constructor. |
25
|
|
|
* @param int $witnessVersion |
26
|
|
|
* @param BufferInterface $hash |
27
|
|
|
*/ |
28
|
|
|
public function __construct($witnessVersion, BufferInterface $hash) |
29
|
|
|
{ |
30
|
|
|
if (!is_int($witnessVersion)) { |
31
|
|
|
throw new \RuntimeException('Witness version must be an integer'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if ($hash->getSize() !== 20) { |
35
|
|
|
throw new \RuntimeException('Hash for P2WPKH address must be 20 bytes'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$this->witnessVersion = $witnessVersion; |
39
|
|
|
$this->hash = $hash->getHex(); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return int |
44
|
|
|
*/ |
45
|
|
|
public function getWitnessVersion() |
46
|
|
|
{ |
47
|
|
|
return $this->witnessVersion; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return BufferInterface|string |
52
|
|
|
*/ |
53
|
|
|
public function getHash() |
54
|
|
|
{ |
55
|
|
|
return $this->hash; |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param NetworkInterface $network |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function getPrefixByte(NetworkInterface $network) |
63
|
|
|
{ |
64
|
|
|
return $network->getP2WPKHByte(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param NetworkInterface|null $network |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function getAddress(NetworkInterface $network = null) |
72
|
|
|
{ |
73
|
|
|
$network = $network ?: Bitcoin::getNetwork(); |
74
|
|
|
$witnessByte = dechex($this->witnessVersion); |
75
|
|
|
$witnessByte = strlen($witnessByte) % 2 == 0 ? $witnessByte : '0' . $witnessByte; |
76
|
|
|
|
77
|
|
|
$payload = Buffer::hex($this->getPrefixByte($network) . $witnessByte . "00" . $this->getHash()); |
78
|
|
|
return Base58::encodeCheck($payload); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..