1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Address; |
6
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Bitcoin; |
8
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\KeyInterface; |
9
|
|
|
use BitWasp\Bitcoin\Key\PublicKeyFactory; |
10
|
|
|
use BitWasp\Bitcoin\Network\NetworkInterface; |
11
|
|
|
use BitWasp\Bitcoin\Script\Classifier\OutputClassifier; |
12
|
|
|
use BitWasp\Bitcoin\Script\ScriptInterface; |
13
|
|
|
use BitWasp\Bitcoin\Script\ScriptType; |
14
|
|
|
use BitWasp\Bitcoin\Script\WitnessProgram; |
15
|
|
|
|
16
|
|
|
class AddressFactory |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Returns a pay-to-pubkey-hash address for the given public key |
20
|
|
|
* |
21
|
|
|
* @param KeyInterface $key |
22
|
|
|
* @return PayToPubKeyHashAddress |
23
|
|
|
*/ |
24
|
|
|
public static function p2pkh(KeyInterface $key): PayToPubKeyHashAddress |
25
|
|
|
{ |
26
|
|
|
return new PayToPubKeyHashAddress($key->getPubKeyHash()); |
27
|
|
|
} |
28
|
|
|
|
29
|
11 |
|
/** |
30
|
|
|
* Takes the $p2shScript and generates the scriptHash address. |
31
|
11 |
|
* |
32
|
|
|
* @param ScriptInterface $p2shScript |
33
|
|
|
* @return ScriptHashAddress |
34
|
|
|
*/ |
35
|
|
|
public static function p2sh(ScriptInterface $p2shScript): ScriptHashAddress |
36
|
|
|
{ |
37
|
|
|
return new ScriptHashAddress($p2shScript->getScriptHash()); |
38
|
|
|
} |
39
|
|
|
|
40
|
10 |
|
/** |
41
|
|
|
* @param WitnessProgram $wp |
42
|
10 |
|
* @return SegwitAddress |
43
|
|
|
*/ |
44
|
|
|
public static function fromWitnessProgram(WitnessProgram $wp): SegwitAddress |
45
|
|
|
{ |
46
|
|
|
return new SegwitAddress($wp); |
47
|
|
|
} |
48
|
|
|
|
49
|
6 |
|
/** |
50
|
|
|
* @param ScriptInterface $outputScript |
51
|
6 |
|
* @return Address |
52
|
|
|
*/ |
53
|
|
|
public static function fromOutputScript(ScriptInterface $outputScript): Address |
54
|
|
|
{ |
55
|
|
|
$reader = new AddressReader(); |
56
|
|
|
return $reader->fromOutputScript($outputScript); |
57
|
|
|
} |
58
|
26 |
|
|
59
|
|
|
/** |
60
|
26 |
|
* @param string $address |
61
|
|
|
* @param NetworkInterface|null $network |
62
|
|
|
* @return Address |
63
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\UnrecognizedAddressException |
64
|
26 |
|
*/ |
65
|
26 |
|
public static function fromString(string $address, NetworkInterface $network = null): Address |
66
|
|
|
{ |
67
|
6 |
|
$network = $network ?: Bitcoin::getNetwork(); |
68
|
|
|
$reader = new AddressReader(); |
69
|
|
|
return $reader->fromString($address, $network); |
70
|
20 |
|
} |
71
|
20 |
|
|
72
|
20 |
|
/** |
73
|
|
|
* @param string $address |
74
|
10 |
|
* @param NetworkInterface $network |
75
|
11 |
|
* @return bool |
76
|
|
|
*/ |
77
|
9 |
|
public static function isValidAddress(string $address, NetworkInterface $network = null): bool |
78
|
|
|
{ |
79
|
2 |
|
try { |
80
|
|
|
self::fromString($address, $network); |
81
|
|
|
$is_valid = true; |
82
|
|
|
} catch (\Exception $e) { |
83
|
|
|
$is_valid = false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $is_valid; |
87
|
|
|
} |
88
|
|
|
|
89
|
36 |
|
/** |
90
|
|
|
* Following a loose definition of 'associated', returns |
91
|
36 |
|
* the current script types, and a PayToPubKeyHash address for P2PK. |
92
|
|
|
* |
93
|
|
|
* @param ScriptInterface $script |
94
|
36 |
|
* @return AddressInterface |
95
|
30 |
|
* @throws \RuntimeException |
96
|
|
|
*/ |
97
|
30 |
|
public static function getAssociatedAddress(ScriptInterface $script): AddressInterface |
98
|
10 |
|
{ |
99
|
20 |
|
$classifier = new OutputClassifier(); |
100
|
20 |
|
$decode = $classifier->decode($script); |
101
|
|
|
if ($decode->getType() === ScriptType::P2PK) { |
102
|
6 |
|
$pubKey = PublicKeyFactory::fromBuffer($decode->getSolution()); |
103
|
|
|
return new PayToPubKeyHashAddress($pubKey->getPubKeyHash()); |
104
|
|
|
} else { |
105
|
|
|
return self::fromOutputScript($script); |
106
|
|
|
} |
107
|
7 |
|
} |
108
|
|
|
} |
109
|
|
|
|