1 | <?php |
||
17 | class AddressFactory |
||
18 | { |
||
19 | /** |
||
20 | * Returns a pay-to-pubkey-hash address for the given public key |
||
21 | * |
||
22 | * @param KeyInterface $key |
||
23 | * @return PayToPubKeyHashAddress |
||
24 | */ |
||
25 | 44 | public static function fromKey(KeyInterface $key) |
|
29 | |||
30 | /** |
||
31 | * Takes the $p2shScript and generates the scriptHash address. |
||
32 | * |
||
33 | * @param ScriptInterface $p2shScript |
||
34 | * @return ScriptHashAddress |
||
35 | */ |
||
36 | 12 | public static function fromScript(ScriptInterface $p2shScript) |
|
40 | |||
41 | /** |
||
42 | * @param WitnessProgram $wp |
||
43 | * @return SegwitAddress |
||
44 | */ |
||
45 | 12 | public static function fromWitnessProgram(WitnessProgram $wp) |
|
49 | |||
50 | /** |
||
51 | * @param ScriptInterface $outputScript |
||
52 | * @return AddressInterface |
||
53 | */ |
||
54 | 44 | public static function fromOutputScript(ScriptInterface $outputScript) |
|
55 | { |
||
56 | 44 | $wp = null; |
|
57 | 44 | if ($outputScript->isWitness($wp)) { |
|
58 | /** @var WitnessProgram $wp */ |
||
59 | 12 | return new SegwitAddress($wp); |
|
60 | } |
||
61 | |||
62 | 32 | $decode = (new OutputClassifier())->decode($outputScript); |
|
63 | 32 | switch ($decode->getType()) { |
|
64 | 32 | case ScriptType::P2PKH: |
|
65 | /** @var BufferInterface $solution */ |
||
66 | 20 | return new PayToPubKeyHashAddress($decode->getSolution()); |
|
67 | 14 | case ScriptType::P2SH: |
|
68 | /** @var BufferInterface $solution */ |
||
69 | 10 | return new ScriptHashAddress($decode->getSolution()); |
|
70 | default: |
||
71 | 4 | throw new \RuntimeException('Script type is not associated with an address'); |
|
72 | } |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param string $address |
||
77 | * @param NetworkInterface $network |
||
78 | * @return AddressInterface |
||
79 | * @throws \BitWasp\Bitcoin\Exceptions\Base58ChecksumFailure |
||
80 | */ |
||
81 | 60 | public static function fromString($address, NetworkInterface $network = null) |
|
106 | |||
107 | /** |
||
108 | * @param string $address |
||
109 | * @param NetworkInterface $network |
||
110 | * @return bool |
||
111 | * @throws \BitWasp\Bitcoin\Exceptions\Base58ChecksumFailure |
||
112 | */ |
||
113 | 38 | public static function isValidAddress($address, NetworkInterface $network = null) |
|
124 | |||
125 | /** |
||
126 | * Following a loose definition of 'associated', returns |
||
127 | * the current script types, and a PayToPubKeyHash address for P2PK. |
||
128 | * |
||
129 | * @param ScriptInterface $script |
||
130 | * @return AddressInterface |
||
131 | * @throws \RuntimeException |
||
132 | */ |
||
133 | 4 | public static function getAssociatedAddress(ScriptInterface $script) |
|
143 | } |
||
144 |