|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Address; |
|
4
|
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Base58; |
|
6
|
|
|
use BitWasp\Bitcoin\Bitcoin; |
|
7
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\KeyInterface; |
|
8
|
|
|
use BitWasp\Bitcoin\Key\PublicKeyFactory; |
|
9
|
|
|
use BitWasp\Bitcoin\Network\NetworkInterface; |
|
10
|
|
|
use BitWasp\Bitcoin\Script\Classifier\OutputClassifier; |
|
11
|
|
|
use BitWasp\Bitcoin\Script\P2shScript; |
|
12
|
|
|
use BitWasp\Bitcoin\Script\ScriptInterface; |
|
13
|
|
|
use BitWasp\Bitcoin\Script\ScriptType; |
|
14
|
|
|
use BitWasp\Bitcoin\Script\WitnessProgram; |
|
15
|
|
|
use BitWasp\Bitcoin\Script\WitnessScript; |
|
16
|
|
|
use BitWasp\Bitcoin\SegwitBech32; |
|
17
|
|
|
use BitWasp\Buffertools\BufferInterface; |
|
18
|
|
|
|
|
19
|
|
|
class AddressFactory |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Returns a pay-to-pubkey-hash address for the given public key |
|
23
|
|
|
* |
|
24
|
|
|
* @param KeyInterface $key |
|
25
|
44 |
|
* @return PayToPubKeyHashAddress |
|
26
|
|
|
*/ |
|
27
|
44 |
|
public static function fromKey(KeyInterface $key) |
|
28
|
|
|
{ |
|
29
|
|
|
return new PayToPubKeyHashAddress($key->getPubKeyHash()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Takes the $p2shScript and generates the scriptHash address. |
|
34
|
|
|
* |
|
35
|
|
|
* @param ScriptInterface $p2shScript |
|
36
|
12 |
|
* @return ScriptHashAddress |
|
37
|
|
|
*/ |
|
38
|
12 |
|
public static function fromScript(ScriptInterface $p2shScript) |
|
39
|
|
|
{ |
|
40
|
|
|
return new ScriptHashAddress($p2shScript->getScriptHash()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param WitnessProgram $wp |
|
45
|
12 |
|
* @return SegwitAddress |
|
46
|
|
|
*/ |
|
47
|
12 |
|
public static function fromWitnessProgram(WitnessProgram $wp) |
|
48
|
|
|
{ |
|
49
|
|
|
return new SegwitAddress($wp); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param ScriptInterface $outputScript |
|
54
|
44 |
|
* @return AddressInterface |
|
55
|
|
|
*/ |
|
56
|
44 |
|
public static function fromOutputScript(ScriptInterface $outputScript) |
|
57
|
44 |
|
{ |
|
58
|
|
|
if ($outputScript instanceof P2shScript || $outputScript instanceof WitnessScript) { |
|
59
|
12 |
|
throw new \RuntimeException("P2shScript & WitnessScript's are not accepted by fromOutputScript"); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
32 |
|
$wp = null; |
|
63
|
32 |
|
if ($outputScript->isWitness($wp)) { |
|
64
|
32 |
|
/** @var WitnessProgram $wp */ |
|
65
|
|
|
return new SegwitAddress($wp); |
|
66
|
20 |
|
} |
|
67
|
14 |
|
|
|
68
|
|
|
$decode = (new OutputClassifier())->decode($outputScript); |
|
69
|
10 |
|
switch ($decode->getType()) { |
|
70
|
|
|
case ScriptType::P2PKH: |
|
71
|
4 |
|
/** @var BufferInterface $solution */ |
|
72
|
|
|
return new PayToPubKeyHashAddress($decode->getSolution()); |
|
73
|
|
|
case ScriptType::P2SH: |
|
74
|
|
|
/** @var BufferInterface $solution */ |
|
75
|
|
|
return new ScriptHashAddress($decode->getSolution()); |
|
76
|
|
|
default: |
|
77
|
|
|
throw new \RuntimeException('Script type is not associated with an address'); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
60 |
|
/** |
|
82
|
|
|
* @param string $address |
|
83
|
60 |
|
* @param NetworkInterface $network |
|
84
|
|
|
* @return AddressInterface |
|
85
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\Base58ChecksumFailure |
|
86
|
60 |
|
*/ |
|
87
|
48 |
|
public static function fromString($address, NetworkInterface $network = null) |
|
88
|
|
|
{ |
|
89
|
48 |
|
$network = $network ?: Bitcoin::getNetwork(); |
|
90
|
12 |
|
|
|
91
|
36 |
|
try { |
|
92
|
36 |
|
$data = Base58::decodeCheck($address); |
|
93
|
|
|
$prefixByte = $data->slice(0, 1)->getHex(); |
|
94
|
12 |
|
|
|
95
|
|
|
if ($prefixByte === $network->getP2shByte()) { |
|
96
|
|
|
return new ScriptHashAddress($data->slice(1)); |
|
97
|
|
|
} else if ($prefixByte === $network->getAddressByte()) { |
|
98
|
|
|
return new PayToPubKeyHashAddress($data->slice(1)); |
|
99
|
14 |
|
} |
|
100
|
2 |
|
} catch (\Exception $e) { |
|
101
|
|
|
// continue on for Bech32 |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
2 |
|
try { |
|
105
|
|
|
return new SegwitAddress(SegwitBech32::decode($address, $network)); |
|
106
|
|
|
} catch (\Exception $e) { |
|
107
|
|
|
// continue on |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
throw new \InvalidArgumentException("Address not recognized"); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
38 |
|
/** |
|
114
|
|
|
* @param string $address |
|
115
|
|
|
* @param NetworkInterface $network |
|
116
|
38 |
|
* @return bool |
|
117
|
36 |
|
* @throws \BitWasp\Bitcoin\Exceptions\Base58ChecksumFailure |
|
118
|
2 |
|
*/ |
|
119
|
2 |
|
public static function isValidAddress($address, NetworkInterface $network = null) |
|
120
|
|
|
{ |
|
121
|
|
|
try { |
|
122
|
38 |
|
self::fromString($address, $network); |
|
123
|
|
|
$is_valid = true; |
|
124
|
|
|
} catch (\Exception $e) { |
|
125
|
|
|
$is_valid = false; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $is_valid; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Following a loose definition of 'associated', returns |
|
133
|
4 |
|
* the current script types, and a PayToPubKeyHash address for P2PK. |
|
134
|
|
|
* |
|
135
|
4 |
|
* @param ScriptInterface $script |
|
136
|
4 |
|
* @return AddressInterface |
|
137
|
4 |
|
* @throws \RuntimeException |
|
138
|
2 |
|
*/ |
|
139
|
|
|
public static function getAssociatedAddress(ScriptInterface $script) |
|
140
|
4 |
|
{ |
|
141
|
|
|
$classifier = new OutputClassifier(); |
|
142
|
|
|
$decode = $classifier->decode($script); |
|
143
|
|
|
if ($decode->getType() === ScriptType::P2PK) { |
|
144
|
|
|
return PublicKeyFactory::fromHex($decode->getSolution())->getAddress(); |
|
145
|
|
|
} else { |
|
146
|
|
|
return self::fromOutputScript($script); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|